summaryrefslogtreecommitdiff
path: root/src/pal/src/cruntime/wchar.cpp
blob: 2d244a639ff85d9cc14a15b8fb8f640ad7acb0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    wchar.c

Abstract:

    Implementation of wide char string functions.



--*/


#include "pal/palinternal.h"
#include "pal/cruntime.h"
#include "pal/dbgmsg.h"
#include "pal/unicode_data.h"

#include "pal/thread.hpp"
#include "pal/threadsusp.hpp"


#if HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_COREFOUNDATION
#define CF_EXCLUDE_CSTD_HEADERS
#include <CoreFoundation/CoreFoundation.h>
#include <wctype.h>
#else
#include <wctype.h>
#endif

#include <errno.h>

SET_DEFAULT_DEBUG_CHANNEL(CRT);


/*--
Function:
  wtolower (internal)

16-bit wide character version of the ANSI tolower() function.

  --*/
static
wchar_16
wtolower(wchar_16 c)
{
    /* Note: Surrogate pairs unicode character are not supported */

#if HAVE_TOWLOWER

    wchar_t w;
    w = (wchar_t) c;
    w = towlower(w);
    return (wchar_16) w;

#else

    return PAL_towlower(c);

#endif

}

/*******************************************************************************
Function:
  Internal_i64tow

Parameters:
  value
    - INT64 value to be converted to a string
  string
    - out buffer to place interger string
  radix
    - numeric base to convert to
  isI64
    - TRUE if value is INT64, FALSE if value is a long

Note:
  - only a radix of ten (and value < 0) will result in a negative
    sign in the output buffer
*******************************************************************************/
LPWSTR Internal_i64tow(INT64 value, LPWSTR string, int radix, BOOL isI64)
{
    int length = 0;
    int n;
    int r;
    UINT64 uval = value;
    LPWSTR stringPtr = string;
    int start = 0;
    int end;
    WCHAR tempCh;

    if (radix < 2 || radix > 36)
    {
        ASSERT( "Invalid radix, radix must be between 2 and 36\n" );
        SetLastError(ERROR_INVALID_PARAMETER);
        return string;
    }
    if (FALSE == isI64)
    {
        uval = (ULONG) uval;
    }
    if (10 == radix && value < 0)
    {
        uval = value * -1;
    }
    if(0 == uval)
    {
        ++length;
        *stringPtr++ = '0';
    }
    else while (uval > 0)
    {
        ++length;
        n = uval / radix;
        r = uval - (n * radix);
        uval /= radix;
        if (r > 9)
        {
            *stringPtr++ = r + 87;
        }
        else
        {
            *stringPtr++ = r + 48;
        }
    }
    if (10 == radix && value < 0)
    {
        *stringPtr++ = '-';
        ++length;
    }
    *stringPtr = 0; /* end the string */

    /* reverse the string */
    end = length - 1;
    while (start < end)
    {
        tempCh = string[start];
        string[start] = string[end];
        string[end] = tempCh;
        ++start;
        --end;
    }

    return string;
}

/*--
Function:
  _itow

16-bit wide character version of the ANSI tolower() function.

  --*/
wchar_16 *
__cdecl
_itow(
    int value,
    wchar_16 *string,
    int radix)
{
    wchar_16 *ret;

    PERF_ENTRY(_itow);
    ENTRY("_itow (value=%d, string=%p, radix=%d)\n",
          value, string, radix);

    ret = Internal_i64tow(value, string, radix, FALSE);

    LOGEXIT("_itow returns wchar_t* %p\n", ret);
    PERF_EXIT(_itow);

    return ret;
}

/*--
Function:
  _i64tow

See MSDN doc
--*/
wchar_16 *
 __cdecl 
_i64tow(
    __int64 value, 
    wchar_16 *string, 
    int radix)
{
    wchar_16 *ret;

    PERF_ENTRY(_i64tow);
    ENTRY("_i64tow (value=%ld, string=%p, radix=%d)\n",
          value, string, radix);

    ret = Internal_i64tow(value, string, radix, TRUE);

    LOGEXIT("_i64tow returns wchar_t* %p\n", ret);
    PERF_EXIT(_i64tow);

    return ret;
}


/*--
Function:
  _wtoi

See MSDN doc
--*/
int
__cdecl
_wtoi(
    const wchar_16 *string)
{
    int len;
    int ret;
    char *tempStr;

    PERF_ENTRY(_wtoi);
    ENTRY("_wtoi (string=%p)\n", string);

    len = WideCharToMultiByte(CP_ACP, 0, string, -1, 0, 0, 0, 0);
    if (!len)
    {
        ASSERT("WideCharToMultiByte failed.  Error is %d\n",
              GetLastError());
        return -1;
    }
    tempStr = (char *) PAL_malloc(len);
    if (!tempStr)
    {
        ERROR("PAL_malloc failed\n");
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        return -1;
    }
    len = WideCharToMultiByte(CP_ACP, 0, string, -1, tempStr, len, 0, 0);
    if (!len)
    {
        ASSERT("WideCharToMultiByte failed.  Error is %d\n",
              GetLastError());
        PAL_free(tempStr);
        return -1;
    }
    ret = atoi(tempStr);

    PAL_free(tempStr);
    LOGEXIT("_wtoi returns int %d\n", ret);
    PERF_EXIT(_wtoi);
    return ret;
}


/*--
Function:
  PAL_iswspace

See MSDN doc
--*/
int
__cdecl
PAL_iswspace(wchar_16 c)
{
    int ret;

    PERF_ENTRY(iswspace);
    ENTRY("PAL_iswspace (c=%C)\n", c);

    ret = iswspace(c);

    LOGEXIT("PAL_iswspace returns int %d\n", ret);
    PERF_EXIT(iswspace);
    return ret;
}

