ntdll: Reimplement bsearch to avoid redundant and possibly out of bounds comparisons.
[wine/hramrach.git] / dlls / msvcrt / wcs.c
blob79be91a0ecacbae9bff9b4669ae401e879cbcd4a
1 /*
2 * msvcrt.dll wide-char functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <limits.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <assert.h>
28 #include "msvcrt.h"
29 #include "winnls.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 /*********************************************************************
37 * _wcsdup (MSVCRT.@)
39 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
41 MSVCRT_wchar_t* ret = NULL;
42 if (str)
44 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
45 ret = MSVCRT_malloc( size );
46 if (ret) memcpy( ret, str, size );
48 return ret;
51 /*********************************************************************
52 * _wcsicoll (MSVCRT.@)
54 INT CDECL _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
56 /* FIXME: handle collates */
57 return strcmpiW( str1, str2 );
60 /*********************************************************************
61 * _wcsnset (MSVCRT.@)
63 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
65 MSVCRT_wchar_t* ret = str;
66 while ((n-- > 0) && *str) *str++ = c;
67 return ret;
70 /*********************************************************************
71 * _wcsrev (MSVCRT.@)
73 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
75 MSVCRT_wchar_t* ret = str;
76 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
77 while (end > str)
79 MSVCRT_wchar_t t = *end;
80 *end-- = *str;
81 *str++ = t;
83 return ret;
86 /*********************************************************************
87 * _wcsset (MSVCRT.@)
89 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
91 MSVCRT_wchar_t* ret = str;
92 while (*str) *str++ = c;
93 return ret;
96 /******************************************************************
97 * _wcsupr_s (MSVCRT.@)
100 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
102 MSVCRT_wchar_t* ptr = str;
104 if (!str || !n)
106 if (str) *str = '\0';
107 *MSVCRT__errno() = MSVCRT_EINVAL;
108 return MSVCRT_EINVAL;
111 while (n--)
113 if (!*ptr) return 0;
114 *ptr = toupperW(*ptr);
115 ptr++;
118 /* MSDN claims that the function should return and set errno to
119 * ERANGE, which doesn't seem to be true based on the tests. */
120 *str = '\0';
121 *MSVCRT__errno() = MSVCRT_EINVAL;
122 return MSVCRT_EINVAL;
125 /*********************************************************************
126 * _wcstod_l - not exported in native msvcrt
128 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
129 MSVCRT__locale_t locale)
131 unsigned __int64 d=0, hlp;
132 unsigned fpcontrol;
133 int exp=0, sign=1;
134 const MSVCRT_wchar_t *p;
135 double ret;
137 if(!str) {
138 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
139 *MSVCRT__errno() = MSVCRT_EINVAL;
140 return 0;
143 if(!locale)
144 locale = get_locale();
146 p = str;
147 while(isspaceW(*p))
148 p++;
150 if(*p == '-') {
151 sign = -1;
152 p++;
153 } else if(*p == '+')
154 p++;
156 while(isdigitW(*p)) {
157 hlp = d*10+*(p++)-'0';
158 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
159 exp++;
160 break;
161 } else
162 d = hlp;
164 while(isdigitW(*p)) {
165 exp++;
166 p++;
168 if(*p == *locale->locinfo->lconv->decimal_point)
169 p++;
171 while(isdigitW(*p)) {
172 hlp = d*10+*(p++)-'0';
173 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
174 break;
176 d = hlp;
177 exp--;
179 while(isdigitW(*p))
180 p++;
182 if(p == str) {
183 if(end)
184 *end = (MSVCRT_wchar_t*)str;
185 return 0.0;
188 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
189 int e=0, s=1;
191 p++;
192 if(*p == '-') {
193 s = -1;
194 p++;
195 } else if(*p == '+')
196 p++;
198 if(isdigitW(*p)) {
199 while(isdigitW(*p)) {
200 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
201 e = INT_MAX;
202 p++;
204 e *= s;
206 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
207 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
208 else exp += e;
209 } else {
210 if(*p=='-' || *p=='+')
211 p--;
212 p--;
216 fpcontrol = _control87(0, 0);
217 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
218 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
220 if(exp>0)
221 ret = (double)sign*d*pow(10, exp);
222 else
223 ret = (double)sign*d/pow(10, -exp);
225 _control87(fpcontrol, 0xffffffff);
227 if((d && ret==0.0) || isinf(ret))
228 *MSVCRT__errno() = MSVCRT_ERANGE;
230 if(end)
231 *end = (MSVCRT_wchar_t*)p;
233 return ret;
236 /*********************************************************************
237 * _wcstombs_l (MSVCRT.@)
239 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
240 MSVCRT_size_t count, MSVCRT__locale_t locale)
242 char default_char = '\0';
243 MSVCRT_size_t tmp;
244 BOOL used_default;
246 if(!locale)
247 locale = get_locale();
249 /* FIXME: Use wcslen here */
250 tmp = strlenW(wcstr);
251 if(tmp>count && mbstr)
252 tmp = count;
254 tmp = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
255 wcstr, tmp, mbstr, count, &default_char, &used_default);
257 if(used_default)
258 return -1;
260 if(tmp<count && mbstr)
261 mbstr[tmp] = '\0';
263 return tmp;
266 /*********************************************************************
267 * wcstombs (MSVCRT.@)
269 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
270 MSVCRT_size_t count)
272 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
275 /*********************************************************************
276 * _wcstombs_s_l (MSVCRT.@)
278 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
279 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
280 MSVCRT_size_t count, MSVCRT__locale_t locale)
282 MSVCRT_size_t conv;
284 if(!mbstr && !size) {
285 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
286 if(ret)
287 *ret = conv;
288 return 0;
291 if(!wcstr || !mbstr) {
292 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
293 if(mbstr && size)
294 mbstr[0] = '\0';
295 *MSVCRT__errno() = MSVCRT_EINVAL;
296 return MSVCRT_EINVAL;
299 if(count==_TRUNCATE || size<count)
300 conv = size;
301 else
302 conv = count;
304 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
305 if(conv<size)
306 mbstr[conv++] = '\0';
307 else if(conv==size && (count==_TRUNCATE || mbstr[conv-1]=='\0'))
308 mbstr[conv-1] = '\0';
309 else {
310 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
311 if(size)
312 mbstr[0] = '\0';
313 *MSVCRT__errno() = MSVCRT_ERANGE;
314 return MSVCRT_ERANGE;
317 if(ret)
318 *ret = conv;
319 return 0;
322 /*********************************************************************
323 * wcstombs_s (MSVCRT.@)
325 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
326 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
328 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
331 /*********************************************************************
332 * wcstod (MSVCRT.@)
334 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
336 return MSVCRT__wcstod_l(lpszStr, end, NULL);
339 /*********************************************************************
340 * _wtof (MSVCRT.@)
342 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
344 return MSVCRT__wcstod_l(str, NULL, NULL);
347 /*********************************************************************
348 * _wtof_l (MSVCRT.@)
350 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
352 return MSVCRT__wcstod_l(str, NULL, locale);
355 typedef struct pf_output_t
357 int used;
358 int len;
359 BOOL unicode;
360 union {
361 LPWSTR W;
362 LPSTR A;
363 } buf;
364 } pf_output;
366 typedef struct pf_flags_t
368 char Sign, LeftAlign, Alternate, PadZero;
369 int FieldLength, Precision;
370 char IntegerLength, IntegerDouble;
371 char WideString;
372 char Format;
373 } pf_flags;
376 * writes a string of characters to the output
377 * returns -1 if the string doesn't fit in the output buffer
378 * return the length of the string if all characters were written
380 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
382 int space = out->len - out->used;
384 if( len < 0 )
385 len = strlenW( str );
386 if( out->unicode )
388 LPWSTR p = out->buf.W + out->used;
390 if( space >= len )
392 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
393 out->used += len;
394 return len;
396 if( space > 0 && out->buf.W )
397 memcpy( p, str, space*sizeof(WCHAR) );
398 out->used += len;
400 else
402 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
403 LPSTR p = out->buf.A + out->used;
405 if( space >= n )
407 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
408 out->used += n;
409 return len;
411 if( space > 0 && out->buf.A )
412 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
413 out->used += n;
415 return -1;
418 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
420 int space = out->len - out->used;
422 if( len < 0 )
423 len = strlen( str );
424 if( !out->unicode )
426 LPSTR p = out->buf.A + out->used;
428 if( space >= len )
430 if (out->buf.A) memcpy( p, str, len );
431 out->used += len;
432 return len;
434 if( space > 0 && out->buf.A )
435 memcpy( p, str, space );
436 out->used += len;
438 else
440 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
441 LPWSTR p = out->buf.W + out->used;
443 if( space >= n )
445 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
446 out->used += n;
447 return len;
449 if( space > 0 && out->buf.W )
450 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
451 out->used += n;
453 return -1;
456 /* pf_fill: takes care of signs, alignment, zero and field padding */
457 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
459 int i, r = 0;
461 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
462 flags->Sign = 0;
464 if( left && flags->Sign )
466 flags->FieldLength--;
467 if( flags->PadZero )
468 r = pf_output_stringA( out, &flags->Sign, 1 );
471 if( ( !left && flags->LeftAlign ) ||
472 ( left && !flags->LeftAlign ))
474 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
476 if( left && flags->PadZero )
477 r = pf_output_stringA( out, "0", 1 );
478 else
479 r = pf_output_stringA( out, " ", 1 );
483 if( left && flags->Sign && !flags->PadZero )
484 r = pf_output_stringA( out, &flags->Sign, 1 );
486 return r;
489 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
490 int len, pf_flags *flags )
492 int r = 0;
494 if( len < 0 )
495 len = strlenW( str );
497 if (flags->Precision >= 0 && flags->Precision < len)
498 len = flags->Precision;
500 r = pf_fill( out, len, flags, 1 );
502 if( r>=0 )
503 r = pf_output_stringW( out, str, len );
505 if( r>=0 )
506 r = pf_fill( out, len, flags, 0 );
508 return r;
511 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
512 int len, pf_flags *flags )
514 int r = 0;
516 if( len < 0 )
517 len = strlen( str );
519 if (flags->Precision >= 0 && flags->Precision < len)
520 len = flags->Precision;
522 r = pf_fill( out, len, flags, 1 );
524 if( r>=0 )
525 r = pf_output_stringA( out, str, len );
527 if( r>=0 )
528 r = pf_fill( out, len, flags, 0 );
530 return r;
533 static int pf_handle_string_format( pf_output *out, const void* str, int len,
534 pf_flags *flags, BOOL capital_letter)
536 if(str == NULL) /* catch NULL pointer */
537 return pf_output_format_A( out, "(null)", -1, flags);
539 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
540 if(flags->WideString || flags->IntegerLength == 'l')
541 return pf_output_format_W( out, str, len, flags);
542 if(flags->IntegerLength == 'h')
543 return pf_output_format_A( out, str, len, flags);
545 /* %s,%c -> chars in ansi functions & wchars in unicode
546 * %S,%C -> wchars in ansi functions & chars in unicode */
547 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
548 return pf_output_format_A( out, str, len, flags);
549 else
550 return pf_output_format_W( out, str, len, flags);
553 static inline BOOL pf_is_integer_format( char fmt )
555 static const char float_fmts[] = "diouxX";
556 if (!fmt)
557 return FALSE;
558 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
561 static inline BOOL pf_is_double_format( char fmt )
563 static const char float_fmts[] = "aeEfgG";
564 if (!fmt)
565 return FALSE;
566 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
569 static inline BOOL pf_is_valid_format( char fmt )
571 static const char float_fmts[] = "acCdeEfgGinouxX";
572 if (!fmt)
573 return FALSE;
574 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
577 static void pf_rebuild_format_string( char *p, pf_flags *flags )
579 *p++ = '%';
580 if( flags->Sign )
581 *p++ = flags->Sign;
582 if( flags->LeftAlign )
583 *p++ = flags->LeftAlign;
584 if( flags->Alternate )
585 *p++ = flags->Alternate;
586 if( flags->PadZero )
587 *p++ = flags->PadZero;
588 if( flags->FieldLength )
590 sprintf(p, "%d", flags->FieldLength);
591 p += strlen(p);
593 if( flags->Precision >= 0 )
595 sprintf(p, ".%d", flags->Precision);
596 p += strlen(p);
598 *p++ = flags->Format;
599 *p++ = 0;
602 /* pf_integer_conv: prints x to buf, including alternate formats and
603 additional precision digits, but not field characters or the sign */
604 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
605 LONGLONG x )
607 unsigned int base;
608 const char *digits;
610 int i, j, k;
611 char number[40], *tmp = number;
613 if( buf_len > sizeof number )
614 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
616 base = 10;
617 if( flags->Format == 'o' )
618 base = 8;
619 else if( flags->Format == 'x' || flags->Format == 'X' )
620 base = 16;
622 if( flags->Format == 'X' )
623 digits = "0123456789ABCDEFX";
624 else
625 digits = "0123456789abcdefx";
627 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
629 x = -x;
630 flags->Sign = '-';
633 /* Do conversion (backwards) */
634 i = 0;
635 if( x == 0 && flags->Precision )
636 tmp[i++] = '0';
637 else
638 while( x != 0 )
640 j = (ULONGLONG) x % base;
641 x = (ULONGLONG) x / base;
642 tmp[i++] = digits[j];
644 k = flags->Precision - i;
645 while( k-- > 0 )
646 tmp[i++] = '0';
647 if( flags->Alternate )
649 if( base == 16 )
651 tmp[i++] = digits[16];
652 tmp[i++] = '0';
654 else if( base == 8 && tmp[i-1] != '0' )
655 tmp[i++] = '0';
658 /* Reverse for buf */
659 j = 0;
660 while( i-- > 0 )
661 buf[j++] = tmp[i];
662 buf[j] = '\0';
664 /* Adjust precision so pf_fill won't truncate the number later */
665 flags->Precision = strlen( buf );
667 if( tmp != number )
668 HeapFree( GetProcessHeap(), 0, tmp );
670 return;
673 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
674 to 3 digits, accounting for padding, in place. Needed to match
675 the native printf's which always use 3 digits. */
676 static void pf_fixup_exponent( char *buf )
678 char* tmp = buf;
680 while (tmp[0] && toupper(tmp[0]) != 'E')
681 tmp++;
683 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
684 isdigit(tmp[2]) && isdigit(tmp[3]))
686 char final;
688 if (isdigit(tmp[4]))
689 return; /* Exponent already 3 digits */
691 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
692 tmp += 2;
693 final = tmp[2];
694 tmp[2] = tmp[1];
695 tmp[1] = tmp[0];
696 tmp[0] = '0';
697 if (final == '\0')
699 /* We didn't expand into trailing space, so this string isn't left
700 * justified. Terminate the string and strip a ' ' at the start of
701 * the string if there is one (as there may be if the string is
702 * right justified).
704 tmp[3] = '\0';
705 if (buf[0] == ' ')
706 memmove(buf, buf + 1, (tmp - buf) + 3);
708 /* Otherwise, we expanded into trailing space -> nothing to do */
712 /*********************************************************************
713 * pf_vsnprintf (INTERNAL)
715 * implements both A and W vsnprintf functions
717 static int pf_vsnprintf( pf_output *out, const WCHAR *format,
718 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
720 int r;
721 LPCWSTR q, p = format;
722 pf_flags flags;
724 if(!locale)
725 locale = get_locale();
727 TRACE("format is %s\n",debugstr_w(format));
728 while (*p)
730 q = strchrW( p, '%' );
732 /* there's no % characters left, output the rest of the string */
733 if( !q )
735 r = pf_output_stringW(out, p, -1);
736 if( r<0 )
737 return r;
738 p += r;
739 continue;
742 /* there's characters before the %, output them */
743 if( q != p )
745 r = pf_output_stringW(out, p, q - p);
746 if( r<0 )
747 return r;
748 p = q;
751 /* we must be at a % now, skip over it */
752 assert( *p == '%' );
753 p++;
755 /* output a single % character */
756 if( *p == '%' )
758 r = pf_output_stringW(out, p++, 1);
759 if( r<0 )
760 return r;
761 continue;
764 /* parse the flags */
765 memset( &flags, 0, sizeof flags );
766 while (*p)
768 if( *p == '+' || *p == ' ' )
770 if ( flags.Sign != '+' )
771 flags.Sign = *p;
773 else if( *p == '-' )
774 flags.LeftAlign = *p;
775 else if( *p == '0' )
776 flags.PadZero = *p;
777 else if( *p == '#' )
778 flags.Alternate = *p;
779 else
780 break;
781 p++;
784 /* deal with the field width specifier */
785 flags.FieldLength = 0;
786 if( *p == '*' )
788 flags.FieldLength = va_arg( valist, int );
789 if (flags.FieldLength < 0)
791 flags.LeftAlign = '-';
792 flags.FieldLength = -flags.FieldLength;
794 p++;
796 else while( isdigit(*p) )
798 flags.FieldLength *= 10;
799 flags.FieldLength += *p++ - '0';
802 /* deal with precision */
803 flags.Precision = -1;
804 if( *p == '.' )
806 flags.Precision = 0;
807 p++;
808 if( *p == '*' )
810 flags.Precision = va_arg( valist, int );
811 p++;
813 else while( isdigit(*p) )
815 flags.Precision *= 10;
816 flags.Precision += *p++ - '0';
820 /* deal with integer width modifier */
821 while( *p )
823 if( *p == 'h' || *p == 'l' || *p == 'L' )
825 flags.IntegerLength = *p;
826 p++;
828 else if( *p == 'I' )
830 if( *(p+1) == '6' && *(p+2) == '4' )
832 flags.IntegerDouble++;
833 p += 3;
835 else if( *(p+1) == '3' && *(p+2) == '2' )
836 p += 3;
837 else if( isdigit(*(p+1)) || *(p+1) == 0 )
838 break;
839 else
840 p++;
842 else if( *p == 'w' )
843 flags.WideString = *p++;
844 else if( *p == 'F' )
845 p++; /* ignore */
846 else
847 break;
850 flags.Format = *p;
851 r = 0;
853 if (flags.Format == '$')
855 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
856 return -1;
858 /* output a string */
859 if( flags.Format == 's' || flags.Format == 'S' )
860 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
861 &flags, (flags.Format == 'S') );
863 /* output a single character */
864 else if( flags.Format == 'c' || flags.Format == 'C' )
866 INT ch = va_arg( valist, int );
868 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
871 /* output a pointer */
872 else if( flags.Format == 'p' )
874 char pointer[32];
875 void *ptr = va_arg( valist, void * );
877 flags.PadZero = 0;
878 if( flags.Alternate )
879 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
880 else
881 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
882 r = pf_output_format_A( out, pointer, -1, &flags );
885 /* deal with %n */
886 else if( flags.Format == 'n' )
888 int *x = va_arg(valist, int *);
889 *x = out->used;
892 /* deal with 64-bit integers */
893 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
895 char number[40], *x = number;
897 /* Estimate largest possible required buffer size:
898 * Chooses the larger of the field or precision
899 * Includes extra bytes: 1 byte for null, 1 byte for sign,
900 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
901 for a decimal, and 1 byte for an additional float digit. */
902 int x_len = ((flags.FieldLength > flags.Precision) ?
903 flags.FieldLength : flags.Precision) + 10;
905 if( x_len >= sizeof number)
906 x = HeapAlloc( GetProcessHeap(), 0, x_len );
908 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
910 r = pf_output_format_A( out, x, -1, &flags );
911 if( x != number )
912 HeapFree( GetProcessHeap(), 0, x );
915 /* deal with integers and floats using libc's printf */
916 else if( pf_is_valid_format( flags.Format ) )
918 char fmt[20], number[40], *x = number, *decimal_point;
920 /* Estimate largest possible required buffer size:
921 * Chooses the larger of the field or precision
922 * Includes extra bytes: 1 byte for null, 1 byte for sign,
923 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
924 for a decimal, and 1 byte for an additional float digit. */
925 int x_len = ((flags.FieldLength > flags.Precision) ?
926 flags.FieldLength : flags.Precision) + 10;
928 if( x_len >= sizeof number)
929 x = HeapAlloc( GetProcessHeap(), 0, x_len );
931 pf_rebuild_format_string( fmt, &flags );
933 if( pf_is_double_format( flags.Format ) )
935 sprintf( x, fmt, va_arg(valist, double) );
936 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
937 pf_fixup_exponent( x );
939 else
940 sprintf( x, fmt, va_arg(valist, int) );
942 decimal_point = strchr(x, '.');
943 if(decimal_point)
944 *decimal_point = *locale->locinfo->lconv->decimal_point;
946 r = pf_output_stringA( out, x, -1 );
947 if( x != number )
948 HeapFree( GetProcessHeap(), 0, x );
949 if(r < 0)
950 return r;
952 else
954 if( valid )
956 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
957 *MSVCRT__errno() = MSVCRT_EINVAL;
958 return -1;
961 continue;
964 if( r<0 )
965 return r;
966 p++;
969 /* check we reached the end, and null terminate the string */
970 assert( *p == 0 );
971 pf_output_stringW( out, p, 1 );
973 return out->used - 1;
976 /*********************************************************************
977 * vsnprintf_internal (INTERNAL)
979 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
980 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
982 DWORD sz;
983 LPWSTR formatW = NULL;
984 pf_output out;
985 int r;
987 out.unicode = FALSE;
988 out.buf.A = str;
989 out.used = 0;
990 out.len = len;
992 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
993 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
994 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
996 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
998 HeapFree( GetProcessHeap(), 0, formatW );
1000 return r;
1003 /*********************************************************************
1004 * _vsnprintf (MSVCRT.@)
1006 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1007 const char *format, __ms_va_list valist )
1009 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1012 /*********************************************************************
1013 * _vsnprintf_l (MSVCRT.@)
1015 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1016 MSVCRT__locale_t locale, __ms_va_list valist )
1018 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1021 /*********************************************************************
1022 * _vsnprintf_s_l (MSVCRT.@)
1024 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1025 MSVCRT_size_t count, const char *format,
1026 MSVCRT__locale_t locale, __ms_va_list valist )
1028 int len, ret;
1030 if(sizeOfBuffer<count+1 || count==-1)
1031 len = sizeOfBuffer;
1032 else
1033 len = count+1;
1035 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1037 if(ret<0 || ret==len) {
1038 if(count!=_TRUNCATE && count>sizeOfBuffer) {
1039 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1040 *MSVCRT__errno() = MSVCRT_ERANGE;
1041 memset(str, 0, sizeOfBuffer);
1042 } else
1043 str[len-1] = '\0';
1045 return -1;
1048 return ret;
1051 /*********************************************************************
1052 * _vsnprintf_s (MSVCRT.@)
1054 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1055 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1057 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1060 /*********************************************************************
1061 * vsprintf (MSVCRT.@)
1063 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1065 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1068 /*********************************************************************
1069 * vsprintf_s (MSVCRT.@)
1071 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1073 return MSVCRT_vsnprintf(str, num, format, valist);
1076 /*********************************************************************
1077 * _vscprintf (MSVCRT.@)
1079 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1081 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1084 /*********************************************************************
1085 * _snprintf (MSVCRT.@)
1087 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1089 int retval;
1090 __ms_va_list valist;
1091 __ms_va_start(valist, format);
1092 retval = MSVCRT_vsnprintf(str, len, format, valist);
1093 __ms_va_end(valist);
1094 return retval;
1097 /*********************************************************************
1098 * vsnwsprintf_internal (INTERNAL)
1100 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1101 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1102 __ms_va_list valist)
1104 pf_output out;
1106 out.unicode = TRUE;
1107 out.buf.W = str;
1108 out.used = 0;
1109 out.len = len;
1111 return pf_vsnprintf( &out, format, locale, valid, valist );
1114 /*********************************************************************
1115 * _vsnwsprintf (MSVCRT.@)
1117 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1118 const MSVCRT_wchar_t *format, __ms_va_list valist)
1120 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1123 /*********************************************************************
1124 * _vsnwsprintf_l (MSVCRT.@)
1126 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1127 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1128 __ms_va_list valist)
1130 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1133 /*********************************************************************
1134 * _vsnwsprintf_s_l (MSVCRT.@)
1136 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1137 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1138 MSVCRT__locale_t locale, __ms_va_list valist)
1140 int len, ret;
1142 len = sizeOfBuffer/sizeof(MSVCRT_wchar_t);
1143 if(count!=-1 && len>count+1)
1144 len = count+1;
1146 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1148 if(ret<0 || ret==len) {
1149 if(count!=_TRUNCATE && count>sizeOfBuffer/sizeof(MSVCRT_wchar_t)) {
1150 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1151 *MSVCRT__errno() = MSVCRT_ERANGE;
1152 memset(str, 0, sizeOfBuffer);
1153 } else
1154 str[len-1] = '\0';
1156 return -1;
1159 return ret;
1162 /*********************************************************************
1163 * _vsnwsprintf_s (MSVCRT.@)
1165 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1166 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1168 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1169 format, NULL, valist);
1172 /*********************************************************************
1173 * _snwprintf (MSVCRT.@)
1175 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1177 int retval;
1178 __ms_va_list valist;
1179 __ms_va_start(valist, format);
1180 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1181 __ms_va_end(valist);
1182 return retval;
1185 /*********************************************************************
1186 * sprintf (MSVCRT.@)
1188 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1190 __ms_va_list ap;
1191 int r;
1193 __ms_va_start( ap, format );
1194 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1195 __ms_va_end( ap );
1196 return r;
1199 /*********************************************************************
1200 * sprintf_s (MSVCRT.@)
1202 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1204 __ms_va_list ap;
1205 int r;
1207 __ms_va_start( ap, format );
1208 r = MSVCRT_vsnprintf( str, num, format, ap );
1209 __ms_va_end( ap );
1210 return r;
1213 /*********************************************************************
1214 * swprintf (MSVCRT.@)
1216 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1218 __ms_va_list ap;
1219 int r;
1221 __ms_va_start( ap, format );
1222 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1223 __ms_va_end( ap );
1224 return r;
1227 /*********************************************************************
1228 * swprintf_s (MSVCRT.@)
1230 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1231 const MSVCRT_wchar_t *format, ... )
1233 __ms_va_list ap;
1234 int r;
1236 __ms_va_start(ap, format);
1237 r = MSVCRT_vsnwprintf_s(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1238 INT_MAX, format, ap);
1239 __ms_va_end(ap);
1241 return r;
1244 /*********************************************************************
1245 * vswprintf (MSVCRT.@)
1247 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1249 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1252 /*********************************************************************
1253 * _vscwprintf (MSVCRT.@)
1255 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1257 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1260 /*********************************************************************
1261 * vswprintf_s (MSVCRT.@)
1263 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1264 const MSVCRT_wchar_t* format, __ms_va_list args)
1266 return MSVCRT_vsnwprintf_s(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1267 INT_MAX, format, args);
1270 /*********************************************************************
1271 * _vswprintf_s_l (MSVCRT.@)
1273 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1274 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1276 return MSVCRT_vsnwprintf_s_l(str, numberOfElements*sizeof(MSVCRT_wchar_t),
1277 INT_MAX, format, locale, args );
1280 /*********************************************************************
1281 * wcscoll (MSVCRT.@)
1283 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1285 /* FIXME: handle collates */
1286 return strcmpW( str1, str2 );
1289 /*********************************************************************
1290 * wcspbrk (MSVCRT.@)
1292 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1294 const MSVCRT_wchar_t* p;
1295 while (*str)
1297 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1298 str++;
1300 return NULL;
1303 /*********************************************************************
1304 * wcstok (MSVCRT.@)
1306 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1308 thread_data_t *data = msvcrt_get_thread_data();
1309 MSVCRT_wchar_t *ret;
1311 if (!str)
1312 if (!(str = data->wcstok_next)) return NULL;
1314 while (*str && strchrW( delim, *str )) str++;
1315 if (!*str) return NULL;
1316 ret = str++;
1317 while (*str && !strchrW( delim, *str )) str++;
1318 if (*str) *str++ = 0;
1319 data->wcstok_next = str;
1320 return ret;
1324 /*********************************************************************
1325 * wctomb (MSVCRT.@)
1327 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1329 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1332 /*********************************************************************
1333 * iswalnum (MSVCRT.@)
1335 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1337 return isalnumW( wc );
1340 /*********************************************************************
1341 * iswalpha (MSVCRT.@)
1343 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1345 return isalphaW( wc );
1348 /*********************************************************************
1349 * iswalpha_l (MSVCRT.@)
1351 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1353 return isalphaW( wc );
1356 /*********************************************************************
1357 * iswcntrl (MSVCRT.@)
1359 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1361 return iscntrlW( wc );
1364 /*********************************************************************
1365 * iswdigit (MSVCRT.@)
1367 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1369 return isdigitW( wc );
1372 /*********************************************************************
1373 * iswgraph (MSVCRT.@)
1375 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1377 return isgraphW( wc );
1380 /*********************************************************************
1381 * iswlower (MSVCRT.@)
1383 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1385 return islowerW( wc );
1388 /*********************************************************************
1389 * iswprint (MSVCRT.@)
1391 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1393 return isprintW( wc );
1396 /*********************************************************************
1397 * iswpunct (MSVCRT.@)
1399 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1401 return ispunctW( wc );
1404 /*********************************************************************
1405 * iswspace (MSVCRT.@)
1407 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1409 return isspaceW( wc );
1412 /*********************************************************************
1413 * iswupper (MSVCRT.@)
1415 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1417 return isupperW( wc );
1420 /*********************************************************************
1421 * iswxdigit (MSVCRT.@)
1423 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1425 return isxdigitW( wc );
1428 /*********************************************************************
1429 * wcscpy_s (MSVCRT.@)
1431 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1433 MSVCRT_size_t size = 0;
1435 if(!wcDest || !numElement)
1436 return MSVCRT_EINVAL;
1438 wcDest[0] = 0;
1440 if(!wcSrc)
1442 return MSVCRT_EINVAL;
1445 size = strlenW(wcSrc) + 1;
1447 if(size > numElement)
1449 return MSVCRT_ERANGE;
1452 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1454 return 0;
1457 /******************************************************************
1458 * wcsncpy_s (MSVCRT.@)
1460 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1461 MSVCRT_size_t count )
1463 MSVCRT_size_t size = 0;
1465 if (!wcDest || !numElement)
1466 return MSVCRT_EINVAL;
1468 wcDest[0] = 0;
1470 if (!wcSrc)
1472 return MSVCRT_EINVAL;
1475 size = min(strlenW(wcSrc), count);
1477 if (size >= numElement)
1479 return MSVCRT_ERANGE;
1482 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1483 wcDest[size] = '\0';
1485 return 0;
1488 /******************************************************************
1489 * wcscat_s (MSVCRT.@)
1492 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1494 MSVCRT_wchar_t* ptr = dst;
1496 if (!dst || elem == 0) return MSVCRT_EINVAL;
1497 if (!src)
1499 dst[0] = '\0';
1500 return MSVCRT_EINVAL;
1503 /* seek to end of dst string (or elem if no end of string is found */
1504 while (ptr < dst + elem && *ptr != '\0') ptr++;
1505 while (ptr < dst + elem)
1507 if ((*ptr++ = *src++) == '\0') return 0;
1509 /* not enough space */
1510 dst[0] = '\0';
1511 return MSVCRT_ERANGE;
1514 /*********************************************************************
1515 * _wctoi64_l (MSVCRT.@)
1517 * FIXME: locale parameter is ignored
1519 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1520 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1522 BOOL negative = FALSE;
1523 __int64 ret = 0;
1525 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1527 if(!nptr || base<0 || base>36 || base==1) {
1528 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1529 return 0;
1532 while(isspaceW(*nptr)) nptr++;
1534 if(*nptr == '-') {
1535 negative = TRUE;
1536 nptr++;
1537 } else if(*nptr == '+')
1538 nptr++;
1540 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1541 base = 16;
1542 nptr += 2;
1545 if(base == 0) {
1546 if(*nptr=='0')
1547 base = 8;
1548 else
1549 base = 10;
1552 while(*nptr) {
1553 char cur = tolowerW(*nptr);
1554 int v;
1556 if(isdigitW(cur)) {
1557 if(cur >= '0'+base)
1558 break;
1559 v = cur-'0';
1560 } else {
1561 if(cur<'a' || cur>='a'+base-10)
1562 break;
1563 v = cur-'a'+10;
1566 if(negative)
1567 v = -v;
1569 nptr++;
1571 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1572 ret = MSVCRT_I64_MAX;
1573 *MSVCRT__errno() = MSVCRT_ERANGE;
1574 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1575 ret = MSVCRT_I64_MIN;
1576 *MSVCRT__errno() = MSVCRT_ERANGE;
1577 } else
1578 ret = ret*base + v;
1581 if(endptr)
1582 *endptr = (MSVCRT_wchar_t*)nptr;
1584 return ret;
1587 /*********************************************************************
1588 * _wcstoi64 (MSVCRT.@)
1590 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1591 MSVCRT_wchar_t **endptr, int base)
1593 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1596 /*********************************************************************
1597 * _wcstoui64_l (MSVCRT.@)
1599 * FIXME: locale parameter is ignored
1601 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1602 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1604 BOOL negative = FALSE;
1605 unsigned __int64 ret = 0;
1607 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1609 if(!nptr || base<0 || base>36 || base==1) {
1610 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1611 return 0;
1614 while(isspaceW(*nptr)) nptr++;
1616 if(*nptr == '-') {
1617 negative = TRUE;
1618 nptr++;
1619 } else if(*nptr == '+')
1620 nptr++;
1622 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1623 base = 16;
1624 nptr += 2;
1627 if(base == 0) {
1628 if(*nptr=='0')
1629 base = 8;
1630 else
1631 base = 10;
1634 while(*nptr) {
1635 char cur = tolowerW(*nptr);
1636 int v;
1638 if(isdigit(cur)) {
1639 if(cur >= '0'+base)
1640 break;
1641 v = *nptr-'0';
1642 } else {
1643 if(cur<'a' || cur>='a'+base-10)
1644 break;
1645 v = cur-'a'+10;
1648 nptr++;
1650 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1651 ret = MSVCRT_UI64_MAX;
1652 *MSVCRT__errno() = MSVCRT_ERANGE;
1653 } else
1654 ret = ret*base + v;
1657 if(endptr)
1658 *endptr = (MSVCRT_wchar_t*)nptr;
1660 return negative ? -ret : ret;
1663 /*********************************************************************
1664 * _wcstoui64 (MSVCRT.@)
1666 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1667 MSVCRT_wchar_t **endptr, int base)
1669 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);