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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
28 #include "msvcrt/mbctype.h"
29 #include "msvcrt/mbstring.h"
30 #include "msvcrt/stdlib.h"
31 #include "msvcrt/string.h"
32 #include "msvcrt/wctype.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
39 unsigned char MSVCRT_mbctype
[257];
40 int MSVCRT___mb_cur_max
= 1;
42 /*********************************************************************
43 * __p__mbctype (MSVCRT.@)
45 unsigned char* __p__mbctype(void)
47 return MSVCRT_mbctype
;
50 /*********************************************************************
51 * __p___mb_cur_max(MSVCRT.@)
53 int* __p___mb_cur_max(void)
55 return &MSVCRT___mb_cur_max
;
58 /*********************************************************************
61 unsigned int _mbsnextc(const unsigned char* str
)
63 if(MSVCRT___mb_cur_max
> 1 && MSVCRT_isleadbyte(*str
))
64 return *str
<< 8 | str
[1];
65 return *str
; /* ASCII CP or SB char */
68 /*********************************************************************
69 * _mbctolower(MSVCRT.@)
71 unsigned int _mbctolower(unsigned int c
)
73 if (MSVCRT_isleadbyte(c
))
75 FIXME("Handle MBC chars\n");
78 return tolower(c
); /* ASCII CP or SB char */
81 /*********************************************************************
82 * _mbctoupper(MSVCRT.@)
84 unsigned int _mbctoupper(unsigned int c
)
86 if (MSVCRT_isleadbyte(c
))
88 FIXME("Handle MBC chars\n");
91 return toupper(c
); /* ASCII CP or SB char */
94 /*********************************************************************
97 unsigned char* _mbsdec(const unsigned char* start
, const unsigned char* cur
)
99 if(MSVCRT___mb_cur_max
> 1)
100 return (char *)(_ismbstrail(start
,cur
-1) ? cur
- 2 : cur
-1);
102 return (char *)cur
- 1; /* ASCII CP or SB char */
105 /*********************************************************************
108 unsigned char* _mbsinc(const unsigned char* str
)
110 if(MSVCRT___mb_cur_max
> 1 && MSVCRT_isleadbyte(*str
))
111 return (unsigned char*)str
+ 2; /* MB char */
113 return (unsigned char*)str
+ 1; /* ASCII CP or SB char */
116 /*********************************************************************
119 unsigned char* _mbsninc(const unsigned char* str
, MSVCRT_size_t num
)
123 if(MSVCRT___mb_cur_max
> 1)
127 return (unsigned char*)str
;
129 return (unsigned char*)str
+ num
; /* ASCII CP */
132 /*********************************************************************
135 unsigned int _mbclen(const unsigned char* str
)
137 return MSVCRT_isleadbyte(*str
) ? 2 : 1;
140 /*********************************************************************
143 int MSVCRT_mblen(const char* str
, MSVCRT_size_t size
)
145 if (str
&& *str
&& size
)
147 if(MSVCRT___mb_cur_max
== 1)
148 return 1; /* ASCII CP */
150 return !MSVCRT_isleadbyte(*str
) ? 1 : (size
>1 ? 2 : -1);
155 /*********************************************************************
158 MSVCRT_size_t
_mbslen(const unsigned char* str
)
160 if(MSVCRT___mb_cur_max
> 1)
162 MSVCRT_size_t len
= 0;
165 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
170 return strlen(str
); /* ASCII CP */
173 /*********************************************************************
174 * _mbstrlen(MSVCRT.@)
176 MSVCRT_size_t
_mbstrlen(const char* str
)
178 if(MSVCRT___mb_cur_max
> 1)
180 MSVCRT_size_t len
= 0;
183 /* FIXME: According to the documentation we are supposed to test for
184 * multi-byte character validity. Whatever that means
186 str
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
191 return strlen(str
); /* ASCII CP */
194 /*********************************************************************
197 void _mbccpy(unsigned char* dest
, const unsigned char* src
)
200 if(MSVCRT___mb_cur_max
> 1 && MSVCRT_isleadbyte(*src
))
201 *dest
= *++src
; /* MB char */
203 ERR("failure.. is this ok?\n");
206 /*********************************************************************
209 unsigned char* _mbsncpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
213 if(MSVCRT___mb_cur_max
> 1)
215 unsigned char* ret
= dst
;
219 if (MSVCRT_isleadbyte(*src
++))
226 return strncpy(dst
, src
, n
); /* ASCII CP */
229 /*********************************************************************
230 * _mbsnbcpy(MSVCRT.@)
232 unsigned char* _mbsnbcpy(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t n
)
236 if(MSVCRT___mb_cur_max
> 1)
238 unsigned char* ret
= dst
;
239 while (*src
&& (n
-- > 1))
242 if (MSVCRT_isleadbyte(*src
++))
248 if (*src
&& n
&& !MSVCRT_isleadbyte(*src
))
250 /* If the last character is a multi-byte character then
251 * we cannot copy it since we have only one byte left
260 return strncpy(dst
, src
, n
); /* ASCII CP */
263 /*********************************************************************
266 int _mbscmp(const unsigned char* str
, const unsigned char* cmp
)
268 if(MSVCRT___mb_cur_max
> 1)
270 unsigned int strc
, cmpc
;
273 return *cmp
? -1 : 0;
276 strc
= _mbsnextc(str
);
277 cmpc
= _mbsnextc(cmp
);
279 return strc
< cmpc
? -1 : 1;
280 str
+=(strc
> 255) ? 2 : 1;
281 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
284 return strcmp(str
, cmp
); /* ASCII CP */
287 /*********************************************************************
290 int _mbsicmp(const unsigned char* str
, const unsigned char* cmp
)
292 if(MSVCRT___mb_cur_max
> 1)
294 unsigned int strc
, cmpc
;
297 return *cmp
? -1 : 0;
300 strc
= _mbctolower(_mbsnextc(str
));
301 cmpc
= _mbctolower(_mbsnextc(cmp
));
303 return strc
< cmpc
? -1 : 1;
304 str
+=(strc
> 255) ? 2 : 1;
305 cmp
+=(strc
> 255) ? 2 : 1; /* equal, use same increment */
308 return strcasecmp(str
, cmp
); /* ASCII CP */
311 /*********************************************************************
314 int _mbsncmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
319 if(MSVCRT___mb_cur_max
> 1)
321 unsigned int strc
, cmpc
;
326 return *cmp
? -1 : 0;
329 strc
= _mbsnextc(str
);
330 cmpc
= _mbsnextc(cmp
);
332 return strc
< cmpc
? -1 : 1;
333 inc
=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
337 return 0; /* Matched len chars */
339 return strncmp(str
, cmp
, len
); /* ASCII CP */
342 /*********************************************************************
343 * _mbsnbcmp(MSVCRT.@)
345 int _mbsnbcmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
349 if(MSVCRT___mb_cur_max
> 1)
351 unsigned int strc
, cmpc
;
356 return *cmp
? -1 : 0;
359 if (MSVCRT_isleadbyte(*str
))
361 strc
=(len
>=2)?_mbsnextc(str
):0;
369 if (MSVCRT_isleadbyte(*cmp
))
370 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
374 return strc
< cmpc
? -1 : 1;
379 return 0; /* Matched len chars */
380 FIXME("%s %s %d\n",str
,cmp
,len
);
382 return strncmp(str
,cmp
,len
);
385 /*********************************************************************
386 * _mbsnicmp(MSVCRT.@)
388 * Compare two multibyte strings case insensitively to 'len' characters.
390 int _mbsnicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
392 /* FIXME: No tolower() for mb strings yet */
393 if(MSVCRT___mb_cur_max
> 1)
395 unsigned int strc
, cmpc
;
399 return *cmp
? -1 : 0;
402 strc
= _mbctolower(_mbsnextc(str
));
403 cmpc
= _mbctolower(_mbsnextc(cmp
));
405 return strc
< cmpc
? -1 : 1;
406 str
+=(strc
> 255) ? 2 : 1;
407 cmp
+=(strc
> 255) ? 2 : 1; /* Equal, use same increment */
409 return 0; /* Matched len chars */
411 return strncasecmp(str
, cmp
, len
); /* ASCII CP */
414 /*********************************************************************
415 * _mbsnbicmp(MSVCRT.@)
417 int _mbsnbicmp(const unsigned char* str
, const unsigned char* cmp
, MSVCRT_size_t len
)
421 if(MSVCRT___mb_cur_max
> 1)
423 unsigned int strc
, cmpc
;
428 return *cmp
? -1 : 0;
431 if (MSVCRT_isleadbyte(*str
))
433 strc
=(len
>=2)?_mbsnextc(str
):0;
441 if (MSVCRT_isleadbyte(*cmp
))
442 cmpc
=(len
>=2)?_mbsnextc(cmp
):0;
445 strc
= _mbctolower(strc
);
446 cmpc
= _mbctolower(cmpc
);
448 return strc
< cmpc
? -1 : 1;
453 return 0; /* Matched len bytes */
454 FIXME("%s %s %d\n",str
,cmp
,len
);
456 return strncmp(str
,cmp
,len
);
459 /*********************************************************************
462 * Find a multibyte character in a multibyte string.
464 unsigned char* _mbschr(const unsigned char* s
, unsigned int x
)
466 if(MSVCRT___mb_cur_max
> 1)
473 return (unsigned char*)s
;
476 s
+= c
> 255 ? 2 : 1;
479 return strchr(s
, x
); /* ASCII CP */
482 /*********************************************************************
485 unsigned char* _mbsrchr(const unsigned char* s
, unsigned int x
)
487 if(MSVCRT___mb_cur_max
> 1)
490 unsigned char* match
=NULL
;
496 match
=(unsigned char*)s
;
499 s
+=(c
> 255) ? 2 : 1;
505 /*********************************************************************
508 int MSVCRT_mbtowc(WCHAR
*dst
, const char* str
, MSVCRT_size_t n
)
512 if(!MultiByteToWideChar(CP_ACP
, 0, str
, n
, dst
, 1))
514 /* return the number of bytes from src that have been used */
517 if(n
>= 2 && MSVCRT_isleadbyte(*str
) && str
[1])
522 /*********************************************************************
523 * _mbbtombc(MSVCRT.@)
525 unsigned int _mbbtombc(unsigned int c
)
527 if(MSVCRT___mb_cur_max
> 1 &&
528 ((c
>= 0x20 && c
<=0x7e) ||(c
>= 0xa1 && c
<= 0xdf)))
530 /* FIXME: I can't get this function to return anything
531 * different to what I pass it...
534 return c
; /* ASCII CP or no MB char */
537 /*********************************************************************
538 * _ismbbkana(MSVCRT.@)
540 int _ismbbkana(unsigned int c
)
542 /* FIXME: use lc_ctype when supported, not lc_all */
543 if(MSVCRT_current_lc_all_cp
== 932)
545 /* Japanese/Katakana, CP 932 */
546 return (c
>= 0xa1 && c
<= 0xdf);
551 /*********************************************************************
552 * _ismbcdigit(MSVCRT.@)
554 int _ismbcdigit(unsigned int ch
)
560 FIXME("Handle MBC chars\n");
565 /*********************************************************************
566 * _ismbcspace (MSVCRT.@)
568 int _ismbcspace(unsigned int c
)
577 /*********************************************************************
578 * _ismbchira(MSVCRT.@)
580 int _ismbchira(unsigned int c
)
582 /* FIXME: use lc_ctype when supported, not lc_all */
583 if(MSVCRT_current_lc_all_cp
== 932)
585 /* Japanese/Hiragana, CP 932 */
586 return (c
>= 0x829f && c
<= 0x82f1);
591 /*********************************************************************
592 * _ismbckata(MSVCRT.@)
594 int _ismbckata(unsigned int c
)
596 /* FIXME: use lc_ctype when supported, not lc_all */
597 if(MSVCRT_current_lc_all_cp
== 932)
600 return _ismbbkana(c
);
601 /* Japanese/Katakana, CP 932 */
602 return (c
>= 0x8340 && c
<= 0x8396 && c
!= 0x837f);
607 /*********************************************************************
608 * _ismbblead(MSVCRT.@)
610 int _ismbblead(unsigned int c
)
612 /* FIXME: should reference MSVCRT_mbctype */
613 return MSVCRT___mb_cur_max
> 1 && MSVCRT_isleadbyte(c
);
617 /*********************************************************************
618 * _ismbbtrail(MSVCRT.@)
620 int _ismbbtrail(unsigned int c
)
622 /* FIXME: should reference MSVCRT_mbctype */
623 return !_ismbblead(c
);
626 /*********************************************************************
627 * _ismbslead(MSVCRT.@)
629 int _ismbslead(const unsigned char* start
, const unsigned char* str
)
631 /* Lead bytes can also be trail bytes if caller messed up
632 * iterating through the string...
634 if(MSVCRT___mb_cur_max
> 1)
637 start
+= MSVCRT_isleadbyte(*str
) ? 2 : 1;
640 return MSVCRT_isleadbyte(*str
);
642 return 0; /* Must have been a trail, we skipped it */
645 /*********************************************************************
646 * _ismbstrail(MSVCRT.@)
648 int _ismbstrail(const unsigned char* start
, const unsigned char* str
)
650 /* Must not be a lead, and must be preceeded by one */
651 return !_ismbslead(start
, str
) && MSVCRT_isleadbyte(str
[-1]);
654 /*********************************************************************
657 unsigned char* _mbsset(unsigned char* str
, unsigned int c
)
659 unsigned char* ret
= str
;
661 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
662 return _strset(str
, c
); /* ASCII CP or SB char */
664 c
&= 0xffff; /* Strip high bits */
666 while(str
[0] && str
[1])
672 str
[0] = '\0'; /* FIXME: OK to shorten? */
677 /*********************************************************************
680 unsigned char* _mbsnset(unsigned char* str
, unsigned int c
, MSVCRT_size_t len
)
682 unsigned char *ret
= str
;
687 if(MSVCRT___mb_cur_max
== 1 || c
< 256)
688 return _strnset(str
, c
, len
); /* ASCII CP or SB char */
690 c
&= 0xffff; /* Strip high bits */
692 while(str
[0] && str
[1] && len
--)
698 str
[0] = '\0'; /* FIXME: OK to shorten? */
703 /*********************************************************************
704 * _mbsnccnt(MSVCRT.@)
705 * 'c' is for 'character'.
707 MSVCRT_size_t
_mbsnccnt(const unsigned char* str
, MSVCRT_size_t len
)
710 if(MSVCRT___mb_cur_max
> 1)
713 while(*str
&& len
-- > 0)
715 if(MSVCRT_isleadbyte(*str
))
728 return min(ret
, len
); /* ASCII CP */
731 /*********************************************************************
732 * _mbsnbcnt(MSVCRT.@)
733 * 'b' is for byte count.
735 MSVCRT_size_t
_mbsnbcnt(const unsigned char* str
, MSVCRT_size_t len
)
738 if(MSVCRT___mb_cur_max
> 1)
740 const unsigned char* xstr
= str
;
741 while(*xstr
&& len
-- > 0)
743 if (MSVCRT_isleadbyte(*xstr
++))
749 return min(ret
, len
); /* ASCII CP */
753 /*********************************************************************
756 unsigned char* _mbsncat(unsigned char* dst
, const unsigned char* src
, MSVCRT_size_t len
)
758 if(MSVCRT___mb_cur_max
> 1)
763 if (MSVCRT_isleadbyte(*dst
++))
766 while (*src
&& len
--)
769 if(MSVCRT_isleadbyte(*src
++))
775 return strncat(dst
, src
, len
); /* ASCII CP */
779 /*********************************************************************
782 unsigned char* _mbslwr(unsigned char* s
)
786 if (MSVCRT___mb_cur_max
> 1)
792 c
= _mbctolower(_mbsnextc(s
));
793 /* Note that I assume that the size of the character is unchanged */
807 /*********************************************************************
810 unsigned char* _mbsupr(unsigned char* s
)
814 if (MSVCRT___mb_cur_max
> 1)
820 c
= _mbctoupper(_mbsnextc(s
));
821 /* Note that I assume that the size of the character is unchanged */
835 /*********************************************************************
838 MSVCRT_size_t
_mbsspn(const unsigned char* string
, const unsigned char* set
)
840 const unsigned char *p
, *q
;
842 for (p
= string
; *p
; p
++)
844 if (MSVCRT_isleadbyte(*p
))
846 for (q
= set
; *q
; q
++)
850 if ((*p
== *q
) && (p
[1] == q
[1]))
858 for (q
= set
; *q
; q
++)
865 /*********************************************************************
868 MSVCRT_size_t
_mbscspn(const unsigned char* str
, const unsigned char* cmp
)
870 if (MSVCRT___mb_cur_max
> 1)
871 FIXME("don't handle double character case\n");
872 return strcspn(str
, cmp
);
875 /*********************************************************************
878 unsigned char* _mbsrev(unsigned char* str
)
880 int i
, len
= _mbslen(str
);
881 unsigned char *p
, *temp
=MSVCRT_malloc(len
*2);
886 /* unpack multibyte string to temp buffer */
890 if (MSVCRT_isleadbyte(*p
))
902 /* repack it in the reverse order */
904 for(i
=len
-1; i
>=0; i
--)
906 if(MSVCRT_isleadbyte(temp
[i
*2]))
922 /*********************************************************************
923 * _mbspbrk (MSVCRT.@)
925 unsigned char* _mbspbrk(const unsigned char* str
, const unsigned char* accept
)
927 const unsigned char* p
;
931 for(p
= accept
; *p
; p
+= (MSVCRT_isleadbyte(*p
)?2:1) )
934 if( !MSVCRT_isleadbyte(*p
) || ( *(p
+1) == *(str
+1) ) )
935 return (unsigned char*)str
;
937 str
+= (MSVCRT_isleadbyte(*str
)?2:1);