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"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
32 unsigned char MSVCRT_mbctype
[257] = { 0 };
33 static int g_mbcp_is_multibyte
= 0;
35 int MSVCRT___mb_cur_max
= 1;
37 /* It seems that the data about valid trail bytes is not available from kernel32
38 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
39 struct cp_extra_info_t
42 BYTE TrailBytes
[MAX_LEADBYTES
];
45 static struct cp_extra_info_t g_cpextrainfo
[] =
47 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
48 {936, {0x40, 0xfe, 0, 0}},
49 {949, {0x41, 0xfe, 0, 0}},
50 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
51 {1361, {0x31, 0x7e, 0x81, 0xfe, 0, 0}},
52 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
53 {0, {1, 255, 0, 0}} /* match all with FIXME */
56 /* Maps multibyte cp932 punctuation marks to single byte equivalents */
57 static const unsigned char mbctombb_932_punct
[] = {
58 0x20,0xa4,0xa1,0x2c,0x2e,0xa5,0x3a,0x3b,0x3f,0x21,0xde,0xdf,0x00,0x00,0x00,0x5e,
59 0x7e,0x5f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x00,0x00,0x2f,0x00,
60 0x00,0x00,0x7c,0x00,0x00,0x60,0x27,0x00,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,0x7b,
61 0x7d,0x00,0x00,0x00,0x00,0xa2,0xa3,0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,0x00,
62 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5c,
63 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40};
65 /* Maps multibyte cp932 hiragana/katakana to single-byte equivalents */
66 static const unsigned char mbctombb_932_kana
[] = {
67 0xa7,0xb1,0xa8,0xb2,0xa9,0xb3,0xaa,0xb4,0xab,0xb5,0xb6,0xb6,0xb7,0xb7,0xb8,0xb8,
68 0xb9,0xb9,0xba,0xba,0xbb,0xbb,0xbc,0xbc,0xbd,0xbd,0xbe,0xbe,0xbf,0xbf,0xc0,0xc0,
69 0xc1,0xc1,0xaf,0xc2,0xc2,0xc3,0xc3,0xc4,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xca,
70 0xca,0xcb,0xcb,0xcb,0xcc,0xcc,0xcc,0xcd,0xcd,0xcd,0xce,0xce,0xce,0xcf,0xd0,0xd1,
71 0xd2,0xd3,0xac,0xd4,0xad,0xd5,0xae,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdc,0xb2,
72 0xb4,0xa6,0xdd,0xb3,0xb6,0xb9};
74 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
84 mbch
[0] = (ch
>> 8) & 0xff;
88 if (!MultiByteToWideChar(get_locale()->locinfo
->lc_codepage
, 0, mbch
, n_chars
, &chW
, 1))
90 WARN("MultiByteToWideChar failed on %x\n", ch
);
96 static inline MSVCRT_size_t
u_strlen( const unsigned char *str
)
98 return strlen( (const char*) str
);
101 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
103 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
106 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
108 return strcmp( (const char*)s1
, (const char*)s2
);
111 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
113 return strcasecmp( (const char*)s1
, (const char*)s2
);
116 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
118 return strncmp( (const char*)s1
, (const char*)s2
, len
);
121 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, MSVCRT_size_t len
)
123 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
126 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
128 return (unsigned char*) strchr( (const char*)s
, x
);
131 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
133 return (unsigned char*) strrchr( (const char*)s
, x
);
136 static inline unsigned char *u_strtok( unsigned char *s
, const unsigned char *delim
)
138 return (unsigned char*) strtok( (char*)s
, (const char*)delim
);
141 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
143 return (unsigned char*) _strset( (char*)s
, c
);
146 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, MSVCRT_size_t len
)
148 return (unsigned char*) MSVCRT__strnset( (char*)s
, c
, len
);
151 static inline MSVCRT_size_t
u_strcspn( const unsigned char *s
, const unsigned char *rej
)
153 return strcspn( (const char *)s
, (const char*)rej
);
156 /*********************************************************************
157 * __p__mbctype (MSVCRT.@)
159 unsigned char* CDECL
__p__mbctype(void)
161 return MSVCRT_mbctype
;
164 /*********************************************************************
165 * ___mb_cur_max_func(MSVCRT.@)
167 int* CDECL
MSVCRT____mb_cur_max_func(void)
169 return &get_locale()->locinfo
->mb_cur_max
;
172 /*********************************************************************
173 * _setmbcp (MSVCRT.@)
175 int CDECL
_setmbcp(int cp
)
177 MSVCRT__locale_t locale
= get_locale();
198 newcp
= locale
->locinfo
->lc_codepage
;
201 newcp
= 20127; /* ASCII */
208 if (!GetCPInfo(newcp
, &cpi
))
210 WARN("Codepage %d not found\n", newcp
);
211 *MSVCRT__errno() = MSVCRT_EINVAL
;
215 /* setup the _mbctype */
216 memset(MSVCRT_mbctype
, 0, sizeof(MSVCRT_mbctype
));
218 bytes
= cpi
.LeadByte
;
219 while (bytes
[0] || bytes
[1])
221 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
222 MSVCRT_mbctype
[i
+ 1] |= _M1
;
226 if (cpi
.MaxCharSize
> 1)
228 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
229 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
231 g_mbcp_is_multibyte
= 1;
234 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
236 if (cpextra
->cp
== 0)
237 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
239 bytes
= cpextra
->TrailBytes
;
240 while (bytes
[0] || bytes
[1])
242 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
243 MSVCRT_mbctype
[i
+ 1] |= _M2
;
252 g_mbcp_is_multibyte
= 0;
254 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
257 for (i
= 0; i
< 256; i
++)
258 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
259 bufA
[charcount
++] = i
;
261 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
262 if (ret
!= charcount
)
263 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
265 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
267 curr_type
= chartypes
;
268 for (i
= 0; i
< 256; i
++)
269 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
271 if ((*curr_type
) & C1_UPPER
)
272 MSVCRT_mbctype
[i
+ 1] |= _SBUP
;
273 if ((*curr_type
) & C1_LOWER
)
274 MSVCRT_mbctype
[i
+ 1] |= _SBLOW
;
278 if (newcp
== 932) /* CP932 only - set _MP and _MS */
280 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
281 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
282 * it hard. As this is set only for codepage 932 we hardcode it what gives
283 * also faster execution.
285 for (i
= 161; i
<= 165; i
++)
286 MSVCRT_mbctype
[i
+ 1] |= _MP
;
287 for (i
= 166; i
<= 223; i
++)
288 MSVCRT_mbctype
[i
+ 1] |= _MS
;
291 locale
->locinfo
->lc_collate_cp
= newcp
;
292 locale
->locinfo
->lc_codepage
= newcp
;
293 TRACE("(%d) -> %d\n", cp
, locale
->locinfo
->lc_codepage
);
297 /*********************************************************************
298 * _getmbcp (MSVCRT.@)
300 int CDECL
_getmbcp(void)
302 /* FIXME: get_locale()->mbcinfo->mbcodepage should be used here */
303 return get_locale()->locinfo
->lc_codepage
;
306 /*********************************************************************
307 * _mbsnextc(MSVCRT.@)
309 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
312 return *str
<< 8 | str
[1];
316 /*********************************************************************
317 * _mbctolower(MSVCRT.@)
319 unsigned int CDECL
_mbctolower(unsigned int c
)
321 if (MSVCRT_isleadbyte(c
))
323 FIXME("Handle MBC chars\n");
326 return tolower(c
); /* ASCII CP or SB char */
329 /*********************************************************************
330 * _mbctoupper(MSVCRT.@)
332 unsigned int CDECL
_mbctoupper(unsigned int c
)
334 if (MSVCRT_isleadbyte(c
))
336 FIXME("Handle MBC chars\n");
339 return toupper(c
); /* ASCII CP or SB char */
342 /*********************************************************************
343 * _mbctombb (MSVCRT.@)
345 unsigned int CDECL
_mbctombb(unsigned int c
)
349 if(get_locale()->locinfo
->lc_codepage
== 932)
351 if(c
>= 0x829f && c
<= 0x82f1) /* Hiragana */
352 return mbctombb_932_kana
[c
- 0x829f];
353 if(c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f) /* Katakana */
354 return mbctombb_932_kana
[c
- 0x8340 - (c
>= 0x837f ? 1 : 0)];
355 if(c
>= 0x8140 && c
<= 0x8197) /* Punctuation */
357 value
= mbctombb_932_punct
[c
- 0x8140];
358 return value
? value
: c
;
360 if((c
>= 0x824f && c
<= 0x8258) || /* Fullwidth digits */
361 (c
>= 0x8260 && c
<= 0x8279)) /* Fullwidth capitals letters */
363 if(c
>= 0x8281 && c
<= 0x829a) /* Fullwidth small letters */
365 /* all other cases return c */
370 /*********************************************************************
371 * _mbcjistojms(MSVCRT.@)
373 * Converts a jis character to sjis.
374 * Based on description from
375 * http://www.slayers.ne.jp/~oouchi/code/jistosjis.html
377 unsigned int CDECL
_mbcjistojms(unsigned int c
)
379 /* Conversion takes place only when codepage is 932.
380 In all other cases, c is returned unchanged */
381 if(get_locale()->locinfo
->lc_codepage
== 932)
383 if(HIBYTE(c
) >= 0x21 && HIBYTE(c
) <= 0x7e &&
384 LOBYTE(c
) >= 0x21 && LOBYTE(c
) <= 0x7e)
394 c
= (((HIBYTE(c
) - 0x21)/2 + 0x81) << 8) | LOBYTE(c
);
400 return 0; /* Codepage is 932, but c can't be converted */
406 /*********************************************************************
409 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
411 if(get_locale()->locinfo
->mb_cur_max
> 1)
412 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
414 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
417 /*********************************************************************
420 unsigned int CDECL
_mbclen(const unsigned char* str
)
422 return _ismbblead(*str
) ? 2 : 1;
425 /*********************************************************************
428 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
430 return (unsigned char *)(str
+ _mbclen(str
));
433 /*********************************************************************
436 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
441 while (num
> 0 && *str
)
443 if (_ismbblead(*str
))
453 return (unsigned char*)str
;
456 /*********************************************************************
459 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
461 MSVCRT_size_t len
= 0;
464 if (_ismbblead(*str
))
467 if (!*str
) /* count only full chars */
476 /*********************************************************************
479 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
483 *++dest
= *++src
; /* MB char */
486 /*********************************************************************
489 * The parameter n is the number or characters to copy, not the size of
490 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
492 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
494 unsigned char* ret
= dst
;
497 if (g_mbcp_is_multibyte
)
502 if (_ismbblead(*src
))
522 if (!(*dst
++ = *src
++)) break;
525 while (n
--) *dst
++ = 0;
529 /*********************************************************************
530 * _mbsnbcpy_s(MSVCRT.@)
532 * Unlike _mbsnbcpy this function does not pad the rest of the dest
535 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
537 MSVCRT_size_t pos
= 0;
539 if(!dst
|| size
== 0)
540 return MSVCRT_EINVAL
;
544 return MSVCRT_EINVAL
;
549 if(g_mbcp_is_multibyte
)
557 return MSVCRT_ERANGE
;
559 is_lead
= (!is_lead
&& _ismbblead(*src
));
564 if (is_lead
) /* if string ends with a lead, remove it */
575 return MSVCRT_ERANGE
;
588 return MSVCRT_ERANGE
;
594 /*********************************************************************
595 * _mbsnbcpy(MSVCRT.@)
597 * Like strncpy this function doesn't enforce the string to be
600 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
602 unsigned char* ret
= dst
;
605 if(g_mbcp_is_multibyte
)
610 is_lead
= (!is_lead
&& _ismbblead(*src
));
615 if (is_lead
) /* if string ends with a lead, remove it */
623 if (!(*dst
++ = *src
++)) break;
626 while (n
--) *dst
++ = 0;
630 /*********************************************************************
633 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
635 if(get_locale()->locinfo
->mb_cur_max
> 1)
637 unsigned int strc
, cmpc
;
640 return *cmp
? -1 : 0;
643 strc
= _mbsnextc(str
);
644 cmpc
= _mbsnextc(cmp
);
646 return strc
< cmpc
? -1 : 1;
647 str
+=(strc
> 255) ? 2 : 1;
648 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
651 return u_strcmp(str
, cmp
); /* ASCII CP */
654 /*********************************************************************
655 * _mbsicoll(MSVCRT.@)
656 * FIXME: handle locales.
658 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
660 if(get_locale()->locinfo
->mb_cur_max
> 1)
662 unsigned int strc
, cmpc
;
665 return *cmp
? -1 : 0;
668 strc
= _mbctolower(_mbsnextc(str
));
669 cmpc
= _mbctolower(_mbsnextc(cmp
));
671 return strc
< cmpc
? -1 : 1;
672 str
+=(strc
> 255) ? 2 : 1;
673 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
676 return u_strcasecmp(str
, cmp
); /* ASCII CP */
679 /*********************************************************************
681 * Performs a case-sensitive comparison according to the current code page
683 * _NLSCMPERROR if error
684 * FIXME: handle locales.
686 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
688 if(get_locale()->locinfo
->mb_cur_max
> 1)
690 unsigned int strc
, cmpc
;
693 return *cmp
? -1 : 0;
696 strc
= _mbsnextc(str
);
697 cmpc
= _mbsnextc(cmp
);
699 return strc
< cmpc
? -1 : 1;
700 str
+=(strc
> 255) ? 2 : 1;
701 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
704 return u_strcmp(str
, cmp
); /* ASCII CP */
708 /*********************************************************************
711 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
713 if(get_locale()->locinfo
->mb_cur_max
> 1)
715 unsigned int strc
, cmpc
;
718 return *cmp
? -1 : 0;
721 strc
= _mbctolower(_mbsnextc(str
));
722 cmpc
= _mbctolower(_mbsnextc(cmp
));
724 return strc
< cmpc
? -1 : 1;
725 str
+=(strc
> 255) ? 2 : 1;
726 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
729 return u_strcasecmp(str
, cmp
); /* ASCII CP */
732 /*********************************************************************
735 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
740 if(get_locale()->locinfo
->mb_cur_max
> 1)
742 unsigned int strc
, cmpc
;
747 return *cmp
? -1 : 0;
750 strc
= _mbsnextc(str
);
751 cmpc
= _mbsnextc(cmp
);
753 return strc
< cmpc
? -1 : 1;
754 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
758 return 0; /* Matched len chars */
760 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
763 /*********************************************************************
764 * _mbsnbcmp(MSVCRT.@)
766 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
770 if(get_locale()->locinfo
->mb_cur_max
> 1)
772 unsigned int strc
, cmpc
;
777 return *cmp
? -1 : 0;
780 if (MSVCRT_isleadbyte(*str
))
782 strc
=(len
>=2)?_mbsnextc(str
):0;
790 if (MSVCRT_isleadbyte(*cmp
))
791 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
795 return strc
< cmpc
? -1 : 1;
800 return 0; /* Matched len chars */
802 return u_strncmp(str
,cmp
,len
);
805 /*********************************************************************
806 * _mbsnicmp(MSVCRT.@)
808 * Compare two multibyte strings case insensitively to 'len' characters.
810 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
812 /* FIXME: No tolower() for mb strings yet */
813 if(get_locale()->locinfo
->mb_cur_max
> 1)
815 unsigned int strc
, cmpc
;
819 return *cmp
? -1 : 0;
822 strc
= _mbctolower(_mbsnextc(str
));
823 cmpc
= _mbctolower(_mbsnextc(cmp
));
825 return strc
< cmpc
? -1 : 1;
826 str
+=(strc
> 255) ? 2 : 1;
827 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
829 return 0; /* Matched len chars */
831 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
834 /*********************************************************************
835 * _mbsnbicmp(MSVCRT.@)
837 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
841 if(get_locale()->locinfo
->mb_cur_max
> 1)
843 unsigned int strc
, cmpc
;
848 return *cmp
? -1 : 0;
851 if (MSVCRT_isleadbyte(*str
))
853 strc
=(len
>=2)?_mbsnextc(str
):0;
861 if (MSVCRT_isleadbyte(*cmp
))
862 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
865 strc
= _mbctolower(strc
);
866 cmpc
= _mbctolower(cmpc
);
868 return strc
< cmpc
? -1 : 1;
873 return 0; /* Matched len bytes */
875 return u_strncasecmp(str
,cmp
,len
);
878 /*********************************************************************
881 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
883 strcat( (char *)dst
, (const char *)src
);
887 /*********************************************************************
890 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
892 strcpy( (char *)dst
, (const char *)src
);
896 /*********************************************************************
899 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
901 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
904 /*********************************************************************
907 * Find a multibyte character in a multibyte string.
909 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
911 if(get_locale()->locinfo
->mb_cur_max
> 1)
918 return (unsigned char*)s
;
921 s
+= c
> 255 ? 2 : 1;
924 return u_strchr(s
, x
); /* ASCII CP */
927 /*********************************************************************
930 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
932 if(get_locale()->locinfo
->mb_cur_max
> 1)
935 unsigned char* match
=NULL
;
941 match
=(unsigned char*)s
;
944 s
+=(c
> 255) ? 2 : 1;
947 return u_strrchr(s
, x
);
950 /*********************************************************************
953 * Find and extract tokens from strings
955 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
957 thread_data_t
*data
= msvcrt_get_thread_data();
960 if(get_locale()->locinfo
->mb_cur_max
> 1)
965 if (!(str
= data
->mbstok_next
)) return NULL
;
967 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
968 str
+= c
> 255 ? 2 : 1;
970 if (!*str
) return NULL
;
972 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
973 str
+= c
> 255 ? 2 : 1;
977 if (c
> 255) *str
++ = 0;
979 data
->mbstok_next
= str
;
982 return u_strtok(str
, delim
); /* ASCII CP */
985 /*********************************************************************
988 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
990 /* temp var needed because MultiByteToWideChar wants non NULL destination */
991 MSVCRT_wchar_t tmpdst
= '\0';
995 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, &tmpdst
, 1))
999 /* return the number of bytes from src that have been used */
1002 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
1007 /*********************************************************************
1008 * _mbbtombc(MSVCRT.@)
1010 unsigned int CDECL
_mbbtombc(unsigned int c
)
1012 if(get_locale()->locinfo
->mb_cur_max
> 1 &&
1013 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
1015 /* FIXME: I can't get this function to return anything
1016 * different from what I pass it...
1019 return c
; /* ASCII CP or no MB char */
1022 /*********************************************************************
1023 * _mbbtype(MSVCRT.@)
1025 int CDECL
_mbbtype(unsigned char c
, int type
)
1029 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1031 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
1034 return _MBC_ILLEGAL
;
1038 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
1040 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
1043 return _MBC_ILLEGAL
;
1047 /*********************************************************************
1048 * _ismbbkana(MSVCRT.@)
1050 int CDECL
_ismbbkana(unsigned int c
)
1052 /* FIXME: use lc_ctype when supported, not lc_all */
1053 if(get_locale()->locinfo
->lc_codepage
== 932)
1055 /* Japanese/Katakana, CP 932 */
1056 return (c
>= 0xa1 && c
<= 0xdf);
1061 /*********************************************************************
1062 * _ismbcdigit(MSVCRT.@)
1064 int CDECL
_ismbcdigit(unsigned int ch
)
1066 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1067 return (get_char_typeW( wch
) & C1_DIGIT
);
1070 /*********************************************************************
1071 * _ismbcgraph(MSVCRT.@)
1073 int CDECL
_ismbcgraph(unsigned int ch
)
1075 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1076 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
1079 /*********************************************************************
1080 * _ismbcalpha (MSVCRT.@)
1082 int CDECL
_ismbcalpha(unsigned int ch
)
1084 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1085 return (get_char_typeW( wch
) & C1_ALPHA
);
1088 /*********************************************************************
1089 * _ismbclower (MSVCRT.@)
1091 int CDECL
_ismbclower(unsigned int ch
)
1093 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1094 return (get_char_typeW( wch
) & C1_UPPER
);
1097 /*********************************************************************
1098 * _ismbcupper (MSVCRT.@)
1100 int CDECL
_ismbcupper(unsigned int ch
)
1102 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1103 return (get_char_typeW( wch
) & C1_LOWER
);
1106 /*********************************************************************
1107 * _ismbcsymbol(MSVCRT.@)
1109 int CDECL
_ismbcsymbol(unsigned int ch
)
1111 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1113 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1115 WARN("GetStringTypeW failed on %x\n", ch
);
1118 return ((ctype
& C3_SYMBOL
) != 0);
1121 /*********************************************************************
1122 * _ismbcalnum (MSVCRT.@)
1124 int CDECL
_ismbcalnum(unsigned int ch
)
1126 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1127 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1130 /*********************************************************************
1131 * _ismbcspace (MSVCRT.@)
1133 int CDECL
_ismbcspace(unsigned int ch
)
1135 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1136 return (get_char_typeW( wch
) & C1_SPACE
);
1139 /*********************************************************************
1140 * _ismbcprint (MSVCRT.@)
1142 int CDECL
_ismbcprint(unsigned int ch
)
1144 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1145 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1148 /*********************************************************************
1149 * _ismbcpunct(MSVCRT.@)
1151 int CDECL
_ismbcpunct(unsigned int ch
)
1153 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1154 return (get_char_typeW( wch
) & C1_PUNCT
);
1157 /*********************************************************************
1158 * _ismbchira(MSVCRT.@)
1160 int CDECL
_ismbchira(unsigned int c
)
1162 /* FIXME: use lc_ctype when supported, not lc_all */
1163 if(get_locale()->locinfo
->lc_codepage
== 932)
1165 /* Japanese/Hiragana, CP 932 */
1166 return (c
>= 0x829f && c
<= 0x82f1);
1171 /*********************************************************************
1172 * _ismbckata(MSVCRT.@)
1174 int CDECL
_ismbckata(unsigned int c
)
1176 /* FIXME: use lc_ctype when supported, not lc_all */
1177 if(get_locale()->locinfo
->lc_codepage
== 932)
1180 return _ismbbkana(c
);
1181 /* Japanese/Katakana, CP 932 */
1182 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1187 /*********************************************************************
1188 * _ismbblead(MSVCRT.@)
1190 int CDECL
_ismbblead(unsigned int c
)
1192 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1196 /*********************************************************************
1197 * _ismbbtrail(MSVCRT.@)
1199 int CDECL
_ismbbtrail(unsigned int c
)
1201 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1204 /*********************************************************************
1205 * _ismbclegal(MSVCRT.@)
1207 int CDECL
_ismbclegal(unsigned int c
)
1209 return _ismbblead(HIBYTE(c
)) && _ismbbtrail(LOBYTE(c
));
1212 /*********************************************************************
1213 * _ismbslead(MSVCRT.@)
1215 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1219 if(!g_mbcp_is_multibyte
)
1222 /* Lead bytes can also be trail bytes so we need to analyse the string
1224 while (start
<= str
)
1228 lead
= !lead
&& _ismbblead(*start
);
1232 return lead
? -1 : 0;
1235 /*********************************************************************
1236 * _ismbstrail(MSVCRT.@)
1238 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1240 /* Note: this function doesn't check _ismbbtrail */
1241 if ((str
> start
) && _ismbslead(start
, str
-1))
1247 /*********************************************************************
1248 * _mbsbtype (MSVCRT.@)
1250 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1253 const unsigned char *end
= str
+ count
;
1254 int mbcp
= g_mbcp_is_multibyte
;
1256 /* Lead bytes can also be trail bytes so we need to analyse the string.
1257 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1259 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1262 return _MBC_ILLEGAL
;
1263 lead
= mbcp
&& !lead
&& _ismbblead(*str
);
1268 if (_ismbbtrail(*str
))
1271 return _MBC_ILLEGAL
;
1273 if (_ismbblead(*str
))
1279 /*********************************************************************
1282 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1284 unsigned char* ret
= str
;
1286 if(get_locale()->locinfo
->mb_cur_max
== 1 || c
< 256)
1287 return u__strset(str
, c
); /* ASCII CP or SB char */
1289 c
&= 0xffff; /* Strip high bits */
1291 while(str
[0] && str
[1])
1297 str
[0] = '\0'; /* FIXME: OK to shorten? */
1302 /*********************************************************************
1303 * _mbsnbset(MSVCRT.@)
1305 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1307 unsigned char *ret
= str
;
1312 if(get_locale()->locinfo
->mb_cur_max
== 1 || c
< 256)
1313 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1315 c
&= 0xffff; /* Strip high bits */
1317 while(str
[0] && str
[1] && (len
> 1))
1325 /* as per msdn pad with a blank character */
1332 /*********************************************************************
1333 * _mbsnset(MSVCRT.@)
1335 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1337 unsigned char *ret
= str
;
1342 if(get_locale()->locinfo
->mb_cur_max
== 1 || c
< 256)
1343 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1345 c
&= 0xffff; /* Strip high bits */
1347 while(str
[0] && str
[1] && len
--)
1353 str
[0] = '\0'; /* FIXME: OK to shorten? */
1358 /*********************************************************************
1359 * _mbsnccnt(MSVCRT.@)
1360 * 'c' is for 'character'.
1362 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1365 if(get_locale()->locinfo
->mb_cur_max
> 1)
1368 while(*str
&& len
-- > 0)
1370 if(MSVCRT_isleadbyte(*str
))
1383 return min(ret
, len
); /* ASCII CP */
1386 /*********************************************************************
1387 * _mbsnbcnt(MSVCRT.@)
1388 * 'b' is for byte count.
1390 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1393 if(get_locale()->locinfo
->mb_cur_max
> 1)
1395 const unsigned char* xstr
= str
;
1396 while(*xstr
&& len
-- > 0)
1398 if (MSVCRT_isleadbyte(*xstr
++))
1404 return min(ret
, len
); /* ASCII CP */
1407 /*********************************************************************
1408 * _mbsnbcat(MSVCRT.@)
1410 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1412 if(get_locale()->locinfo
->mb_cur_max
> 1)
1414 unsigned char *res
= dst
;
1416 if (MSVCRT_isleadbyte(*dst
++)) {
1420 /* as per msdn overwrite the lead byte in front of '\0' */
1426 while (*src
&& len
--) *dst
++ = *src
++;
1430 return u_strncat(dst
, src
, len
); /* ASCII CP */
1434 /*********************************************************************
1435 * _mbsncat(MSVCRT.@)
1437 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1439 if(get_locale()->locinfo
->mb_cur_max
> 1)
1441 unsigned char *res
= dst
;
1444 if (MSVCRT_isleadbyte(*dst
++))
1447 while (*src
&& len
--)
1450 if(MSVCRT_isleadbyte(*src
++))
1456 return u_strncat(dst
, src
, len
); /* ASCII CP */
1460 /*********************************************************************
1463 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1465 unsigned char *ret
= s
;
1468 if (get_locale()->locinfo
->mb_cur_max
> 1)
1473 c
= _mbctolower(_mbsnextc(s
));
1474 /* Note that I assume that the size of the character is unchanged */
1483 else for ( ; *s
; s
++) *s
= tolower(*s
);
1488 /*********************************************************************
1491 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1493 unsigned char *ret
= s
;
1496 if (get_locale()->locinfo
->mb_cur_max
> 1)
1501 c
= _mbctoupper(_mbsnextc(s
));
1502 /* Note that I assume that the size of the character is unchanged */
1511 else for ( ; *s
; s
++) *s
= toupper(*s
);
1516 /*********************************************************************
1517 * _mbsspn (MSVCRT.@)
1519 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1521 const unsigned char *p
, *q
;
1523 for (p
= string
; *p
; p
++)
1525 if (MSVCRT_isleadbyte(*p
))
1527 for (q
= set
; *q
; q
++)
1531 if ((*p
== *q
) && (p
[1] == q
[1]))
1535 if (!q
[0] || !q
[1]) break;
1539 for (q
= set
; *q
; q
++)
1548 /*********************************************************************
1549 * _mbsspnp (MSVCRT.@)
1551 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1553 const unsigned char *p
, *q
;
1555 for (p
= string
; *p
; p
++)
1557 if (MSVCRT_isleadbyte(*p
))
1559 for (q
= set
; *q
; q
++)
1563 if ((*p
== *q
) && (p
[1] == q
[1]))
1567 if (!q
[0] || !q
[1]) break;
1571 for (q
= set
; *q
; q
++)
1579 return (unsigned char *)p
;
1582 /*********************************************************************
1583 * _mbscspn(MSVCRT.@)
1585 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1587 if (get_locale()->locinfo
->mb_cur_max
> 1)
1588 FIXME("don't handle double character case\n");
1589 return u_strcspn(str
, cmp
);
1592 /*********************************************************************
1593 * _mbsrev (MSVCRT.@)
1595 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1597 int i
, len
= _mbslen(str
);
1598 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1603 /* unpack multibyte string to temp buffer */
1605 for(i
=0; i
<len
; i
++)
1607 if (MSVCRT_isleadbyte(*p
))
1619 /* repack it in the reverse order */
1621 for(i
=len
-1; i
>=0; i
--)
1623 if(MSVCRT_isleadbyte(temp
[i
*2]))
1639 /*********************************************************************
1640 * _mbspbrk (MSVCRT.@)
1642 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1644 const unsigned char* p
;
1648 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
1651 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
1652 return (unsigned char*)str
;
1654 str
+= (MSVCRT_isleadbyte(*str
)?2:1);
1661 * Functions depending on locale codepage
1664 /*********************************************************************
1667 * Unlike most of the multibyte string functions this function uses
1668 * the locale codepage, not the codepage set by _setmbcp
1670 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1672 if (str
&& *str
&& size
)
1674 if(get_locale()->locinfo
->mb_cur_max
== 1)
1675 return 1; /* ASCII CP */
1677 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1682 /*********************************************************************
1683 * _mbstrlen(MSVCRT.@)
1685 * Unlike most of the multibyte string functions this function uses
1686 * the locale codepage, not the codepage set by _setmbcp
1688 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1690 if(get_locale()->locinfo
->mb_cur_max
> 1)
1692 MSVCRT_size_t len
= 0;
1695 /* FIXME: According to the documentation we are supposed to test for
1696 * multi-byte character validity. Whatever that means
1698 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1703 return strlen(str
); /* ASCII CP */