/*++
Function:
  _wcsnicmp

Compare characters of two strings without regard to case

Return Value

The return value indicates the relationship between the substrings as follows.

Return Value

Description

< 0        string1 substring less than string2 substring
  0        string1 substring identical to string2 substring
> 0        string1 substring greater than string2 substring

Parameters

string1, string2        Null-terminated strings to compare
count                   Number of characters to compare

Remarks

The _strnicmp function lexicographically compares, at most, the first
count characters of string1 and string2. The comparison is performed
without regard to case; _strnicmp is a case-insensitive version of
strncmp. The comparison ends if a terminating null character is
reached in either string before count characters are compared. If the
strings are equal when a terminating null character is reached in
either string before count characters are compared, the shorter string
is lesser.

--*/
int
__cdecl
_wcsnicmp(
          const wchar_16 *string1,
          const wchar_16 *string2, 
          size_t count)
{
    size_t i;
    int diff = 0;

    PERF_ENTRY(_wcsnicmp);
    ENTRY("_wcsnicmp (string1=%p (%S), string2=%p (%S), count=%lu)\n", 
          string1?string1:W16_NULLSTRING, 
          string1?string1:W16_NULLSTRING, string2?string2:W16_NULLSTRING, string2?string2:W16_NULLSTRING,
         (unsigned long) count);

    for (i = 0; i < count; i++)
    {
        diff = wtolower(string1[i]) - wtolower(string2[i]);
        if (diff != 0 || 0 == string1[i] || 0 == string2[i])
        {
            break;
        }
    }
    LOGEXIT("_wcsnicmp returning int %d\n", diff);
    PERF_EXIT(_wcsnicmp);
    return diff;
}

/*++
Function:
  _wcsicmp

Compare characters of two strings without regard to case

Return Value

The return value indicates the relationship between the substrings as follows.

Return Value

Description

< 0        string1 substring less than string2 substring
  0        string1 substring identical to string2 substring
> 0        string1 substring greater than string2 substring

Parameters

string1, string2        Null-terminated strings to compare

--*/
int
__cdecl
_wcsicmp(
          const wchar_16 *string1,
          const wchar_16 *string2)
{
    int ret;

    PERF_ENTRY(_wcsicmp);
    ENTRY("_wcsicmp (string1=%p (%S), string2=%p (%S))\n", 
          string1?string1:W16_NULLSTRING, 
          string1?string1:W16_NULLSTRING, string2?string2:W16_NULLSTRING, string2?string2:W16_NULLSTRING);

    ret = _wcsnicmp(string1, string2, 0x7fffffff);

    LOGEXIT("_wcsnicmp returns int %d\n", ret);
    PERF_EXIT(_wcsicmp);
    return ret;
}


/*++
Function:
  _wcslwr

Convert a string to lowercase.

Return Value

Returns a pointer to the converted string. Because the modification is
done in place, the pointer returned is the same as the pointer passed
as the input argument. No return value is reserved to indicate an
error.

Parameter

string   Null-terminated string to convert to lowercase

Remarks

--*/
wchar_16 * 
__cdecl
_wcslwr(
        wchar_16 *string)
{
    int i;

    PERF_ENTRY(_wcslwr);
    ENTRY("_wcslwr (string=%p (%S))\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING);

    for (i=0 ; string[i] != 0; i++)
    {
        string[i] = wtolower(string[i]);
    }
  
    LOGEXIT("_wcslwr returning wchar_t %p (%S)\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING);
    PERF_EXIT(_wcslwr);
    return string;
}


/*++
Function:
  PAL_wcstol

Convert string to a long-integer value.

Return Value

wcstol returns the value represented in the string nptr, except when
the representation would cause an overflow, in which case it returns
LONG_MAX or LONG_MIN. strtol returns 0 if no conversion can be
performed. errno is set to ERANGE if overflow or underflow occurs.

Parameters

nptr    Null-terminated string to convert 
endptr  Pointer to character that stops scan
base    Number base to use

Remarks

The wcstol function converts nptr to a long. It stops reading the
string nptr at the first character it cannot recognize as part of a
number. This may be the terminating null character, or it may be the
first numeric character greater than or equal to base.

Notes :
    MSDN states that only space and tab are accepted as leading whitespace, but
    tests indicate that other whitespace characters (newline, carriage return,
    etc) are also accepted. This matches the behavior on Unix systems.

    For wcstol and wcstoul, we need to check if the value to be returned 
    is outside the 32 bit range. If so, the returned value needs to be set  
    as appropriate, according to the MSDN pages for wcstol and wcstoul,
    and in all instances errno must be set to ERANGE (The one exception
    is converting a string representing a negative value to unsigned long).
    Note that on 64 bit Windows, long's are still 32 bit. Thus, to match
    Windows behavior, we must return long's in the 32 bit range.  
--*/

/* The use of LONG is by design, to ensure that a 32 bit value is always 
returned from this function. If "long" is used instead of LONG, then a 64 bit 
value could be returned on 64 bit platforms like HP-UX, thus breaking 
Windows behavior. */
LONG
__cdecl
PAL_wcstol(
        const wchar_16 *nptr,
        wchar_16 **endptr,
        int base)
{
    char *s_nptr = 0;
    char *s_endptr = 0;
    long res;
    int size;
    DWORD dwLastError = 0;

    PERF_ENTRY(wcstol);
    ENTRY("wcstol (nptr=%p (%S), endptr=%p, base=%d)\n", nptr?nptr:W16_NULLSTRING, nptr?nptr:W16_NULLSTRING,
          endptr, base);

    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, NULL, 0, NULL, NULL);
    if (!size)
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL_wcstolExit;
    }
    s_nptr = (char *)PAL_malloc(size);
    if (!s_nptr)
    {
        ERROR("PAL_malloc failed\n");
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        res = 0;
        goto PAL_wcstolExit;
    }
    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, s_nptr, size, NULL, NULL);
    if( size==0 )
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL_wcstolExit;
    }

    res = strtol(s_nptr, &s_endptr, base);

#ifdef BIT64
    if (res > _I32_MAX)
    {
        res = _I32_MAX;
        errno = ERANGE;
    }
    else if (res < _I32_MIN)
    {
        res = _I32_MIN;
        errno = ERANGE;
    }
#endif

    /* only ASCII characters will be accepted by strtol, and those always get
       mapped to single-byte characters, so the first rejected character will
       have the same index in the multibyte and widechar strings */
    if( endptr )
    {
        size = s_endptr - s_nptr;
        *endptr = (wchar_16 *)&nptr[size];
    }

