wcstring.c 16.9 KB
Newer Older
1 2 3 4
/*
 * NTDLL wide-char functions
 *
 * Copyright 2000 Alexandre Julliard
5
 * Copyright 2000 Jon Griffiths
6
 * Copyright 2003 Thomas Mertes
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23 24 25
 */

#include "config.h"

#include <ctype.h>
26
#include <limits.h>
27 28
#include <stdlib.h>
#include <string.h>
29
#include <stdarg.h>
30
#include <stdio.h>
31

32
#include "windef.h"
33
#include "winternl.h"
34 35 36
#include "wine/unicode.h"

/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
37
 *           _wcsicmp    (NTDLL.@)
38 39 40 41 42 43 44 45
 */
INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
{
    return strcmpiW( str1, str2 );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
46
 *           _wcslwr    (NTDLL.@)
47 48 49 50 51 52 53 54
 */
LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
{
    return strlwrW( str );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
55
 *           _wcsnicmp    (NTDLL.@)
56 57 58 59 60 61 62 63
 */
INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
{
    return strncmpiW( str1, str2, n );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
64
 *           _wcsupr    (NTDLL.@)
65 66 67 68 69 70 71 72
 */
LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
{
    return struprW( str );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
73
 *           towlower    (NTDLL.@)
74 75 76 77 78 79 80 81
 */
WCHAR __cdecl NTDLL_towlower( WCHAR ch )
{
    return tolowerW(ch);
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
82
 *           towupper    (NTDLL.@)
83 84 85 86 87 88 89 90
 */
WCHAR __cdecl NTDLL_towupper( WCHAR ch )
{
    return toupperW(ch);
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
91
 *           wcscat    (NTDLL.@)
92 93 94 95 96 97 98 99
 */
LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
{
    return strcatW( dst, src );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
100
 *           wcschr    (NTDLL.@)
101 102 103 104 105 106 107 108
 */
LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
{
    return strchrW( str, ch );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
109
 *           wcscmp    (NTDLL.@)
110 111 112 113 114 115 116 117
 */
INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
{
    return strcmpW( str1, str2 );
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
118
 *           wcscpy    (NTDLL.@)
119 120 121 122 123 124 125 126
 */
LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
{
    return strcpyW( dst, src );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
127
 *           wcscspn    (NTDLL.@)
128 129 130
 */
INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
{
131
    return strcspnW( str, reject );
132 133 134 135
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
136
 *           wcslen    (NTDLL.@)
137 138 139 140 141 142 143 144
 */
INT __cdecl NTDLL_wcslen( LPCWSTR str )
{
    return strlenW( str );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
145
 *           wcsncat    (NTDLL.@)
146 147 148 149 150 151 152 153 154 155 156 157
 */
LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
{
    LPWSTR ret = s1;
    while (*s1) s1++;
    while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
    *s1 = 0;
    return ret;
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
158
 *           wcsncmp    (NTDLL.@)
159 160 161 162 163 164 165 166
 */
INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
{
    return strncmpW( str1, str2, n );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
167
 *           wcsncpy    (NTDLL.@)
168 169 170
 */
LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
{
171 172 173 174
    WCHAR *ret = s1;
    while (n-- > 0) if (!(*s1++ = *s2++)) break;
    while (n-- > 0) *s1++ = 0;
    return ret;
175 176 177 178
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
179
 *           wcspbrk    (NTDLL.@)
180 181 182
 */
LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
{
183
    return strpbrkW( str, accept );
184 185 186 187
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
188
 *           wcsrchr    (NTDLL.@)
189 190 191
 */
LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
{
192
    return strrchrW( str, ch );
193 194 195 196
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
197
 *           wcsspn    (NTDLL.@)
198 199 200
 */
INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
{
201
    return strspnW( str, accept );
202 203 204 205
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
206
 *           wcsstr    (NTDLL.@)
207 208 209 210 211 212 213 214
 */
LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
{
    return strstrW( str, sub );
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
215
 *           wcstok    (NTDLL.@)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
 */
LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
{
    static LPWSTR next = NULL;
    LPWSTR ret;

    if (!str)
        if (!(str = next)) return NULL;

    while (*str && NTDLL_wcschr( delim, *str )) str++;
    if (!*str) return NULL;
    ret = str++;
    while (*str && !NTDLL_wcschr( delim, *str )) str++;
    if (*str) *str++ = 0;
    next = str;
    return ret;
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
236
 *           wcstombs    (NTDLL.@)
237 238 239
 */
INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
{
240 241 242 243 244 245 246 247 248 249 250 251 252 253
    DWORD len;

    if (!dst)
    {
        RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
        return len;
    }
    else
    {
        if (n <= 0) return 0;
        RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
        if (len < n) dst[len] = 0;
    }
    return len;
254 255 256 257
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
258
 *           mbstowcs    (NTDLL.@)
259 260 261
 */
INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
{
262 263 264 265 266 267 268 269 270 271 272 273 274
    DWORD len;

    if (!dst)
    {
        RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
    }
    else
    {
        if (n <= 0) return 0;
        RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
        if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
    }
    return len / sizeof(WCHAR);
275 276 277 278
}


/*********************************************************************
279
 *                  wcstol  (NTDLL.@)
280
 */
281
LONG __cdecl NTDLL_wcstol(LPCWSTR s, LPWSTR *end, INT base)
282
{
283
    return strtolW( s, end, base );
284 285 286
}


287 288 289
/*********************************************************************
 *                  wcstoul  (NTDLL.@)
 */
290
ULONG __cdecl NTDLL_wcstoul(LPCWSTR s, LPWSTR *end, INT base)
291
{
292
    return strtoulW( s, end, base );
293 294 295
}


296
/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
297
 *           iswctype    (NTDLL.@)
298 299 300
 */
INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
{
301
    return (get_char_typeW(wc) & 0xfff) & wct;
302 303 304 305
}


/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
306
 *           iswalpha    (NTDLL.@)
307
 *
308
 * Checks if a unicode char wc is a letter
309 310 311 312
 *
 * RETURNS
 *  TRUE: The unicode char wc is a letter.
 *  FALSE: Otherwise
313 314 315
 */
INT __cdecl NTDLL_iswalpha( WCHAR wc )
{
316 317 318 319 320 321 322
    return isalphaW(wc);
}


/*********************************************************************
 *		iswdigit (NTDLL.@)
 *
323
 * Checks if a unicode char wc is a digit
324 325 326 327 328 329 330 331 332 333 334 335 336 337
 *
 * RETURNS
 *  TRUE: The unicode char wc is a digit.
 *  FALSE: Otherwise
 */
INT __cdecl NTDLL_iswdigit( WCHAR wc )
{
    return isdigitW(wc);
}


/*********************************************************************
 *		iswlower (NTDLL.@)
 *
338
 * Checks if a unicode char wc is a lower case letter
339 340 341 342 343 344 345 346 347 348 349 350 351 352
 *
 * RETURNS
 *  TRUE: The unicode char wc is a lower case letter.
 *  FALSE: Otherwise
 */
INT __cdecl NTDLL_iswlower( WCHAR wc )
{
    return islowerW(wc);
}


/*********************************************************************
 *		iswspace (NTDLL.@)
 *
353
 * Checks if a unicode char wc is a white space character
354 355 356 357 358 359 360 361 362 363 364 365 366 367
 *
 * RETURNS
 *  TRUE: The unicode char wc is a white space character.
 *  FALSE: Otherwise
 */
INT __cdecl NTDLL_iswspace( WCHAR wc )
{
    return isspaceW(wc);
}


/*********************************************************************
 *		iswxdigit (NTDLL.@)
 *
368
 * Checks if a unicode char wc is an extended digit
369 370 371 372 373 374 375 376
 *
 * RETURNS
 *  TRUE: The unicode char wc is an extended digit.
 *  FALSE: Otherwise
 */
INT __cdecl NTDLL_iswxdigit( WCHAR wc )
{
    return isxdigitW(wc);
377
}
378 379 380


/*********************************************************************
381 382
 *      _ultow   (NTDLL.@)
 *
383
 * Converts an unsigned long integer to a unicode string.
384
 *
385 386 387 388 389 390 391 392
 * RETURNS
 *  Always returns str.
 *
 * NOTES
 *  Converts value to a '\0' terminated wstring which is copied to str.
 *  The maximum length of the copied str is 33 bytes.
 *  Does not check if radix is in the range of 2 to 36.
 *  If str is NULL it just returns NULL.
393
 */
394
LPWSTR __cdecl _ultow(
395
    ULONG value,         /* [I] Value to be converted */
396 397
    LPWSTR str,          /* [O] Destination for the converted value */
    INT radix)           /* [I] Number base for conversion */
398
{
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    WCHAR buffer[33];
    PWCHAR pos;
    WCHAR digit;

    pos = &buffer[32];
    *pos = '\0';

    do {
	digit = value % radix;
	value = value / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (value != 0L);

    if (str != NULL) {
	memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
    } /* if */
    return str;
}
421 422


423 424 425
/*********************************************************************
 *      _ltow   (NTDLL.@)
 *
426
 * Converts a long integer to a unicode string.
427
 *
428 429 430 431 432 433 434 435 436 437 438
 * RETURNS
 *  Always returns str.
 *
 * NOTES
 *  Converts value to a '\0' terminated wstring which is copied to str.
 *  The maximum length of the copied str is 33 bytes. If radix
 *  is 10 and value is negative, the value is converted with sign.
 *  Does not check if radix is in the range of 2 to 36.
 *  If str is NULL it just returns NULL.
 */
LPWSTR __cdecl _ltow(
439
    LONG value, /* [I] Value to be converted */
440 441
    LPWSTR str, /* [O] Destination for the converted value */
    INT radix)  /* [I] Number base for conversion */
442
{
443
    ULONG val;
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    int negative;
    WCHAR buffer[33];
    PWCHAR pos;
    WCHAR digit;

    if (value < 0 && radix == 10) {
	negative = 1;
        val = -value;
    } else {
	negative = 0;
        val = value;
    } /* if */

    pos = &buffer[32];
    *pos = '\0';

    do {
	digit = val % radix;
	val = val / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (val != 0L);

    if (negative) {
	*--pos = '-';
    } /* if */

    if (str != NULL) {
	memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
    } /* if */
    return str;
}

480

481 482 483
/*********************************************************************
 *      _itow    (NTDLL.@)
 *
484
 * Converts an integer to a unicode string.
485
 *
486 487 488 489 490 491 492 493 494
 * RETURNS
 *  Always returns str.
 *
 * NOTES
 *  Converts value to a '\0' terminated wstring which is copied to str.
 *  The maximum length of the copied str is 33 bytes. If radix
 *  is 10 and value is negative, the value is converted with sign.
 *  Does not check if radix is in the range of 2 to 36.
 *  If str is NULL it just returns NULL.
495
 *
496 497
 * DIFFERENCES
 * - The native function crashes when the string is longer than 19 chars.
498 499
 *   This function does not have this bug.
 */
500 501 502 503
LPWSTR __cdecl _itow(
    int value,  /* [I] Value to be converted */
    LPWSTR str, /* [O] Destination for the converted value */
    INT radix)  /* [I] Number base for conversion */
504 505
{
    return _ltow(value, str, radix);
506
}
507

508

509
/*********************************************************************
510 511
 *      _ui64tow   (NTDLL.@)
 *
512
 * Converts a large unsigned integer to a unicode string.
513
 *
514 515 516 517 518 519 520 521
 * RETURNS
 *  Always returns str.
 *
 * NOTES
 *  Converts value to a '\0' terminated wstring which is copied to str.
 *  The maximum length of the copied str is 33 bytes.
 *  Does not check if radix is in the range of 2 to 36.
 *  If str is NULL it just returns NULL.
522
 *
523
 * DIFFERENCES
524 525 526
 * - This function does not exist in the native DLL (but in msvcrt).
 *   But since the maintenance of all these functions is better done
 *   in one place we implement it here.
527
 */
528 529 530 531
LPWSTR __cdecl _ui64tow(
    ULONGLONG value, /* [I] Value to be converted */
    LPWSTR str,      /* [O] Destination for the converted value */
    INT radix)       /* [I] Number base for conversion */
532
{
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    WCHAR buffer[65];
    PWCHAR pos;
    WCHAR digit;

    pos = &buffer[64];
    *pos = '\0';

    do {
	digit = value % radix;
	value = value / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (value != 0L);

    if (str != NULL) {
	memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
    } /* if */
    return str;
554 555
}

556

557
/*********************************************************************
558 559
 *      _i64tow   (NTDLL.@)
 *
560
 * Converts a large integer to a unicode string.
561
 *
562 563
 * RETURNS
 *  Always returns str.
564
 *
565 566 567 568 569 570 571 572
 * NOTES
 *  Converts value to a '\0' terminated wstring which is copied to str.
 *  The maximum length of the copied str is 33 bytes. If radix
 *  is 10 and value is negative, the value is converted with sign.
 *  Does not check if radix is in the range of 2 to 36.
 *  If str is NULL it just returns NULL.
 *
 * DIFFERENCES
573 574 575 576 577 578 579
 * - The native DLL converts negative values (for base 10) wrong:
 *                     -1 is converted to -18446744073709551615
 *                     -2 is converted to -18446744073709551614
 *   -9223372036854775807 is converted to  -9223372036854775809
 *   -9223372036854775808 is converted to  -9223372036854775808
 *   The native msvcrt _i64tow function and our ntdll function do
 *   not have this bug.
580
 */
581 582 583 584
LPWSTR __cdecl _i64tow(
    LONGLONG value, /* [I] Value to be converted */
    LPWSTR str,     /* [O] Destination for the converted value */
    INT radix)      /* [I] Number base for conversion */
585
{
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    ULONGLONG val;
    int negative;
    WCHAR buffer[65];
    PWCHAR pos;
    WCHAR digit;

    if (value < 0 && radix == 10) {
	negative = 1;
        val = -value;
    } else {
	negative = 0;
        val = value;
    } /* if */

    pos = &buffer[64];
    *pos = '\0';

    do {
	digit = val % radix;
	val = val / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (val != 0L);

    if (negative) {
	*--pos = '-';
    } /* if */

    if (str != NULL) {
	memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
    } /* if */
    return str;
621
}
622

623 624 625 626

/*********************************************************************
 *      _wtol    (NTDLL.@)
 *
627
 * Converts a unicode string to a long integer.
628
 *
629 630 631 632 633 634 635 636 637 638
 * PARAMS
 *  str [I] Wstring to be converted
 *
 * RETURNS
 *  On success it returns the integer value otherwise it returns 0.
 *
 * NOTES
 *  Accepts: {whitespace} [+|-] {digits}
 *  No check is made for value overflow, only the lower 32 bits are assigned.
 *  If str is NULL it crashes, as the native function does.
639
 */
640
LONG __cdecl _wtol( LPCWSTR str )
641 642
{
    ULONG RunningTotal = 0;
643
    BOOL bMinus = FALSE;
644 645 646 647 648 649 650 651

    while (isspaceW(*str)) {
	str++;
    } /* while */

    if (*str == '+') {
	str++;
    } else if (*str == '-') {
652
        bMinus = TRUE;
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	str++;
    } /* if */

    while (*str >= '0' && *str <= '9') {
	RunningTotal = RunningTotal * 10 + *str - '0';
	str++;
    } /* while */

    return bMinus ? -RunningTotal : RunningTotal;
}


/*********************************************************************
 *      _wtoi    (NTDLL.@)
 *
668
 * Converts a unicode string to an integer.
669
 *
670 671 672 673 674 675 676 677 678 679
 * PARAMS
 *  str [I] Wstring to be converted
 *
 * RETURNS
 *  On success it returns the integer value otherwise it returns 0.
 *
 * NOTES
 *  Accepts: {whitespace} [+|-] {digits}
 *  No check is made for value overflow, only the lower 32 bits are assigned.
 *  If str is NULL it crashes, as the native function does.
680
 */
681
int __cdecl _wtoi( LPCWSTR str )
682
{
683
    return _wtol(str);
684 685 686 687 688 689
}


/*********************************************************************
 *      _wtoi64   (NTDLL.@)
 *
690
 * Converts a unicode string to a large integer.
691
 *
692 693 694 695 696 697 698 699 700 701
 * PARAMS
 *  str [I] Wstring to be converted
 *
 * RETURNS
 *  On success it returns the integer value otherwise it returns 0.
 *
 * NOTES
 *  Accepts: {whitespace} [+|-] {digits}
 *  No check is made for value overflow, only the lower 64 bits are assigned.
 *  If str is NULL it crashes, as the native function does.
702
 */
703
LONGLONG  __cdecl _wtoi64( LPCWSTR str )
704 705
{
    ULONGLONG RunningTotal = 0;
706
    BOOL bMinus = FALSE;
707 708 709 710 711 712 713 714

    while (isspaceW(*str)) {
	str++;
    } /* while */

    if (*str == '+') {
	str++;
    } else if (*str == '-') {
715
        bMinus = TRUE;
716 717 718 719 720 721 722 723 724 725
	str++;
    } /* if */

    while (*str >= '0' && *str <= '9') {
	RunningTotal = RunningTotal * 10 + *str - '0';
	str++;
    } /* while */

    return bMinus ? -RunningTotal : RunningTotal;
}