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 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_set_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(MSVCRT.@)
448 * Like strncpy this function doesn't enforce the string to be
451 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
453 unsigned char* ret
= dst
;
456 if(g_mbcp_is_multibyte
)
461 is_lead
= (!is_lead
&& _ismbblead(*src
));
466 if (is_lead
) /* if string ends with a lead, remove it */
474 if (!(*dst
++ = *src
++)) break;
477 while (n
--) *dst
++ = 0;
481 /*********************************************************************
484 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
486 if(MSVCRT___mb_cur_max
> 1)
488 unsigned int strc
, cmpc
;
491 return *cmp
? -1 : 0;
494 strc
= _mbsnextc(str
);
495 cmpc
= _mbsnextc(cmp
);
497 return strc
< cmpc
? -1 : 1;
498 str
+=(strc
> 255) ? 2 : 1;
499 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
502 return u_strcmp(str
, cmp
); /* ASCII CP */
505 /*********************************************************************
506 * _mbsicoll(MSVCRT.@)
507 * FIXME: handle locales.
509 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
511 if(MSVCRT___mb_cur_max
> 1)
513 unsigned int strc
, cmpc
;
516 return *cmp
? -1 : 0;
519 strc
= _mbctolower(_mbsnextc(str
));
520 cmpc
= _mbctolower(_mbsnextc(cmp
));
522 return strc
< cmpc
? -1 : 1;
523 str
+=(strc
> 255) ? 2 : 1;
524 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
527 return u_strcasecmp(str
, cmp
); /* ASCII CP */
530 /*********************************************************************
532 * Performs a case-sensitive comparison according to the current code page
534 * _NLSCMPERROR if error
535 * FIXME: handle locales.
537 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
539 if(MSVCRT___mb_cur_max
> 1)
541 unsigned int strc
, cmpc
;
544 return *cmp
? -1 : 0;
547 strc
= _mbsnextc(str
);
548 cmpc
= _mbsnextc(cmp
);
550 return strc
< cmpc
? -1 : 1;
551 str
+=(strc
> 255) ? 2 : 1;
552 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
555 return u_strcmp(str
, cmp
); /* ASCII CP */
559 /*********************************************************************
562 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
564 if(MSVCRT___mb_cur_max
> 1)
566 unsigned int strc
, cmpc
;
569 return *cmp
? -1 : 0;
572 strc
= _mbctolower(_mbsnextc(str
));
573 cmpc
= _mbctolower(_mbsnextc(cmp
));
575 return strc
< cmpc
? -1 : 1;
576 str
+=(strc
> 255) ? 2 : 1;
577 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
580 return u_strcasecmp(str
, cmp
); /* ASCII CP */
583 /*********************************************************************
586 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
591 if(MSVCRT___mb_cur_max
> 1)
593 unsigned int strc
, cmpc
;
598 return *cmp
? -1 : 0;
601 strc
= _mbsnextc(str
);
602 cmpc
= _mbsnextc(cmp
);
604 return strc
< cmpc
? -1 : 1;
605 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
609 return 0; /* Matched len chars */
611 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
614 /*********************************************************************
615 * _mbsnbcmp(MSVCRT.@)
617 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
621 if(MSVCRT___mb_cur_max
> 1)
623 unsigned int strc
, cmpc
;
628 return *cmp
? -1 : 0;
631 if (MSVCRT_isleadbyte(*str
))
633 strc
=(len
>=2)?_mbsnextc(str
):0;
641 if (MSVCRT_isleadbyte(*cmp
))
642 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
646 return strc
< cmpc
? -1 : 1;
651 return 0; /* Matched len chars */
653 return u_strncmp(str
,cmp
,len
);
656 /*********************************************************************
657 * _mbsnicmp(MSVCRT.@)
659 * Compare two multibyte strings case insensitively to 'len' characters.
661 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
663 /* FIXME: No tolower() for mb strings yet */
664 if(MSVCRT___mb_cur_max
> 1)
666 unsigned int strc
, cmpc
;
670 return *cmp
? -1 : 0;
673 strc
= _mbctolower(_mbsnextc(str
));
674 cmpc
= _mbctolower(_mbsnextc(cmp
));
676 return strc
< cmpc
? -1 : 1;
677 str
+=(strc
> 255) ? 2 : 1;
678 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
680 return 0; /* Matched len chars */
682 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
685 /*********************************************************************
686 * _mbsnbicmp(MSVCRT.@)
688 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
692 if(MSVCRT___mb_cur_max
> 1)
694 unsigned int strc
, cmpc
;
699 return *cmp
? -1 : 0;
702 if (MSVCRT_isleadbyte(*str
))
704 strc
=(len
>=2)?_mbsnextc(str
):0;
712 if (MSVCRT_isleadbyte(*cmp
))
713 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
716 strc
= _mbctolower(strc
);
717 cmpc
= _mbctolower(cmpc
);
719 return strc
< cmpc
? -1 : 1;
724 return 0; /* Matched len bytes */
726 return u_strncasecmp(str
,cmp
,len
);
729 /*********************************************************************
732 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
734 strcat( (char *)dst
, (const char *)src
);
738 /*********************************************************************
741 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
743 strcpy( (char *)dst
, (const char *)src
);
747 /*********************************************************************
750 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
752 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
755 /*********************************************************************
758 * Find a multibyte character in a multibyte string.
760 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
762 if(MSVCRT___mb_cur_max
> 1)
769 return (unsigned char*)s
;
772 s
+= c
> 255 ? 2 : 1;
775 return u_strchr(s
, x
); /* ASCII CP */
778 /*********************************************************************
781 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
783 if(MSVCRT___mb_cur_max
> 1)
786 unsigned char* match
=NULL
;
792 match
=(unsigned char*)s
;
795 s
+=(c
> 255) ? 2 : 1;
798 return u_strrchr(s
, x
);
801 /*********************************************************************
804 * Find and extract tokens from strings
806 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
808 thread_data_t
*data
= msvcrt_get_thread_data();
811 if(MSVCRT___mb_cur_max
> 1)
816 if (!(str
= data
->mbstok_next
)) return NULL
;
818 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
819 str
+= c
> 255 ? 2 : 1;
821 if (!*str
) return NULL
;
823 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
824 str
+= c
> 255 ? 2 : 1;
828 if (c
> 255) *str
++ = 0;
830 data
->mbstok_next
= str
;
833 return u_strtok(str
, delim
); /* ASCII CP */
836 /*********************************************************************
839 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
841 /* temp var needed because MultiByteToWideChar wants non NULL destination */
842 MSVCRT_wchar_t tmpdst
= '\0';
846 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, &tmpdst
, 1))
850 /* return the number of bytes from src that have been used */
853 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
858 /*********************************************************************
859 * _mbbtombc(MSVCRT.@)
861 unsigned int CDECL
_mbbtombc(unsigned int c
)
863 if(MSVCRT___mb_cur_max
> 1 &&
864 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
866 /* FIXME: I can't get this function to return anything
867 * different from what I pass it...
870 return c
; /* ASCII CP or no MB char */
873 /*********************************************************************
876 int CDECL
_mbbtype(unsigned char c
, int type
)
880 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
882 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
889 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
891 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
898 /*********************************************************************
899 * _ismbbkana(MSVCRT.@)
901 int CDECL
_ismbbkana(unsigned int c
)
903 /* FIXME: use lc_ctype when supported, not lc_all */
904 if(MSVCRT___lc_codepage
== 932)
906 /* Japanese/Katakana, CP 932 */
907 return (c
>= 0xa1 && c
<= 0xdf);
912 /*********************************************************************
913 * _ismbcdigit(MSVCRT.@)
915 int CDECL
_ismbcdigit(unsigned int ch
)
917 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
918 return (get_char_typeW( wch
) & C1_DIGIT
);
921 /*********************************************************************
922 * _ismbcgraph(MSVCRT.@)
924 int CDECL
_ismbcgraph(unsigned int ch
)
926 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
927 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
930 /*********************************************************************
931 * _ismbcalpha (MSVCRT.@)
933 int CDECL
_ismbcalpha(unsigned int ch
)
935 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
936 return (get_char_typeW( wch
) & C1_ALPHA
);
939 /*********************************************************************
940 * _ismbclower (MSVCRT.@)
942 int CDECL
_ismbclower(unsigned int ch
)
944 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
945 return (get_char_typeW( wch
) & C1_UPPER
);
948 /*********************************************************************
949 * _ismbcupper (MSVCRT.@)
951 int CDECL
_ismbcupper(unsigned int ch
)
953 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
954 return (get_char_typeW( wch
) & C1_LOWER
);
957 /*********************************************************************
958 * _ismbcsymbol(MSVCRT.@)
960 int CDECL
_ismbcsymbol(unsigned int ch
)
962 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
964 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
966 WARN("GetStringTypeW failed on %x\n", ch
);
969 return ((ctype
& C3_SYMBOL
) != 0);
972 /*********************************************************************
973 * _ismbcalnum (MSVCRT.@)
975 int CDECL
_ismbcalnum(unsigned int ch
)
977 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
978 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
981 /*********************************************************************
982 * _ismbcspace (MSVCRT.@)
984 int CDECL
_ismbcspace(unsigned int ch
)
986 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
987 return (get_char_typeW( wch
) & C1_SPACE
);
990 /*********************************************************************
991 * _ismbcprint (MSVCRT.@)
993 int CDECL
_ismbcprint(unsigned int ch
)
995 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
996 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
999 /*********************************************************************
1000 * _ismbcpunct(MSVCRT.@)
1002 int CDECL
_ismbcpunct(unsigned int ch
)
1004 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1005 return (get_char_typeW( wch
) & C1_PUNCT
);
1008 /*********************************************************************
1009 * _ismbchira(MSVCRT.@)
1011 int CDECL
_ismbchira(unsigned int c
)
1013 /* FIXME: use lc_ctype when supported, not lc_all */
1014 if(MSVCRT___lc_codepage
== 932)
1016 /* Japanese/Hiragana, CP 932 */
1017 return (c
>= 0x829f && c
<= 0x82f1);
1022 /*********************************************************************
1023 * _ismbckata(MSVCRT.@)
1025 int CDECL
_ismbckata(unsigned int c
)
1027 /* FIXME: use lc_ctype when supported, not lc_all */
1028 if(MSVCRT___lc_codepage
== 932)
1031 return _ismbbkana(c
);
1032 /* Japanese/Katakana, CP 932 */
1033 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1038 /*********************************************************************
1039 * _ismbblead(MSVCRT.@)
1041 int CDECL
_ismbblead(unsigned int c
)
1043 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1047 /*********************************************************************
1048 * _ismbbtrail(MSVCRT.@)
1050 int CDECL
_ismbbtrail(unsigned int c
)
1052 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1055 /*********************************************************************
1056 * _ismbslead(MSVCRT.@)
1058 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1062 if(!g_mbcp_is_multibyte
)
1065 /* Lead bytes can also be trail bytes so we need to analyse the string
1067 while (start
<= str
)
1071 lead
= !lead
&& _ismbblead(*start
);
1075 return lead
? -1 : 0;
1078 /*********************************************************************
1079 * _ismbstrail(MSVCRT.@)
1081 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1083 /* Note: this function doesn't check _ismbbtrail */
1084 if ((str
> start
) && _ismbslead(start
, str
-1))
1090 /*********************************************************************
1091 * _mbsbtype (MSVCRT.@)
1093 int CDECL
_mbsbtype(const unsigned char *str
, MSVCRT_size_t count
)
1096 const unsigned char *end
= str
+ count
;
1097 int mbcp
= g_mbcp_is_multibyte
;
1099 /* Lead bytes can also be trail bytes so we need to analyse the string.
1100 * Also we must return _MBC_ILLEGAL for chars past the end of the string
1102 while (str
< end
) /* Note: we skip the last byte - will check after the loop */
1105 return _MBC_ILLEGAL
;
1106 lead
= mbcp
&& !lead
&& _ismbblead(*str
);
1111 if (_ismbbtrail(*str
))
1114 return _MBC_ILLEGAL
;
1116 if (_ismbblead(*str
))
1122 /*********************************************************************
1125 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1127 unsigned char* ret
= str
;
1129 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1130 return u__strset(str
, c
); /* ASCII CP or SB char */
1132 c
&= 0xffff; /* Strip high bits */
1134 while(str
[0] && str
[1])
1140 str
[0] = '\0'; /* FIXME: OK to shorten? */
1145 /*********************************************************************
1146 * _mbsnbset(MSVCRT.@)
1148 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1150 unsigned char *ret
= str
;
1155 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1156 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1158 c
&= 0xffff; /* Strip high bits */
1160 while(str
[0] && str
[1] && (len
> 1))
1168 /* as per msdn pad with a blank character */
1175 /*********************************************************************
1176 * _mbsnset(MSVCRT.@)
1178 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1180 unsigned char *ret
= str
;
1185 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1186 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1188 c
&= 0xffff; /* Strip high bits */
1190 while(str
[0] && str
[1] && len
--)
1196 str
[0] = '\0'; /* FIXME: OK to shorten? */
1201 /*********************************************************************
1202 * _mbsnccnt(MSVCRT.@)
1203 * 'c' is for 'character'.
1205 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1208 if(MSVCRT___mb_cur_max
> 1)
1211 while(*str
&& len
-- > 0)
1213 if(MSVCRT_isleadbyte(*str
))
1226 return min(ret
, len
); /* ASCII CP */
1229 /*********************************************************************
1230 * _mbsnbcnt(MSVCRT.@)
1231 * 'b' is for byte count.
1233 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1236 if(MSVCRT___mb_cur_max
> 1)
1238 const unsigned char* xstr
= str
;
1239 while(*xstr
&& len
-- > 0)
1241 if (MSVCRT_isleadbyte(*xstr
++))
1247 return min(ret
, len
); /* ASCII CP */
1250 /*********************************************************************
1251 * _mbsnbcat(MSVCRT.@)
1253 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1255 if(MSVCRT___mb_cur_max
> 1)
1257 unsigned char *res
= dst
;
1259 if (MSVCRT_isleadbyte(*dst
++)) {
1263 /* as per msdn overwrite the lead byte in front of '\0' */
1269 while (*src
&& len
--) *dst
++ = *src
++;
1273 return u_strncat(dst
, src
, len
); /* ASCII CP */
1277 /*********************************************************************
1278 * _mbsncat(MSVCRT.@)
1280 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1282 if(MSVCRT___mb_cur_max
> 1)
1284 unsigned char *res
= dst
;
1287 if (MSVCRT_isleadbyte(*dst
++))
1290 while (*src
&& len
--)
1293 if(MSVCRT_isleadbyte(*src
++))
1299 return u_strncat(dst
, src
, len
); /* ASCII CP */
1303 /*********************************************************************
1306 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1308 unsigned char *ret
= s
;
1311 if (MSVCRT___mb_cur_max
> 1)
1316 c
= _mbctolower(_mbsnextc(s
));
1317 /* Note that I assume that the size of the character is unchanged */
1326 else for ( ; *s
; s
++) *s
= tolower(*s
);
1331 /*********************************************************************
1334 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1336 unsigned char *ret
= s
;
1339 if (MSVCRT___mb_cur_max
> 1)
1344 c
= _mbctoupper(_mbsnextc(s
));
1345 /* Note that I assume that the size of the character is unchanged */
1354 else for ( ; *s
; s
++) *s
= toupper(*s
);
1359 /*********************************************************************
1360 * _mbsspn (MSVCRT.@)
1362 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1364 const unsigned char *p
, *q
;
1366 for (p
= string
; *p
; p
++)
1368 if (MSVCRT_isleadbyte(*p
))
1370 for (q
= set
; *q
; q
++)
1374 if ((*p
== *q
) && (p
[1] == q
[1]))
1378 if (!q
[0] || !q
[1]) break;
1382 for (q
= set
; *q
; q
++)
1391 /*********************************************************************
1392 * _mbsspnp (MSVCRT.@)
1394 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1396 const unsigned char *p
, *q
;
1398 for (p
= string
; *p
; p
++)
1400 if (MSVCRT_isleadbyte(*p
))
1402 for (q
= set
; *q
; q
++)
1406 if ((*p
== *q
) && (p
[1] == q
[1]))
1410 if (!q
[0] || !q
[1]) break;
1414 for (q
= set
; *q
; q
++)
1422 return (unsigned char *)p
;
1425 /*********************************************************************
1426 * _mbscspn(MSVCRT.@)
1428 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1430 if (MSVCRT___mb_cur_max
> 1)
1431 FIXME("don't handle double character case\n");
1432 return u_strcspn(str
, cmp
);
1435 /*********************************************************************
1436 * _mbsrev (MSVCRT.@)
1438 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1440 int i
, len
= _mbslen(str
);
1441 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1446 /* unpack multibyte string to temp buffer */
1448 for(i
=0; i
<len
; i
++)
1450 if (MSVCRT_isleadbyte(*p
))
1462 /* repack it in the reverse order */
1464 for(i
=len
-1; i
>=0; i
--)
1466 if(MSVCRT_isleadbyte(temp
[i
*2]))
1482 /*********************************************************************
1483 * _mbspbrk (MSVCRT.@)
1485 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1487 const unsigned char* p
;
1491 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
1494 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
1495 return (unsigned char*)str
;
1497 str
+= (MSVCRT_isleadbyte(*str
)?2:1);
1504 * Functions depending on locale codepage
1507 /*********************************************************************
1510 * Unlike most of the multibyte string functions this function uses
1511 * the locale codepage, not the codepage set by _setmbcp
1513 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1515 if (str
&& *str
&& size
)
1517 if(MSVCRT___mb_cur_max
== 1)
1518 return 1; /* ASCII CP */
1520 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1525 /*********************************************************************
1526 * _mbstrlen(MSVCRT.@)
1528 * Unlike most of the multibyte string functions this function uses
1529 * the locale codepage, not the codepage set by _setmbcp
1531 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1533 if(MSVCRT___mb_cur_max
> 1)
1535 MSVCRT_size_t len
= 0;
1538 /* FIXME: According to the documentation we are supposed to test for
1539 * multi-byte character validity. Whatever that means
1541 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1546 return strlen(str
); /* ASCII CP */