PAL_wcstolExit:
    PAL_free(s_nptr);
    LOGEXIT("wcstol returning long %ld\n", res);
    PERF_EXIT(wcstol);
    /* This explicit cast to LONG is used to silence any potential warnings
    due to implicitly casting the native long res to LONG when returning. */
    return (LONG)res;
}


/*++
Function:
  PAL_wcstoul

Convert string to an unsigned long-integer value.

Return Value

wcstoul returns the converted value, if any, or ULONG_MAX on
overflow. It returns 0 if no conversion can be performed. errno is
set to ERANGE if overflow or underflow occurs.

Parameters

nptr    Null-terminated string to convert 
endptr  Pointer to character that stops scan
base    Number base to use

Remarks

wcstoul stops reading the string nptr at the first character it cannot
recognize as part of a number. This may be the terminating null
character, or it may be the first numeric character greater than or
equal to base. The LC_NUMERIC category setting of the current locale
determines recognition of the radix character in nptr; for more
information, see setlocale. If endptr is not NULL, a pointer to the
character that stopped the scan is stored at the location pointed to
by endptr. If no conversion can be performed (no valid digits were
found or an invalid base was specified), the value of nptr is stored
at the location pointed to by endptr.

Notes :
    MSDN states that only space and tab are accepted as leading whitespace, but
    tests indicate that other whitespace characters (newline, carriage return,
    etc) are also accepted. This matches the behavior on Unix systems.

    For wcstol and wcstoul, we need to check if the value to be returned 
    is outside the 32 bit range. If so, the returned value needs to be set
    as appropriate, according to the MSDN pages for wcstol and wcstoul,
    and in all instances errno must be set to ERANGE (The one exception
    is converting a string representing a negative value to unsigned long).
    Note that on 64 bit Windows, long's are still 32 bit. Thus, to match
    Windows behavior, we must return long's in the 32 bit range.
--*/

/* The use of ULONG is by design, to ensure that a 32 bit value is always 
returned from this function. If "unsigned long" is used instead of ULONG,
then a 64 bit value could be returned on 64 bit platforms like HP-UX, thus
breaking Windows behavior .*/
ULONG
__cdecl
PAL_wcstoul(
        const wchar_16 *nptr,
        wchar_16 **endptr,
        int base)
{
    char *s_nptr = 0;
    char *s_endptr = 0;
    unsigned long res;
    int size;
    DWORD dwLastError = 0;

    PERF_ENTRY(wcstoul);
    ENTRY("wcstoul (nptr=%p (%S), endptr=%p, base=%d)\n", nptr?nptr:W16_NULLSTRING, nptr?nptr:W16_NULLSTRING,
          endptr, base);

    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, NULL, 0, NULL, NULL);
    if (!size)
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL_wcstoulExit;
    }
    s_nptr = (char *)PAL_malloc(size);
    if (!s_nptr)
    {
        ERROR("PAL_malloc failed\n");
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        res = 0;
        goto PAL_wcstoulExit;
    }
    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, s_nptr, size, NULL, NULL);
    if (!size)
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL_wcstoulExit;
    }

    res = strtoul(s_nptr, &s_endptr, base);

#ifdef BIT64
    if (res > _UI32_MAX)
    {
        wchar_16 wc = *nptr;
        while (PAL_iswspace(wc))
        {
            wc = *nptr++;
        }
        /* If the string represents a positive number that is greater than 
           _UI32_MAX, set errno to ERANGE. Otherwise, don't set errno
           to match Windows behavior. */
        if (wc != '-')
        {
            res = _UI32_MAX;
            errno = ERANGE;
        }
    }
#endif

    /* only ASCII characters will be accepted by strtol, and those always get
       mapped to single-byte characters, so the first rejected character will
       have the same index in the multibyte and widechar strings */
    if( endptr )
    {
        size = s_endptr - s_nptr;
        *endptr = (wchar_16 *)&nptr[size];
    }

PAL_wcstoulExit:
    PAL_free(s_nptr);
    LOGEXIT("wcstoul returning unsigned long %lu\n", res);
    PERF_EXIT(wcstoul);

    /* When returning unsigned long res from this function, it will be 
    implicitly cast to ULONG. This handles situations where a string that
    represents a negative number is passed in to wcstoul. The Windows
    behavior is analogous to taking the binary equivalent of the negative
    value and treating it as a positive number. Returning a ULONG from
    this function, as opposed to native unsigned long, allows us to match
    this behavior. The explicit case to ULONG below is used to silence any
    potential warnings due to the implicit casting.  */
    return (ULONG)res;
}

ULONGLONG
__cdecl
PAL__wcstoui64(
        const wchar_16 *nptr,
        wchar_16 **endptr,
        int base)
{
    char *s_nptr = 0;
    char *s_endptr = 0;
    unsigned long long res;
    int size;
    DWORD dwLastError = 0;

    PERF_ENTRY(wcstoul);
    ENTRY("_wcstoui64 (nptr=%p (%S), endptr=%p, base=%d)\n", nptr?nptr:W16_NULLSTRING, nptr?nptr:W16_NULLSTRING,
          endptr, base);

    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, NULL, 0, NULL, NULL);
    if (!size)
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL__wcstoui64Exit;
    }
    s_nptr = (char *)PAL_malloc(size);
    if (!s_nptr)
    {
        ERROR("PAL_malloc failed\n");
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        res = 0;
        goto PAL__wcstoui64Exit;
    }
    size = WideCharToMultiByte(CP_ACP, 0, nptr, -1, s_nptr, size, NULL, NULL);
    if (!size)
    {
        dwLastError = GetLastError();
        ASSERT("WideCharToMultiByte failed.  Error is %d\n", dwLastError);
        SetLastError(ERROR_INVALID_PARAMETER);
        res = 0;
        goto PAL__wcstoui64Exit;
    }

    res = strtoull(s_nptr, &s_endptr, base);

    /* only ASCII characters will be accepted by strtoull, and those always get
       mapped to single-byte characters, so the first rejected character will
       have the same index in the multibyte and widechar strings */
    if( endptr )
    {
        size = s_endptr - s_nptr;
        *endptr = (wchar_16 *)&nptr[size];
    }

