2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
39 /*********************************************************************
43 char* CDECL
_strdup(const char* str
)
47 char * ret
= malloc(strlen(str
)+1);
48 if (ret
) strcpy( ret
, str
);
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL
_strlwr_s_l(char *str
, size_t len
, _locale_t locale
)
59 pthreadlocinfo locinfo
;
82 locinfo
= get_locinfo();
84 locinfo
= locale
->locinfo
;
86 if(!locinfo
->lc_handle
[LC_CTYPE
])
90 if (*str
>= 'A' && *str
<= 'Z')
99 *str
= _tolower_l((unsigned char)*str
, locale
);
107 /*********************************************************************
108 * _strlwr_s (MSVCRT.@)
110 int CDECL
_strlwr_s(char *str
, size_t len
)
112 return _strlwr_s_l(str
, len
, NULL
);
115 /*********************************************************************
116 * _strlwr_l (MSVCRT.@)
118 char* CDECL
_strlwr_l(char *str
, _locale_t locale
)
120 _strlwr_s_l(str
, -1, locale
);
124 /*********************************************************************
127 char* CDECL
_strlwr(char *str
)
129 _strlwr_s_l(str
, -1, NULL
);
133 /*********************************************************************
134 * _strupr_s_l (MSVCRT.@)
136 int CDECL
_strupr_s_l(char *str
, size_t len
, _locale_t locale
)
138 pthreadlocinfo locinfo
;
161 locinfo
= get_locinfo();
163 locinfo
= locale
->locinfo
;
165 if(!locinfo
->lc_handle
[LC_CTYPE
])
169 if (*str
>= 'a' && *str
<= 'z')
178 *str
= _toupper_l((unsigned char)*str
, locale
);
186 /*********************************************************************
187 * _strupr_s (MSVCRT.@)
189 int CDECL
_strupr_s(char *str
, size_t len
)
191 return _strupr_s_l(str
, len
, NULL
);
194 /*********************************************************************
195 * _strupr_l (MSVCRT.@)
197 char* CDECL
_strupr_l(char *str
, _locale_t locale
)
199 _strupr_s_l(str
, -1, locale
);
203 /*********************************************************************
206 char* CDECL
_strupr(char *str
)
208 _strupr_s_l(str
, -1, NULL
);
212 /*********************************************************************
213 * _strnset_s (MSVCRT.@)
215 int CDECL
_strnset_s(char *str
, size_t size
, int c
, size_t count
)
219 if(!str
&& !size
&& !count
) return 0;
220 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
221 if(!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
223 for(i
=0; i
<size
-1 && i
<count
; i
++) {
224 if(!str
[i
]) return 0;
228 if(!str
[i
]) return 0;
231 _invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
236 /*********************************************************************
237 * _strnset (MSVCRT.@)
239 char* CDECL
_strnset(char* str
, int value
, size_t len
)
242 while (*str
&& len
--)
247 /*********************************************************************
250 char* CDECL
_strrev(char* str
)
256 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
266 /*********************************************************************
269 char* CDECL
_strset(char* str
, int value
)
278 /*********************************************************************
281 char * CDECL
strtok( char *str
, const char *delim
)
283 thread_data_t
*data
= msvcrt_get_thread_data();
287 if (!(str
= data
->strtok_next
)) return NULL
;
289 while (*str
&& strchr( delim
, *str
)) str
++;
292 data
->strtok_next
= str
;
296 while (*str
&& !strchr( delim
, *str
)) str
++;
297 if (*str
) *str
++ = 0;
298 data
->strtok_next
= str
;
302 /*********************************************************************
303 * strtok_s (MSVCRT.@)
305 char * CDECL
strtok_s(char *str
, const char *delim
, char **ctx
)
307 if (!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
308 if (!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
309 if (!MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) return NULL
;
314 while(*str
&& strchr(delim
, *str
))
323 while(**ctx
&& !strchr(delim
, **ctx
))
331 /*********************************************************************
334 void CDECL
_swab(char* src
, char* dst
, int len
)
338 len
= (unsigned)len
>> 1;
350 static struct fpnum
fpnum(int sign
, int exp
, ULONGLONG m
, enum fpmod mod
)
361 int fpnum_double(struct fpnum
*fp
, double *d
)
365 if (fp
->mod
== FP_VAL_INFINITY
)
367 *d
= fp
->sign
* INFINITY
;
371 if (fp
->mod
== FP_VAL_NAN
)
375 bits
&= ~((ULONGLONG
)1 << (MANT_BITS
+ EXP_BITS
- 1));
376 *d
= *(double*)&bits
;
380 TRACE("%c %s *2^%d (round %d)\n", fp
->sign
== -1 ? '-' : '+',
381 wine_dbgstr_longlong(fp
->m
), fp
->exp
, fp
->mod
);
388 /* make sure that we don't overflow modifying exponent */
389 if (fp
->exp
> 1<<EXP_BITS
)
391 *d
= fp
->sign
* INFINITY
;
394 if (fp
->exp
< -(1<<EXP_BITS
))
399 fp
->exp
+= MANT_BITS
- 1;
401 /* normalize mantissa */
402 while(fp
->m
< (ULONGLONG
)1 << (MANT_BITS
-1))
407 while(fp
->m
>= (ULONGLONG
)1 << MANT_BITS
)
409 if (fp
->m
& 1 || fp
->mod
!= FP_ROUND_ZERO
)
411 if (!(fp
->m
& 1)) fp
->mod
= FP_ROUND_DOWN
;
412 else if(fp
->mod
== FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_EVEN
;
413 else fp
->mod
= FP_ROUND_UP
;
418 fp
->exp
+= (1 << (EXP_BITS
-1)) - 1;
420 /* handle subnormals */
423 if (fp
->m
& 1 && fp
->mod
== FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_EVEN
;
424 else if (fp
->m
& 1) fp
->mod
= FP_ROUND_UP
;
425 else if (fp
->mod
!= FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_DOWN
;
428 while(fp
->m
&& fp
->exp
<0)
430 if (fp
->m
& 1 && fp
->mod
== FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_EVEN
;
431 else if (fp
->m
& 1) fp
->mod
= FP_ROUND_UP
;
432 else if (fp
->mod
!= FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_DOWN
;
438 if (fp
->mod
== FP_ROUND_UP
|| (fp
->mod
== FP_ROUND_EVEN
&& fp
->m
& 1))
442 /* handle subnormal that falls into regular range due to rounding */
443 if (fp
->m
== (ULONGLONG
)1 << (MANT_BITS
- 1))
447 else if (fp
->m
>= (ULONGLONG
)1 << MANT_BITS
)
454 if (fp
->exp
>= (1<<EXP_BITS
)-1)
456 *d
= fp
->sign
* INFINITY
;
459 if (!fp
->m
|| fp
->exp
< 0)
466 bits
|= (ULONGLONG
)1 << (MANT_BITS
+ EXP_BITS
- 1);
467 bits
|= (ULONGLONG
)fp
->exp
<< (MANT_BITS
- 1);
468 bits
|= fp
->m
& (((ULONGLONG
)1 << (MANT_BITS
- 1)) - 1);
470 TRACE("returning %s\n", wine_dbgstr_longlong(bits
));
471 *d
= *(double*)&bits
;
475 #define LDBL_EXP_BITS 15
476 #define LDBL_MANT_BITS 64
477 int fpnum_ldouble(struct fpnum
*fp
, MSVCRT__LDOUBLE
*d
)
479 if (fp
->mod
== FP_VAL_INFINITY
)
482 d
->x80
[1] = 0x80000000;
483 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
485 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
489 if (fp
->mod
== FP_VAL_NAN
)
493 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
495 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
499 TRACE("%c %s *2^%d (round %d)\n", fp
->sign
== -1 ? '-' : '+',
500 wine_dbgstr_longlong(fp
->m
), fp
->exp
, fp
->mod
);
507 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
511 /* make sure that we don't overflow modifying exponent */
512 if (fp
->exp
> 1<<LDBL_EXP_BITS
)
515 d
->x80
[1] = 0x80000000;
516 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
518 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
521 if (fp
->exp
< -(1<<LDBL_EXP_BITS
))
527 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
530 fp
->exp
+= LDBL_MANT_BITS
- 1;
532 /* normalize mantissa */
533 while(fp
->m
< (ULONGLONG
)1 << (LDBL_MANT_BITS
-1))
538 fp
->exp
+= (1 << (LDBL_EXP_BITS
-1)) - 1;
540 /* handle subnormals */
543 if (fp
->m
& 1 && fp
->mod
== FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_EVEN
;
544 else if (fp
->m
& 1) fp
->mod
= FP_ROUND_UP
;
545 else if (fp
->mod
!= FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_DOWN
;
548 while(fp
->m
&& fp
->exp
<0)
550 if (fp
->m
& 1 && fp
->mod
== FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_EVEN
;
551 else if (fp
->m
& 1) fp
->mod
= FP_ROUND_UP
;
552 else if (fp
->mod
!= FP_ROUND_ZERO
) fp
->mod
= FP_ROUND_DOWN
;
558 if (fp
->mod
== FP_ROUND_UP
|| (fp
->mod
== FP_ROUND_EVEN
&& fp
->m
& 1))
560 if (fp
->m
== UI64_MAX
)
562 fp
->m
= (ULONGLONG
)1 << (LDBL_MANT_BITS
- 1);
569 /* handle subnormal that falls into regular range due to rounding */
570 if ((fp
->m
^ (fp
->m
- 1)) & ((ULONGLONG
)1 << (LDBL_MANT_BITS
- 1))) fp
->exp
++;
574 if (fp
->exp
>= (1<<LDBL_EXP_BITS
)-1)
577 d
->x80
[1] = 0x80000000;
578 d
->x80
[2] = (1 << LDBL_EXP_BITS
) - 1;
580 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
583 if (!fp
->m
|| fp
->exp
< 0)
589 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
594 d
->x80
[1] = fp
->m
>> 32;
597 d
->x80
[2] |= 1 << LDBL_EXP_BITS
;
601 #if _MSVCR_VER >= 140
603 static inline int hex2int(char c
)
605 if (c
>= '0' && c
<= '9')
607 else if (c
>= 'a' && c
<= 'f')
609 else if (c
>= 'A' && c
<= 'F')
614 static struct fpnum
fpnum_parse16(wchar_t get(void *ctx
), void unget(void *ctx
),
615 void *ctx
, int sign
, pthreadlocinfo locinfo
)
617 BOOL found_digit
= FALSE
, found_dp
= FALSE
;
618 enum fpmod round
= FP_ROUND_ZERO
;
624 while(m
< UI64_MAX
/16)
627 if (val
== -1) break;
636 if (val
== -1) break;
640 if (val
|| round
!= FP_ROUND_ZERO
)
642 if (val
< 8) round
= FP_ROUND_DOWN
;
643 else if (val
== 8 && round
== FP_ROUND_ZERO
) round
= FP_ROUND_EVEN
;
644 else round
= FP_ROUND_UP
;
648 if(nch
== *locinfo
->lconv
->decimal_point
)
653 else if (!found_digit
)
655 if(nch
!=WEOF
) unget(ctx
);
657 return fpnum(0, 0, 0, 0);
660 while(m
<= UI64_MAX
/16)
663 if (val
== -1) break;
673 if (val
== -1) break;
676 if (val
|| round
!= FP_ROUND_ZERO
)
678 if (val
< 8) round
= FP_ROUND_DOWN
;
679 else if (val
== 8 && round
== FP_ROUND_ZERO
) round
= FP_ROUND_EVEN
;
680 else round
= FP_ROUND_UP
;
686 if (nch
!= WEOF
) unget(ctx
);
687 if (found_dp
) unget(ctx
);
689 return fpnum(0, 0, 0, 0);
692 if(nch
=='p' || nch
=='P') {
693 BOOL found_sign
= FALSE
;
701 } else if(nch
== '+') {
705 if(nch
>='0' && nch
<='9') {
706 while(nch
>='0' && nch
<='9') {
707 if(e
>INT_MAX
/10 || e
*10>INT_MAX
-nch
+'0')
713 if((nch
!=WEOF
) && (nch
< '0' || nch
> '9')) unget(ctx
);
716 if(e
<0 && exp
<INT_MIN
-e
) exp
= INT_MIN
;
717 else if(e
>0 && exp
>INT_MAX
-e
) exp
= INT_MAX
;
720 if(nch
!= WEOF
) unget(ctx
);
721 if(found_sign
) unget(ctx
);
726 return fpnum(sign
, exp
, m
, round
);
730 /* Converts first 3 limbs to ULONGLONG */
731 /* Return FALSE on overflow */
732 static inline BOOL
bnum_to_mant(struct bnum
*b
, ULONGLONG
*m
)
734 if(UI64_MAX
/ LIMB_MAX
/ LIMB_MAX
< b
->data
[bnum_idx(b
, b
->e
-1)]) return FALSE
;
735 *m
= (ULONGLONG
)b
->data
[bnum_idx(b
, b
->e
-1)] * LIMB_MAX
* LIMB_MAX
;
736 if(b
->b
== b
->e
-1) return TRUE
;
737 if(UI64_MAX
- *m
< (ULONGLONG
)b
->data
[bnum_idx(b
, b
->e
-2)] * LIMB_MAX
) return FALSE
;
738 *m
+= (ULONGLONG
)b
->data
[bnum_idx(b
, b
->e
-2)] * LIMB_MAX
;
739 if(b
->b
== b
->e
-2) return TRUE
;
740 if(UI64_MAX
- *m
< b
->data
[bnum_idx(b
, b
->e
-3)]) return FALSE
;
741 *m
+= b
->data
[bnum_idx(b
, b
->e
-3)];
745 static struct fpnum
fpnum_parse_bnum(wchar_t (*get
)(void *ctx
), void (*unget
)(void *ctx
),
746 void *ctx
, pthreadlocinfo locinfo
, BOOL ldouble
, struct bnum
*b
)
748 #if _MSVCR_VER >= 140
749 const wchar_t _infinity
[] = L
"infinity";
750 const wchar_t _nan
[] = L
"nan";
751 const wchar_t *str_match
= NULL
;
754 BOOL found_digit
= FALSE
, found_dp
= FALSE
, found_sign
= FALSE
;
755 int e2
= 0, dp
=0, sign
=1, off
, limb_digits
= 0, i
;
756 enum fpmod round
= FP_ROUND_ZERO
;
765 } else if(nch
== '+') {
770 #if _MSVCR_VER >= 140
771 if(nch
== _infinity
[0] || nch
== _toupper(_infinity
[0]))
772 str_match
= _infinity
;
773 if(nch
== _nan
[0] || nch
== _toupper(_nan
[0]))
775 while(str_match
&& nch
!= WEOF
&&
776 (nch
== str_match
[matched
] || nch
== _toupper(str_match
[matched
]))) {
782 if(matched
>= 8) keep
= 8;
783 else if(matched
>= 3) keep
= 3;
784 if(nch
!= WEOF
) unget(ctx
);
785 for (; matched
> keep
; matched
--) {
789 if (str_match
== _infinity
)
790 return fpnum(sign
, 0, 0, FP_VAL_INFINITY
);
791 if (str_match
== _nan
)
792 return fpnum(sign
, 0, 0, FP_VAL_NAN
);
793 } else if(found_sign
) {
797 return fpnum(0, 0, 0, 0);
803 if(nch
== 'x' || nch
== 'X')
804 return fpnum_parse16(get
, unget
, ctx
, sign
, locinfo
);
816 while(nch
>='0' && nch
<='9') {
818 if(limb_digits
== LIMB_DIGITS
) {
819 if(bnum_idx(b
, b
->b
-1) == bnum_idx(b
, b
->e
)) break;
822 b
->data
[bnum_idx(b
, b
->b
)] = 0;
827 b
->data
[bnum_idx(b
, b
->b
)] = b
->data
[bnum_idx(b
, b
->b
)] * 10 + nch
- '0';
832 while(nch
>='0' && nch
<='9') {
833 if(nch
!= '0') b
->data
[bnum_idx(b
, b
->b
)] |= 1;
838 if(nch
== *locinfo
->lconv
->decimal_point
) {
843 /* skip leading '0' */
844 if(nch
=='0' && !limb_digits
&& !b
->b
) {
852 while(nch
>='0' && nch
<='9') {
854 if(limb_digits
== LIMB_DIGITS
) {
855 if(bnum_idx(b
, b
->b
-1) == bnum_idx(b
, b
->e
)) break;
858 b
->data
[bnum_idx(b
, b
->b
)] = 0;
863 b
->data
[bnum_idx(b
, b
->b
)] = b
->data
[bnum_idx(b
, b
->b
)] * 10 + nch
- '0';
867 while(nch
>='0' && nch
<='9') {
868 if(nch
!= '0') b
->data
[bnum_idx(b
, b
->b
)] |= 1;
873 if(nch
!= WEOF
) unget(ctx
);
874 if(found_dp
) unget(ctx
);
875 if(found_sign
) unget(ctx
);
876 return fpnum(0, 0, 0, 0);
879 if(nch
=='e' || nch
=='E' || nch
=='d' || nch
=='D') {
887 } else if(nch
== '+') {
894 if(nch
>='0' && nch
<='9') {
895 while(nch
>='0' && nch
<='9') {
896 if(e
>INT_MAX
/10 || e
*10>INT_MAX
-nch
+'0')
902 if(nch
!= WEOF
) unget(ctx
);
905 if(e
<0 && dp
<INT_MIN
-e
) dp
= INT_MIN
;
906 else if(e
>0 && dp
>INT_MAX
-e
) dp
= INT_MAX
;
909 if(nch
!= WEOF
) unget(ctx
);
910 if(found_sign
) unget(ctx
);
913 } else if(nch
!= WEOF
) {
917 if(!b
->data
[bnum_idx(b
, b
->e
-1)])
918 return fpnum(sign
, 0, 0, 0);
920 /* Fill last limb with 0 if needed */
922 for(; limb_digits
!= LIMB_DIGITS
; limb_digits
++)
923 b
->data
[bnum_idx(b
, b
->b
)] *= 10;
925 for(; bnum_idx(b
, b
->b
) < bnum_idx(b
, b
->e
); b
->b
++) {
926 if(b
->data
[bnum_idx(b
, b
->b
)]) break;
929 /* move decimal point to limb boundary */
930 if(limb_digits
==dp
&& b
->b
==b
->e
-1)
931 return fpnum(sign
, 0, b
->data
[bnum_idx(b
, b
->e
-1)], FP_ROUND_ZERO
);
932 off
= (dp
- limb_digits
) % LIMB_DIGITS
;
933 if(off
< 0) off
+= LIMB_DIGITS
;
934 if(off
) bnum_mult(b
, p10s
[off
]);
936 if(dp
-1 > (ldouble
? DBL80_MAX_10_EXP
: DBL_MAX_10_EXP
))
937 return fpnum(sign
, INT_MAX
, 1, FP_ROUND_ZERO
);
938 /* Count part of exponent stored in denormalized mantissa. */
939 /* Increase exponent range to handle subnormals. */
940 if(dp
-1 < (ldouble
? DBL80_MIN_10_EXP
: DBL_MIN_10_EXP
-DBL_DIG
-18))
941 return fpnum(sign
, INT_MIN
, 1, FP_ROUND_ZERO
);
943 while(dp
> 3*LIMB_DIGITS
) {
944 if(bnum_rshift(b
, 9)) dp
-= LIMB_DIGITS
;
947 while(dp
<= 2*LIMB_DIGITS
) {
948 if(bnum_lshift(b
, 29)) dp
+= LIMB_DIGITS
;
951 /* Make sure most significant mantissa bit will be set */
952 while(b
->data
[bnum_idx(b
, b
->e
-1)] <= 9) {
956 while(!bnum_to_mant(b
, &m
)) {
961 if(b
->e
-4 >= b
->b
&& b
->data
[bnum_idx(b
, b
->e
-4)]) {
962 if(b
->data
[bnum_idx(b
, b
->e
-4)] > LIMB_MAX
/2) round
= FP_ROUND_UP
;
963 else if(b
->data
[bnum_idx(b
, b
->e
-4)] == LIMB_MAX
/2) round
= FP_ROUND_EVEN
;
964 else round
= FP_ROUND_DOWN
;
966 if(round
== FP_ROUND_ZERO
|| round
== FP_ROUND_EVEN
) {
967 for(i
=b
->e
-5; i
>=b
->b
; i
--) {
968 if(!b
->data
[bnum_idx(b
, b
->b
)]) continue;
969 if(round
== FP_ROUND_EVEN
) round
= FP_ROUND_UP
;
970 else round
= FP_ROUND_DOWN
;
974 return fpnum(sign
, e2
, m
, round
);
977 struct fpnum
fpnum_parse(wchar_t (*get
)(void *ctx
), void (*unget
)(void *ctx
),
978 void *ctx
, pthreadlocinfo locinfo
, BOOL ldouble
)
981 BYTE bnum_data
[FIELD_OFFSET(struct bnum
, data
[BNUM_PREC64
])];
982 struct bnum
*b
= (struct bnum
*)bnum_data
;
984 b
->size
= BNUM_PREC64
;
985 return fpnum_parse_bnum(get
, unget
, ctx
, locinfo
, ldouble
, b
);
987 BYTE bnum_data
[FIELD_OFFSET(struct bnum
, data
[BNUM_PREC80
])];
988 struct bnum
*b
= (struct bnum
*)bnum_data
;
990 b
->size
= BNUM_PREC80
;
991 return fpnum_parse_bnum(get
, unget
, ctx
, locinfo
, ldouble
, b
);
995 static wchar_t strtod_str_get(void *ctx
)
997 const char **p
= ctx
;
998 if (!**p
) return WEOF
;
1002 static void strtod_str_unget(void *ctx
)
1004 const char **p
= ctx
;
1008 static inline double strtod_helper(const char *str
, char **end
, _locale_t locale
, int *perr
)
1010 pthreadlocinfo locinfo
;
1011 const char *beg
, *p
;
1016 if (perr
) *perr
= 0;
1021 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) {
1022 if (end
) *end
= NULL
;
1027 locinfo
= get_locinfo();
1029 locinfo
= locale
->locinfo
;
1032 while(_isspace_l((unsigned char)*p
, locale
))
1036 fp
= fpnum_parse(strtod_str_get
, strtod_str_unget
, &p
, locinfo
, FALSE
);
1037 if (end
) *end
= (p
== beg
? (char*)str
: (char*)p
);
1039 err
= fpnum_double(&fp
, &ret
);
1040 if (perr
) *perr
= err
;
1041 else if(err
) *_errno() = err
;
1045 /*********************************************************************
1046 * _strtod_l (MSVCRT.@)
1048 double CDECL
_strtod_l(const char *str
, char **end
, _locale_t locale
)
1050 return strtod_helper(str
, end
, locale
, NULL
);
1053 /*********************************************************************
1056 double CDECL
strtod( const char *str
, char **end
)
1058 return _strtod_l( str
, end
, NULL
);
1063 /*********************************************************************
1064 * strtof_l (MSVCR120.@)
1066 float CDECL
_strtof_l( const char *str
, char **end
, _locale_t locale
)
1068 double ret
= _strtod_l(str
, end
, locale
);
1069 if (ret
&& isfinite(ret
)) {
1071 if (!f
|| !isfinite(f
))
1077 /*********************************************************************
1078 * strtof (MSVCR120.@)
1080 float CDECL
strtof( const char *str
, char **end
)
1082 return _strtof_l(str
, end
, NULL
);
1085 #endif /* _MSVCR_VER>=120 */
1087 /*********************************************************************
1090 double CDECL
atof( const char *str
)
1092 return _strtod_l(str
, NULL
, NULL
);
1095 /*********************************************************************
1096 * _atof_l (MSVCRT.@)
1098 double CDECL
_atof_l( const char *str
, _locale_t locale
)
1100 return _strtod_l(str
, NULL
, locale
);
1103 /*********************************************************************
1104 * _atoflt_l (MSVCRT.@)
1106 int CDECL
_atoflt_l(_CRT_FLOAT
*value
, char *str
, _locale_t locale
)
1111 d
= strtod_helper(str
, NULL
, locale
, &err
);
1115 if((d
!=0 || err
) && value
->f
>-FLT_MIN
&& value
->f
<FLT_MIN
)
1120 /*********************************************************************
1121 * _atoflt (MSVCR100.@)
1123 int CDECL
_atoflt(_CRT_FLOAT
*value
, char *str
)
1125 return _atoflt_l(value
, str
, NULL
);
1128 /*********************************************************************
1129 * _atodbl_l (MSVCRT.@)
1131 int CDECL
_atodbl_l(_CRT_DOUBLE
*value
, char *str
, _locale_t locale
)
1135 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
1138 if((value
->x
!=0 || err
) && value
->x
>-DBL_MIN
&& value
->x
<DBL_MIN
)
1143 /*********************************************************************
1144 * _atodbl (MSVCRT.@)
1146 int CDECL
_atodbl(_CRT_DOUBLE
*value
, char *str
)
1148 return _atodbl_l(value
, str
, NULL
);
1151 /*********************************************************************
1152 * _strcoll_l (MSVCRT.@)
1154 int CDECL
_strcoll_l( const char* str1
, const char* str2
, _locale_t locale
)
1156 pthreadlocinfo locinfo
;
1159 locinfo
= get_locinfo();
1161 locinfo
= locale
->locinfo
;
1163 if(!locinfo
->lc_handle
[LC_COLLATE
])
1164 return strcmp(str1
, str2
);
1165 return CompareStringA(locinfo
->lc_handle
[LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
1168 /*********************************************************************
1169 * strcoll (MSVCRT.@)
1171 int CDECL
strcoll( const char* str1
, const char* str2
)
1173 return _strcoll_l(str1
, str2
, NULL
);
1176 /*********************************************************************
1177 * _stricoll_l (MSVCRT.@)
1179 int CDECL
_stricoll_l( const char* str1
, const char* str2
, _locale_t locale
)
1181 pthreadlocinfo locinfo
;
1184 locinfo
= get_locinfo();
1186 locinfo
= locale
->locinfo
;
1188 if(!locinfo
->lc_handle
[LC_COLLATE
])
1189 return _stricmp(str1
, str2
);
1190 return CompareStringA(locinfo
->lc_handle
[LC_COLLATE
], NORM_IGNORECASE
,
1191 str1
, -1, str2
, -1)-CSTR_EQUAL
;
1194 /*********************************************************************
1195 * _stricoll (MSVCRT.@)
1197 int CDECL
_stricoll( const char* str1
, const char* str2
)
1199 return _stricoll_l(str1
, str2
, NULL
);
1202 /*********************************************************************
1203 * _strncoll_l (MSVCRT.@)
1205 int CDECL
_strncoll_l( const char* str1
, const char* str2
, size_t count
, _locale_t locale
)
1207 pthreadlocinfo locinfo
;
1210 locinfo
= get_locinfo();
1212 locinfo
= locale
->locinfo
;
1214 if(!locinfo
->lc_handle
[LC_COLLATE
])
1215 return strncmp(str1
, str2
, count
);
1216 return CompareStringA(locinfo
->lc_handle
[LC_COLLATE
], 0,
1217 str1
, strnlen(str1
, count
),
1218 str2
, strnlen(str2
, count
))-CSTR_EQUAL
;
1221 /*********************************************************************
1222 * _strncoll (MSVCRT.@)
1224 int CDECL
_strncoll( const char* str1
, const char* str2
, size_t count
)
1226 return _strncoll_l(str1
, str2
, count
, NULL
);
1229 /*********************************************************************
1230 * _strnicoll_l (MSVCRT.@)
1232 int CDECL
_strnicoll_l( const char* str1
, const char* str2
, size_t count
, _locale_t locale
)
1234 pthreadlocinfo locinfo
;
1237 locinfo
= get_locinfo();
1239 locinfo
= locale
->locinfo
;
1241 if(!locinfo
->lc_handle
[LC_COLLATE
])
1242 return _strnicmp(str1
, str2
, count
);
1243 return CompareStringA(locinfo
->lc_handle
[LC_COLLATE
], NORM_IGNORECASE
,
1244 str1
, strnlen(str1
, count
),
1245 str2
, strnlen(str2
, count
))-CSTR_EQUAL
;
1248 /*********************************************************************
1249 * _strnicoll (MSVCRT.@)
1251 int CDECL
_strnicoll( const char* str1
, const char* str2
, size_t count
)
1253 return _strnicoll_l(str1
, str2
, count
, NULL
);
1256 /*********************************************************************
1257 * strncpy (MSVCRT.@)
1259 char* __cdecl
strncpy(char *dst
, const char *src
, size_t len
)
1263 for(i
=0; i
<len
; i
++)
1264 if((dst
[i
] = src
[i
]) == '\0') break;
1266 while (i
< len
) dst
[i
++] = 0;
1271 /*********************************************************************
1274 char* CDECL
strcpy(char *dst
, const char *src
)
1277 while ((*dst
++ = *src
++));
1281 /*********************************************************************
1282 * strcpy_s (MSVCRT.@)
1284 int CDECL
strcpy_s( char* dst
, size_t elem
, const char* src
)
1287 if(!elem
) return EINVAL
;
1288 if(!dst
) return EINVAL
;
1295 for(i
= 0; i
< elem
; i
++)
1297 if((dst
[i
] = src
[i
]) == '\0') return 0;
1303 /*********************************************************************
1304 * strcat_s (MSVCRT.@)
1306 int CDECL
strcat_s( char* dst
, size_t elem
, const char* src
)
1309 if(!dst
) return EINVAL
;
1310 if(elem
== 0) return EINVAL
;
1317 for(i
= 0; i
< elem
; i
++)
1321 for(j
= 0; (j
+ i
) < elem
; j
++)
1323 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
1327 /* Set the first element to 0, not the first element after the skipped part */
1332 /*********************************************************************
1335 char* __cdecl
strcat( char *dst
, const char *src
)
1339 while ((*d
++ = *src
++));
1343 /*********************************************************************
1344 * strncat_s (MSVCRT.@)
1346 int CDECL
strncat_s( char* dst
, size_t elem
, const char* src
, size_t count
)
1350 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return EINVAL
;
1351 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return EINVAL
;
1352 if (!MSVCRT_CHECK_PMT(src
!= 0))
1358 for(i
= 0; i
< elem
; i
++)
1362 for(j
= 0; (j
+ i
) < elem
; j
++)
1364 if(count
== _TRUNCATE
&& j
+ i
== elem
- 1)
1369 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
1377 /* Set the first element to 0, not the first element after the skipped part */
1382 /*********************************************************************
1383 * strncat (MSVCRT.@)
1385 char* __cdecl
strncat(char *dst
, const char *src
, size_t len
)
1389 for ( ; len
&& *src
; d
++, src
++, len
--) *d
= *src
;
1394 /*********************************************************************
1395 * _strxfrm_l (MSVCRT.@)
1397 size_t CDECL
_strxfrm_l( char *dest
, const char *src
,
1398 size_t len
, _locale_t locale
)
1400 pthreadlocinfo locinfo
;
1403 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
1404 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
1407 FIXME("len > INT_MAX not supported\n");
1412 locinfo
= get_locinfo();
1414 locinfo
= locale
->locinfo
;
1416 if(!locinfo
->lc_handle
[LC_COLLATE
]) {
1417 strncpy(dest
, src
, len
);
1421 ret
= LCMapStringA(locinfo
->lc_handle
[LC_COLLATE
],
1422 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
1424 if(len
) dest
[0] = 0;
1428 if(!len
) return ret
-1;
1436 return LCMapStringA(locinfo
->lc_handle
[LC_COLLATE
],
1437 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
1440 /*********************************************************************
1441 * strxfrm (MSVCRT.@)
1443 size_t CDECL
strxfrm( char *dest
, const char *src
, size_t len
)
1445 return _strxfrm_l(dest
, src
, len
, NULL
);
1448 /********************************************************************
1449 * __STRINGTOLD_L (MSVCR80.@)
1451 int CDECL
__STRINGTOLD_L( MSVCRT__LDOUBLE
*value
, char **endptr
,
1452 const char *str
, int flags
, _locale_t locale
)
1454 pthreadlocinfo locinfo
;
1455 const char *beg
, *p
;
1459 if (flags
) FIXME("flags not supported: %x\n", flags
);
1462 locinfo
= get_locinfo();
1464 locinfo
= locale
->locinfo
;
1467 while (_isspace_l((unsigned char)*p
, locale
))
1471 fp
= fpnum_parse(strtod_str_get
, strtod_str_unget
, &p
, locinfo
, TRUE
);
1472 if (endptr
) *endptr
= (p
== beg
? (char*)str
: (char*)p
);
1473 if (p
== beg
) ret
= 4;
1475 err
= fpnum_ldouble(&fp
, value
);
1476 if (err
) ret
= (value
->x80
[2] & 0x7fff ? 2 : 1);
1480 /********************************************************************
1481 * __STRINGTOLD (MSVCRT.@)
1483 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
1485 return __STRINGTOLD_L( value
, endptr
, str
, flags
, NULL
);
1488 /********************************************************************
1489 * _atoldbl_l (MSVCRT.@)
1491 int CDECL
_atoldbl_l( MSVCRT__LDOUBLE
*value
, char *str
, _locale_t locale
)
1494 switch(__STRINGTOLD_L( value
, &endptr
, str
, 0, locale
))
1496 case 1: return _UNDERFLOW
;
1497 case 2: return _OVERFLOW
;
1502 /********************************************************************
1503 * _atoldbl (MSVCRT.@)
1505 int CDECL
_atoldbl(_LDOUBLE
*value
, char *str
)
1507 return _atoldbl_l( (MSVCRT__LDOUBLE
*)value
, str
, NULL
);
1510 /*********************************************************************
1513 size_t __cdecl
strlen(const char *str
)
1515 const char *s
= str
;
1520 /******************************************************************
1521 * strnlen (MSVCRT.@)
1523 size_t CDECL
strnlen(const char *s
, size_t maxlen
)
1527 for(i
=0; i
<maxlen
; i
++)
1533 /*********************************************************************
1534 * _strtoi64_l (MSVCRT.@)
1536 * FIXME: locale parameter is ignored
1538 __int64 CDECL
_strtoi64_l(const char *nptr
, char **endptr
, int base
, _locale_t locale
)
1540 const char *p
= nptr
;
1541 BOOL negative
= FALSE
;
1542 BOOL got_digit
= FALSE
;
1545 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1547 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1548 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1549 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1551 while(_isspace_l((unsigned char)*nptr
, locale
)) nptr
++;
1556 } else if(*nptr
== '+')
1559 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1572 char cur
= _tolower_l(*nptr
, locale
);
1575 if(cur
>='0' && cur
<='9') {
1580 if(cur
<'a' || cur
>='a'+base
-10)
1591 if(!negative
&& (ret
>I64_MAX
/base
|| ret
*base
>I64_MAX
-v
)) {
1594 } else if(negative
&& (ret
<I64_MIN
/base
|| ret
*base
<I64_MIN
-v
)) {
1602 *endptr
= (char*)(got_digit
? nptr
: p
);
1607 /*********************************************************************
1608 * _strtoi64 (MSVCRT.@)
1610 __int64 CDECL
_strtoi64(const char *nptr
, char **endptr
, int base
)
1612 return _strtoi64_l(nptr
, endptr
, base
, NULL
);
1615 /*********************************************************************
1616 * _atoi_l (MSVCRT.@)
1618 int __cdecl
_atoi_l(const char *str
, _locale_t locale
)
1620 __int64 ret
= _strtoi64_l(str
, NULL
, 10, locale
);
1625 } else if(ret
< INT_MIN
) {
1632 /*********************************************************************
1636 int __cdecl
atoi(const char *str
)
1644 while(_isspace_l((unsigned char)*str
, NULL
)) str
++;
1648 }else if(*str
== '-') {
1653 while(*str
>='0' && *str
<='9') {
1654 ret
= ret
*10+*str
-'0';
1658 return minus
? -ret
: ret
;
1661 int CDECL
atoi(const char *str
)
1663 return _atoi_l(str
, NULL
);
1667 /******************************************************************
1668 * _atoi64_l (MSVCRT.@)
1670 __int64 CDECL
_atoi64_l(const char *str
, _locale_t locale
)
1672 return _strtoi64_l(str
, NULL
, 10, locale
);
1675 /******************************************************************
1676 * _atoi64 (MSVCRT.@)
1678 __int64 CDECL
_atoi64(const char *str
)
1680 return _strtoi64_l(str
, NULL
, 10, NULL
);
1683 /******************************************************************
1684 * _atol_l (MSVCRT.@)
1686 __msvcrt_long CDECL
_atol_l(const char *str
, _locale_t locale
)
1688 __int64 ret
= _strtoi64_l(str
, NULL
, 10, locale
);
1690 if(ret
> LONG_MAX
) {
1693 } else if(ret
< LONG_MIN
) {
1700 /******************************************************************
1703 __msvcrt_long CDECL
atol(const char *str
)
1708 return _atol_l(str
, NULL
);
1714 /******************************************************************
1715 * _atoll_l (MSVCR120.@)
1717 __int64 CDECL
_atoll_l(const char* str
, _locale_t locale
)
1719 return _strtoi64_l(str
, NULL
, 10, locale
);
1722 /******************************************************************
1723 * atoll (MSVCR120.@)
1725 __int64 CDECL
atoll(const char* str
)
1727 return _atoll_l(str
, NULL
);
1730 #endif /* _MSVCR_VER>=120 */
1732 /******************************************************************
1733 * _strtol_l (MSVCRT.@)
1735 __msvcrt_long CDECL
_strtol_l(const char* nptr
,
1736 char** end
, int base
, _locale_t locale
)
1738 __int64 ret
= _strtoi64_l(nptr
, end
, base
, locale
);
1740 if(ret
> LONG_MAX
) {
1743 } else if(ret
< LONG_MIN
) {
1751 /******************************************************************
1754 __msvcrt_long CDECL
strtol(const char* nptr
, char** end
, int base
)
1756 return _strtol_l(nptr
, end
, base
, NULL
);
1759 /******************************************************************
1760 * _strtoul_l (MSVCRT.@)
1762 __msvcrt_ulong CDECL
_strtoul_l(const char* nptr
, char** end
, int base
, _locale_t locale
)
1764 __int64 ret
= _strtoi64_l(nptr
, end
, base
, locale
);
1766 if(ret
> ULONG_MAX
) {
1769 }else if(ret
< -(__int64
)ULONG_MAX
) {
1777 /******************************************************************
1778 * strtoul (MSVCRT.@)
1780 __msvcrt_ulong CDECL
strtoul(const char* nptr
, char** end
, int base
)
1782 return _strtoul_l(nptr
, end
, base
, NULL
);
1785 /*********************************************************************
1786 * _strtoui64_l (MSVCRT.@)
1788 * FIXME: locale parameter is ignored
1790 unsigned __int64 CDECL
_strtoui64_l(const char *nptr
, char **endptr
, int base
, _locale_t locale
)
1792 const char *p
= nptr
;
1793 BOOL negative
= FALSE
;
1794 BOOL got_digit
= FALSE
;
1795 unsigned __int64 ret
= 0;
1797 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1799 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1800 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1801 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1803 while(_isspace_l((unsigned char)*nptr
, locale
)) nptr
++;
1808 } else if(*nptr
== '+')
1811 if((base
==0 || base
==16) && *nptr
=='0' && _tolower_l(*(nptr
+1), locale
)=='x') {
1824 char cur
= _tolower_l(*nptr
, locale
);
1827 if(cur
>='0' && cur
<='9') {
1832 if(cur
<'a' || cur
>='a'+base
-10)
1840 if(ret
>UI64_MAX
/base
|| ret
*base
>UI64_MAX
-v
) {
1848 *endptr
= (char*)(got_digit
? nptr
: p
);
1850 return negative
? -ret
: ret
;
1853 /*********************************************************************
1854 * _strtoui64 (MSVCRT.@)
1856 unsigned __int64 CDECL
_strtoui64(const char *nptr
, char **endptr
, int base
)
1858 return _strtoui64_l(nptr
, endptr
, base
, NULL
);
1861 static int ltoa_helper(__msvcrt_long value
, char *str
, size_t size
, int radix
)
1866 char buffer
[33], *pos
;
1869 if (value
< 0 && radix
== 10)
1876 is_negative
= FALSE
;
1885 digit
= val
% radix
;
1889 *--pos
= '0' + digit
;
1891 *--pos
= 'a' + digit
- 10;
1898 len
= buffer
+ 33 - pos
;
1904 /* Copy the temporary buffer backwards up to the available number of
1905 * characters. Don't copy the negative sign if present. */
1913 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1917 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
1921 memcpy(str
, pos
, len
);
1925 /*********************************************************************
1926 * _ltoa_s (MSVCRT.@)
1928 int CDECL
_ltoa_s(__msvcrt_long value
, char *str
, size_t size
, int radix
)
1930 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
1931 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
1932 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1938 return ltoa_helper(value
, str
, size
, radix
);
1941 /*********************************************************************
1942 * _ltow_s (MSVCRT.@)
1944 int CDECL
_ltow_s(__msvcrt_long value
, wchar_t *str
, size_t size
, int radix
)
1949 wchar_t buffer
[33], *pos
;
1952 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
1953 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
1954 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1960 if (value
< 0 && radix
== 10)
1967 is_negative
= FALSE
;
1976 digit
= val
% radix
;
1980 *--pos
= '0' + digit
;
1982 *--pos
= 'a' + digit
- 10;
1989 len
= buffer
+ 33 - pos
;
1995 /* Copy the temporary buffer backwards up to the available number of
1996 * characters. Don't copy the negative sign if present. */
2004 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2008 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2012 memcpy(str
, pos
, len
* sizeof(wchar_t));
2016 /*********************************************************************
2017 * _itoa_s (MSVCRT.@)
2019 int CDECL
_itoa_s(int value
, char *str
, size_t size
, int radix
)
2021 return _ltoa_s(value
, str
, size
, radix
);
2024 /*********************************************************************
2027 char* CDECL
_itoa(int value
, char *str
, int radix
)
2029 return ltoa_helper(value
, str
, SIZE_MAX
, radix
) ? NULL
: str
;
2032 /*********************************************************************
2033 * _itow_s (MSVCRT.@)
2035 int CDECL
_itow_s(int value
, wchar_t *str
, size_t size
, int radix
)
2037 return _ltow_s(value
, str
, size
, radix
);
2040 /*********************************************************************
2041 * _ui64toa_s (MSVCRT.@)
2043 int CDECL
_ui64toa_s(unsigned __int64 value
, char *str
,
2044 size_t size
, int radix
)
2046 char buffer
[65], *pos
;
2049 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2050 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2051 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2061 digit
= value
%radix
;
2067 *--pos
= 'a'+digit
-10;
2070 if(buffer
-pos
+65 > size
) {
2071 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL
);
2075 memcpy(str
, pos
, buffer
-pos
+65);
2079 /*********************************************************************
2080 * _ui64tow_s (MSVCRT.@)
2082 int CDECL
_ui64tow_s( unsigned __int64 value
, wchar_t *str
,
2083 size_t size
, int radix
)
2085 wchar_t buffer
[65], *pos
;
2088 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2089 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2090 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2100 digit
= value
% radix
;
2101 value
= value
/ radix
;
2103 *--pos
= '0' + digit
;
2105 *--pos
= 'a' + digit
- 10;
2106 } while (value
!= 0);
2108 if(buffer
-pos
+65 > size
) {
2109 MSVCRT_INVALID_PMT("str[size] is too small", EINVAL
);
2113 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(wchar_t));
2117 /*********************************************************************
2118 * _ultoa_s (MSVCRT.@)
2120 int CDECL
_ultoa_s(__msvcrt_ulong value
, char *str
, size_t size
, int radix
)
2122 __msvcrt_ulong digit
;
2123 char buffer
[33], *pos
;
2126 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2140 digit
= value
% radix
;
2144 *--pos
= '0' + digit
;
2146 *--pos
= 'a' + digit
- 10;
2150 len
= buffer
+ 33 - pos
;
2156 /* Copy the temporary buffer backwards up to the available number of
2159 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2167 memcpy(str
, pos
, len
);
2171 /*********************************************************************
2172 * _ultow_s (MSVCRT.@)
2174 int CDECL
_ultow_s(__msvcrt_ulong value
, wchar_t *str
, size_t size
, int radix
)
2176 __msvcrt_ulong digit
;
2177 WCHAR buffer
[33], *pos
;
2180 if (!str
|| !size
|| radix
< 2 || radix
> 36)
2194 digit
= value
% radix
;
2198 *--pos
= '0' + digit
;
2200 *--pos
= 'a' + digit
- 10;
2204 len
= buffer
+ 33 - pos
;
2210 /* Copy the temporary buffer backwards up to the available number of
2213 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
2221 memcpy(str
, pos
, len
* sizeof(wchar_t));
2225 /*********************************************************************
2226 * _i64toa_s (MSVCRT.@)
2228 int CDECL
_i64toa_s(__int64 value
, char *str
, size_t size
, int radix
)
2230 unsigned __int64 val
;
2233 char buffer
[65], *pos
;
2236 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2237 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2238 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2244 if (value
< 0 && radix
== 10)
2251 is_negative
= FALSE
;
2260 digit
= val
% radix
;
2264 *--pos
= '0' + digit
;
2266 *--pos
= 'a' + digit
- 10;
2273 len
= buffer
+ 65 - pos
;
2279 /* Copy the temporary buffer backwards up to the available number of
2280 * characters. Don't copy the negative sign if present. */
2288 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2292 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2296 memcpy(str
, pos
, len
);
2300 /*********************************************************************
2301 * _i64tow_s (MSVCRT.@)
2303 int CDECL
_i64tow_s(__int64 value
, wchar_t *str
, size_t size
, int radix
)
2305 unsigned __int64 val
;
2308 wchar_t buffer
[65], *pos
;
2311 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return EINVAL
;
2312 if (!MSVCRT_CHECK_PMT(size
> 0)) return EINVAL
;
2313 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
2319 if (value
< 0 && radix
== 10)
2326 is_negative
= FALSE
;
2335 digit
= val
% radix
;
2339 *--pos
= '0' + digit
;
2341 *--pos
= 'a' + digit
- 10;
2348 len
= buffer
+ 65 - pos
;
2354 /* Copy the temporary buffer backwards up to the available number of
2355 * characters. Don't copy the negative sign if present. */
2363 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
2367 MSVCRT_INVALID_PMT("str[size] is too small", ERANGE
);
2371 memcpy(str
, pos
, len
* sizeof(wchar_t));
2375 #define I10_OUTPUT_MAX_PREC 21
2376 /* Internal structure used by $I10_OUTPUT */
2377 struct _I10_OUTPUT_DATA
{
2381 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
2384 /*********************************************************************
2385 * $I10_OUTPUT (MSVCRT.@)
2386 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
2387 * prec - precision of part, we're interested in
2388 * flag - 0 for first prec digits, 1 for fractional part
2389 * data - data to be populated
2392 * 0 if given double is NaN or INF
2396 * Native sets last byte of data->str to '0' or '9', I don't know what
2397 * it means. Current implementation sets it always to '0'.
2399 int CDECL
I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
2404 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
2407 if ((ld80
.x80
[2] & 0x7fff) == 0x7fff)
2409 if (ld80
.x80
[0] == 0 && ld80
.x80
[1] == 0x80000000)
2410 strcpy( data
->str
, "1#INF" );
2412 strcpy( data
->str
, (ld80
.x80
[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
2414 data
->sign
= (ld80
.x80
[2] & 0x8000) ? '-' : ' ';
2415 data
->len
= strlen(data
->str
);
2419 num
.sign
= (ld80
.x80
[2] & 0x8000) ? -1 : 1;
2420 num
.exp
= (ld80
.x80
[2] & 0x7fff) - 0x3fff - 63;
2421 num
.m
= ld80
.x80
[0] | ((ULONGLONG
)ld80
.x80
[1] << 32);
2422 num
.mod
= FP_ROUND_EVEN
;
2423 fpnum_double( &num
, &d
);
2424 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
2433 int exp
= 1 + floor(log10(d
));
2441 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
2442 prec
= I10_OUTPUT_MAX_PREC
-1;
2448 sprintf(format
, "%%.%dle", prec
);
2449 sprintf(buf
, format
, d
);
2452 data
->pos
= atoi(buf
+prec
+3);
2456 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
2459 memcpy(data
->str
, buf
+1, data
->len
);
2460 data
->str
[data
->len
] = '\0';
2462 if(buf
[1]!='0' && prec
-data
->len
+1>0)
2463 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
2467 #undef I10_OUTPUT_MAX_PREC
2469 /*********************************************************************
2472 int __cdecl
memcmp(const void *ptr1
, const void *ptr2
, size_t n
)
2474 const unsigned char *p1
, *p2
;
2476 for (p1
= ptr1
, p2
= ptr2
; n
; n
--, p1
++, p2
++)
2478 if (*p1
< *p2
) return -1;
2479 if (*p1
> *p2
) return 1;
2484 #if defined(__i386__) || defined(__x86_64__)
2488 #define DEST_REG "%edi"
2489 #define SRC_REG "%esi"
2490 #define LEN_REG "%ecx"
2491 #define TMP_REG "%edx"
2493 #define MEMMOVE_INIT \
2494 "pushl " SRC_REG "\n\t" \
2495 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2496 "pushl " DEST_REG "\n\t" \
2497 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
2498 "movl 12(%esp), " DEST_REG "\n\t" \
2499 "movl 16(%esp), " SRC_REG "\n\t" \
2500 "movl 20(%esp), " LEN_REG "\n\t"
2502 #define MEMMOVE_CLEANUP \
2503 "movl 12(%esp), %eax\n\t" \
2504 "popl " DEST_REG "\n\t" \
2505 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
2506 "popl " SRC_REG "\n\t" \
2507 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2511 #define DEST_REG "%rdi"
2512 #define SRC_REG "%rsi"
2513 #define LEN_REG "%r8"
2514 #define TMP_REG "%r9"
2516 #define MEMMOVE_INIT \
2517 "pushq " SRC_REG "\n\t" \
2518 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2519 "pushq " DEST_REG "\n\t" \
2520 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
2521 "movq %rcx, " DEST_REG "\n\t" \
2522 "movq %rdx, " SRC_REG "\n\t"
2524 #define MEMMOVE_CLEANUP \
2525 "movq %rcx, %rax\n\t" \
2526 "popq " DEST_REG "\n\t" \
2527 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t") \
2528 "popq " SRC_REG "\n\t" \
2529 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2532 void * __cdecl
sse2_memmove(void *dst
, const void *src
, size_t n
);
2533 __ASM_GLOBAL_FUNC( sse2_memmove
,
2535 "mov " DEST_REG
", " TMP_REG
"\n\t" /* check copying direction */
2536 "sub " SRC_REG
", " TMP_REG
"\n\t"
2537 "cmp " LEN_REG
", " TMP_REG
"\n\t"
2540 "cmp $4, " LEN_REG
"\n\t" /* 4-bytes align */
2542 "mov " DEST_REG
", " TMP_REG
"\n\t"
2543 "shr $1, " TMP_REG
"\n\t"
2546 "dec " LEN_REG
"\n\t"
2547 "inc " TMP_REG
"\n\t"
2549 "shr $1, " TMP_REG
"\n\t"
2552 "sub $2, " LEN_REG
"\n\t"
2553 "inc " TMP_REG
"\n\t"
2554 "1:\n\t" /* 16-bytes align */
2555 "cmp $16, " LEN_REG
"\n\t"
2557 "shr $1, " TMP_REG
"\n\t"
2560 "sub $4, " LEN_REG
"\n\t"
2561 "inc " TMP_REG
"\n\t"
2563 "shr $1, " TMP_REG
"\n\t"
2567 "sub $8, " LEN_REG
"\n\t"
2569 "cmp $64, " LEN_REG
"\n\t"
2571 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2572 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2573 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2574 "movdqu 0x20(" SRC_REG
"), %xmm2\n\t"
2575 "movdqu 0x30(" SRC_REG
"), %xmm3\n\t"
2576 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2577 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2578 "movdqa %xmm2, 0x20(" DEST_REG
")\n\t"
2579 "movdqa %xmm3, 0x30(" DEST_REG
")\n\t"
2580 "add $64, " SRC_REG
"\n\t"
2581 "add $64, " DEST_REG
"\n\t"
2582 "sub $64, " LEN_REG
"\n\t"
2583 "cmp $64, " LEN_REG
"\n\t"
2585 "copy_fwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2586 "mov " LEN_REG
", " TMP_REG
"\n\t"
2587 "and $15, " LEN_REG
"\n\t"
2588 "shr $5, " TMP_REG
"\n\t"
2590 "movdqu 0(" SRC_REG
"), %xmm0\n\t"
2591 "movdqa %xmm0, 0(" DEST_REG
")\n\t"
2592 "add $16, " SRC_REG
"\n\t"
2593 "add $16, " DEST_REG
"\n\t"
2595 "shr $1, " TMP_REG
"\n\t"
2596 "jnc copy_fwd15\n\t"
2597 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2598 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2599 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2600 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2601 "add $32, " SRC_REG
"\n\t"
2602 "add $32, " DEST_REG
"\n\t"
2603 "copy_fwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2604 "mov " LEN_REG
", " TMP_REG
"\n\t"
2605 "and $3, " LEN_REG
"\n\t"
2606 "shr $3, " TMP_REG
"\n\t"
2610 "shr $1, " TMP_REG
"\n\t"
2614 "copy_fwd3:\n\t" /* copy last 3 bytes */
2615 "shr $1, " LEN_REG
"\n\t"
2619 "shr $1, " LEN_REG
"\n\t"
2626 "lea (" DEST_REG
", " LEN_REG
"), " DEST_REG
"\n\t"
2627 "lea (" SRC_REG
", " LEN_REG
"), " SRC_REG
"\n\t"
2628 "cmp $4, " LEN_REG
"\n\t" /* 4-bytes align */
2630 "mov " DEST_REG
", " TMP_REG
"\n\t"
2631 "shr $1, " TMP_REG
"\n\t"
2633 "dec " SRC_REG
"\n\t"
2634 "dec " DEST_REG
"\n\t"
2635 "movb (" SRC_REG
"), %al\n\t"
2636 "movb %al, (" DEST_REG
")\n\t"
2637 "dec " LEN_REG
"\n\t"
2639 "shr $1, " TMP_REG
"\n\t"
2641 "sub $2, " SRC_REG
"\n\t"
2642 "sub $2, " DEST_REG
"\n\t"
2643 "movw (" SRC_REG
"), %ax\n\t"
2644 "movw %ax, (" DEST_REG
")\n\t"
2645 "sub $2, " LEN_REG
"\n\t"
2646 "1:\n\t" /* 16-bytes align */
2647 "cmp $16, " LEN_REG
"\n\t"
2649 "shr $1, " TMP_REG
"\n\t"
2651 "sub $4, " SRC_REG
"\n\t"
2652 "sub $4, " DEST_REG
"\n\t"
2653 "movl (" SRC_REG
"), %eax\n\t"
2654 "movl %eax, (" DEST_REG
")\n\t"
2655 "sub $4, " LEN_REG
"\n\t"
2657 "shr $1, " TMP_REG
"\n\t"
2659 "sub $8, " SRC_REG
"\n\t"
2660 "sub $8, " DEST_REG
"\n\t"
2661 "movl 4(" SRC_REG
"), %eax\n\t"
2662 "movl %eax, 4(" DEST_REG
")\n\t"
2663 "movl (" SRC_REG
"), %eax\n\t"
2664 "movl %eax, (" DEST_REG
")\n\t"
2665 "sub $8, " LEN_REG
"\n\t"
2667 "cmp $64, " LEN_REG
"\n\t"
2669 "1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
2670 "sub $64, " SRC_REG
"\n\t"
2671 "sub $64, " DEST_REG
"\n\t"
2672 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2673 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2674 "movdqu 0x20(" SRC_REG
"), %xmm2\n\t"
2675 "movdqu 0x30(" SRC_REG
"), %xmm3\n\t"
2676 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2677 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2678 "movdqa %xmm2, 0x20(" DEST_REG
")\n\t"
2679 "movdqa %xmm3, 0x30(" DEST_REG
")\n\t"
2680 "sub $64, " LEN_REG
"\n\t"
2681 "cmp $64, " LEN_REG
"\n\t"
2683 "copy_bwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
2684 "mov " LEN_REG
", " TMP_REG
"\n\t"
2685 "and $15, " LEN_REG
"\n\t"
2686 "shr $5, " TMP_REG
"\n\t"
2688 "sub $16, " SRC_REG
"\n\t"
2689 "sub $16, " DEST_REG
"\n\t"
2690 "movdqu (" SRC_REG
"), %xmm0\n\t"
2691 "movdqa %xmm0, (" DEST_REG
")\n\t"
2693 "shr $1, " TMP_REG
"\n\t"
2694 "jnc copy_bwd15\n\t"
2695 "sub $32, " SRC_REG
"\n\t"
2696 "sub $32, " DEST_REG
"\n\t"
2697 "movdqu 0x00(" SRC_REG
"), %xmm0\n\t"
2698 "movdqu 0x10(" SRC_REG
"), %xmm1\n\t"
2699 "movdqa %xmm0, 0x00(" DEST_REG
")\n\t"
2700 "movdqa %xmm1, 0x10(" DEST_REG
")\n\t"
2701 "copy_bwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
2702 "mov " LEN_REG
", " TMP_REG
"\n\t"
2703 "and $3, " LEN_REG
"\n\t"
2704 "shr $3, " TMP_REG
"\n\t"
2706 "sub $4, " SRC_REG
"\n\t"
2707 "sub $4, " DEST_REG
"\n\t"
2708 "movl (" SRC_REG
"), %eax\n\t"
2709 "movl %eax, (" DEST_REG
")\n\t"
2711 "shr $1, " TMP_REG
"\n\t"
2713 "sub $8, " SRC_REG
"\n\t"
2714 "sub $8, " DEST_REG
"\n\t"
2715 "movl 4(" SRC_REG
"), %eax\n\t"
2716 "movl %eax, 4(" DEST_REG
")\n\t"
2717 "movl (" SRC_REG
"), %eax\n\t"
2718 "movl %eax, (" DEST_REG
")\n\t"
2719 "copy_bwd3:\n\t" /* copy last 3 bytes */
2720 "shr $1, " LEN_REG
"\n\t"
2722 "dec " SRC_REG
"\n\t"
2723 "dec " DEST_REG
"\n\t"
2724 "movb (" SRC_REG
"), %al\n\t"
2725 "movb %al, (" DEST_REG
")\n\t"
2727 "shr $1, " LEN_REG
"\n\t"
2729 "movw -2(" SRC_REG
"), %ax\n\t"
2730 "movw %ax, -2(" DEST_REG
")\n\t"
2737 /*********************************************************************
2738 * memmove (MSVCRT.@)
2740 #ifdef WORDS_BIGENDIAN
2741 # define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
2743 # define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
2745 void * __cdecl
memmove(void *dst
, const void *src
, size_t n
)
2748 return sse2_memmove(dst
, src
, n
);
2750 unsigned char *d
= dst
;
2751 const unsigned char *s
= src
;
2756 return sse2_memmove(dst
, src
, n
);
2761 if ((size_t)dst
- (size_t)src
>= n
)
2763 for (; (size_t)d
% sizeof(size_t) && n
; n
--) *d
++ = *s
++;
2765 sh1
= 8 * ((size_t)s
% sizeof(size_t));
2768 while (n
>= sizeof(size_t))
2770 *(size_t*)d
= *(size_t*)s
;
2771 s
+= sizeof(size_t);
2772 d
+= sizeof(size_t);
2773 n
-= sizeof(size_t);
2776 else if (n
>= 2 * sizeof(size_t))
2778 int sh2
= 8 * sizeof(size_t) - sh1
;
2785 s
+= sizeof(size_t);
2787 *(size_t*)d
= MERGE(x
, sh1
, y
, sh2
);
2788 d
+= sizeof(size_t);
2790 s
+= sizeof(size_t);
2792 *(size_t*)d
= MERGE(y
, sh1
, x
, sh2
);
2793 d
+= sizeof(size_t);
2795 n
-= 2 * sizeof(size_t);
2796 } while (n
>= 2 * sizeof(size_t));
2799 while (n
--) *d
++ = *s
++;
2807 for (; (size_t)d
% sizeof(size_t) && n
; n
--) *--d
= *--s
;
2809 sh1
= 8 * ((size_t)s
% sizeof(size_t));
2812 while (n
>= sizeof(size_t))
2814 s
-= sizeof(size_t);
2815 d
-= sizeof(size_t);
2816 *(size_t*)d
= *(size_t*)s
;
2817 n
-= sizeof(size_t);
2820 else if (n
>= 2 * sizeof(size_t))
2822 int sh2
= 8 * sizeof(size_t) - sh1
;
2829 s
-= sizeof(size_t);
2831 d
-= sizeof(size_t);
2832 *(size_t*)d
= MERGE(y
, sh1
, x
, sh2
);
2834 s
-= sizeof(size_t);
2836 d
-= sizeof(size_t);
2837 *(size_t*)d
= MERGE(x
, sh1
, y
, sh2
);
2839 n
-= 2 * sizeof(size_t);
2840 } while (n
>= 2 * sizeof(size_t));
2843 while (n
--) *--d
= *--s
;
2850 /*********************************************************************
2853 void * __cdecl
memcpy(void *dst
, const void *src
, size_t n
)
2855 return memmove(dst
, src
, n
);
2858 /*********************************************************************
2861 void* __cdecl
memset(void *dst
, int c
, size_t n
)
2863 volatile unsigned char *d
= dst
; /* avoid gcc optimizations */
2864 while (n
--) *d
++ = c
;
2868 /*********************************************************************
2871 char* __cdecl
strchr(const char *str
, int c
)
2875 if (*str
== (char)c
) return (char*)str
;
2880 /*********************************************************************
2881 * strrchr (MSVCRT.@)
2883 char* __cdecl
strrchr(const char *str
, int c
)
2886 do { if (*str
== (char)c
) ret
= (char*)str
; } while (*str
++);
2890 /*********************************************************************
2893 void* __cdecl
memchr(const void *ptr
, int c
, size_t n
)
2895 const unsigned char *p
= ptr
;
2897 for (p
= ptr
; n
; n
--, p
++) if (*p
== (unsigned char)c
) return (void *)(ULONG_PTR
)p
;
2901 /*********************************************************************
2904 int __cdecl
strcmp(const char *str1
, const char *str2
)
2906 while (*str1
&& *str1
== *str2
) { str1
++; str2
++; }
2907 if ((unsigned char)*str1
> (unsigned char)*str2
) return 1;
2908 if ((unsigned char)*str1
< (unsigned char)*str2
) return -1;
2912 /*********************************************************************
2913 * strncmp (MSVCRT.@)
2915 int __cdecl
strncmp(const char *str1
, const char *str2
, size_t len
)
2918 while (--len
&& *str1
&& *str1
== *str2
) { str1
++; str2
++; }
2919 return (unsigned char)*str1
- (unsigned char)*str2
;
2922 /*********************************************************************
2923 * _strnicmp_l (MSVCRT.@)
2925 int __cdecl
_strnicmp_l(const char *s1
, const char *s2
,
2926 size_t count
, _locale_t locale
)
2928 pthreadlocinfo locinfo
;
2931 if(s1
==NULL
|| s2
==NULL
)
2932 return _NLSCMPERROR
;
2938 locinfo
= get_locinfo();
2940 locinfo
= locale
->locinfo
;
2942 if(!locinfo
->lc_handle
[LC_CTYPE
])
2945 if ((c1
= *s1
++) >= 'A' && c1
<= 'Z')
2947 if ((c2
= *s2
++) >= 'A' && c2
<= 'Z')
2949 }while(--count
&& c1
&& c1
==c2
);
2955 c1
= _tolower_l((unsigned char)*s1
++, locale
);
2956 c2
= _tolower_l((unsigned char)*s2
++, locale
);
2957 }while(--count
&& c1
&& c1
==c2
);
2962 /*********************************************************************
2963 * _stricmp_l (MSVCRT.@)
2965 int __cdecl
_stricmp_l(const char *s1
, const char *s2
, _locale_t locale
)
2967 return _strnicmp_l(s1
, s2
, -1, locale
);
2970 /*********************************************************************
2971 * _strnicmp (MSVCRT.@)
2973 int __cdecl
_strnicmp(const char *s1
, const char *s2
, size_t count
)
2975 return _strnicmp_l(s1
, s2
, count
, NULL
);
2978 /*********************************************************************
2979 * _stricmp (MSVCRT.@)
2981 int __cdecl
_stricmp(const char *s1
, const char *s2
)
2983 return _strnicmp_l(s1
, s2
, -1, NULL
);
2986 /*********************************************************************
2989 char* __cdecl
strstr(const char *haystack
, const char *needle
)
2991 size_t i
, j
, len
, needle_len
, lps_len
;
2994 needle_len
= strlen(needle
);
2995 if (!needle_len
) return (char*)haystack
;
2996 lps_len
= needle_len
> ARRAY_SIZE(lps
) ? ARRAY_SIZE(lps
) : needle_len
;
3003 if (needle
[i
] == needle
[len
]) lps
[i
++] = ++len
;
3004 else if (len
) len
= lps
[len
-1];
3011 while (j
< lps_len
&& haystack
[i
] && haystack
[i
] == needle
[j
])
3017 if (j
== needle_len
) return (char*)haystack
+ i
- j
;
3020 if (j
== ARRAY_SIZE(lps
) && !strncmp(haystack
+ i
, needle
+ j
, needle_len
- j
))
3021 return (char*)haystack
+ i
- j
;
3024 else if (haystack
[i
]) i
++;
3029 /*********************************************************************
3030 * _memicmp_l (MSVCRT.@)
3032 int __cdecl
_memicmp_l(const void *v1
, const void *v2
, size_t len
, _locale_t locale
)
3034 const char *s1
= v1
, *s2
= v2
;
3037 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
3041 MSVCRT_INVALID_PMT(NULL
, EINVAL
);
3042 return len
? _NLSCMPERROR
: 0;
3048 if ((ret
= _tolower_l(*s1
, locale
) - _tolower_l(*s2
, locale
)))
3056 /*********************************************************************
3057 * _memicmp (MSVCRT.@)
3059 int __cdecl
_memicmp(const void *s1
, const void *s2
, size_t len
)
3061 return _memicmp_l(s1
, s2
, len
, NULL
);
3064 /*********************************************************************
3065 * strcspn (MSVCRT.@)
3067 size_t __cdecl
strcspn(const char *str
, const char *reject
)
3072 memset(rejects
, 0, sizeof(rejects
));
3077 rejects
[(unsigned char)*p
] = TRUE
;
3082 while(*p
&& !rejects
[(unsigned char)*p
]) p
++;
3086 /*********************************************************************
3087 * strpbrk (MSVCRT.@)
3089 char* __cdecl
strpbrk(const char *str
, const char *accept
)
3091 for (; *str
; str
++) if (strchr( accept
, *str
)) return (char*)str
;
3095 /*********************************************************************
3096 * __strncnt (MSVCRT.@)
3098 size_t __cdecl
__strncnt(const char *str
, size_t size
)
3102 #if _MSVCR_VER >= 140
3103 while (*str
++ && size
--)
3105 while (size
-- && *str
++)
3116 /*********************************************************************
3117 * _strdec (CRTDLL.@)
3119 char * CDECL
_strdec(const char *str1
, const char *str2
)
3121 return (char *)(str2
- 1);
3124 /*********************************************************************
3125 * _strinc (CRTDLL.@)
3127 char * CDECL
_strinc(const char *str
)
3129 return (char *)(str
+ 1);
3132 /*********************************************************************
3133 * _strnextc (CRTDLL.@)
3135 unsigned int CDECL
_strnextc(const char *str
)
3137 return (unsigned char)str
[0];
3140 /*********************************************************************
3141 * _strninc (CRTDLL.@)
3143 char * CDECL
_strninc(const char *str
, size_t len
)
3145 return (char *)(str
+ len
);
3148 /*********************************************************************
3149 * _strspnp (CRTDLL.@)
3151 char * CDECL
_strspnp( const char *str1
, const char *str2
)
3153 str1
+= strspn( str1
, str2
);
3154 return *str1
? (char*)str1
: NULL
;