wined3d: Add UpdatOverlayZOrder.
[wine.git] / dlls / msvcrt / mbcs.c
blob06c07e49f27001aff123ab0d12d208c45ab62380
1 /*
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
21 * FIXME
22 * Not currently binary compatible with win32. MSVCRT_mbctype must be
23 * populated correctly and the ismb* functions should reference it.
26 #include "msvcrt.h"
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 unsigned char MSVCRT_mbctype[257];
33 int MSVCRT___mb_cur_max = 1;
35 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
37 MSVCRT_wchar_t chW;
38 char mbch[2];
39 int n_chars;
41 if (ch <= 0xff) {
42 mbch[0] = ch;
43 n_chars = 1;
44 } else {
45 mbch[0] = (ch >> 8) & 0xff;
46 mbch[1] = ch & 0xff;
47 n_chars = 2;
49 if (!MultiByteToWideChar(msvcrt_current_lc_all_cp, 0, mbch, n_chars, &chW, 1))
51 WARN("MultiByteToWideChar failed on %x\n", ch);
52 return 0;
54 return chW;
57 static inline size_t u_strlen( const unsigned char *str )
59 return strlen( (const char*) str );
62 static inline unsigned char* u_strncat( unsigned char* dst, const unsigned char* src, size_t len )
64 return (unsigned char*)strncat( (char*)dst, (const char*)src, len);
67 static inline int u_strcmp( const unsigned char *s1, const unsigned char *s2 )
69 return strcmp( (const char*)s1, (const char*)s2 );
72 static inline int u_strcasecmp( const unsigned char *s1, const unsigned char *s2 )
74 return strcasecmp( (const char*)s1, (const char*)s2 );
77 static inline int u_strncmp( const unsigned char *s1, const unsigned char *s2, size_t len )
79 return strncmp( (const char*)s1, (const char*)s2, len );
82 static inline int u_strncasecmp( const unsigned char *s1, const unsigned char *s2, size_t len )
84 return strncasecmp( (const char*)s1, (const char*)s2, len );
87 static inline unsigned char *u_strchr( const unsigned char *s, unsigned char x )
89 return (unsigned char*) strchr( (const char*)s, x );
92 static inline unsigned char *u_strrchr( const unsigned char *s, unsigned char x )
94 return (unsigned char*) strrchr( (const char*)s, x );
97 static inline unsigned char *u_strtok( unsigned char *s, const unsigned char *delim )
99 return (unsigned char*) strtok( (char*)s, (const char*)delim );
102 static inline unsigned char *u__strset( unsigned char *s, unsigned char c )
104 return (unsigned char*) _strset( (char*)s, c);
107 static inline unsigned char *u__strnset( unsigned char *s, unsigned char c, size_t len )
109 return (unsigned char*) _strnset( (char*)s, c, len );
112 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
114 return strcspn( (const char *)s, (const char*)rej );
117 /*********************************************************************
118 * __p__mbctype (MSVCRT.@)
120 unsigned char* CDECL __p__mbctype(void)
122 return MSVCRT_mbctype;
125 /*********************************************************************
126 * __p___mb_cur_max(MSVCRT.@)
128 int* CDECL __p___mb_cur_max(void)
130 return &MSVCRT___mb_cur_max;
133 /*********************************************************************
134 * _mbsnextc(MSVCRT.@)
136 unsigned int CDECL _mbsnextc(const unsigned char* str)
138 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
139 return *str << 8 | str[1];
140 return *str; /* ASCII CP or SB char */
143 /*********************************************************************
144 * _mbctolower(MSVCRT.@)
146 unsigned int CDECL _mbctolower(unsigned int c)
148 if (MSVCRT_isleadbyte(c))
150 FIXME("Handle MBC chars\n");
151 return c;
153 return tolower(c); /* ASCII CP or SB char */
156 /*********************************************************************
157 * _mbctoupper(MSVCRT.@)
159 unsigned int CDECL _mbctoupper(unsigned int c)
161 if (MSVCRT_isleadbyte(c))
163 FIXME("Handle MBC chars\n");
164 return c;
166 return toupper(c); /* ASCII CP or SB char */
169 /*********************************************************************
170 * _mbsdec(MSVCRT.@)
172 unsigned char* CDECL _mbsdec(const unsigned char* start, const unsigned char* cur)
174 if(MSVCRT___mb_cur_max > 1)
175 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
177 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
180 /*********************************************************************
181 * _mbsinc(MSVCRT.@)
183 unsigned char* CDECL _mbsinc(const unsigned char* str)
185 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
186 return (unsigned char*)str + 2; /* MB char */
188 return (unsigned char*)str + 1; /* ASCII CP or SB char */
191 /*********************************************************************
192 * _mbsninc(MSVCRT.@)
194 unsigned char* CDECL _mbsninc(const unsigned char* str, MSVCRT_size_t num)
196 if(!str || num < 1)
197 return NULL;
198 if(MSVCRT___mb_cur_max > 1)
200 while(num--)
201 str = _mbsinc(str);
202 return (unsigned char*)str;
204 return (unsigned char*)str + num; /* ASCII CP */
207 /*********************************************************************
208 * _mbclen(MSVCRT.@)
210 unsigned int CDECL _mbclen(const unsigned char* str)
212 return MSVCRT_isleadbyte(*str) ? 2 : 1;
215 /*********************************************************************
216 * mblen(MSVCRT.@)
218 int CDECL MSVCRT_mblen(const char* str, MSVCRT_size_t size)
220 if (str && *str && size)
222 if(MSVCRT___mb_cur_max == 1)
223 return 1; /* ASCII CP */
225 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
227 return 0;
230 /*********************************************************************
231 * _mbslen(MSVCRT.@)
233 MSVCRT_size_t CDECL _mbslen(const unsigned char* str)
235 if(MSVCRT___mb_cur_max > 1)
237 MSVCRT_size_t len = 0;
238 while(*str)
240 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
241 len++;
243 return len;
245 return u_strlen(str); /* ASCII CP */
248 /*********************************************************************
249 * _mbstrlen(MSVCRT.@)
251 MSVCRT_size_t CDECL _mbstrlen(const char* str)
253 if(MSVCRT___mb_cur_max > 1)
255 MSVCRT_size_t len = 0;
256 while(*str)
258 /* FIXME: According to the documentation we are supposed to test for
259 * multi-byte character validity. Whatever that means
261 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
262 len++;
264 return len;
266 return strlen(str); /* ASCII CP */
269 /*********************************************************************
270 * _mbccpy(MSVCRT.@)
272 void CDECL _mbccpy(unsigned char* dest, const unsigned char* src)
274 *dest = *src;
275 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
276 *++dest = *++src; /* MB char */
279 /*********************************************************************
280 * _mbsncpy(MSVCRT.@)
282 unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
284 unsigned char* ret = dst;
285 if(!n)
286 return dst;
287 if(MSVCRT___mb_cur_max > 1)
289 while (*src && n)
291 n--;
292 *dst++ = *src;
293 if (MSVCRT_isleadbyte(*src++))
294 *dst++ = *src++;
297 else
299 while (n)
301 n--;
302 if (!(*dst++ = *src++)) break;
305 while (n--) *dst++ = 0;
306 return ret;
309 /*********************************************************************
310 * _mbsnbcpy(MSVCRT.@)
312 unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
314 unsigned char* ret = dst;
315 if(!n)
316 return dst;
317 if(MSVCRT___mb_cur_max > 1)
319 while (*src && (n > 1))
321 n--;
322 *dst++ = *src;
323 if (MSVCRT_isleadbyte(*src++))
325 *dst++ = *src++;
326 n--;
329 if (*src && n && !MSVCRT_isleadbyte(*src))
331 /* If the last character is a multi-byte character then
332 * we cannot copy it since we have only one byte left
334 *dst++ = *src;
335 n--;
338 else
340 while (n)
342 n--;
343 if (!(*dst++ = *src++)) break;
346 while (n--) *dst++ = 0;
347 return ret;
350 /*********************************************************************
351 * _mbscmp(MSVCRT.@)
353 int CDECL _mbscmp(const unsigned char* str, const unsigned char* cmp)
355 if(MSVCRT___mb_cur_max > 1)
357 unsigned int strc, cmpc;
358 do {
359 if(!*str)
360 return *cmp ? -1 : 0;
361 if(!*cmp)
362 return 1;
363 strc = _mbsnextc(str);
364 cmpc = _mbsnextc(cmp);
365 if(strc != cmpc)
366 return strc < cmpc ? -1 : 1;
367 str +=(strc > 255) ? 2 : 1;
368 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
369 } while(1);
371 return u_strcmp(str, cmp); /* ASCII CP */
374 /*********************************************************************
375 * _mbsicoll(MSVCRT.@)
376 * FIXME: handle locales.
378 int CDECL _mbsicoll(const unsigned char* str, const unsigned char* cmp)
380 if(MSVCRT___mb_cur_max > 1)
382 unsigned int strc, cmpc;
383 do {
384 if(!*str)
385 return *cmp ? -1 : 0;
386 if(!*cmp)
387 return 1;
388 strc = _mbctolower(_mbsnextc(str));
389 cmpc = _mbctolower(_mbsnextc(cmp));
390 if(strc != cmpc)
391 return strc < cmpc ? -1 : 1;
392 str +=(strc > 255) ? 2 : 1;
393 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
394 } while(1);
396 return u_strcasecmp(str, cmp); /* ASCII CP */
399 /*********************************************************************
400 * _mbscoll(MSVCRT.@)
401 * Performs a case-sensitive comparison according to the current code page
402 * RETURN
403 * _NLSCMPERROR if error
404 * FIXME: handle locales.
406 int CDECL _mbscoll(const unsigned char* str, const unsigned char* cmp)
408 if(MSVCRT___mb_cur_max > 1)
410 unsigned int strc, cmpc;
411 do {
412 if(!*str)
413 return *cmp ? -1 : 0;
414 if(!*cmp)
415 return 1;
416 strc = _mbsnextc(str);
417 cmpc = _mbsnextc(cmp);
418 if(strc != cmpc)
419 return strc < cmpc ? -1 : 1;
420 str +=(strc > 255) ? 2 : 1;
421 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
422 } while(1);
424 return u_strcmp(str, cmp); /* ASCII CP */
428 /*********************************************************************
429 * _mbsicmp(MSVCRT.@)
431 int CDECL _mbsicmp(const unsigned char* str, const unsigned char* cmp)
433 if(MSVCRT___mb_cur_max > 1)
435 unsigned int strc, cmpc;
436 do {
437 if(!*str)
438 return *cmp ? -1 : 0;
439 if(!*cmp)
440 return 1;
441 strc = _mbctolower(_mbsnextc(str));
442 cmpc = _mbctolower(_mbsnextc(cmp));
443 if(strc != cmpc)
444 return strc < cmpc ? -1 : 1;
445 str +=(strc > 255) ? 2 : 1;
446 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
447 } while(1);
449 return u_strcasecmp(str, cmp); /* ASCII CP */
452 /*********************************************************************
453 * _mbsncmp(MSVCRT.@)
455 int CDECL _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
457 if(!len)
458 return 0;
460 if(MSVCRT___mb_cur_max > 1)
462 unsigned int strc, cmpc;
463 while(len--)
465 int inc;
466 if(!*str)
467 return *cmp ? -1 : 0;
468 if(!*cmp)
469 return 1;
470 strc = _mbsnextc(str);
471 cmpc = _mbsnextc(cmp);
472 if(strc != cmpc)
473 return strc < cmpc ? -1 : 1;
474 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
475 str += inc;
476 cmp += inc;
478 return 0; /* Matched len chars */
480 return u_strncmp(str, cmp, len); /* ASCII CP */
483 /*********************************************************************
484 * _mbsnbcmp(MSVCRT.@)
486 int CDECL _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
488 if (!len)
489 return 0;
490 if(MSVCRT___mb_cur_max > 1)
492 unsigned int strc, cmpc;
493 while (len)
495 int clen;
496 if(!*str)
497 return *cmp ? -1 : 0;
498 if(!*cmp)
499 return 1;
500 if (MSVCRT_isleadbyte(*str))
502 strc=(len>=2)?_mbsnextc(str):0;
503 clen=2;
505 else
507 strc=*str;
508 clen=1;
510 if (MSVCRT_isleadbyte(*cmp))
511 cmpc=(len>=2)?_mbsnextc(cmp):0;
512 else
513 cmpc=*str;
514 if(strc != cmpc)
515 return strc < cmpc ? -1 : 1;
516 len -= clen;
517 str += clen;
518 cmp += clen;
520 return 0; /* Matched len chars */
522 return u_strncmp(str,cmp,len);
525 /*********************************************************************
526 * _mbsnicmp(MSVCRT.@)
528 * Compare two multibyte strings case insensitively to 'len' characters.
530 int CDECL _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
532 /* FIXME: No tolower() for mb strings yet */
533 if(MSVCRT___mb_cur_max > 1)
535 unsigned int strc, cmpc;
536 while(len--)
538 if(!*str)
539 return *cmp ? -1 : 0;
540 if(!*cmp)
541 return 1;
542 strc = _mbctolower(_mbsnextc(str));
543 cmpc = _mbctolower(_mbsnextc(cmp));
544 if(strc != cmpc)
545 return strc < cmpc ? -1 : 1;
546 str +=(strc > 255) ? 2 : 1;
547 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
549 return 0; /* Matched len chars */
551 return u_strncasecmp(str, cmp, len); /* ASCII CP */
554 /*********************************************************************
555 * _mbsnbicmp(MSVCRT.@)
557 int CDECL _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
559 if (!len)
560 return 0;
561 if(MSVCRT___mb_cur_max > 1)
563 unsigned int strc, cmpc;
564 while (len)
566 int clen;
567 if(!*str)
568 return *cmp ? -1 : 0;
569 if(!*cmp)
570 return 1;
571 if (MSVCRT_isleadbyte(*str))
573 strc=(len>=2)?_mbsnextc(str):0;
574 clen=2;
576 else
578 strc=*str;
579 clen=1;
581 if (MSVCRT_isleadbyte(*cmp))
582 cmpc=(len>=2)?_mbsnextc(cmp):0;
583 else
584 cmpc=*str;
585 strc = _mbctolower(strc);
586 cmpc = _mbctolower(cmpc);
587 if(strc != cmpc)
588 return strc < cmpc ? -1 : 1;
589 len -= clen;
590 str += clen;
591 cmp += clen;
593 return 0; /* Matched len bytes */
595 return u_strncmp(str,cmp,len);
598 /*********************************************************************
599 * _mbscat (MSVCRT.@)
601 unsigned char * CDECL _mbscat( unsigned char *dst, const unsigned char *src )
603 strcat( (char *)dst, (const char *)src );
604 return dst;
607 /*********************************************************************
608 * _mbscpy (MSVCRT.@)
610 unsigned char* CDECL _mbscpy( unsigned char *dst, const unsigned char *src )
612 strcpy( (char *)dst, (const char *)src );
613 return dst;
616 /*********************************************************************
617 * _mbsstr (MSVCRT.@)
619 unsigned char * CDECL _mbsstr(const unsigned char *haystack, const unsigned char *needle)
621 return (unsigned char *)strstr( (const char *)haystack, (const char *)needle );
624 /*********************************************************************
625 * _mbschr(MSVCRT.@)
627 * Find a multibyte character in a multibyte string.
629 unsigned char* CDECL _mbschr(const unsigned char* s, unsigned int x)
631 if(MSVCRT___mb_cur_max > 1)
633 unsigned int c;
634 while (1)
636 c = _mbsnextc(s);
637 if (c == x)
638 return (unsigned char*)s;
639 if (!c)
640 return NULL;
641 s += c > 255 ? 2 : 1;
644 return u_strchr(s, x); /* ASCII CP */
647 /*********************************************************************
648 * _mbsrchr(MSVCRT.@)
650 unsigned char* CDECL _mbsrchr(const unsigned char* s, unsigned int x)
652 if(MSVCRT___mb_cur_max > 1)
654 unsigned int c;
655 unsigned char* match=NULL;
656 if(!s)
657 return NULL;
658 while (1) {
659 c = _mbsnextc(s);
660 if (c == x)
661 match=(unsigned char*)s;
662 if (!c)
663 return match;
664 s +=(c > 255) ? 2 : 1;
667 return u_strrchr(s, x);
670 /*********************************************************************
671 * _mbstok(MSVCRT.@)
673 * Find and extract tokens from strings
675 unsigned char* CDECL _mbstok(unsigned char *str, const unsigned char *delim)
677 thread_data_t *data = msvcrt_get_thread_data();
678 unsigned char *ret;
680 if(MSVCRT___mb_cur_max > 1)
682 unsigned int c;
684 if (!str)
685 if (!(str = data->mbstok_next)) return NULL;
687 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
688 str += c > 255 ? 2 : 1;
690 if (!*str) return NULL;
691 ret = str++;
692 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
693 str += c > 255 ? 2 : 1;
695 if (*str) {
696 *str++ = 0;
697 if (c > 255) *str++ = 0;
699 data->mbstok_next = str;
700 return ret;
702 return u_strtok(str, delim); /* ASCII CP */
705 /*********************************************************************
706 * mbtowc(MSVCRT.@)
708 int CDECL MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
710 /* temp var needed because MultiByteToWideChar wants non NULL destination */
711 MSVCRT_wchar_t tmpdst = '\0';
713 if(n <= 0 || !str)
714 return 0;
715 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
716 return -1;
717 if(dst)
718 *dst = tmpdst;
719 /* return the number of bytes from src that have been used */
720 if(!*str)
721 return 0;
722 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
723 return 2;
724 return 1;
727 /*********************************************************************
728 * _mbbtombc(MSVCRT.@)
730 unsigned int CDECL _mbbtombc(unsigned int c)
732 if(MSVCRT___mb_cur_max > 1 &&
733 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
735 /* FIXME: I can't get this function to return anything
736 * different from what I pass it...
739 return c; /* ASCII CP or no MB char */
742 /*********************************************************************
743 * _ismbbkana(MSVCRT.@)
745 int CDECL _ismbbkana(unsigned int c)
747 /* FIXME: use lc_ctype when supported, not lc_all */
748 if(msvcrt_current_lc_all_cp == 932)
750 /* Japanese/Katakana, CP 932 */
751 return (c >= 0xa1 && c <= 0xdf);
753 return 0;
756 /*********************************************************************
757 * _ismbcdigit(MSVCRT.@)
759 int CDECL _ismbcdigit(unsigned int ch)
761 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
762 return (get_char_typeW( wch ) & C1_DIGIT);
765 /*********************************************************************
766 * _ismbcgraph(MSVCRT.@)
768 int CDECL _ismbcgraph(unsigned int ch)
770 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
771 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
774 /*********************************************************************
775 * _ismbcalpha (MSVCRT.@)
777 int CDECL _ismbcalpha(unsigned int ch)
779 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
780 return (get_char_typeW( wch ) & C1_ALPHA);
783 /*********************************************************************
784 * _ismbclower (MSVCRT.@)
786 int CDECL _ismbclower(unsigned int ch)
788 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
789 return (get_char_typeW( wch ) & C1_UPPER);
792 /*********************************************************************
793 * _ismbcupper (MSVCRT.@)
795 int CDECL _ismbcupper(unsigned int ch)
797 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
798 return (get_char_typeW( wch ) & C1_LOWER);
801 /*********************************************************************
802 * _ismbcsymbol(MSVCRT.@)
804 int CDECL _ismbcsymbol(unsigned int ch)
806 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
807 WORD ctype;
808 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
810 WARN("GetStringTypeW failed on %x\n", ch);
811 return 0;
813 return ((ctype & C3_SYMBOL) != 0);
816 /*********************************************************************
817 * _ismbcalnum (MSVCRT.@)
819 int CDECL _ismbcalnum(unsigned int ch)
821 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
822 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
825 /*********************************************************************
826 * _ismbcspace (MSVCRT.@)
828 int CDECL _ismbcspace(unsigned int ch)
830 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
831 return (get_char_typeW( wch ) & C1_SPACE);
834 /*********************************************************************
835 * _ismbcprint (MSVCRT.@)
837 int CDECL _ismbcprint(unsigned int ch)
839 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
840 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
843 /*********************************************************************
844 * _ismbcpunct(MSVCRT.@)
846 int CDECL _ismbcpunct(unsigned int ch)
848 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
849 return (get_char_typeW( wch ) & C1_PUNCT);
852 /*********************************************************************
853 * _ismbchira(MSVCRT.@)
855 int CDECL _ismbchira(unsigned int c)
857 /* FIXME: use lc_ctype when supported, not lc_all */
858 if(msvcrt_current_lc_all_cp == 932)
860 /* Japanese/Hiragana, CP 932 */
861 return (c >= 0x829f && c <= 0x82f1);
863 return 0;
866 /*********************************************************************
867 * _ismbckata(MSVCRT.@)
869 int CDECL _ismbckata(unsigned int c)
871 /* FIXME: use lc_ctype when supported, not lc_all */
872 if(msvcrt_current_lc_all_cp == 932)
874 if(c < 256)
875 return _ismbbkana(c);
876 /* Japanese/Katakana, CP 932 */
877 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
879 return 0;
882 /*********************************************************************
883 * _ismbblead(MSVCRT.@)
885 int CDECL _ismbblead(unsigned int c)
887 /* FIXME: should reference MSVCRT_mbctype */
888 return MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(c);
892 /*********************************************************************
893 * _ismbbtrail(MSVCRT.@)
895 int CDECL _ismbbtrail(unsigned int c)
897 /* FIXME: should reference MSVCRT_mbctype */
898 return !_ismbblead(c);
901 /*********************************************************************
902 * _ismbslead(MSVCRT.@)
904 int CDECL _ismbslead(const unsigned char* start, const unsigned char* str)
906 /* Lead bytes can also be trail bytes if caller messed up
907 * iterating through the string...
909 if(MSVCRT___mb_cur_max > 1)
911 while(start < str)
912 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
914 if(start == str)
915 return MSVCRT_isleadbyte(*str);
917 return 0; /* Must have been a trail, we skipped it */
920 /*********************************************************************
921 * _ismbstrail(MSVCRT.@)
923 int CDECL _ismbstrail(const unsigned char* start, const unsigned char* str)
925 /* Must not be a lead, and must be preceded by one */
926 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
929 /*********************************************************************
930 * _mbsset(MSVCRT.@)
932 unsigned char* CDECL _mbsset(unsigned char* str, unsigned int c)
934 unsigned char* ret = str;
936 if(MSVCRT___mb_cur_max == 1 || c < 256)
937 return u__strset(str, c); /* ASCII CP or SB char */
939 c &= 0xffff; /* Strip high bits */
941 while(str[0] && str[1])
943 *str++ = c >> 8;
944 *str++ = c & 0xff;
946 if(str[0])
947 str[0] = '\0'; /* FIXME: OK to shorten? */
949 return ret;
952 /*********************************************************************
953 * _mbsnbset(MSVCRT.@)
955 unsigned char* CDECL _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
957 unsigned char *ret = str;
959 if(!len)
960 return ret;
962 if(MSVCRT___mb_cur_max == 1 || c < 256)
963 return u__strnset(str, c, len); /* ASCII CP or SB char */
965 c &= 0xffff; /* Strip high bits */
967 while(str[0] && str[1] && (len > 1))
969 *str++ = c >> 8;
970 len--;
971 *str++ = c & 0xff;
972 len--;
974 if(len && str[0]) {
975 /* as per msdn pad with a blank character */
976 str[0] = ' ';
979 return ret;
982 /*********************************************************************
983 * _mbsnset(MSVCRT.@)
985 unsigned char* CDECL _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
987 unsigned char *ret = str;
989 if(!len)
990 return ret;
992 if(MSVCRT___mb_cur_max == 1 || c < 256)
993 return u__strnset(str, c, len); /* ASCII CP or SB char */
995 c &= 0xffff; /* Strip high bits */
997 while(str[0] && str[1] && len--)
999 *str++ = c >> 8;
1000 *str++ = c & 0xff;
1002 if(len && str[0])
1003 str[0] = '\0'; /* FIXME: OK to shorten? */
1005 return ret;
1008 /*********************************************************************
1009 * _mbsnccnt(MSVCRT.@)
1010 * 'c' is for 'character'.
1012 MSVCRT_size_t CDECL _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1014 MSVCRT_size_t ret;
1015 if(MSVCRT___mb_cur_max > 1)
1017 ret=0;
1018 while(*str && len-- > 0)
1020 if(MSVCRT_isleadbyte(*str))
1022 if (!len)
1023 break;
1024 len--;
1025 str++;
1027 str++;
1028 ret++;
1030 return ret;
1032 ret=u_strlen(str);
1033 return min(ret, len); /* ASCII CP */
1036 /*********************************************************************
1037 * _mbsnbcnt(MSVCRT.@)
1038 * 'b' is for byte count.
1040 MSVCRT_size_t CDECL _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1042 MSVCRT_size_t ret;
1043 if(MSVCRT___mb_cur_max > 1)
1045 const unsigned char* xstr = str;
1046 while(*xstr && len-- > 0)
1048 if (MSVCRT_isleadbyte(*xstr++))
1049 xstr++;
1051 return xstr-str;
1053 ret=u_strlen(str);
1054 return min(ret, len); /* ASCII CP */
1057 /*********************************************************************
1058 * _mbsnbcat(MSVCRT.@)
1060 unsigned char* CDECL _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1062 if(MSVCRT___mb_cur_max > 1)
1064 unsigned char *res = dst;
1065 while (*dst) {
1066 if (MSVCRT_isleadbyte(*dst++)) {
1067 if (*dst) {
1068 dst++;
1069 } else {
1070 /* as per msdn overwrite the lead byte in front of '\0' */
1071 dst--;
1072 break;
1076 while (*src && len--) *dst++ = *src++;
1077 *dst = '\0';
1078 return res;
1080 return u_strncat(dst, src, len); /* ASCII CP */
1084 /*********************************************************************
1085 * _mbsncat(MSVCRT.@)
1087 unsigned char* CDECL _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1089 if(MSVCRT___mb_cur_max > 1)
1091 unsigned char *res = dst;
1092 while (*dst)
1094 if (MSVCRT_isleadbyte(*dst++))
1095 dst++;
1097 while (*src && len--)
1099 *dst++ = *src;
1100 if(MSVCRT_isleadbyte(*src++))
1101 *dst++ = *src++;
1103 *dst = '\0';
1104 return res;
1106 return u_strncat(dst, src, len); /* ASCII CP */
1110 /*********************************************************************
1111 * _mbslwr(MSVCRT.@)
1113 unsigned char* CDECL _mbslwr(unsigned char* s)
1115 unsigned char *ret = s;
1116 if (!s)
1117 return NULL;
1118 if (MSVCRT___mb_cur_max > 1)
1120 unsigned int c;
1121 while (*s)
1123 c = _mbctolower(_mbsnextc(s));
1124 /* Note that I assume that the size of the character is unchanged */
1125 if (c > 255)
1127 *s++=(c>>8);
1128 c=c & 0xff;
1130 *s++=c;
1133 else for ( ; *s; s++) *s = tolower(*s);
1134 return ret;
1138 /*********************************************************************
1139 * _mbsupr(MSVCRT.@)
1141 unsigned char* CDECL _mbsupr(unsigned char* s)
1143 unsigned char *ret = s;
1144 if (!s)
1145 return NULL;
1146 if (MSVCRT___mb_cur_max > 1)
1148 unsigned int c;
1149 while (*s)
1151 c = _mbctoupper(_mbsnextc(s));
1152 /* Note that I assume that the size of the character is unchanged */
1153 if (c > 255)
1155 *s++=(c>>8);
1156 c=c & 0xff;
1158 *s++=c;
1161 else for ( ; *s; s++) *s = toupper(*s);
1162 return ret;
1166 /*********************************************************************
1167 * _mbsspn (MSVCRT.@)
1169 MSVCRT_size_t CDECL _mbsspn(const unsigned char* string, const unsigned char* set)
1171 const unsigned char *p, *q;
1173 for (p = string; *p; p++)
1175 if (MSVCRT_isleadbyte(*p))
1177 for (q = set; *q; q++)
1179 if (!q[1])
1180 break;
1181 if ((*p == *q) && (p[1] == q[1]))
1182 break;
1183 q++;
1185 if (!q[0] || !q[1]) break;
1187 else
1189 for (q = set; *q; q++)
1190 if (*p == *q)
1191 break;
1192 if (!*q) break;
1195 return p - string;
1198 /*********************************************************************
1199 * _mbscspn(MSVCRT.@)
1201 MSVCRT_size_t CDECL _mbscspn(const unsigned char* str, const unsigned char* cmp)
1203 if (MSVCRT___mb_cur_max > 1)
1204 FIXME("don't handle double character case\n");
1205 return u_strcspn(str, cmp);
1208 /*********************************************************************
1209 * _mbsrev (MSVCRT.@)
1211 unsigned char* CDECL _mbsrev(unsigned char* str)
1213 int i, len = _mbslen(str);
1214 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1216 if(!temp)
1217 return str;
1219 /* unpack multibyte string to temp buffer */
1220 p=str;
1221 for(i=0; i<len; i++)
1223 if (MSVCRT_isleadbyte(*p))
1225 temp[i*2]=*p++;
1226 temp[i*2+1]=*p++;
1228 else
1230 temp[i*2]=*p++;
1231 temp[i*2+1]=0;
1235 /* repack it in the reverse order */
1236 p=str;
1237 for(i=len-1; i>=0; i--)
1239 if(MSVCRT_isleadbyte(temp[i*2]))
1241 *p++=temp[i*2];
1242 *p++=temp[i*2+1];
1244 else
1246 *p++=temp[i*2];
1250 MSVCRT_free(temp);
1252 return str;
1255 /*********************************************************************
1256 * _mbspbrk (MSVCRT.@)
1258 unsigned char* CDECL _mbspbrk(const unsigned char* str, const unsigned char* accept)
1260 const unsigned char* p;
1262 while(*str)
1264 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1266 if (*p == *str)
1267 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1268 return (unsigned char*)str;
1270 str += (MSVCRT_isleadbyte(*str)?2:1);
1272 return NULL;