forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwchar.cpp
More file actions
2036 lines (1695 loc) · 46.1 KB
/
wchar.cpp
File metadata and controls
2036 lines (1695 loc) · 46.1 KB
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license 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
#include "runtime_proxy.h"
#include <errno.h>
SET_DEFAULT_DEBUG_CHANNEL(CRT);
/*--
Function:
wtolower (internal)
16-bit wide character version of the ANSI tolower() function.
--*/
static
char16_t
wtolower(char16_t c)
{
/* Note: Surrogate pairs unicode character are not supported */
#if HAVE_TOWLOWER
char16_t w;
w = (char16_t) c;
w = towlower(w);
return (char16_t) 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.
--*/
char16_t *
__cdecl
_itow(
int value,
char16_t *string,
int radix)
{
char16_t *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 char16_t* %p\n", ret);
PERF_EXIT(_itow);
return ret;
}
/*--
Function:
_itow
16-bit wide character version of the ANSI ltow() function.
--*/
char16_t *
__cdecl
_ltow(
long value,
char16_t *string,
int radix)
{
char16_t *ret;
PERF_ENTRY(_ltow);
ENTRY("_ltow (value=%d, string=%p, radix=%d)\n",
value, string, radix);
ret = Internal_i64tow(value, string, radix, FALSE);
LOGEXIT("_ltow returns char16_t* %p\n", ret);
PERF_EXIT(_ltow);
return ret;
}
/*--
Function:
_i64tow
See MSDN doc
--*/
char16_t *
__cdecl
_i64tow(
__int64 value,
char16_t *string,
int radix)
{
char16_t *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 char16_t* %p\n", ret);
PERF_EXIT(_i64tow);
return ret;
}
/*--
Function:
_wtoi
See MSDN doc
--*/
int
__cdecl
_wtoi(
const char16_t *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(char16_t c)
{
int ret;
PERF_ENTRY(iswspace);
ENTRY("PAL_iswspace (c=%C)\n", c);
ret = proxy_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 char16_t *string1,
const char16_t *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 char16_t *string1,
const char16_t *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
--*/
char16_t *
__cdecl
_wcslwr(
char16_t *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 char16_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 char16_t *nptr,
char16_t **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 = (char16_t *)&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_wcstoll
Convert string to a long-long-integer value.
Return Value
wcstoll returns the value represented in the string nptr, except when
the representation would cause an overflow, in which case it returns
LONGLONG_MAX or LONGLONG_MIN. strtoll 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 wcstoll function converts nptr to a long 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.
--*/
LONGLONG
__cdecl
PAL_wcstoll(
const char16_t *nptr,
char16_t **endptr,
int base)
{
char *s_nptr = 0;
char *s_endptr = 0;
long long res;
int size;
DWORD dwLastError = 0;
PERF_ENTRY(wcstoll);
ENTRY("wcstoll (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 = strtoll(s_nptr, &s_endptr, base);
/* 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 = (char16_t *)&nptr[size];
}
PAL_wcstolExit:
PAL_free(s_nptr);
LOGEXIT("wcstoll returning long %lld\n", res);
PERF_EXIT(wcstoll);
/* This explicit cast to LONGLONG is used to silence any potential warnings
due to implicitly casting the native long res to LONGLONG when returning. */
return (LONGLONG)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 char16_t *nptr,
char16_t **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)
{
char16_t 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 = (char16_t *)&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 char16_t *nptr,
char16_t **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 = (char16_t *)&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
--*/
char16_t
__cdecl
PAL_towlower( char16_t 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
--*/
char16_t
__cdecl
PAL_towupper( char16_t 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
--*/