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] = { 0 };
34 static int g_mbcp_is_multibyte
= 0;
36 int MSVCRT___mb_cur_max
= 1;
37 extern int MSVCRT___lc_collate_cp
;
39 /* It seems that the data about valid trail bytes is not available from kernel32
40 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
41 struct cp_extra_info_t
44 BYTE TrailBytes
[MAX_LEADBYTES
];
47 static struct cp_extra_info_t g_cpextrainfo
[] =
49 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
50 {936, {0x40, 0xfe, 0, 0}},
51 {949, {0x41, 0xfe, 0, 0}},
52 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
53 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
54 {0, {1, 255, 0, 0}} /* match all with FIXME */
57 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
67 mbch
[0] = (ch
>> 8) & 0xff;
71 if (!MultiByteToWideChar(MSVCRT___lc_codepage
, 0, mbch
, n_chars
, &chW
, 1))
73 WARN("MultiByteToWideChar failed on %x\n", ch
);
79 static inline size_t u_strlen( const unsigned char *str
)
81 return strlen( (const char*) str
);
84 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, size_t len
)
86 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
89 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
91 return strcmp( (const char*)s1
, (const char*)s2
);
94 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
96 return strcasecmp( (const char*)s1
, (const char*)s2
);
99 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
101 return strncmp( (const char*)s1
, (const char*)s2
, len
);
104 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
106 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
109 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
111 return (unsigned char*) strchr( (const char*)s
, x
);
114 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
116 return (unsigned char*) strrchr( (const char*)s
, x
);
119 static inline unsigned char *u_strtok( unsigned char *s
, const unsigned char *delim
)
121 return (unsigned char*) strtok( (char*)s
, (const char*)delim
);
124 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
126 return (unsigned char*) _strset( (char*)s
, c
);
129 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, size_t len
)
131 return (unsigned char*) _strnset( (char*)s
, c
, len
);
134 static inline size_t u_strcspn( const unsigned char *s
, const unsigned char *rej
)
136 return strcspn( (const char *)s
, (const char*)rej
);
139 /*********************************************************************
140 * __p__mbctype (MSVCRT.@)
142 unsigned char* CDECL
__p__mbctype(void)
144 return MSVCRT_mbctype
;
147 /*********************************************************************
148 * __p___mb_cur_max(MSVCRT.@)
150 int* CDECL
__p___mb_cur_max(void)
152 return &MSVCRT___mb_cur_max
;
155 /*********************************************************************
156 * _setmbcp (MSVCRT.@)
158 int CDECL
_setmbcp(int cp
)
180 newcp
= MSVCRT___lc_codepage
;
183 newcp
= 20127; /* ASCII */
190 if (!GetCPInfo(newcp
, &cpi
))
192 WARN("Codepage %d not found\n", newcp
);
193 *MSVCRT__errno() = MSVCRT_EINVAL
;
197 /* setup the _mbctype */
198 memset(MSVCRT_mbctype
, 0, sizeof(MSVCRT_mbctype
));
200 bytes
= cpi
.LeadByte
;
201 while (bytes
[0] || bytes
[1])
203 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
204 MSVCRT_mbctype
[i
+ 1] |= _M1
;
208 if (cpi
.MaxCharSize
> 1)
210 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
211 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
213 g_mbcp_is_multibyte
= 1;
216 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
218 if (cpextra
->cp
== 0)
219 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
221 bytes
= cpextra
->TrailBytes
;
222 while (bytes
[0] || bytes
[1])
224 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
225 MSVCRT_mbctype
[i
+ 1] |= _M2
;
234 g_mbcp_is_multibyte
= 0;
236 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
239 for (i
= 0; i
< 256; i
++)
240 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
241 bufA
[charcount
++] = i
;
243 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
244 if (ret
!= charcount
)
245 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
247 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
249 curr_type
= chartypes
;
250 for (i
= 0; i
< 256; i
++)
251 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
253 if ((*curr_type
) & C1_UPPER
)
254 MSVCRT_mbctype
[i
+ 1] |= _SBUP
;
255 if ((*curr_type
) & C1_LOWER
)
256 MSVCRT_mbctype
[i
+ 1] |= _SBLOW
;
260 if (newcp
== 932) /* CP932 only - set _MP and _MS */
262 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
263 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
264 * it hard. As this is set only for codepage 932 we hardcode it what gives
265 * also faster execution.
267 for (i
= 161; i
<= 165; i
++)
268 MSVCRT_mbctype
[i
+ 1] |= _MP
;
269 for (i
= 166; i
<= 223; i
++)
270 MSVCRT_mbctype
[i
+ 1] |= _MS
;
273 MSVCRT___lc_collate_cp
= MSVCRT___lc_codepage
= newcp
;
274 TRACE("(%d) -> %d\n", cp
, MSVCRT___lc_codepage
);
278 /*********************************************************************
279 * _getmbcp (MSVCRT.@)
281 int CDECL
_getmbcp(void)
283 return MSVCRT___lc_codepage
;
286 /*********************************************************************
287 * _mbsnextc(MSVCRT.@)
289 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
292 return *str
<< 8 | str
[1];
296 /*********************************************************************
297 * _mbctolower(MSVCRT.@)
299 unsigned int CDECL
_mbctolower(unsigned int c
)
301 if (MSVCRT_isleadbyte(c
))
303 FIXME("Handle MBC chars\n");
306 return tolower(c
); /* ASCII CP or SB char */
309 /*********************************************************************
310 * _mbctoupper(MSVCRT.@)
312 unsigned int CDECL
_mbctoupper(unsigned int c
)
314 if (MSVCRT_isleadbyte(c
))
316 FIXME("Handle MBC chars\n");
319 return toupper(c
); /* ASCII CP or SB char */
322 /*********************************************************************
325 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
327 if(MSVCRT___mb_cur_max
> 1)
328 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
330 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
333 /*********************************************************************
336 unsigned int CDECL
_mbclen(const unsigned char* str
)
338 return _ismbblead(*str
) ? 2 : 1;
341 /*********************************************************************
344 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
346 return (unsigned char *)(str
+ _mbclen(str
));
349 /*********************************************************************
352 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
357 while (num
> 0 && *str
)
359 if (_ismbblead(*str
))
369 return (unsigned char*)str
;
372 /*********************************************************************
375 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
377 MSVCRT_size_t len
= 0;
380 if (_ismbblead(*str
))
383 if (!*str
) /* count only full chars */
392 /*********************************************************************
395 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
399 *++dest
= *++src
; /* MB char */
402 /*********************************************************************
405 * The parameter n is the number or characters to copy, not the size of
406 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
408 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
410 unsigned char* ret
= dst
;
413 if (g_mbcp_is_multibyte
)
418 if (_ismbblead(*src
))
438 if (!(*dst
++ = *src
++)) break;
441 while (n
--) *dst
++ = 0;
445 /*********************************************************************
446 * _mbsnbcpy_s(MSVCRT.@)
448 * Unlike _mbsnbcpy this function does not pad the rest of the dest
451 int CDECL
_mbsnbcpy_s(unsigned char* dst
, MSVCRT_size_t size
, const unsigned char* src
, MSVCRT_size_t n
)
453 MSVCRT_size_t pos
= 0;
455 if(!dst
|| size
== 0)
456 return MSVCRT_EINVAL
;
460 return MSVCRT_EINVAL
;
465 if(g_mbcp_is_multibyte
)
473 return MSVCRT_ERANGE
;
475 is_lead
= (!is_lead
&& _ismbblead(*src
));
480 if (is_lead
) /* if string ends with a lead, remove it */
491 return MSVCRT_ERANGE
;
504 return MSVCRT_ERANGE
;
510 /*********************************************************************
511 * _mbsnbcpy(MSVCRT.@)
513 * Like strncpy this function doesn't enforce the string to be
516 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
518 unsigned char* ret
= dst
;
521 if(g_mbcp_is_multibyte
)
526 is_lead
= (!is_lead
&& _ismbblead(*src
));
531 if (is_lead
) /* if string ends with a lead, remove it */
539 if (!(*dst
++ = *src
++)) break;
542 while (n
--) *dst
++ = 0;
546 /*********************************************************************
549 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
551 if(MSVCRT___mb_cur_max
> 1)
553 unsigned int strc
, cmpc
;
556 return *cmp
? -1 : 0;
559 strc
= _mbsnextc(str
);
560 cmpc
= _mbsnextc(cmp
);
562 return strc
< cmpc
? -1 : 1;
563 str
+=(strc
> 255) ? 2 : 1;
564 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
567 return u_strcmp(str
, cmp
); /* ASCII CP */
570 /*********************************************************************
571 * _mbsicoll(MSVCRT.@)
572 * FIXME: handle locales.
574 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
576 if(MSVCRT___mb_cur_max
> 1)
578 unsigned int strc
, cmpc
;
581 return *cmp
? -1 : 0;
584 strc
= _mbctolower(_mbsnextc(str
));
585 cmpc
= _mbctolower(_mbsnextc(cmp
));
587 return strc
< cmpc
? -1 : 1;
588 str
+=(strc
> 255) ? 2 : 1;
589 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
592 return u_strcasecmp(str
, cmp
); /* ASCII CP */
595 /*********************************************************************
597 * Performs a case-sensitive comparison according to the current code page
599 * _NLSCMPERROR if error
600 * FIXME: handle locales.
602 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
604 if(MSVCRT___mb_cur_max
> 1)
606 unsigned int strc
, cmpc
;
609 return *cmp
? -1 : 0;
612 strc
= _mbsnextc(str
);
613 cmpc
= _mbsnextc(cmp
);
615 return strc
< cmpc
? -1 : 1;
616 str
+=(strc
> 255) ? 2 : 1;
617 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
620 return u_strcmp(str
, cmp
); /* ASCII CP */
624 /*********************************************************************
627 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
629 if(MSVCRT___mb_cur_max
> 1)
631 unsigned int strc
, cmpc
;
634 return *cmp
? -1 : 0;
637 strc
= _mbctolower(_mbsnextc(str
));
638 cmpc
= _mbctolower(_mbsnextc(cmp
));
640 return strc
< cmpc
? -1 : 1;
641 str
+=(strc
> 255) ? 2 : 1;
642 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
645 return u_strcasecmp(str
, cmp
); /* ASCII CP */
648 /*********************************************************************
651 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
656 if(MSVCRT___mb_cur_max
> 1)
658 unsigned int strc
, cmpc
;
663 return *cmp
? -1 : 0;
666 strc
= _mbsnextc(str
);
667 cmpc
= _mbsnextc(cmp
);
669 return strc
< cmpc
? -1 : 1;
670 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
674 return 0; /* Matched len chars */
676 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
679 /*********************************************************************
680 * _mbsnbcmp(MSVCRT.@)
682 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
686 if(MSVCRT___mb_cur_max
> 1)
688 unsigned int strc
, cmpc
;
693 return *cmp
? -1 : 0;
696 if (MSVCRT_isleadbyte(*str
))
698 strc
=(len
>=2)?_mbsnextc(str
):0;
706 if (MSVCRT_isleadbyte(*cmp
))
707 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
711 return strc
< cmpc
? -1 : 1;
716 return 0; /* Matched len chars */
718 return u_strncmp(str
,cmp
,len
);
721 /*********************************************************************
722 * _mbsnicmp(MSVCRT.@)
724 * Compare two multibyte strings case insensitively to 'len' characters.
726 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
728 /* FIXME: No tolower() for mb strings yet */
729 if(MSVCRT___mb_cur_max
> 1)
731 unsigned int strc
, cmpc
;
735 return *cmp
? -1 : 0;
738 strc
= _mbctolower(_mbsnextc(str
));
739 cmpc
= _mbctolower(_mbsnextc(cmp
));
741 return strc
< cmpc
? -1 : 1;
742 str
+=(strc
> 255) ? 2 : 1;
743 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
745 return 0; /* Matched len chars */
747 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
750 /*********************************************************************
751 * _mbsnbicmp(MSVCRT.@)
753 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
757 if(MSVCRT___mb_cur_max
> 1)
759 unsigned int strc
, cmpc
;
764 return *cmp
? -1 : 0;
767 if (MSVCRT_isleadbyte(*str
))
769 strc
=(len
>=2)?_mbsnextc(str
):0;
777 if (MSVCRT_isleadbyte(*cmp
))
778 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
781 strc
= _mbctolower(strc
);
782 cmpc
= _mbctolower(cmpc
);
784 return strc
< cmpc
? -1 : 1;
789 return 0; /* Matched len bytes */
791 return u_strncasecmp(str
,cmp
,len
);
794 /*********************************************************************
797 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
799 strcat( (char *)dst
, (const char *)src
);
803 /*********************************************************************
806 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
808 strcpy( (char *)dst
, (const char *)src
);
812 /*********************************************************************
815 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
817 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
820 /*********************************************************************
823 * Find a multibyte character in a multibyte string.
825 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
827 if(MSVCRT___mb_cur_max
> 1)
834 return (unsigned char*)s
;
837 s
+= c
> 255 ? 2 : 1;
840 return u_strchr(s
, x
); /* ASCII CP */
843 /*********************************************************************
846 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
848 if(MSVCRT___mb_cur_max
> 1)
851 unsigned char* match
=NULL
;
857 match
=(unsigned char*)s
;
860 s
+=(c
> 255) ? 2 : 1;
863 return u_strrchr(s
, x
);
866 /*********************************************************************
869 * Find and extract tokens from strings
871 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
873 thread_data_t
*data
= msvcrt_get_thread_data();
876 if(MSVCRT___mb_cur_max
> 1)
881 if (!(str
= data
->mbstok_next
)) return NULL
;
883 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
884 str
+= c
> 255 ? 2 : 1;
886 if (!*str
) return NULL
;
888 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
889 str
+= c
> 255 ? 2 : 1;
893 if (c
> 255) *str
++ = 0;
895 data
->mbstok_next
= str
;
898 return u_strtok(str
, delim
); /* ASCII CP */
901 /*********************************************************************
904 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
906 /* temp var needed because MultiByteToWideChar wants non NULL destination */
907 MSVCRT_wchar_t tmpdst
= '\0';
911 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, &tmpdst
, 1))
915 /* return the number of bytes from src that have been used */
918 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
923 /*********************************************************************
924 * _mbbtombc(MSVCRT.@)
926 unsigned int CDECL
_mbbtombc(unsigned int c
)
928 if(MSVCRT___mb_cur_max
> 1 &&
929 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
931 /* FIXME: I can't get this function to return anything
932 * different from what I pass it...
935 return c
; /* ASCII CP or no MB char */
938 /*********************************************************************
941 int CDECL
_mbbtype(unsigned char c
, int type
)
945 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
947 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
954 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
956 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
963 /*********************************************************************
964 * _ismbbkana(MSVCRT.@)
966 int CDECL
_ismbbkana(unsigned int c
)
968 /* FIXME: use lc_ctype when supported, not lc_all */
969 if(MSVCRT___lc_codepage
== 932)
971 /* Japanese/Katakana, CP 932 */
972 return (c
>= 0xa1 && c
<= 0xdf);
977 /*********************************************************************
978 * _ismbcdigit(MSVCRT.@)
980 int CDECL
_ismbcdigit(unsigned int ch
)
982 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
983 return (get_char_typeW( wch
) & C1_DIGIT
);
986 /*********************************************************************
987 * _ismbcgraph(MSVCRT.@)
989 int CDECL
_ismbcgraph(unsigned int ch
)
991 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
992 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
995 /*********************************************************************
996 * _ismbcalpha (MSVCRT.@)
998 int CDECL
_ismbcalpha(unsigned int ch
)
1000 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1001 return (get_char_typeW( wch
) & C1_ALPHA
);
1004 /*********************************************************************
1005 * _ismbclower (MSVCRT.@)
1007 int CDECL
_ismbclower(unsigned int ch
)
1009 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1010 return (get_char_typeW( wch
) & C1_UPPER
);
1013 /*********************************************************************
1014 * _ismbcupper (MSVCRT.@)
1016 int CDECL
_ismbcupper(unsigned int ch
)
1018 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1019 return (get_char_typeW( wch
) & C1_LOWER
);
1022 /*********************************************************************
1023 * _ismbcsymbol(MSVCRT.@)
1025 int CDECL
_ismbcsymbol(unsigned int ch
)
1027 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1029 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
1031 WARN("GetStringTypeW failed on %x\n", ch
);
1034 return ((ctype
& C3_SYMBOL
) != 0);
1037 /*********************************************************************
1038 * _ismbcalnum (MSVCRT.@)
1040 int CDECL
_ismbcalnum(unsigned int ch
)
1042 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1043 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
1046 /*********************************************************************
1047 * _ismbcspace (MSVCRT.@)
1049 int CDECL
_ismbcspace(unsigned int ch
)
1051 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1052 return (get_char_typeW( wch
) & C1_SPACE
);
1055 /*********************************************************************
1056 * _ismbcprint (MSVCRT.@)
1058 int CDECL
_ismbcprint(unsigned int ch
)
1060 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1061 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1064 /*********************************************************************
1065 * _ismbcpunct(MSVCRT.@)
1067 int CDECL
_ismbcpunct(unsigned int ch
)
1069 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1070 return (get_char_typeW( wch
) & C1_PUNCT
);
1073 /*********************************************************************
1074 * _ismbchira(MSVCRT.@)
1076 int CDECL
_ismbchira(unsigned int c
)
1078 /* FIXME: use lc_ctype when supported, not lc_all */
1079 if(MSVCRT___lc_codepage
== 932)
1081 /* Japanese/Hiragana, CP 932 */
1082 return (c
>= 0x829f && c
<= 0x82f1);
1087 /*********************************************************************
1088 * _ismbckata(MSVCRT.@)
1090 int CDECL
_ismbckata(unsigned int c
)
1092 /* FIXME: use lc_ctype when supported, not lc_all */
1093 if(MSVCRT___lc_codepage
== 932)
1096 return _ismbbkana(c
);
1097 /* Japanese/Katakana, CP 932 */
1098 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1103 /*********************************************************************
1104 * _ismbblead(MSVCRT.@)
1106 int CDECL
_ismbblead(unsigned int c
)
1108 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1112 /*********************************************************************
1113 * _ismbbtrail(MSVCRT.@)
1115 int CDECL
_ismbbtrail(unsigned int c
)
1117 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1120 /*********************************************************************
1121 * _ismbslead(MSVCRT.@)
1123 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1127 if(!g_mbcp_is_multibyte
)
1130 /* Lead bytes can also be trail bytes so we need to analyse the string
1132 while (start
<= str
)
1136 lead
= !lead
&& _ismbblead(*start
);
1140 return lead
? -1 : 0;
1143 /*********************************************************************
1144 * _ismbstrail(MSVCRT.@)
1146 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1148 /* Note: this function doesn't check _ismbbtrail */
1149 if ((str
> start
) && _ismbslead(start
, str
-1))
1155 /*********************************************************************
1156 * _mbsbtype (MSVCRT.@)
1158 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1161 const unsigned char *end
= str
+ count
;
1162 int mbcp
= g_mbcp_is_multibyte
;
1164 /* Lead bytes can also be trail bytes so we need to analyse the string.
1165 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1167 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1170 return _MBC_ILLEGAL
;
1171 lead
= mbcp
&& !lead
&& _ismbblead(*str
);
1176 if (_ismbbtrail(*str
))
1179 return _MBC_ILLEGAL
;
1181 if (_ismbblead(*str
))
1187 /*********************************************************************
1190 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1192 unsigned char* ret
= str
;
1194 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1195 return u__strset(str
, c
); /* ASCII CP or SB char */
1197 c
&= 0xffff; /* Strip high bits */
1199 while(str
[0] && str
[1])
1205 str
[0] = '\0'; /* FIXME: OK to shorten? */
1210 /*********************************************************************
1211 * _mbsnbset(MSVCRT.@)
1213 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1215 unsigned char *ret
= str
;
1220 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1221 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1223 c
&= 0xffff; /* Strip high bits */
1225 while(str
[0] && str
[1] && (len
> 1))
1233 /* as per msdn pad with a blank character */
1240 /*********************************************************************
1241 * _mbsnset(MSVCRT.@)
1243 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1245 unsigned char *ret
= str
;
1250 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1251 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1253 c
&= 0xffff; /* Strip high bits */
1255 while(str
[0] && str
[1] && len
--)
1261 str
[0] = '\0'; /* FIXME: OK to shorten? */
1266 /*********************************************************************
1267 * _mbsnccnt(MSVCRT.@)
1268 * 'c' is for 'character'.
1270 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1273 if(MSVCRT___mb_cur_max
> 1)
1276 while(*str
&& len
-- > 0)
1278 if(MSVCRT_isleadbyte(*str
))
1291 return min(ret
, len
); /* ASCII CP */
1294 /*********************************************************************
1295 * _mbsnbcnt(MSVCRT.@)
1296 * 'b' is for byte count.
1298 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1301 if(MSVCRT___mb_cur_max
> 1)
1303 const unsigned char* xstr
= str
;
1304 while(*xstr
&& len
-- > 0)
1306 if (MSVCRT_isleadbyte(*xstr
++))
1312 return min(ret
, len
); /* ASCII CP */
1315 /*********************************************************************
1316 * _mbsnbcat(MSVCRT.@)
1318 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1320 if(MSVCRT___mb_cur_max
> 1)
1322 unsigned char *res
= dst
;
1324 if (MSVCRT_isleadbyte(*dst
++)) {
1328 /* as per msdn overwrite the lead byte in front of '\0' */
1334 while (*src
&& len
--) *dst
++ = *src
++;
1338 return u_strncat(dst
, src
, len
); /* ASCII CP */
1342 /*********************************************************************
1343 * _mbsncat(MSVCRT.@)
1345 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1347 if(MSVCRT___mb_cur_max
> 1)
1349 unsigned char *res
= dst
;
1352 if (MSVCRT_isleadbyte(*dst
++))
1355 while (*src
&& len
--)
1358 if(MSVCRT_isleadbyte(*src
++))
1364 return u_strncat(dst
, src
, len
); /* ASCII CP */
1368 /*********************************************************************
1371 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1373 unsigned char *ret
= s
;
1376 if (MSVCRT___mb_cur_max
> 1)
1381 c
= _mbctolower(_mbsnextc(s
));
1382 /* Note that I assume that the size of the character is unchanged */
1391 else for ( ; *s
; s
++) *s
= tolower(*s
);
1396 /*********************************************************************
1399 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1401 unsigned char *ret
= s
;
1404 if (MSVCRT___mb_cur_max
> 1)
1409 c
= _mbctoupper(_mbsnextc(s
));
1410 /* Note that I assume that the size of the character is unchanged */
1419 else for ( ; *s
; s
++) *s
= toupper(*s
);
1424 /*********************************************************************
1425 * _mbsspn (MSVCRT.@)
1427 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1429 const unsigned char *p
, *q
;
1431 for (p
= string
; *p
; p
++)
1433 if (MSVCRT_isleadbyte(*p
))
1435 for (q
= set
; *q
; q
++)
1439 if ((*p
== *q
) && (p
[1] == q
[1]))
1443 if (!q
[0] || !q
[1]) break;
1447 for (q
= set
; *q
; q
++)
1456 /*********************************************************************
1457 * _mbsspnp (MSVCRT.@)
1459 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1461 const unsigned char *p
, *q
;
1463 for (p
= string
; *p
; p
++)
1465 if (MSVCRT_isleadbyte(*p
))
1467 for (q
= set
; *q
; q
++)
1471 if ((*p
== *q
) && (p
[1] == q
[1]))
1475 if (!q
[0] || !q
[1]) break;
1479 for (q
= set
; *q
; q
++)
1487 return (unsigned char *)p
;
1490 /*********************************************************************
1491 * _mbscspn(MSVCRT.@)
1493 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1495 if (MSVCRT___mb_cur_max
> 1)
1496 FIXME("don't handle double character case\n");
1497 return u_strcspn(str
, cmp
);
1500 /*********************************************************************
1501 * _mbsrev (MSVCRT.@)
1503 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1505 int i
, len
= _mbslen(str
);
1506 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1511 /* unpack multibyte string to temp buffer */
1513 for(i
=0; i
<len
; i
++)
1515 if (MSVCRT_isleadbyte(*p
))
1527 /* repack it in the reverse order */
1529 for(i
=len
-1; i
>=0; i
--)
1531 if(MSVCRT_isleadbyte(temp
[i
*2]))
1547 /*********************************************************************
1548 * _mbspbrk (MSVCRT.@)
1550 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1552 const unsigned char* p
;
1556 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
1559 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
1560 return (unsigned char*)str
;
1562 str
+= (MSVCRT_isleadbyte(*str
)?2:1);
1569 * Functions depending on locale codepage
1572 /*********************************************************************
1575 * Unlike most of the multibyte string functions this function uses
1576 * the locale codepage, not the codepage set by _setmbcp
1578 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1580 if (str
&& *str
&& size
)
1582 if(MSVCRT___mb_cur_max
== 1)
1583 return 1; /* ASCII CP */
1585 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1590 /*********************************************************************
1591 * _mbstrlen(MSVCRT.@)
1593 * Unlike most of the multibyte string functions this function uses
1594 * the locale codepage, not the codepage set by _setmbcp
1596 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1598 if(MSVCRT___mb_cur_max
> 1)
1600 MSVCRT_size_t len
= 0;
1603 /* FIXME: According to the documentation we are supposed to test for
1604 * multi-byte character validity. Whatever that means
1606 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1611 return strlen(str
); /* ASCII CP */