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"
30 #include "msvcrt/mbstring.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
34 unsigned char MSVCRT_mbctype
[257];
35 static int g_mbcp_is_multibyte
= 0;
37 int MSVCRT___mb_cur_max
= 1;
38 extern int MSVCRT___lc_collate_cp
;
40 /* It seems that the data about valid trail bytes is not available from kernel32
41 * so we have to store is here. The format is the same as for lead bytes in CPINFO */
42 struct cp_extra_info_t
45 BYTE TrailBytes
[MAX_LEADBYTES
];
48 static struct cp_extra_info_t g_cpextrainfo
[] =
50 {932, {0x40, 0x7e, 0x80, 0xfc, 0, 0}},
51 {936, {0x40, 0xfe, 0, 0}},
52 {949, {0x41, 0xfe, 0, 0}},
53 {950, {0x40, 0x7e, 0xa1, 0xfe, 0, 0}},
54 {20932, {1, 255, 0, 0}}, /* seems to give different results on different systems */
55 {0, {1, 255, 0, 0}} /* match all with FIXME */
58 static MSVCRT_wchar_t
msvcrt_mbc_to_wc(unsigned int ch
)
68 mbch
[0] = (ch
>> 8) & 0xff;
72 if (!MultiByteToWideChar(MSVCRT___lc_codepage
, 0, mbch
, n_chars
, &chW
, 1))
74 WARN("MultiByteToWideChar failed on %x\n", ch
);
80 static inline size_t u_strlen( const unsigned char *str
)
82 return strlen( (const char*) str
);
85 static inline unsigned char* u_strncat( unsigned char* dst
, const unsigned char* src
, size_t len
)
87 return (unsigned char*)strncat( (char*)dst
, (const char*)src
, len
);
90 static inline int u_strcmp( const unsigned char *s1
, const unsigned char *s2
)
92 return strcmp( (const char*)s1
, (const char*)s2
);
95 static inline int u_strcasecmp( const unsigned char *s1
, const unsigned char *s2
)
97 return strcasecmp( (const char*)s1
, (const char*)s2
);
100 static inline int u_strncmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
102 return strncmp( (const char*)s1
, (const char*)s2
, len
);
105 static inline int u_strncasecmp( const unsigned char *s1
, const unsigned char *s2
, size_t len
)
107 return strncasecmp( (const char*)s1
, (const char*)s2
, len
);
110 static inline unsigned char *u_strchr( const unsigned char *s
, unsigned char x
)
112 return (unsigned char*) strchr( (const char*)s
, x
);
115 static inline unsigned char *u_strrchr( const unsigned char *s
, unsigned char x
)
117 return (unsigned char*) strrchr( (const char*)s
, x
);
120 static inline unsigned char *u_strtok( unsigned char *s
, const unsigned char *delim
)
122 return (unsigned char*) strtok( (char*)s
, (const char*)delim
);
125 static inline unsigned char *u__strset( unsigned char *s
, unsigned char c
)
127 return (unsigned char*) _strset( (char*)s
, c
);
130 static inline unsigned char *u__strnset( unsigned char *s
, unsigned char c
, size_t len
)
132 return (unsigned char*) _strnset( (char*)s
, c
, len
);
135 static inline size_t u_strcspn( const unsigned char *s
, const unsigned char *rej
)
137 return strcspn( (const char *)s
, (const char*)rej
);
140 /*********************************************************************
141 * __p__mbctype (MSVCRT.@)
143 unsigned char* CDECL
__p__mbctype(void)
145 return MSVCRT_mbctype
;
148 /*********************************************************************
149 * __p___mb_cur_max(MSVCRT.@)
151 int* CDECL
__p___mb_cur_max(void)
153 return &MSVCRT___mb_cur_max
;
156 /*********************************************************************
157 * _setmbcp (MSVCRT.@)
159 int CDECL
_setmbcp(int cp
)
181 newcp
= MSVCRT___lc_codepage
;
184 newcp
= 20127; /* ASCII */
191 if (!GetCPInfo(newcp
, &cpi
))
193 WARN("Codepage %d not found\n", newcp
);
194 msvcrt_set_errno(MSVCRT_EINVAL
);
198 /* setup the _mbctype */
199 memset(MSVCRT_mbctype
, 0, sizeof(MSVCRT_mbctype
));
201 bytes
= cpi
.LeadByte
;
202 while (bytes
[0] || bytes
[1])
204 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
205 MSVCRT_mbctype
[i
+ 1] |= _M1
;
209 if (cpi
.MaxCharSize
> 1)
211 /* trail bytes not available through kernel32 but stored in a structure in msvcrt */
212 struct cp_extra_info_t
*cpextra
= g_cpextrainfo
;
214 g_mbcp_is_multibyte
= 1;
217 if (cpextra
->cp
== 0 || cpextra
->cp
== newcp
)
219 if (cpextra
->cp
== 0)
220 FIXME("trail bytes data not available for DBCS codepage %d - assuming all bytes\n", newcp
);
222 bytes
= cpextra
->TrailBytes
;
223 while (bytes
[0] || bytes
[1])
225 for (i
= bytes
[0]; i
<= bytes
[1]; i
++)
226 MSVCRT_mbctype
[i
+ 1] |= _M2
;
235 g_mbcp_is_multibyte
= 0;
237 /* we can't use GetStringTypeA directly because we don't have a locale - only a code page
240 for (i
= 0; i
< 256; i
++)
241 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
242 bufA
[charcount
++] = i
;
244 ret
= MultiByteToWideChar(newcp
, 0, bufA
, charcount
, bufW
, charcount
);
245 if (ret
!= charcount
)
246 ERR("MultiByteToWideChar of chars failed for cp %d, ret=%d (exp %d), error=%d\n", newcp
, ret
, charcount
, GetLastError());
248 GetStringTypeW(CT_CTYPE1
, bufW
, charcount
, chartypes
);
250 curr_type
= chartypes
;
251 for (i
= 0; i
< 256; i
++)
252 if (!(MSVCRT_mbctype
[i
+ 1] & _M1
))
254 if ((*curr_type
) & C1_UPPER
)
255 MSVCRT_mbctype
[i
+ 1] |= _SBUP
;
256 if ((*curr_type
) & C1_LOWER
)
257 MSVCRT_mbctype
[i
+ 1] |= _SBLOW
;
261 if (newcp
== 932) /* CP932 only - set _MP and _MS */
263 /* On Windows it's possible to calculate the _MP and _MS from CT_CTYPE1
264 * and CT_CTYPE3. But as of Wine 0.9.43 we return wrong values what makes
265 * it hard. As this is set only for codepage 932 we hardcode it what gives
266 * also faster execution.
268 for (i
= 161; i
<= 165; i
++)
269 MSVCRT_mbctype
[i
+ 1] |= _MP
;
270 for (i
= 166; i
<= 223; i
++)
271 MSVCRT_mbctype
[i
+ 1] |= _MS
;
274 MSVCRT___lc_collate_cp
= MSVCRT___lc_codepage
= newcp
;
275 TRACE("(%d) -> %d\n", cp
, MSVCRT___lc_codepage
);
279 /*********************************************************************
280 * _getmbcp (MSVCRT.@)
282 int CDECL
_getmbcp(void)
284 return MSVCRT___lc_codepage
;
287 /*********************************************************************
288 * _mbsnextc(MSVCRT.@)
290 unsigned int CDECL
_mbsnextc(const unsigned char* str
)
293 return *str
<< 8 | str
[1];
297 /*********************************************************************
298 * _mbctolower(MSVCRT.@)
300 unsigned int CDECL
_mbctolower(unsigned int c
)
302 if (MSVCRT_isleadbyte(c
))
304 FIXME("Handle MBC chars\n");
307 return tolower(c
); /* ASCII CP or SB char */
310 /*********************************************************************
311 * _mbctoupper(MSVCRT.@)
313 unsigned int CDECL
_mbctoupper(unsigned int c
)
315 if (MSVCRT_isleadbyte(c
))
317 FIXME("Handle MBC chars\n");
320 return toupper(c
); /* ASCII CP or SB char */
323 /*********************************************************************
326 unsigned char* CDECL
_mbsdec(const unsigned char* start
, const unsigned char* cur
)
328 if(MSVCRT___mb_cur_max
> 1)
329 return (unsigned char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
331 return (unsigned char *)cur
- 1; /* ASCII CP or SB char */
334 /*********************************************************************
337 unsigned char* CDECL
_mbsinc(const unsigned char* str
)
339 return (unsigned char *)(str
+ _mbclen(str
));
342 /*********************************************************************
345 unsigned char* CDECL
_mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
350 while (num
> 0 && *str
)
352 if (_ismbblead(*str
))
362 return (unsigned char*)str
;
365 /*********************************************************************
368 unsigned int CDECL
_mbclen(const unsigned char* str
)
370 return _ismbblead(*str
) ? 2 : 1;
373 /*********************************************************************
376 MSVCRT_size_t CDECL
_mbslen(const unsigned char* str
)
378 MSVCRT_size_t len
= 0;
381 if (_ismbblead(*str
))
384 if (!*str
) /* count only full chars */
393 /*********************************************************************
396 void CDECL
_mbccpy(unsigned char* dest
, const unsigned char* src
)
400 *++dest
= *++src
; /* MB char */
403 /*********************************************************************
406 * The parameter n is the number or characters to copy, not the size of
407 * the buffer. Use _mbsnbcpy for a function analogical to strncpy
409 unsigned char* CDECL
_mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
411 unsigned char* ret
= dst
;
414 if (g_mbcp_is_multibyte
)
419 if (_ismbblead(*src
))
439 if (!(*dst
++ = *src
++)) break;
442 while (n
--) *dst
++ = 0;
446 /*********************************************************************
447 * _mbsnbcpy(MSVCRT.@)
449 * Like strncpy this function doesn't enforce the string to be
452 unsigned char* CDECL
_mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
454 unsigned char* ret
= dst
;
457 if(g_mbcp_is_multibyte
)
462 is_lead
= (!is_lead
&& _ismbblead(*src
));
467 if (is_lead
) /* if string ends with a lead, remove it */
475 if (!(*dst
++ = *src
++)) break;
478 while (n
--) *dst
++ = 0;
482 /*********************************************************************
485 int CDECL
_mbscmp(const unsigned char* str
, const unsigned char* cmp
)
487 if(MSVCRT___mb_cur_max
> 1)
489 unsigned int strc
, cmpc
;
492 return *cmp
? -1 : 0;
495 strc
= _mbsnextc(str
);
496 cmpc
= _mbsnextc(cmp
);
498 return strc
< cmpc
? -1 : 1;
499 str
+=(strc
> 255) ? 2 : 1;
500 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
503 return u_strcmp(str
, cmp
); /* ASCII CP */
506 /*********************************************************************
507 * _mbsicoll(MSVCRT.@)
508 * FIXME: handle locales.
510 int CDECL
_mbsicoll(const unsigned char* str
, const unsigned char* cmp
)
512 if(MSVCRT___mb_cur_max
> 1)
514 unsigned int strc
, cmpc
;
517 return *cmp
? -1 : 0;
520 strc
= _mbctolower(_mbsnextc(str
));
521 cmpc
= _mbctolower(_mbsnextc(cmp
));
523 return strc
< cmpc
? -1 : 1;
524 str
+=(strc
> 255) ? 2 : 1;
525 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
528 return u_strcasecmp(str
, cmp
); /* ASCII CP */
531 /*********************************************************************
533 * Performs a case-sensitive comparison according to the current code page
535 * _NLSCMPERROR if error
536 * FIXME: handle locales.
538 int CDECL
_mbscoll(const unsigned char* str
, const unsigned char* cmp
)
540 if(MSVCRT___mb_cur_max
> 1)
542 unsigned int strc
, cmpc
;
545 return *cmp
? -1 : 0;
548 strc
= _mbsnextc(str
);
549 cmpc
= _mbsnextc(cmp
);
551 return strc
< cmpc
? -1 : 1;
552 str
+=(strc
> 255) ? 2 : 1;
553 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
556 return u_strcmp(str
, cmp
); /* ASCII CP */
560 /*********************************************************************
563 int CDECL
_mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
565 if(MSVCRT___mb_cur_max
> 1)
567 unsigned int strc
, cmpc
;
570 return *cmp
? -1 : 0;
573 strc
= _mbctolower(_mbsnextc(str
));
574 cmpc
= _mbctolower(_mbsnextc(cmp
));
576 return strc
< cmpc
? -1 : 1;
577 str
+=(strc
> 255) ? 2 : 1;
578 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
581 return u_strcasecmp(str
, cmp
); /* ASCII CP */
584 /*********************************************************************
587 int CDECL
_mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
592 if(MSVCRT___mb_cur_max
> 1)
594 unsigned int strc
, cmpc
;
599 return *cmp
? -1 : 0;
602 strc
= _mbsnextc(str
);
603 cmpc
= _mbsnextc(cmp
);
605 return strc
< cmpc
? -1 : 1;
606 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
610 return 0; /* Matched len chars */
612 return u_strncmp(str
, cmp
, len
); /* ASCII CP */
615 /*********************************************************************
616 * _mbsnbcmp(MSVCRT.@)
618 int CDECL
_mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
622 if(MSVCRT___mb_cur_max
> 1)
624 unsigned int strc
, cmpc
;
629 return *cmp
? -1 : 0;
632 if (MSVCRT_isleadbyte(*str
))
634 strc
=(len
>=2)?_mbsnextc(str
):0;
642 if (MSVCRT_isleadbyte(*cmp
))
643 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
647 return strc
< cmpc
? -1 : 1;
652 return 0; /* Matched len chars */
654 return u_strncmp(str
,cmp
,len
);
657 /*********************************************************************
658 * _mbsnicmp(MSVCRT.@)
660 * Compare two multibyte strings case insensitively to 'len' characters.
662 int CDECL
_mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
664 /* FIXME: No tolower() for mb strings yet */
665 if(MSVCRT___mb_cur_max
> 1)
667 unsigned int strc
, cmpc
;
671 return *cmp
? -1 : 0;
674 strc
= _mbctolower(_mbsnextc(str
));
675 cmpc
= _mbctolower(_mbsnextc(cmp
));
677 return strc
< cmpc
? -1 : 1;
678 str
+=(strc
> 255) ? 2 : 1;
679 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
681 return 0; /* Matched len chars */
683 return u_strncasecmp(str
, cmp
, len
); /* ASCII CP */
686 /*********************************************************************
687 * _mbsnbicmp(MSVCRT.@)
689 int CDECL
_mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
693 if(MSVCRT___mb_cur_max
> 1)
695 unsigned int strc
, cmpc
;
700 return *cmp
? -1 : 0;
703 if (MSVCRT_isleadbyte(*str
))
705 strc
=(len
>=2)?_mbsnextc(str
):0;
713 if (MSVCRT_isleadbyte(*cmp
))
714 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
717 strc
= _mbctolower(strc
);
718 cmpc
= _mbctolower(cmpc
);
720 return strc
< cmpc
? -1 : 1;
725 return 0; /* Matched len bytes */
727 return u_strncasecmp(str
,cmp
,len
);
730 /*********************************************************************
733 unsigned char * CDECL
_mbscat( unsigned char *dst
, const unsigned char *src
)
735 strcat( (char *)dst
, (const char *)src
);
739 /*********************************************************************
742 unsigned char* CDECL
_mbscpy( unsigned char *dst
, const unsigned char *src
)
744 strcpy( (char *)dst
, (const char *)src
);
748 /*********************************************************************
751 unsigned char * CDECL
_mbsstr(const unsigned char *haystack
, const unsigned char *needle
)
753 return (unsigned char *)strstr( (const char *)haystack
, (const char *)needle
);
756 /*********************************************************************
759 * Find a multibyte character in a multibyte string.
761 unsigned char* CDECL
_mbschr(const unsigned char* s
, unsigned int x
)
763 if(MSVCRT___mb_cur_max
> 1)
770 return (unsigned char*)s
;
773 s
+= c
> 255 ? 2 : 1;
776 return u_strchr(s
, x
); /* ASCII CP */
779 /*********************************************************************
782 unsigned char* CDECL
_mbsrchr(const unsigned char* s
, unsigned int x
)
784 if(MSVCRT___mb_cur_max
> 1)
787 unsigned char* match
=NULL
;
793 match
=(unsigned char*)s
;
796 s
+=(c
> 255) ? 2 : 1;
799 return u_strrchr(s
, x
);
802 /*********************************************************************
805 * Find and extract tokens from strings
807 unsigned char* CDECL
_mbstok(unsigned char *str
, const unsigned char *delim
)
809 thread_data_t
*data
= msvcrt_get_thread_data();
812 if(MSVCRT___mb_cur_max
> 1)
817 if (!(str
= data
->mbstok_next
)) return NULL
;
819 while ((c
= _mbsnextc(str
)) && _mbschr(delim
, c
)) {
820 str
+= c
> 255 ? 2 : 1;
822 if (!*str
) return NULL
;
824 while ((c
= _mbsnextc(str
)) && !_mbschr(delim
, c
)) {
825 str
+= c
> 255 ? 2 : 1;
829 if (c
> 255) *str
++ = 0;
831 data
->mbstok_next
= str
;
834 return u_strtok(str
, delim
); /* ASCII CP */
837 /*********************************************************************
840 int CDECL
MSVCRT_mbtowc(MSVCRT_wchar_t
*dst
, const char* str
, MSVCRT_size_t n
)
842 /* temp var needed because MultiByteToWideChar wants non NULL destination */
843 MSVCRT_wchar_t tmpdst
= '\0';
847 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, &tmpdst
, 1))
851 /* return the number of bytes from src that have been used */
854 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
859 /*********************************************************************
860 * _mbbtombc(MSVCRT.@)
862 unsigned int CDECL
_mbbtombc(unsigned int c
)
864 if(MSVCRT___mb_cur_max
> 1 &&
865 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
867 /* FIXME: I can't get this function to return anything
868 * different from what I pass it...
871 return c
; /* ASCII CP or no MB char */
874 /*********************************************************************
877 int CDECL
_mbbtype(unsigned char c
, int type
)
881 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
883 else if ((c
>= 0x40 && c
<= 0x7e) || (c
>= 0x80 && c
<= 0xfc))
890 if ((c
>= 0x20 && c
<= 0x7e) || (c
>= 0xa1 && c
<= 0xdf))
892 else if ((c
>= 0x81 && c
<= 0x9f) || (c
>= 0xe0 && c
<= 0xfc))
899 /*********************************************************************
900 * _ismbbkana(MSVCRT.@)
902 int CDECL
_ismbbkana(unsigned int c
)
904 /* FIXME: use lc_ctype when supported, not lc_all */
905 if(MSVCRT___lc_codepage
== 932)
907 /* Japanese/Katakana, CP 932 */
908 return (c
>= 0xa1 && c
<= 0xdf);
913 /*********************************************************************
914 * _ismbcdigit(MSVCRT.@)
916 int CDECL
_ismbcdigit(unsigned int ch
)
918 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
919 return (get_char_typeW( wch
) & C1_DIGIT
);
922 /*********************************************************************
923 * _ismbcgraph(MSVCRT.@)
925 int CDECL
_ismbcgraph(unsigned int ch
)
927 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
928 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
));
931 /*********************************************************************
932 * _ismbcalpha (MSVCRT.@)
934 int CDECL
_ismbcalpha(unsigned int ch
)
936 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
937 return (get_char_typeW( wch
) & C1_ALPHA
);
940 /*********************************************************************
941 * _ismbclower (MSVCRT.@)
943 int CDECL
_ismbclower(unsigned int ch
)
945 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
946 return (get_char_typeW( wch
) & C1_UPPER
);
949 /*********************************************************************
950 * _ismbcupper (MSVCRT.@)
952 int CDECL
_ismbcupper(unsigned int ch
)
954 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
955 return (get_char_typeW( wch
) & C1_LOWER
);
958 /*********************************************************************
959 * _ismbcsymbol(MSVCRT.@)
961 int CDECL
_ismbcsymbol(unsigned int ch
)
963 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
965 if (!GetStringTypeW(CT_CTYPE3
, &wch
, 1, &ctype
))
967 WARN("GetStringTypeW failed on %x\n", ch
);
970 return ((ctype
& C3_SYMBOL
) != 0);
973 /*********************************************************************
974 * _ismbcalnum (MSVCRT.@)
976 int CDECL
_ismbcalnum(unsigned int ch
)
978 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
979 return (get_char_typeW( wch
) & (C1_ALPHA
| C1_DIGIT
));
982 /*********************************************************************
983 * _ismbcspace (MSVCRT.@)
985 int CDECL
_ismbcspace(unsigned int ch
)
987 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
988 return (get_char_typeW( wch
) & C1_SPACE
);
991 /*********************************************************************
992 * _ismbcprint (MSVCRT.@)
994 int CDECL
_ismbcprint(unsigned int ch
)
996 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
997 return (get_char_typeW( wch
) & (C1_UPPER
| C1_LOWER
| C1_DIGIT
| C1_PUNCT
| C1_ALPHA
| C1_SPACE
));
1000 /*********************************************************************
1001 * _ismbcpunct(MSVCRT.@)
1003 int CDECL
_ismbcpunct(unsigned int ch
)
1005 MSVCRT_wchar_t wch
= msvcrt_mbc_to_wc( ch
);
1006 return (get_char_typeW( wch
) & C1_PUNCT
);
1009 /*********************************************************************
1010 * _ismbchira(MSVCRT.@)
1012 int CDECL
_ismbchira(unsigned int c
)
1014 /* FIXME: use lc_ctype when supported, not lc_all */
1015 if(MSVCRT___lc_codepage
== 932)
1017 /* Japanese/Hiragana, CP 932 */
1018 return (c
>= 0x829f && c
<= 0x82f1);
1023 /*********************************************************************
1024 * _ismbckata(MSVCRT.@)
1026 int CDECL
_ismbckata(unsigned int c
)
1028 /* FIXME: use lc_ctype when supported, not lc_all */
1029 if(MSVCRT___lc_codepage
== 932)
1032 return _ismbbkana(c
);
1033 /* Japanese/Katakana, CP 932 */
1034 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
1039 /*********************************************************************
1040 * _ismbblead(MSVCRT.@)
1042 int CDECL
_ismbblead(unsigned int c
)
1044 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M1
) != 0;
1048 /*********************************************************************
1049 * _ismbbtrail(MSVCRT.@)
1051 int CDECL
_ismbbtrail(unsigned int c
)
1053 return (MSVCRT_mbctype
[(c
&0xff) + 1] & _M2
) != 0;
1056 /*********************************************************************
1057 * _ismbslead(MSVCRT.@)
1059 int CDECL
_ismbslead(const unsigned char* start
, const unsigned char* str
)
1061 /* Lead bytes can also be trail bytes if caller messed up
1062 * iterating through the string...
1064 if(MSVCRT___mb_cur_max
> 1)
1067 start
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1070 return MSVCRT_isleadbyte(*str
);
1072 return 0; /* Must have been a trail, we skipped it */
1075 /*********************************************************************
1076 * _ismbstrail(MSVCRT.@)
1078 int CDECL
_ismbstrail(const unsigned char* start
, const unsigned char* str
)
1080 /* Must not be a lead, and must be preceded by one */
1081 return !_ismbslead(start
, str
) && MSVCRT_isleadbyte(str
[-1]);
1084 /*********************************************************************
1087 unsigned char* CDECL
_mbsset(unsigned char* str
, unsigned int c
)
1089 unsigned char* ret
= str
;
1091 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1092 return u__strset(str
, c
); /* ASCII CP or SB char */
1094 c
&= 0xffff; /* Strip high bits */
1096 while(str
[0] && str
[1])
1102 str
[0] = '\0'; /* FIXME: OK to shorten? */
1107 /*********************************************************************
1108 * _mbsnbset(MSVCRT.@)
1110 unsigned char* CDECL
_mbsnbset(unsigned char *str
, unsigned int c
, MSVCRT_size_t len
)
1112 unsigned char *ret
= str
;
1117 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1118 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1120 c
&= 0xffff; /* Strip high bits */
1122 while(str
[0] && str
[1] && (len
> 1))
1130 /* as per msdn pad with a blank character */
1137 /*********************************************************************
1138 * _mbsnset(MSVCRT.@)
1140 unsigned char* CDECL
_mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
1142 unsigned char *ret
= str
;
1147 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
1148 return u__strnset(str
, c
, len
); /* ASCII CP or SB char */
1150 c
&= 0xffff; /* Strip high bits */
1152 while(str
[0] && str
[1] && len
--)
1158 str
[0] = '\0'; /* FIXME: OK to shorten? */
1163 /*********************************************************************
1164 * _mbsnccnt(MSVCRT.@)
1165 * 'c' is for 'character'.
1167 MSVCRT_size_t CDECL
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
1170 if(MSVCRT___mb_cur_max
> 1)
1173 while(*str
&& len
-- > 0)
1175 if(MSVCRT_isleadbyte(*str
))
1188 return min(ret
, len
); /* ASCII CP */
1191 /*********************************************************************
1192 * _mbsnbcnt(MSVCRT.@)
1193 * 'b' is for byte count.
1195 MSVCRT_size_t CDECL
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
1198 if(MSVCRT___mb_cur_max
> 1)
1200 const unsigned char* xstr
= str
;
1201 while(*xstr
&& len
-- > 0)
1203 if (MSVCRT_isleadbyte(*xstr
++))
1209 return min(ret
, len
); /* ASCII CP */
1212 /*********************************************************************
1213 * _mbsnbcat(MSVCRT.@)
1215 unsigned char* CDECL
_mbsnbcat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1217 if(MSVCRT___mb_cur_max
> 1)
1219 unsigned char *res
= dst
;
1221 if (MSVCRT_isleadbyte(*dst
++)) {
1225 /* as per msdn overwrite the lead byte in front of '\0' */
1231 while (*src
&& len
--) *dst
++ = *src
++;
1235 return u_strncat(dst
, src
, len
); /* ASCII CP */
1239 /*********************************************************************
1240 * _mbsncat(MSVCRT.@)
1242 unsigned char* CDECL
_mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
1244 if(MSVCRT___mb_cur_max
> 1)
1246 unsigned char *res
= dst
;
1249 if (MSVCRT_isleadbyte(*dst
++))
1252 while (*src
&& len
--)
1255 if(MSVCRT_isleadbyte(*src
++))
1261 return u_strncat(dst
, src
, len
); /* ASCII CP */
1265 /*********************************************************************
1268 unsigned char* CDECL
_mbslwr(unsigned char* s
)
1270 unsigned char *ret
= s
;
1273 if (MSVCRT___mb_cur_max
> 1)
1278 c
= _mbctolower(_mbsnextc(s
));
1279 /* Note that I assume that the size of the character is unchanged */
1288 else for ( ; *s
; s
++) *s
= tolower(*s
);
1293 /*********************************************************************
1296 unsigned char* CDECL
_mbsupr(unsigned char* s
)
1298 unsigned char *ret
= s
;
1301 if (MSVCRT___mb_cur_max
> 1)
1306 c
= _mbctoupper(_mbsnextc(s
));
1307 /* Note that I assume that the size of the character is unchanged */
1316 else for ( ; *s
; s
++) *s
= toupper(*s
);
1321 /*********************************************************************
1322 * _mbsspn (MSVCRT.@)
1324 MSVCRT_size_t CDECL
_mbsspn(const unsigned char* string
, const unsigned char* set
)
1326 const unsigned char *p
, *q
;
1328 for (p
= string
; *p
; p
++)
1330 if (MSVCRT_isleadbyte(*p
))
1332 for (q
= set
; *q
; q
++)
1336 if ((*p
== *q
) && (p
[1] == q
[1]))
1340 if (!q
[0] || !q
[1]) break;
1344 for (q
= set
; *q
; q
++)
1353 /*********************************************************************
1354 * _mbsspnp (MSVCRT.@)
1356 unsigned char* CDECL
_mbsspnp(const unsigned char* string
, const unsigned char* set
)
1358 const unsigned char *p
, *q
;
1360 for (p
= string
; *p
; p
++)
1362 if (MSVCRT_isleadbyte(*p
))
1364 for (q
= set
; *q
; q
++)
1368 if ((*p
== *q
) && (p
[1] == q
[1]))
1372 if (!q
[0] || !q
[1]) break;
1376 for (q
= set
; *q
; q
++)
1384 return (unsigned char *)p
;
1387 /*********************************************************************
1388 * _mbscspn(MSVCRT.@)
1390 MSVCRT_size_t CDECL
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
1392 if (MSVCRT___mb_cur_max
> 1)
1393 FIXME("don't handle double character case\n");
1394 return u_strcspn(str
, cmp
);
1397 /*********************************************************************
1398 * _mbsrev (MSVCRT.@)
1400 unsigned char* CDECL
_mbsrev(unsigned char* str
)
1402 int i
, len
= _mbslen(str
);
1403 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
1408 /* unpack multibyte string to temp buffer */
1410 for(i
=0; i
<len
; i
++)
1412 if (MSVCRT_isleadbyte(*p
))
1424 /* repack it in the reverse order */
1426 for(i
=len
-1; i
>=0; i
--)
1428 if(MSVCRT_isleadbyte(temp
[i
*2]))
1444 /*********************************************************************
1445 * _mbspbrk (MSVCRT.@)
1447 unsigned char* CDECL
_mbspbrk(const unsigned char* str
, const unsigned char* accept
)
1449 const unsigned char* p
;
1453 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
1456 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
1457 return (unsigned char*)str
;
1459 str
+= (MSVCRT_isleadbyte(*str
)?2:1);
1466 * Functions depending on locale codepage
1469 /*********************************************************************
1472 * Unlike most of the multibyte string functions this function uses
1473 * the locale codepage, not the codepage set by _setmbcp
1475 int CDECL
MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
1477 if (str
&& *str
&& size
)
1479 if(MSVCRT___mb_cur_max
== 1)
1480 return 1; /* ASCII CP */
1482 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
1487 /*********************************************************************
1488 * _mbstrlen(MSVCRT.@)
1490 * Unlike most of the multibyte string functions this function uses
1491 * the locale codepage, not the codepage set by _setmbcp
1493 MSVCRT_size_t CDECL
_mbstrlen(const char* str
)
1495 if(MSVCRT___mb_cur_max
> 1)
1497 MSVCRT_size_t len
= 0;
1500 /* FIXME: According to the documentation we are supposed to test for
1501 * multi-byte character validity. Whatever that means
1503 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
1508 return strlen(str
); /* ASCII CP */