PAL__wcstoui64Exit:
    PAL_free(s_nptr);
    LOGEXIT("_wcstoui64 returning unsigned long long %llu\n", res);
    PERF_EXIT(_wcstoui64);

    return res;
}

/*++
Function:
  PAL_towlower

See MSDN

--*/
wchar_16
__cdecl
PAL_towlower( wchar_16 c )
{
#if HAVE_COREFOUNDATION
    PERF_ENTRY(towlower);
    ENTRY("towlower (c=%d)\n", c);
    if (!PAL_iswlower(c))
    {
        CFMutableStringRef cfString = CFStringCreateMutable(
                                            kCFAllocatorDefault, 1);
        if (cfString != NULL)
        {
            CFStringAppendCharacters(cfString, (const UniChar*)&c, 1);
            CFStringLowercase(cfString, NULL);
            c = CFStringGetCharacterAtIndex(cfString, 0);
            CFRelease(cfString);
        }
    }
    LOGEXIT("towlower returns int %d\n", c );
    PERF_EXIT(towlower);
    return c;
#else   /* HAVE_COREFOUNDATION */
    UnicodeDataRec dataRec;
    
    PERF_ENTRY(towlower);
    ENTRY("towlower (c=%d)\n", c);
    
    if (!GetUnicodeData(c, &dataRec))
    {
        TRACE( "Unable to retrieve unicode data for the character %c.\n", c );
        LOGEXIT("towlower returns int %d\n", c );
        PERF_EXIT(towlower);
        return c;
    }

    if ( (dataRec.C1_TYPE_FLAGS & C1_LOWER) || (dataRec.nOpposingCase ==  0 ))
    {
        LOGEXIT("towlower returns int %d\n", c );
        PERF_EXIT(towlower);
        return c;
    }
    else
    {
        LOGEXIT("towlower returns int %d\n", dataRec.nOpposingCase );
        PERF_EXIT(towlower);
        return dataRec.nOpposingCase;
    }
#endif  /* HAVE_COREFOUNDATION */
}


/*++
Function:
  PAL_towupper

See MSDN

--*/
wchar_16
__cdecl
PAL_towupper( wchar_16 c )
{
#if HAVE_COREFOUNDATION
    PERF_ENTRY(towupper);
    ENTRY("towupper (c=%d)\n", c);
    if (!PAL_iswupper(c))
    {
        CFMutableStringRef cfString = CFStringCreateMutable(
                                            kCFAllocatorDefault, 1);
        if (cfString != NULL)
        {
            CFStringAppendCharacters(cfString, (const UniChar*)&c, 1);
            CFStringUppercase(cfString, NULL);
            c = CFStringGetCharacterAtIndex(cfString, 0);
            CFRelease(cfString);
        }
    }
    LOGEXIT("towupper returns int %d\n", c );
    PERF_EXIT(towupper);
    return c;
#else   /* HAVE_COREFOUNDATION */
    UnicodeDataRec dataRec;

    PERF_ENTRY(towupper);
    ENTRY("towupper (c=%d)\n", c);

    if (!GetUnicodeData(c, &dataRec))
    {
        TRACE( "Unable to retrieve unicode data for the character %c.\n", c );
        LOGEXIT("towupper returns int %d\n", c );
        PERF_EXIT(towupper);
        return c;
    }
    
    if ( (dataRec.C1_TYPE_FLAGS & C1_UPPER) || (dataRec.nOpposingCase ==  0 ))
    {
        LOGEXIT("towupper returns int %d\n", c );
        PERF_EXIT(towupper);
        return c;
    }
    else
    {
        LOGEXIT("towupper returns int %d\n", dataRec.nOpposingCase );
        PERF_EXIT(towupper);
        return dataRec.nOpposingCase;
    }
#endif  /* HAVE_COREFOUNDATION */
}

/*++
Function:
  PAL_iswupper

See MSDN

--*/
int
__cdecl
PAL_iswupper( wchar_16 c )
{
    BOOL bRetVal = FALSE;
#if HAVE_COREFOUNDATION
    static CFCharacterSetRef sUppercaseSet;
    
    if (sUppercaseSet == NULL)
    {
        sUppercaseSet = CFCharacterSetGetPredefined(
                                        kCFCharacterSetUppercaseLetter);
    }
    PERF_ENTRY(iswupper);
    ENTRY( "iswupper (c=%d)\n", c );
    bRetVal = CFCharacterSetIsCharacterMember(sUppercaseSet, c);
#else   /* HAVE_COREFOUNDATION */
    UnicodeDataRec dataRec;

    PERF_ENTRY(iswupper);
    ENTRY( "iswupper (c=%d)\n", c );

    if (!GetUnicodeData(c, &dataRec))
    {
        TRACE( "Unable to retrieve unicode data for the character %c.\n", c );
        goto exit;
    }
    
    if (dataRec.C1_TYPE_FLAGS & C1_UPPER)
    {
        bRetVal = TRUE;
    }
exit:
#endif  /* HAVE_COREFOUNDATION */
    LOGEXIT( "iswupper returns %s.\n", bRetVal == TRUE ? "TRUE" : "FALSE" );
    PERF_EXIT(iswupper);
    return bRetVal;
}

/*++
Function:
  PAL_iswlower

See MSDN

--*/
int
__cdecl
PAL_iswlower( wchar_16 c )
{
    BOOL bRetVal = FALSE;
#if HAVE_COREFOUNDATION
    static CFCharacterSetRef sLowercaseSet;
    
    if (sLowercaseSet == NULL)
    {
        sLowercaseSet = CFCharacterSetGetPredefined(
                                        kCFCharacterSetLowercaseLetter);
    }
    PERF_ENTRY(iswlower);
    ENTRY("PAL_iswlower (c=%d)\n", c);
    bRetVal = CFCharacterSetIsCharacterMember(sLowercaseSet, c);
#else   /* HAVE_COREFOUNDATION */
    UnicodeDataRec dataRec;

    PERF_ENTRY(iswlower);
    ENTRY("PAL_iswlower (c=%d)\n", c);
    
    if (!GetUnicodeData(c, &dataRec))
    {
        TRACE( "Unable to retrieve unicode data for the character %c.\n", c );
        goto exit;
    }
    
    if (dataRec.C1_TYPE_FLAGS & C1_LOWER)
    {
        bRetVal = TRUE;
    }
exit:
#endif  /* HAVE_COREFOUNDATION */
    LOGEXIT("PAL_iswlower returns %s.\n", bRetVal == TRUE ? "TRUE" : "FALSE");
    PERF_EXIT(iswlower);
    return bRetVal;
}

