2 * msvcrt.dll mbcs functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffths
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
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
29 #include "msvcrt/mbctype.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33 unsigned char MSVCRT_mbctype[257];
34 int MSVCRT___mb_cur_max = 1;
36 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
46 mbch[0] = (ch >> 8) & 0xff;
50 if (!MultiByteToWideChar(MSVCRT___lc_codepage, 0, mbch, n_chars, &chW, 1))
52 WARN("MultiByteToWideChar failed on %x\n", ch);
58 static inline size_t u_strlen( const unsigned char *str )
60 return strlen( (const char*) str );
63 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
65 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
68 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
70 return strcmp( (const char*)s1, (const char*)s2 );
73 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
75 return strcasecmp( (const char*)s1, (const char*)s2 );
78 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
80 return strncmp( (const char*)s1, (const char*)s2, len );
83 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
85 return strncasecmp( (const char*)s1, (const char*)s2, len );
88 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
90 return (unsigned char*) strchr( (const char*)s, x );
93 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
95 return (unsigned char*) strrchr( (const char*)s, x );
98 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
100 return (unsigned char*) strtok( (char*)s, (const char*)delim );
103 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
105 return (unsigned char*) _strset( (char*)s, c);
108 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
110 return (unsigned char*) _strnset( (char*)s, c, len );
113 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
115 return strcspn( (const char *)s, (const char*)rej );
118 /*********************************************************************
119 * __p__mbctype (MSVCRT.@)
121 unsigned char* CDECL __p__mbctype(void)
123 return MSVCRT_mbctype;
126 /*********************************************************************
127 * __p___mb_cur_max(MSVCRT.@)
129 int* CDECL __p___mb_cur_max(void)
131 return &MSVCRT___mb_cur_max;
134 /*********************************************************************
135 * _mbsnextc(MSVCRT.@)
137 unsigned int CDECL _mbsnextc(const unsigned char* str)
139 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
140 return *str << 8 | str[1];
141 return *str; /* ASCII CP or SB char */
144 /*********************************************************************
145 * _mbctolower(MSVCRT.@)
147 unsigned int CDECL _mbctolower(unsigned int c)
149 if (MSVCRT_isleadbyte(c))
151 FIXME("Handle MBC chars\n");
154 return tolower(c); /* ASCII CP or SB char */
157 /*********************************************************************
158 * _mbctoupper(MSVCRT.@)
160 unsigned int CDECL _mbctoupper(unsigned int c)
162 if (MSVCRT_isleadbyte(c))
164 FIXME("Handle MBC chars\n");
167 return toupper(c); /* ASCII CP or SB char */
170 /*********************************************************************
173 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
175 if(MSVCRT___mb_cur_max > 1)
176 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
178 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
181 /*********************************************************************
184 unsigned char* CDECL _mbsinc(const unsigned char* str)
186 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
187 return (unsigned char*)str + 2; /* MB char */
189 return (unsigned char*)str + 1; /* ASCII CP or SB char */
192 /*********************************************************************
195 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
199 if(MSVCRT___mb_cur_max > 1)
203 return (unsigned char*)str;
205 return (unsigned char*)str + num; /* ASCII CP */
208 /*********************************************************************
211 unsigned int CDECL _mbclen(const unsigned char* str)
213 return MSVCRT_isleadbyte(*str) ? 2 : 1;
216 /*********************************************************************
219 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
221 if (str && *str && size)
223 if(MSVCRT___mb_cur_max == 1)
224 return 1; /* ASCII CP */
226 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
231 /*********************************************************************
234 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
236 if(MSVCRT___mb_cur_max > 1)
238 MSVCRT_size_t len = 0;
241 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
246 return u_strlen(str); /* ASCII CP */
249 /*********************************************************************
250 * _mbstrlen(MSVCRT.@)
252 MSVCRT_size_t CDECL _mbstrlen(const char* str)
254 if(MSVCRT___mb_cur_max > 1)
256 MSVCRT_size_t len = 0;
259 /* FIXME: According to the documentation we are supposed to test for
260 * multi-byte character validity. Whatever that means
262 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
267 return strlen(str); /* ASCII CP */
270 /*********************************************************************
273 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
276 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
277 *++dest = *++src; /* MB char */
280 /*********************************************************************
283 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
285 unsigned char* ret = dst;
288 if(MSVCRT___mb_cur_max > 1)
294 if (MSVCRT_isleadbyte(*src++))
303 if (!(*dst++ = *src++)) break;
306 while (n--) *dst++ = 0;
310 /*********************************************************************
311 * _mbsnbcpy(MSVCRT.@)
313 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
315 unsigned char* ret = dst;
318 if(MSVCRT___mb_cur_max > 1)
320 while (*src && (n > 1))
324 if (MSVCRT_isleadbyte(*src++))
330 if (*src && n && !MSVCRT_isleadbyte(*src))
332 /* If the last character is a multi-byte character then
333 * we cannot copy it since we have only one byte left
344 if (!(*dst++ = *src++)) break;
347 while (n--) *dst++ = 0;
351 /*********************************************************************
354 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
356 if(MSVCRT___mb_cur_max > 1)
358 unsigned int strc, cmpc;
361 return *cmp ? -1 : 0;
364 strc = _mbsnextc(str);
365 cmpc = _mbsnextc(cmp);
367 return strc < cmpc ? -1 : 1;
368 str +=(strc > 255) ? 2 : 1;
369 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
372 return u_strcmp(str, cmp); /* ASCII CP */
375 /*********************************************************************
376 * _mbsicoll(MSVCRT.@)
377 * FIXME: handle locales.
379 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
381 if(MSVCRT___mb_cur_max > 1)
383 unsigned int strc, cmpc;
386 return *cmp ? -1 : 0;
389 strc = _mbctolower(_mbsnextc(str));
390 cmpc = _mbctolower(_mbsnextc(cmp));
392 return strc < cmpc ? -1 : 1;
393 str +=(strc > 255) ? 2 : 1;
394 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
397 return u_strcasecmp(str, cmp); /* ASCII CP */
400 /*********************************************************************
402 * Performs a case-sensitive comparison according to the current code page
404 * _NLSCMPERROR if error
405 * FIXME: handle locales.
407 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
409 if(MSVCRT___mb_cur_max > 1)
411 unsigned int strc, cmpc;
414 return *cmp ? -1 : 0;
417 strc = _mbsnextc(str);
418 cmpc = _mbsnextc(cmp);
420 return strc < cmpc ? -1 : 1;
421 str +=(strc > 255) ? 2 : 1;
422 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
425 return u_strcmp(str, cmp); /* ASCII CP */
429 /*********************************************************************
432 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
434 if(MSVCRT___mb_cur_max > 1)
436 unsigned int strc, cmpc;
439 return *cmp ? -1 : 0;
442 strc = _mbctolower(_mbsnextc(str));
443 cmpc = _mbctolower(_mbsnextc(cmp));
445 return strc < cmpc ? -1 : 1;
446 str +=(strc > 255) ? 2 : 1;
447 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
450 return u_strcasecmp(str, cmp); /* ASCII CP */
453 /*********************************************************************
456 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
461 if(MSVCRT___mb_cur_max > 1)
463 unsigned int strc, cmpc;
468 return *cmp ? -1 : 0;
471 strc = _mbsnextc(str);
472 cmpc = _mbsnextc(cmp);
474 return strc < cmpc ? -1 : 1;
475 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
479 return 0; /* Matched len chars */
481 return u_strncmp(str, cmp, len); /* ASCII CP */
484 /*********************************************************************
485 * _mbsnbcmp(MSVCRT.@)
487 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
491 if(MSVCRT___mb_cur_max > 1)
493 unsigned int strc, cmpc;
498 return *cmp ? -1 : 0;
501 if (MSVCRT_isleadbyte(*str))
503 strc=(len>=2)?_mbsnextc(str):0;
511 if (MSVCRT_isleadbyte(*cmp))
512 cmpc=(len>=2)?_mbsnextc(cmp):0;
516 return strc < cmpc ? -1 : 1;
521 return 0; /* Matched len chars */
523 return u_strncmp(str,cmp,len);
526 /*********************************************************************
527 * _mbsnicmp(MSVCRT.@)
529 * Compare two multibyte strings case insensitively to 'len' characters.
531 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
533 /* FIXME: No tolower() for mb strings yet */
534 if(MSVCRT___mb_cur_max > 1)
536 unsigned int strc, cmpc;
540 return *cmp ? -1 : 0;
543 strc = _mbctolower(_mbsnextc(str));
544 cmpc = _mbctolower(_mbsnextc(cmp));
546 return strc < cmpc ? -1 : 1;
547 str +=(strc > 255) ? 2 : 1;
548 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
550 return 0; /* Matched len chars */
552 return u_strncasecmp(str, cmp, len); /* ASCII CP */
555 /*********************************************************************
556 * _mbsnbicmp(MSVCRT.@)
558 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
562 if(MSVCRT___mb_cur_max > 1)
564 unsigned int strc, cmpc;
569 return *cmp ? -1 : 0;
572 if (MSVCRT_isleadbyte(*str))
574 strc=(len>=2)?_mbsnextc(str):0;
582 if (MSVCRT_isleadbyte(*cmp))
583 cmpc=(len>=2)?_mbsnextc(cmp):0;
586 strc = _mbctolower(strc);
587 cmpc = _mbctolower(cmpc);
589 return strc < cmpc ? -1 : 1;
594 return 0; /* Matched len bytes */
596 return u_strncmp(str,cmp,len);
599 /*********************************************************************
602 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
604 strcat( (char *)dst, (const char *)src );
608 /*********************************************************************
611 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
613 strcpy( (char *)dst, (const char *)src );
617 /*********************************************************************
620 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
622 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
625 /*********************************************************************
628 * Find a multibyte character in a multibyte string.
630 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
632 if(MSVCRT___mb_cur_max > 1)
639 return (unsigned char*)s;
642 s += c > 255 ? 2 : 1;
645 return u_strchr(s, x); /* ASCII CP */
648 /*********************************************************************
651 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
653 if(MSVCRT___mb_cur_max > 1)
656 unsigned char* match=NULL;
662 match=(unsigned char*)s;
665 s +=(c > 255) ? 2 : 1;
668 return u_strrchr(s, x);
671 /*********************************************************************
674 * Find and extract tokens from strings
676 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
678 thread_data_t *data = msvcrt_get_thread_data();
681 if(MSVCRT___mb_cur_max > 1)
686 if (!(str = data->mbstok_next)) return NULL;
688 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
689 str += c > 255 ? 2 : 1;
691 if (!*str) return NULL;
693 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
694 str += c > 255 ? 2 : 1;
698 if (c > 255) *str++ = 0;
700 data->mbstok_next = str;
703 return u_strtok(str, delim); /* ASCII CP */
706 /*********************************************************************
709 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
711 /* temp var needed because MultiByteToWideChar wants non NULL destination */
712 MSVCRT_wchar_t tmpdst = '\0';
716 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
720 /* return the number of bytes from src that have been used */
723 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
728 /*********************************************************************
729 * _mbbtombc(MSVCRT.@)
731 unsigned int CDECL _mbbtombc(unsigned int c)
733 if(MSVCRT___mb_cur_max > 1 &&
734 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
736 /* FIXME: I can't get this function to return anything
737 * different from what I pass it...
740 return c; /* ASCII CP or no MB char */
743 /*********************************************************************
746 int CDECL _mbbtype(unsigned char c, int type)
750 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
752 else if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
759 if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa1 && c <= 0xdf))
761 else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
768 /*********************************************************************
769 * _ismbbkana(MSVCRT.@)
771 int CDECL _ismbbkana(unsigned int c)
773 /* FIXME: use lc_ctype when supported, not lc_all */
774 if(MSVCRT___lc_codepage == 932)
776 /* Japanese/Katakana, CP 932 */
777 return (c >= 0xa1 && c <= 0xdf);
782 /*********************************************************************
783 * _ismbcdigit(MSVCRT.@)
785 int CDECL _ismbcdigit(unsigned int ch)
787 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
788 return (get_char_typeW( wch ) & C1_DIGIT);
791 /*********************************************************************
792 * _ismbcgraph(MSVCRT.@)
794 int CDECL _ismbcgraph(unsigned int ch)
796 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
797 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
800 /*********************************************************************
801 * _ismbcalpha (MSVCRT.@)
803 int CDECL _ismbcalpha(unsigned int ch)
805 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
806 return (get_char_typeW( wch ) & C1_ALPHA);
809 /*********************************************************************
810 * _ismbclower (MSVCRT.@)
812 int CDECL _ismbclower(unsigned int ch)
814 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
815 return (get_char_typeW( wch ) & C1_UPPER);
818 /*********************************************************************
819 * _ismbcupper (MSVCRT.@)
821 int CDECL _ismbcupper(unsigned int ch)
823 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
824 return (get_char_typeW( wch ) & C1_LOWER);
827 /*********************************************************************
828 * _ismbcsymbol(MSVCRT.@)
830 int CDECL _ismbcsymbol(unsigned int ch)
832 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
834 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
836 WARN("GetStringTypeW failed on %x\n", ch);
839 return ((ctype & C3_SYMBOL) != 0);
842 /*********************************************************************
843 * _ismbcalnum (MSVCRT.@)
845 int CDECL _ismbcalnum(unsigned int ch)
847 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
848 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
851 /*********************************************************************
852 * _ismbcspace (MSVCRT.@)
854 int CDECL _ismbcspace(unsigned int ch)
856 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
857 return (get_char_typeW( wch ) & C1_SPACE);
860 /*********************************************************************
861 * _ismbcprint (MSVCRT.@)
863 int CDECL _ismbcprint(unsigned int ch)
865 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
866 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
869 /*********************************************************************
870 * _ismbcpunct(MSVCRT.@)
872 int CDECL _ismbcpunct(unsigned int ch)
874 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
875 return (get_char_typeW( wch ) & C1_PUNCT);
878 /*********************************************************************
879 * _ismbchira(MSVCRT.@)
881 int CDECL _ismbchira(unsigned int c)
883 /* FIXME: use lc_ctype when supported, not lc_all */
884 if(MSVCRT___lc_codepage == 932)
886 /* Japanese/Hiragana, CP 932 */
887 return (c >= 0x829f && c <= 0x82f1);
892 /*********************************************************************
893 * _ismbckata(MSVCRT.@)
895 int CDECL _ismbckata(unsigned int c)
897 /* FIXME: use lc_ctype when supported, not lc_all */
898 if(MSVCRT___lc_codepage == 932)
901 return _ismbbkana(c);
902 /* Japanese/Katakana, CP 932 */
903 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
908 /*********************************************************************
909 * _ismbblead(MSVCRT.@)
911 int CDECL _ismbblead(unsigned int c)
913 /* FIXME: should reference MSVCRT_mbctype */
914 return MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(c);
918 /*********************************************************************
919 * _ismbbtrail(MSVCRT.@)
921 int CDECL _ismbbtrail(unsigned int c)
923 /* FIXME: should reference MSVCRT_mbctype */
924 return !_ismbblead(c);
927 /*********************************************************************
928 * _ismbslead(MSVCRT.@)
930 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
932 /* Lead bytes can also be trail bytes if caller messed up
933 * iterating through the string...
935 if(MSVCRT___mb_cur_max > 1)
938 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
941 return MSVCRT_isleadbyte(*str);
943 return 0; /* Must have been a trail, we skipped it */
946 /*********************************************************************
947 * _ismbstrail(MSVCRT.@)
949 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
951 /* Must not be a lead, and must be preceded by one */
952 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
955 /*********************************************************************
958 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
960 unsigned char* ret = str;
962 if(MSVCRT___mb_cur_max == 1 || c < 256)
963 return u__strset(str, c); /* ASCII CP or SB char */
965 c &= 0xffff; /* Strip high bits */
967 while(str[0] && str[1])
973 str[0] = '\0'; /* FIXME: OK to shorten? */
978 /*********************************************************************
979 * _mbsnbset(MSVCRT.@)
981 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
983 unsigned char *ret = str;
988 if(MSVCRT___mb_cur_max == 1 || c < 256)
989 return u__strnset(str, c, len); /* ASCII CP or SB char */
991 c &= 0xffff; /* Strip high bits */
993 while(str[0] && str[1] && (len > 1))
1001 /* as per msdn pad with a blank character */
1008 /*********************************************************************
1009 * _mbsnset(MSVCRT.@)
1011 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
1013 unsigned char *ret = str;
1018 if(MSVCRT___mb_cur_max == 1 || c < 256)
1019 return u__strnset(str, c, len); /* ASCII CP or SB char */
1021 c &= 0xffff; /* Strip high bits */
1023 while(str[0] && str[1] && len--)
1029 str[0] = '\0'; /* FIXME: OK to shorten? */
1034 /*********************************************************************
1035 * _mbsnccnt(MSVCRT.@)
1036 * 'c' is for 'character'.
1038 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1041 if(MSVCRT___mb_cur_max > 1)
1044 while(*str && len-- > 0)
1046 if(MSVCRT_isleadbyte(*str))
1059 return min(ret, len); /* ASCII CP */
1062 /*********************************************************************
1063 * _mbsnbcnt(MSVCRT.@)
1064 * 'b' is for byte count.
1066 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1069 if(MSVCRT___mb_cur_max > 1)
1071 const unsigned char* xstr = str;
1072 while(*xstr && len-- > 0)
1074 if (MSVCRT_isleadbyte(*xstr++))
1080 return min(ret, len); /* ASCII CP */
1083 /*********************************************************************
1084 * _mbsnbcat(MSVCRT.@)
1086 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1088 if(MSVCRT___mb_cur_max > 1)
1090 unsigned char *res = dst;
1092 if (MSVCRT_isleadbyte(*dst++)) {
1096 /* as per msdn overwrite the lead byte in front of '\0' */
1102 while (*src && len--) *dst++ = *src++;
1106 return u_strncat(dst, src, len); /* ASCII CP */
1110 /*********************************************************************
1111 * _mbsncat(MSVCRT.@)
1113 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1115 if(MSVCRT___mb_cur_max > 1)
1117 unsigned char *res = dst;
1120 if (MSVCRT_isleadbyte(*dst++))
1123 while (*src && len--)
1126 if(MSVCRT_isleadbyte(*src++))
1132 return u_strncat(dst, src, len); /* ASCII CP */
1136 /*********************************************************************
1139 unsigned char* CDECL _mbslwr(unsigned char* s)
1141 unsigned char *ret = s;
1144 if (MSVCRT___mb_cur_max > 1)
1149 c = _mbctolower(_mbsnextc(s));
1150 /* Note that I assume that the size of the character is unchanged */
1159 else for ( ; *s; s++) *s = tolower(*s);
1164 /*********************************************************************
1167 unsigned char* CDECL _mbsupr(unsigned char* s)
1169 unsigned char *ret = s;
1172 if (MSVCRT___mb_cur_max > 1)
1177 c = _mbctoupper(_mbsnextc(s));
1178 /* Note that I assume that the size of the character is unchanged */
1187 else for ( ; *s; s++) *s = toupper(*s);
1192 /*********************************************************************
1193 * _mbsspn (MSVCRT.@)
1195 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1197 const unsigned char *p, *q;
1199 for (p = string; *p; p++)
1201 if (MSVCRT_isleadbyte(*p))
1203 for (q = set; *q; q++)
1207 if ((*p == *q) && (p[1] == q[1]))
1211 if (!q[0] || !q[1]) break;
1215 for (q = set; *q; q++)
1224 /*********************************************************************
1225 * _mbsspnp (MSVCRT.@)
1227 const unsigned char* CDECL _mbsspnp(const unsigned char* string, const unsigned char* set)
1229 const unsigned char *p, *q;
1231 for (p = string; *p; p++)
1233 if (MSVCRT_isleadbyte(*p))
1235 for (q = set; *q; q++)
1239 if ((*p == *q) && (p[1] == q[1]))
1243 if (!q[0] || !q[1]) break;
1247 for (q = set; *q; q++)
1258 /*********************************************************************
1259 * _mbscspn(MSVCRT.@)
1261 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1263 if (MSVCRT___mb_cur_max > 1)
1264 FIXME("don't handle double character case\n");
1265 return u_strcspn(str, cmp);
1268 /*********************************************************************
1269 * _mbsrev (MSVCRT.@)
1271 unsigned char* CDECL _mbsrev(unsigned char* str)
1273 int i, len = _mbslen(str);
1274 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1279 /* unpack multibyte string to temp buffer */
1281 for(i=0; i<len; i++)
1283 if (MSVCRT_isleadbyte(*p))
1295 /* repack it in the reverse order */
1297 for(i=len-1; i>=0; i--)
1299 if(MSVCRT_isleadbyte(temp[i*2]))
1315 /*********************************************************************
1316 * _mbspbrk (MSVCRT.@)
1318 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1320 const unsigned char* p;
1324 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1327 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1328 return (unsigned char*)str;
1330 str += (MSVCRT_isleadbyte(*str)?2:1);