/*++
Function:
  PAL_iswalpha

See MSDN

--*/
int
__cdecl
PAL_iswalpha( wchar_16 c )
{
    PERF_ENTRY(iswalpha);
    ENTRY( "PAL_iswalpha (c=%d)\n", c);
    
    if ( PAL_iswupper( c ) || PAL_iswlower( c ) )
    {
        LOGEXIT( "PAL_iswalpha returns 1.\n" );
        PERF_EXIT(iswalpha);
        return 1;
    }

    LOGEXIT( "PAL_iswalpha returns 0.\n" );
    PERF_EXIT(iswalpha);
    return 0;
}


/*++
Function:
  PAL_wcscat

See MSDN or the man page for mcscat.

--*/
wchar_16 * 
__cdecl
PAL_wcscat(
        wchar_16 *strDestination, 
        const wchar_16 *strSource)
{
    wchar_16 *ret;
    PERF_ENTRY(wcscat);
    ENTRY("wcscat (strDestination=%p (%S), strSource=%p (%S))\n", 
          strDestination?strDestination:W16_NULLSTRING, 
          strDestination?strDestination:W16_NULLSTRING, strSource?strSource:W16_NULLSTRING, strSource?strSource:W16_NULLSTRING);

    ret = PAL_wcsncat( strDestination, strSource, PAL_wcslen( strSource ) );
    
    LOGEXIT("wcscat returnng wchar_t %p (%S)\n", ret, ret);
    PERF_EXIT(wcscat);
    return ret;
}


/*++
Function:
  PAL_wcscpy

See MSDN or the man page for mcscpy.

--*/
wchar_16 * 
__cdecl
PAL_wcscpy(
        wchar_16 *strDestination, 
        const wchar_16 *strSource)
{
    wchar_16 *start = strDestination;

    PERF_ENTRY(wcscpy);
    ENTRY("wcscpy (strDestination=%p, strSource=%p (%S))\n", 
          strDestination, strSource ? strSource:W16_NULLSTRING, strSource ? strSource:W16_NULLSTRING);

    if (strDestination == NULL)
    {
        ERROR("invalid strDestination argument\n");
        LOGEXIT("wcscpy returning wchar_t NULL\n");
        PERF_EXIT(wcscpy);
        return NULL;
    }

    if (strSource == NULL)
    {
        ERROR("invalid strSource argument\n");
        LOGEXIT("wcscpy returning wchar_t NULL\n");
        PERF_EXIT(wcscpy);
        return NULL;
    }

    /* copy source string to destination string */
    while(*strSource)
    {
        *strDestination++ = *strSource++;
    }

    /* add terminating null */
    *strDestination = '\0';

    LOGEXIT("wcscpy returning wchar_t %p (%S)\n", start, start);
    PERF_EXIT(wcscpy);
    return start;
}


/*++
Function:
  PAL_wcslen

See MSDN or the man page for wcslen.

--*/
size_t 
__cdecl
PAL_wcslen(
        const wchar_16 *string)
{
    size_t nChar = 0;

    PERF_ENTRY(wcslen);
    ENTRY("wcslen (string=%p (%S))\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING);
    
    if ( !string )
    {
        LOGEXIT("wcslen returning size_t %u\n", 0);
        PERF_EXIT(wcslen);
        return 0;
    }
    while (*string++)
    {
        nChar++;
    }

    LOGEXIT("wcslen returning size_t %u\n", nChar);
    PERF_EXIT(wcslen);
    return nChar;
}


/*++
Function:
  PAL_wcsncmp

See MSDN or the man page for wcsncmp.
--*/
int 
__cdecl
PAL_wcsncmp(
        const wchar_16 *string1,
        const wchar_16 *string2,
        size_t count)
{
    size_t i;
    int diff = 0;

    PERF_ENTRY(wcsncmp);
    ENTRY("wcsncmp (string1=%p (%S), string2=%p (%S) count=%lu)\n", 
          string1?string1:W16_NULLSTRING, 
          string1?string1:W16_NULLSTRING, string2?string2:W16_NULLSTRING, string2?string2:W16_NULLSTRING, 
          (unsigned long) count);

    for (i = 0; i < count; i++)
    {
        diff = string1[i] - string2[i];
        if (diff != 0)
        {
            break;
        }

        /* stop if we reach the end of the string */
        if(string1[i]==0)
        {
            break;
        }
    }
    LOGEXIT("wcsncmp returning int %d\n", diff);
    PERF_EXIT(wcsncmp);
    return diff;
}

/*++
Function:
  PAL_wcscmp

See MSDN or the man page for wcscmp.
--*/
int
__cdecl
PAL_wcscmp(
        const wchar_16 *string1,
        const wchar_16 *string2)
{
    int ret;

    PERF_ENTRY(wcscmp);
    ENTRY("wcscmp (string1=%p (%S), string2=%p (%S))\n",
          string1?string1:W16_NULLSTRING,
          string1?string1:W16_NULLSTRING, string2?string2:W16_NULLSTRING, string2?string2:W16_NULLSTRING);

    ret = PAL_wcsncmp(string1, string2, 0x7fffffff);

    LOGEXIT("wcscmp returns int %d\n", ret);
    PERF_EXIT(wcscmp);
    return ret;
}

/*++
Function:
  PAL_wcschr

See MSDN or man page for wcschr.

--*/
wchar_16 _WConst_return * 
__cdecl
PAL_wcschr(
        const wchar_16 * string, 
        wchar_16 c)
{
    PERF_ENTRY(wcschr);
    ENTRY("wcschr (string=%p (%S), c=%C)\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING, c);
    
    while (*string)
    {
        if (*string == c)
        {
            LOGEXIT("wcschr returning wchar_t %p (%S)\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING);
            PERF_EXIT(wcschr);
            return (wchar_16 *) string;
        }
        string++;
    }

    // Check if the comparand was \000
    if (*string == c)
        return (wchar_16 *) string;
    
    LOGEXIT("wcschr returning wchar_t NULL\n");
    PERF_EXIT(wcschr);
    return NULL;
}


/*++
Function:
  PAL_wcsrchr

See MSDN or man page for wcsrchr.

--*/
wchar_16 _WConst_return * 
__cdecl
PAL_wcsrchr(
        const wchar_16 * string, 
        wchar_16 c)
{
    wchar_16 *last = NULL;

    PERF_ENTRY(wcsrchr);
    ENTRY("wcsrchr (string=%p (%S), c=%C)\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING, c);

    while (*string)
    {
        if (*string == c)
        {
            last = (wchar_16 *) string;
        }
        string++;
    }
    
    LOGEXIT("wcsrchr returning wchar_t %p (%S)\n", last?last:W16_NULLSTRING, last?last:W16_NULLSTRING);
    PERF_EXIT(wcsrchr);
    return (wchar_16 *)last;
}


/*++
Function:
  PAL_wcsspn

See MSDN or man page for wcspbrk.
--*/
size_t
__cdecl
PAL_wcsspn (const wchar_16 *string, const wchar_16 *stringCharSet)
{
    ASSERT(0);
    return 0;
}


/*++
Function:
  PAL_wcspbrk

See MSDN or man page for wcspbrk.
--*/
const wchar_16 * 
__cdecl
PAL_wcspbrk(
        const wchar_16 *string, 
        const wchar_16 *strCharSet)
{
    PERF_ENTRY(wcspbrk);
    ENTRY("wcspbrk (string=%p (%S), strCharSet=%p (%S))\n",
          string?string:W16_NULLSTRING,
          string?string:W16_NULLSTRING, strCharSet?strCharSet:W16_NULLSTRING, strCharSet?strCharSet:W16_NULLSTRING);

    while (*string)
    {
        if (PAL_wcschr(strCharSet, *string) != NULL)
        {
            LOGEXIT("wcspbrk returning wchar_t %p (%S)\n", string?string:W16_NULLSTRING, string?string:W16_NULLSTRING);
            PERF_EXIT(wcspbrk);
            return (wchar_16 *) string;
        }

        string++;
    }

    LOGEXIT("wcspbrk returning wchar_t NULL\n");
    PERF_EXIT(wcspbrk);
    return NULL;
}


/*++
Function:
  PAL_wcsstr

See MSDN or man page for wcsstr.
--*/
const wchar_16 *
__cdecl
PAL_wcsstr(
        const wchar_16 *string, 
        const wchar_16 *strCharSet)
{
    wchar_16 *ret = NULL;
    int i;

    PERF_ENTRY(wcsstr);
    ENTRY("wcsstr (string=%p (%S), strCharSet=%p (%S))\n", 
      string?string:W16_NULLSTRING, 
      string?string:W16_NULLSTRING, strCharSet?strCharSet:W16_NULLSTRING, strCharSet?strCharSet:W16_NULLSTRING);
  
    if (string == NULL)
    {
        ret = NULL;
        goto leave;
    }
  
    if (strCharSet == NULL)
    {
        ret = NULL;
        goto leave;
    }
  
    if (*strCharSet == 0)
    {
        ret = (wchar_16 *)string;
        goto leave;
    } 
  
    while (*string != 0)
    {
        i = 0;
        while (1)
        {
            if (*(string + i) == 0 || *(strCharSet + i) == 0)
            {
                ret = (wchar_16 *) string;
                goto leave;
            }
            if (*(string + i) != *(strCharSet + i))
            {
                break;
            }
        i++;
        }
        string++;
    }
  
 leave:    
    LOGEXIT("wcsstr returning wchar_t %p (%S)\n", ret?ret:W16_NULLSTRING, ret?ret:W16_NULLSTRING);
    PERF_EXIT(wcsstr);
    return ret;
}

/*++
Function : 
    
    PAL_wcsncpy            
    
see msdn doc.
--*/
wchar_16 * 
__cdecl
PAL_wcsncpy( wchar_16 * strDest, const wchar_16 *strSource, size_t count )
{
    UINT length = sizeof( wchar_16 ) * count;    
    PERF_ENTRY(wcsncpy);
    ENTRY("wcsncpy( strDest:%p, strSource:%p (%S), count:%lu)\n", 
          strDest, strSource, strSource, (unsigned long) count);
    
    memset( strDest, 0, length );
    length = min( count, PAL_wcslen( strSource ) ) * sizeof( wchar_16 );
    memcpy( strDest, strSource, length );
    
    LOGEXIT("wcsncpy returning (wchar_16*): %p\n", strDest);
    PERF_EXIT(wcsncpy);
    return strDest;
}

/*++
Function : 
    
    wcsncat            
    
see msdn doc.
--*/
wchar_16 * 
__cdecl
PAL_wcsncat( wchar_16 * strDest, const wchar_16 *strSource, size_t count )
{
    wchar_16 *start = strDest;
    UINT LoopCount = 0;
    UINT StrSourceLength = 0;

    PERF_ENTRY(wcsncat);
    ENTRY( "wcsncat (strDestination=%p (%S), strSource=%p (%S), count=%lu )\n", 
            strDest ? strDest : W16_NULLSTRING, 
            strDest ? strDest : W16_NULLSTRING,
            strSource ? strSource : W16_NULLSTRING,
            strSource ? strSource : W16_NULLSTRING, (unsigned long) count);

    if ( strDest == NULL )
    {
        ERROR("invalid strDest argument\n");
        LOGEXIT("wcsncat returning wchar_t NULL\n");
        PERF_EXIT(wcsncat);
        return NULL;
    }

    if ( strSource == NULL )
    {
        ERROR("invalid strSource argument\n");
        LOGEXIT("wcsncat returning wchar_t NULL\n");
        PERF_EXIT(wcsncat);
        return NULL;
    }

    /* find end of source string */
    while ( *strDest )
    {
        strDest++;
    }

    StrSourceLength = PAL_wcslen( strSource ); 
    if ( StrSourceLength < count )
    {
        count = StrSourceLength;
    }
    
    /* concatenate new string */
    while( *strSource && LoopCount < count )
    {
      *strDest++ = *strSource++;
      LoopCount++;
    }

    /* add terminating null */
    *strDest = '\0';

    LOGEXIT("wcsncat returning wchar_t %p (%S)\n", start, start);
    PERF_EXIT(wcsncat);
    return start;
}

static BOOL MISC_CRT_WCSTOD_IsValidCharacter( WCHAR c )
{
    if ( c == '+' || c == '-' || c == '.' || ( c >= '0' && c <= '9' ) || 
         c == 'e' || c == 'E' || c == 'd' || c == 'D' )
    {
        return TRUE;
    }
    return FALSE;
}                                                               

/*++
Function : 
    
    wcstod

    There is a slight difference between the Windows version of wcstod
    and the BSD versio of wcstod.

    Under Windows the string "  -1b  " returns -1.000000 stop char = 'b'
    Under BSD the same string returns 0.000000 stop ' '

see msdn doc.
--*/
double
__cdecl
PAL_wcstod( const wchar_16 * nptr, wchar_16 **endptr )
{
    double RetVal = 0.0;
    LPSTR  lpStringRep = NULL;
    LPWSTR lpStartOfExpression = (LPWSTR)nptr;
    LPWSTR lpEndOfExpression = NULL;
    UINT Length = 0;

    PERF_ENTRY(wcstod);
    ENTRY( "wcstod( %p (%S), %p (%S) )\n", nptr, nptr, endptr , endptr );

    if ( !nptr )
    {
        ERROR( "nptr is invalid.\n" );
        LOGEXIT( "wcstod returning 0.0\n" );
        PERF_EXIT(wcstod);
        return 0.0;
    }

    /* Eat white space. */
    while ( PAL_iswspace( *lpStartOfExpression ) )
    {
        lpStartOfExpression++;
    }

    /* Get the end of the expression. */
    lpEndOfExpression = lpStartOfExpression;
    while ( *lpEndOfExpression )
    {
        if ( !MISC_CRT_WCSTOD_IsValidCharacter( *lpEndOfExpression ) )
        {
            break;
        }
        lpEndOfExpression++;
    }

    if ( lpEndOfExpression != lpStartOfExpression )
    {
        Length = lpEndOfExpression - lpStartOfExpression;
        lpStringRep = (LPSTR)PAL_malloc( Length + 1);

        if ( lpStringRep )
        {
            if ( WideCharToMultiByte( CP_ACP, 0, lpStartOfExpression, Length,
                                      lpStringRep, Length + 1 ,
                                      NULL, 0 ) != 0 )
            {
                LPSTR ScanStop = NULL;
                lpStringRep[Length]= 0;
                RetVal = strtod( lpStringRep, &ScanStop );

                /* See if strtod failed. */
                if ( RetVal == 0.0 && ScanStop == lpStringRep )
                {
                    ASSERT( "An error occurred in the conversion.\n" );
                    lpEndOfExpression = (LPWSTR)nptr;
                }
            }
            else
            {
                ASSERT( "Wide char to multibyte conversion failed.\n" );
                lpEndOfExpression = (LPWSTR)nptr;
            }
        }
        else
        {
            ERROR( "Not enough memory.\n" );
            lpEndOfExpression = (LPWSTR)nptr;
        }
    }
    else
    {
        ERROR( "Malformed expression.\n" );
        lpEndOfExpression = (LPWSTR)nptr;
    }

    /* Set the stop scan character. */
    if ( endptr != NULL )
    {
        *endptr = lpEndOfExpression;
    }

    PAL_free( lpStringRep );
    LOGEXIT( "wcstod returning %f.\n", RetVal );
    PERF_EXIT(wcstod);
    return RetVal;
}

/*++
Function :

    _ui64tow

See MSDN for more details.
--*/
wchar_16 *
__cdecl
_ui64tow( unsigned __int64 value , wchar_16 * string , int radix )
{
    UINT ReversedIndex = 0;
    WCHAR ReversedString[ 65 ];
    LPWSTR lpString = string;
    UINT Index = 0;

    PERF_ENTRY(_ui64tow);
    ENTRY( "_ui64tow( value=%I64d, string=%p (%S), radix=%d )\n", 
           value, string, string, radix );

    if ( !string )
    {
        ERROR( "string has to be a valid pointer.\n" );
        LOGEXIT( "_ui64tow returning NULL.\n" );
        PERF_EXIT(_ui64tow);
        return NULL;
    }
    if ( radix < 2 || radix > 36 )
    {
        ERROR( "radix has to be between 2 and 36.\n" );
        LOGEXIT( "_ui64tow returning NULL.\n" );
        PERF_EXIT(_ui64tow);
        return NULL;
    }

    if(0 == value)
    {
        ReversedString[0] = '0';
        Index++;
    }
    else while ( value )
    {
        int temp = value % radix;
        value /= radix;
        
        if ( temp < 10 )
        {
            ReversedString[ Index ] = temp + '0';
            Index++;
        }
        else
        {
            ReversedString[ Index ] = temp - 10 + 'a';
            Index++;
        }
    }
    
    /* Reverse the string. */
    ReversedIndex = Index;
    for ( Index = 0; ReversedIndex > 0; ReversedIndex--, Index++ )
    {
        string[ Index ] = ReversedString[ ReversedIndex - 1 ];
    }

    string[ Index ] = '\0';
    LOGEXIT( "_ui64tow returning %p (%S).\n", lpString , lpString );
    PERF_EXIT(_ui64tow);
    return lpString;
}


/*++
Function:

    iswdigit
    
See MSDN for more details.
--*/
int
__cdecl
PAL_iswdigit( wchar_16 c )
{
    UINT nRetVal = 0;
#if HAVE_COREFOUNDATION
    static CFCharacterSetRef sDigitSet;
    
    if (sDigitSet == NULL)
    {
        sDigitSet = CFCharacterSetGetPredefined(
                                        kCFCharacterSetDecimalDigit);
    }
    PERF_ENTRY(iswdigit);
    ENTRY("PAL_iswdigit (c=%d)\n", c);
    nRetVal = CFCharacterSetIsCharacterMember(sDigitSet, c);
#else   /* HAVE_COREFOUNDATION */
    UnicodeDataRec dataRec;

    PERF_ENTRY(iswdigit);
    ENTRY("PAL_iswdigit (c=%d)\n", c);
    
    if (GetUnicodeData(c, &dataRec))
    {
        if (dataRec.C1_TYPE_FLAGS & C1_DIGIT)
        {
            nRetVal = 1; 
        }
        else
        {
            nRetVal = 0;
        }
    }
    else
    {
        TRACE( "No corresonding unicode record for character %d.\n", c );
    }
#endif  /* HAVE_COREFOUNDATION */
    LOGEXIT("PAL_iswdigit returning %d\n", nRetVal);
    PERF_EXIT(iswdigit);
    return nRetVal;
}

/*++
Function:

    iswxdigit
    
See MSDN for more details.

Notes :
the information in UnicodeData doesn't help us, it doesn't have enough 
granularity. Results in windows show that only ASCII and "Fullwidth" (>0xFF10)
numbers and letters are considered as "hex"; other "numbers" 
(nGeneralCategory==8) aren't.
--*/
int
__cdecl
PAL_iswxdigit( wchar_16 c )
{
    UINT nRetVal = 0;

    PERF_ENTRY(iswxdigit);
    ENTRY("PAL_iswxdigit( c=%d )\n", c);
    
    /* ASCII characters */
    if((c>= 'A' && c<='F') ||        /* uppercase hex letters */
       (c>= 'a' && c<='f') ||        /* lowercase hex letters */
       (c>= '0' && c<='9'))          /* digits */
    {
        nRetVal = 1;
    }
    else
    /* "fullwidth" characters, whatever that is */
    if((c>= 0xFF10 && c<=0xFF19) ||  /* digits */
       (c>= 0xFF21 && c<=0xFF26) ||  /* uppercase hex letters */
       (c>= 0xFF41 && c<=0xFF46))    /* lowercase hex letters */
    {
        nRetVal = 1;
    }
    else
    {
        nRetVal = 0;
    }               
    LOGEXIT("PAL_iswxdigit returning %d\n", nRetVal);
    PERF_EXIT(iswxdigit);
    return nRetVal;
}


/*++
Function:

    iswprint
     
See MSDN for more details.
--*/
int
__cdecl
PAL_iswprint( wchar_16 c ) 
{
    int ret;
    

    PERF_ENTRY(iswprint);
    ENTRY("PAL_iswprint (%#X)\n", c);   

    ret = iswprint(c);

    LOGEXIT("PAL_iswprint returns %d\n", ret);
    PERF_EXIT(iswprint);
    return (ret);
}


/*++
Function:
   PAL_wcscspn

Finds the number of consecutive characters from the start of the string
that are not in the set.

Return value:

The number of characters from the start of the string that are not in
the set.

Parameters:
string          String
strCharSet      Set of delimiter characters

--*/
size_t
__cdecl
PAL_wcscspn(const wchar_16 *string, const wchar_16 *strCharSet)
{
    const wchar_16 *temp;
    size_t count = 0;

    PERF_ENTRY(wcscspn);
    
    while(*string != 0)
    {
        for(temp = strCharSet; *temp != 0; temp++)
        {
            if (*string == *temp)
            {
                PERF_EXIT(wcscspn);
                return count;
            }
        }
        count++;
        string++;
    }
    PERF_EXIT(wcscspn);
    return count;
}

#if HAVE_COREFOUNDATION
/*--
Function:
  PAL_iswblank

Returns TRUE if c is a Win32 "blank" character.
--*/
int 
__cdecl 
PAL_iswblank(wchar_16 c)
{
    int ret;
    static CFCharacterSetRef sSpaceAndNewlineSet;
    
    if (sSpaceAndNewlineSet == NULL)
    {
        sSpaceAndNewlineSet = CFCharacterSetGetPredefined(
                                            kCFCharacterSetWhitespaceAndNewline);
    }
    switch (c)
    {
        case 0x0085:
        case 0x1680:
        case 0x202f:
        case 0xfeff:
            // These are blank characters on Windows, but are not part
            // of the SpaceAndNewline character set in Core Foundation.
            ret = TRUE;
            break;
        case 0x2028:
        case 0x2029:
            // These are not blank characters on Windows, but are part
            // of the SpaceAndNewline character set in Core Foundation.
            ret = FALSE;
            break;
        default:
            ret = CFCharacterSetIsCharacterMember(sSpaceAndNewlineSet, c);
            break;
    }
    return ret;
}

/*--
Function:
  PAL_iswcntrl

Returns TRUE if c is a control character.
--*/
int 
__cdecl 
PAL_iswcntrl(wchar_16 c)
{
    int ret;
    static CFCharacterSetRef sControlSet;
    
    if (sControlSet == NULL)
    {
        sControlSet = CFCharacterSetGetPredefined(kCFCharacterSetControl);
    }
    ret = CFCharacterSetIsCharacterMember(sControlSet, c);
    return ret;
}

/*--
Function:
  PAL_iswpunct

Returns TRUE if c is a punctuation character.
--*/
int 
__cdecl 
PAL_iswpunct(wchar_16 c)
{
    int ret;
    static CFCharacterSetRef sPunctuationSet = NULL;
    static CFCharacterSetRef sSymbolSet = NULL;

    if (sPunctuationSet == NULL)
    {
        sPunctuationSet = CFCharacterSetGetPredefined(kCFCharacterSetPunctuation);
    }
    if (sSymbolSet == NULL)
    {
        sSymbolSet = CFCharacterSetGetPredefined(kCFCharacterSetSymbol);
    }
    ret = CFCharacterSetIsCharacterMember(sPunctuationSet, c) ||
          CFCharacterSetIsCharacterMember(sSymbolSet, c);
    return ret;
}
#endif  // HAVE_COREFOUNDATION