Fix msvcrt symbol demangling for non MS symbols.
[wine/multimedia.git] / dlls / msvcrt / mbcs.c
blobc1f355cf208aceb3d10581797e8ff296c959fddf
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 unsigned char *u__strlwr( unsigned char *s )
114 return (unsigned char*) _strlwr( (char*)s );
117 static inline unsigned char *u__strupr( unsigned char *s )
119 return (unsigned char*) _strupr( (char*)s );
122 static inline size_t u_strcspn( const unsigned char *s, const unsigned char *rej )
124 return strcspn( (const char *)s, (const char*)rej );
127 /*********************************************************************
128 * __p__mbctype (MSVCRT.@)
130 unsigned char* __p__mbctype(void)
132 return MSVCRT_mbctype;
135 /*********************************************************************
136 * __p___mb_cur_max(MSVCRT.@)
138 int* __p___mb_cur_max(void)
140 return &MSVCRT___mb_cur_max;
143 /*********************************************************************
144 * _mbsnextc(MSVCRT.@)
146 unsigned int _mbsnextc(const unsigned char* str)
148 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
149 return *str << 8 | str[1];
150 return *str; /* ASCII CP or SB char */
153 /*********************************************************************
154 * _mbctolower(MSVCRT.@)
156 unsigned int _mbctolower(unsigned int c)
158 if (MSVCRT_isleadbyte(c))
160 FIXME("Handle MBC chars\n");
161 return c;
163 return tolower(c); /* ASCII CP or SB char */
166 /*********************************************************************
167 * _mbctoupper(MSVCRT.@)
169 unsigned int _mbctoupper(unsigned int c)
171 if (MSVCRT_isleadbyte(c))
173 FIXME("Handle MBC chars\n");
174 return c;
176 return toupper(c); /* ASCII CP or SB char */
179 /*********************************************************************
180 * _mbsdec(MSVCRT.@)
182 unsigned char* _mbsdec(const unsigned char* start, const unsigned char* cur)
184 if(MSVCRT___mb_cur_max > 1)
185 return (unsigned char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
187 return (unsigned char *)cur - 1; /* ASCII CP or SB char */
190 /*********************************************************************
191 * _mbsinc(MSVCRT.@)
193 unsigned char* _mbsinc(const unsigned char* str)
195 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
196 return (unsigned char*)str + 2; /* MB char */
198 return (unsigned char*)str + 1; /* ASCII CP or SB char */
201 /*********************************************************************
202 * _mbsninc(MSVCRT.@)
204 unsigned char* _mbsninc(const unsigned char* str, MSVCRT_size_t num)
206 if(!str || num < 1)
207 return NULL;
208 if(MSVCRT___mb_cur_max > 1)
210 while(num--)
211 str = _mbsinc(str);
212 return (unsigned char*)str;
214 return (unsigned char*)str + num; /* ASCII CP */
217 /*********************************************************************
218 * _mbclen(MSVCRT.@)
220 unsigned int _mbclen(const unsigned char* str)
222 return MSVCRT_isleadbyte(*str) ? 2 : 1;
225 /*********************************************************************
226 * mblen(MSVCRT.@)
228 int MSVCRT_mblen(const char* str, MSVCRT_size_t size)
230 if (str && *str && size)
232 if(MSVCRT___mb_cur_max == 1)
233 return 1; /* ASCII CP */
235 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
237 return 0;
240 /*********************************************************************
241 * _mbslen(MSVCRT.@)
243 MSVCRT_size_t _mbslen(const unsigned char* str)
245 if(MSVCRT___mb_cur_max > 1)
247 MSVCRT_size_t len = 0;
248 while(*str)
250 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
251 len++;
253 return len;
255 return u_strlen(str); /* ASCII CP */
258 /*********************************************************************
259 * _mbstrlen(MSVCRT.@)
261 MSVCRT_size_t _mbstrlen(const char* str)
263 if(MSVCRT___mb_cur_max > 1)
265 MSVCRT_size_t len = 0;
266 while(*str)
268 /* FIXME: According to the documentation we are supposed to test for
269 * multi-byte character validity. Whatever that means
271 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
272 len++;
274 return len;
276 return strlen(str); /* ASCII CP */
279 /*********************************************************************
280 * _mbccpy(MSVCRT.@)
282 void _mbccpy(unsigned char* dest, const unsigned char* src)
284 *dest++ = *src;
285 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
286 *dest = *++src; /* MB char */
287 else
288 ERR("failure.. is this ok?\n");
291 /*********************************************************************
292 * _mbsncpy(MSVCRT.@)
294 unsigned char* _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
296 unsigned char* ret = dst;
297 if(!n)
298 return dst;
299 if(MSVCRT___mb_cur_max > 1)
301 while (*src && n)
303 n--;
304 *dst++ = *src;
305 if (MSVCRT_isleadbyte(*src++))
306 *dst++ = *src++;
309 else
311 while (n)
313 n--;
314 if (!(*dst++ = *src++)) break;
317 while (n--) *dst++ = 0;
318 return ret;
321 /*********************************************************************
322 * _mbsnbcpy(MSVCRT.@)
324 unsigned char* _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
326 unsigned char* ret = dst;
327 if(!n)
328 return dst;
329 if(MSVCRT___mb_cur_max > 1)
331 while (*src && (n > 1))
333 n--;
334 *dst++ = *src;
335 if (MSVCRT_isleadbyte(*src++))
337 *dst++ = *src++;
338 n--;
341 if (*src && n && !MSVCRT_isleadbyte(*src))
343 /* If the last character is a multi-byte character then
344 * we cannot copy it since we have only one byte left
346 *dst++ = *src;
347 n--;
350 else
352 while (n)
354 n--;
355 if (!(*dst++ = *src++)) break;
358 while (n--) *dst++ = 0;
359 return ret;
362 /*********************************************************************
363 * _mbscmp(MSVCRT.@)
365 int _mbscmp(const unsigned char* str, const unsigned char* cmp)
367 if(MSVCRT___mb_cur_max > 1)
369 unsigned int strc, cmpc;
370 do {
371 if(!*str)
372 return *cmp ? -1 : 0;
373 if(!*cmp)
374 return 1;
375 strc = _mbsnextc(str);
376 cmpc = _mbsnextc(cmp);
377 if(strc != cmpc)
378 return strc < cmpc ? -1 : 1;
379 str +=(strc > 255) ? 2 : 1;
380 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
381 } while(1);
383 return u_strcmp(str, cmp); /* ASCII CP */
386 /*********************************************************************
387 * _mbsicoll(MSVCRT.@)
388 * FIXME: handle locales.
390 int _mbsicoll(const unsigned char* str, const unsigned char* cmp)
392 if(MSVCRT___mb_cur_max > 1)
394 unsigned int strc, cmpc;
395 do {
396 if(!*str)
397 return *cmp ? -1 : 0;
398 if(!*cmp)
399 return 1;
400 strc = _mbctolower(_mbsnextc(str));
401 cmpc = _mbctolower(_mbsnextc(cmp));
402 if(strc != cmpc)
403 return strc < cmpc ? -1 : 1;
404 str +=(strc > 255) ? 2 : 1;
405 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
406 } while(1);
408 return u_strcasecmp(str, cmp); /* ASCII CP */
411 /*********************************************************************
412 * _mbscoll(MSVCRT.@)
413 * Performs a case-sensitive comparison according to the current code page
414 * RETURN
415 * _NLSCMPERROR if error
416 * FIXME: handle locales.
418 int _mbscoll(const unsigned char* str, const unsigned char* cmp)
420 if(MSVCRT___mb_cur_max > 1)
422 unsigned int strc, cmpc;
423 do {
424 if(!*str)
425 return *cmp ? -1 : 0;
426 if(!*cmp)
427 return 1;
428 strc = _mbsnextc(str);
429 cmpc = _mbsnextc(cmp);
430 if(strc != cmpc)
431 return strc < cmpc ? -1 : 1;
432 str +=(strc > 255) ? 2 : 1;
433 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
434 } while(1);
436 return u_strcmp(str, cmp); /* ASCII CP */
440 /*********************************************************************
441 * _mbsicmp(MSVCRT.@)
443 int _mbsicmp(const unsigned char* str, const unsigned char* cmp)
445 if(MSVCRT___mb_cur_max > 1)
447 unsigned int strc, cmpc;
448 do {
449 if(!*str)
450 return *cmp ? -1 : 0;
451 if(!*cmp)
452 return 1;
453 strc = _mbctolower(_mbsnextc(str));
454 cmpc = _mbctolower(_mbsnextc(cmp));
455 if(strc != cmpc)
456 return strc < cmpc ? -1 : 1;
457 str +=(strc > 255) ? 2 : 1;
458 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
459 } while(1);
461 return u_strcasecmp(str, cmp); /* ASCII CP */
464 /*********************************************************************
465 * _mbsncmp(MSVCRT.@)
467 int _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
469 if(!len)
470 return 0;
472 if(MSVCRT___mb_cur_max > 1)
474 unsigned int strc, cmpc;
475 while(len--)
477 int inc;
478 if(!*str)
479 return *cmp ? -1 : 0;
480 if(!*cmp)
481 return 1;
482 strc = _mbsnextc(str);
483 cmpc = _mbsnextc(cmp);
484 if(strc != cmpc)
485 return strc < cmpc ? -1 : 1;
486 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
487 str += inc;
488 cmp += inc;
490 return 0; /* Matched len chars */
492 return u_strncmp(str, cmp, len); /* ASCII CP */
495 /*********************************************************************
496 * _mbsnbcmp(MSVCRT.@)
498 int _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
500 if (!len)
501 return 0;
502 if(MSVCRT___mb_cur_max > 1)
504 unsigned int strc, cmpc;
505 while (len)
507 int clen;
508 if(!*str)
509 return *cmp ? -1 : 0;
510 if(!*cmp)
511 return 1;
512 if (MSVCRT_isleadbyte(*str))
514 strc=(len>=2)?_mbsnextc(str):0;
515 clen=2;
517 else
519 strc=*str;
520 clen=1;
522 if (MSVCRT_isleadbyte(*cmp))
523 cmpc=(len>=2)?_mbsnextc(cmp):0;
524 else
525 cmpc=*str;
526 if(strc != cmpc)
527 return strc < cmpc ? -1 : 1;
528 len -= clen;
529 str += clen;
530 cmp += clen;
532 return 0; /* Matched len chars */
534 return u_strncmp(str,cmp,len);
537 /*********************************************************************
538 * _mbsnicmp(MSVCRT.@)
540 * Compare two multibyte strings case insensitively to 'len' characters.
542 int _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
544 /* FIXME: No tolower() for mb strings yet */
545 if(MSVCRT___mb_cur_max > 1)
547 unsigned int strc, cmpc;
548 while(len--)
550 if(!*str)
551 return *cmp ? -1 : 0;
552 if(!*cmp)
553 return 1;
554 strc = _mbctolower(_mbsnextc(str));
555 cmpc = _mbctolower(_mbsnextc(cmp));
556 if(strc != cmpc)
557 return strc < cmpc ? -1 : 1;
558 str +=(strc > 255) ? 2 : 1;
559 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
561 return 0; /* Matched len chars */
563 return u_strncasecmp(str, cmp, len); /* ASCII CP */
566 /*********************************************************************
567 * _mbsnbicmp(MSVCRT.@)
569 int _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
571 if (!len)
572 return 0;
573 if(MSVCRT___mb_cur_max > 1)
575 unsigned int strc, cmpc;
576 while (len)
578 int clen;
579 if(!*str)
580 return *cmp ? -1 : 0;
581 if(!*cmp)
582 return 1;
583 if (MSVCRT_isleadbyte(*str))
585 strc=(len>=2)?_mbsnextc(str):0;
586 clen=2;
588 else
590 strc=*str;
591 clen=1;
593 if (MSVCRT_isleadbyte(*cmp))
594 cmpc=(len>=2)?_mbsnextc(cmp):0;
595 else
596 cmpc=*str;
597 strc = _mbctolower(strc);
598 cmpc = _mbctolower(cmpc);
599 if(strc != cmpc)
600 return strc < cmpc ? -1 : 1;
601 len -= clen;
602 str += clen;
603 cmp += clen;
605 return 0; /* Matched len bytes */
607 return u_strncmp(str,cmp,len);
610 /*********************************************************************
611 * _mbschr(MSVCRT.@)
613 * Find a multibyte character in a multibyte string.
615 unsigned char* _mbschr(const unsigned char* s, unsigned int x)
617 if(MSVCRT___mb_cur_max > 1)
619 unsigned int c;
620 while (1)
622 c = _mbsnextc(s);
623 if (c == x)
624 return (unsigned char*)s;
625 if (!c)
626 return NULL;
627 s += c > 255 ? 2 : 1;
630 return u_strchr(s, x); /* ASCII CP */
633 /*********************************************************************
634 * _mbsrchr(MSVCRT.@)
636 unsigned char* _mbsrchr(const unsigned char* s, unsigned int x)
638 if(MSVCRT___mb_cur_max > 1)
640 unsigned int c;
641 unsigned char* match=NULL;
642 if(!s)
643 return NULL;
644 while (1) {
645 c = _mbsnextc(s);
646 if (c == x)
647 match=(unsigned char*)s;
648 if (!c)
649 return match;
650 s +=(c > 255) ? 2 : 1;
653 return u_strrchr(s, x);
656 /*********************************************************************
657 * _mbstok(MSVCRT.@)
659 * Find and extract tokens from strings
661 unsigned char* _mbstok(unsigned char *str, const unsigned char *delim)
663 thread_data_t *data = msvcrt_get_thread_data();
664 unsigned char *ret;
666 if(MSVCRT___mb_cur_max > 1)
668 unsigned int c;
670 if (!str)
671 if (!(str = data->mbstok_next)) return NULL;
673 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
674 str += c > 255 ? 2 : 1;
676 if (!*str) return NULL;
677 ret = str++;
678 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
679 str += c > 255 ? 2 : 1;
681 if (*str) {
682 *str++ = 0;
683 if (c > 255) *str++ = 0;
685 data->mbstok_next = str;
686 return ret;
688 return u_strtok(str, delim); /* ASCII CP */
691 /*********************************************************************
692 * mbtowc(MSVCRT.@)
694 int MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
696 /* temp var needed because MultiByteToWideChar wants non NULL destination */
697 MSVCRT_wchar_t tmpdst = '\0';
699 if(n <= 0 || !str)
700 return 0;
701 if(!MultiByteToWideChar(CP_ACP, 0, str, n, &tmpdst, 1))
702 return -1;
703 if(dst)
704 *dst = tmpdst;
705 /* return the number of bytes from src that have been used */
706 if(!*str)
707 return 0;
708 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
709 return 2;
710 return 1;
713 /*********************************************************************
714 * _mbbtombc(MSVCRT.@)
716 unsigned int _mbbtombc(unsigned int c)
718 if(MSVCRT___mb_cur_max > 1 &&
719 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
721 /* FIXME: I can't get this function to return anything
722 * different from what I pass it...
725 return c; /* ASCII CP or no MB char */
728 /*********************************************************************
729 * _ismbbkana(MSVCRT.@)
731 int _ismbbkana(unsigned int c)
733 /* FIXME: use lc_ctype when supported, not lc_all */
734 if(msvcrt_current_lc_all_cp == 932)
736 /* Japanese/Katakana, CP 932 */
737 return (c >= 0xa1 && c <= 0xdf);
739 return 0;
742 /*********************************************************************
743 * _ismbcdigit(MSVCRT.@)
745 int _ismbcdigit(unsigned int ch)
747 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
748 return (get_char_typeW( wch ) & C1_DIGIT);
751 /*********************************************************************
752 * _ismbcgraph(MSVCRT.@)
754 int _ismbcgraph(unsigned int ch)
756 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
757 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
760 /*********************************************************************
761 * _ismbcalpha (MSVCRT.@)
763 int _ismbcalpha(unsigned int ch)
765 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
766 return (get_char_typeW( wch ) & C1_ALPHA);
769 /*********************************************************************
770 * _ismbclower (MSVCRT.@)
772 int _ismbclower(unsigned int ch)
774 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
775 return (get_char_typeW( wch ) & C1_UPPER);
778 /*********************************************************************
779 * _ismbcupper (MSVCRT.@)
781 int _ismbcupper(unsigned int ch)
783 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
784 return (get_char_typeW( wch ) & C1_LOWER);
787 /*********************************************************************
788 * _ismbcsymbol(MSVCRT.@)
790 int _ismbcsymbol(unsigned int ch)
792 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
793 WORD ctype;
794 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
796 WARN("GetStringTypeW failed on %x\n", ch);
797 return 0;
799 return ((ctype & C3_SYMBOL) != 0);
802 /*********************************************************************
803 * _ismbcalnum (MSVCRT.@)
805 int _ismbcalnum(unsigned int ch)
807 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
808 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
811 /*********************************************************************
812 * _ismbcspace (MSVCRT.@)
814 int _ismbcspace(unsigned int ch)
816 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
817 return (get_char_typeW( wch ) & C1_SPACE);
820 /*********************************************************************
821 * _ismbcprint (MSVCRT.@)
823 int _ismbcprint(unsigned int ch)
825 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
826 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
829 /*********************************************************************
830 * _ismbcpunct(MSVCRT.@)
832 int _ismbcpunct(unsigned int ch)
834 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
835 return (get_char_typeW( wch ) & C1_PUNCT);
838 /*********************************************************************
839 * _ismbchira(MSVCRT.@)
841 int _ismbchira(unsigned int c)
843 /* FIXME: use lc_ctype when supported, not lc_all */
844 if(msvcrt_current_lc_all_cp == 932)
846 /* Japanese/Hiragana, CP 932 */
847 return (c >= 0x829f && c <= 0x82f1);
849 return 0;
852 /*********************************************************************
853 * _ismbckata(MSVCRT.@)
855 int _ismbckata(unsigned int c)
857 /* FIXME: use lc_ctype when supported, not lc_all */
858 if(msvcrt_current_lc_all_cp == 932)
860 if(c < 256)
861 return _ismbbkana(c);
862 /* Japanese/Katakana, CP 932 */
863 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
865 return 0;
868 /*********************************************************************
869 * _ismbblead(MSVCRT.@)
871 int _ismbblead(unsigned int c)
873 /* FIXME: should reference MSVCRT_mbctype */
874 return MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(c);
878 /*********************************************************************
879 * _ismbbtrail(MSVCRT.@)
881 int _ismbbtrail(unsigned int c)
883 /* FIXME: should reference MSVCRT_mbctype */
884 return !_ismbblead(c);
887 /*********************************************************************
888 * _ismbslead(MSVCRT.@)
890 int _ismbslead(const unsigned char* start, const unsigned char* str)
892 /* Lead bytes can also be trail bytes if caller messed up
893 * iterating through the string...
895 if(MSVCRT___mb_cur_max > 1)
897 while(start < str)
898 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
900 if(start == str)
901 return MSVCRT_isleadbyte(*str);
903 return 0; /* Must have been a trail, we skipped it */
906 /*********************************************************************
907 * _ismbstrail(MSVCRT.@)
909 int _ismbstrail(const unsigned char* start, const unsigned char* str)
911 /* Must not be a lead, and must be preceded by one */
912 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
915 /*********************************************************************
916 * _mbsset(MSVCRT.@)
918 unsigned char* _mbsset(unsigned char* str, unsigned int c)
920 unsigned char* ret = str;
922 if(MSVCRT___mb_cur_max == 1 || c < 256)
923 return u__strset(str, c); /* ASCII CP or SB char */
925 c &= 0xffff; /* Strip high bits */
927 while(str[0] && str[1])
929 *str++ = c >> 8;
930 *str++ = c & 0xff;
932 if(str[0])
933 str[0] = '\0'; /* FIXME: OK to shorten? */
935 return ret;
938 /*********************************************************************
939 * _mbsnbset(MSVCRT.@)
941 unsigned char* _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
943 unsigned char *ret = str;
945 if(!len)
946 return ret;
948 if(MSVCRT___mb_cur_max == 1 || c < 256)
949 return u__strnset(str, c, len); /* ASCII CP or SB char */
951 c &= 0xffff; /* Strip high bits */
953 while(str[0] && str[1] && (len > 1))
955 *str++ = c >> 8;
956 len--;
957 *str++ = c & 0xff;
958 len--;
960 if(len && str[0]) {
961 /* as per msdn pad with a blank character */
962 str[0] = ' ';
965 return ret;
968 /*********************************************************************
969 * _mbsnset(MSVCRT.@)
971 unsigned char* _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
973 unsigned char *ret = str;
975 if(!len)
976 return ret;
978 if(MSVCRT___mb_cur_max == 1 || c < 256)
979 return u__strnset(str, c, len); /* ASCII CP or SB char */
981 c &= 0xffff; /* Strip high bits */
983 while(str[0] && str[1] && len--)
985 *str++ = c >> 8;
986 *str++ = c & 0xff;
988 if(len && str[0])
989 str[0] = '\0'; /* FIXME: OK to shorten? */
991 return ret;
994 /*********************************************************************
995 * _mbsnccnt(MSVCRT.@)
996 * 'c' is for 'character'.
998 MSVCRT_size_t _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
1000 MSVCRT_size_t ret;
1001 if(MSVCRT___mb_cur_max > 1)
1003 ret=0;
1004 while(*str && len-- > 0)
1006 if(MSVCRT_isleadbyte(*str))
1008 if (!len)
1009 break;
1010 len--;
1011 str++;
1013 str++;
1014 ret++;
1016 return ret;
1018 ret=u_strlen(str);
1019 return min(ret, len); /* ASCII CP */
1022 /*********************************************************************
1023 * _mbsnbcnt(MSVCRT.@)
1024 * 'b' is for byte count.
1026 MSVCRT_size_t _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
1028 MSVCRT_size_t ret;
1029 if(MSVCRT___mb_cur_max > 1)
1031 const unsigned char* xstr = str;
1032 while(*xstr && len-- > 0)
1034 if (MSVCRT_isleadbyte(*xstr++))
1035 xstr++;
1037 return xstr-str;
1039 ret=u_strlen(str);
1040 return min(ret, len); /* ASCII CP */
1043 /*********************************************************************
1044 * _mbsnbcat(MSVCRT.@)
1046 unsigned char* _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1048 if(MSVCRT___mb_cur_max > 1)
1050 unsigned char *res = dst;
1051 while (*dst) {
1052 if (MSVCRT_isleadbyte(*dst++)) {
1053 if (*dst) {
1054 dst++;
1055 } else {
1056 /* as per msdn overwrite the lead byte in front of '\0' */
1057 dst--;
1058 break;
1062 while (*src && len--) *dst++ = *src++;
1063 *dst = '\0';
1064 return res;
1066 return u_strncat(dst, src, len); /* ASCII CP */
1070 /*********************************************************************
1071 * _mbsncat(MSVCRT.@)
1073 unsigned char* _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
1075 if(MSVCRT___mb_cur_max > 1)
1077 unsigned char *res = dst;
1078 while (*dst)
1080 if (MSVCRT_isleadbyte(*dst++))
1081 dst++;
1083 while (*src && len--)
1085 *dst++ = *src;
1086 if(MSVCRT_isleadbyte(*src++))
1087 *dst++ = *src++;
1089 *dst = '\0';
1090 return res;
1092 return u_strncat(dst, src, len); /* ASCII CP */
1096 /*********************************************************************
1097 * _mbslwr(MSVCRT.@)
1099 unsigned char* _mbslwr(unsigned char* s)
1101 if (!s)
1102 return NULL;
1103 if (MSVCRT___mb_cur_max > 1)
1105 unsigned int c;
1106 unsigned char* p=s;
1107 while (*s)
1109 c = _mbctolower(_mbsnextc(s));
1110 /* Note that I assume that the size of the character is unchanged */
1111 if (c > 255)
1113 *s++=(c>>8);
1114 c=c & 0xff;
1116 *s++=c;
1118 return p;
1120 return u__strlwr(s);
1124 /*********************************************************************
1125 * _mbsupr(MSVCRT.@)
1127 unsigned char* _mbsupr(unsigned char* s)
1129 if (!s)
1130 return NULL;
1131 if (MSVCRT___mb_cur_max > 1)
1133 unsigned int c;
1134 unsigned char* p=s;
1135 while (*s)
1137 c = _mbctoupper(_mbsnextc(s));
1138 /* Note that I assume that the size of the character is unchanged */
1139 if (c > 255)
1141 *s++=(c>>8);
1142 c=c & 0xff;
1144 *s++=c;
1146 return p;
1148 return u__strupr(s);
1152 /*********************************************************************
1153 * _mbsspn (MSVCRT.@)
1155 MSVCRT_size_t _mbsspn(const unsigned char* string, const unsigned char* set)
1157 const unsigned char *p, *q;
1159 for (p = string; *p; p++)
1161 if (MSVCRT_isleadbyte(*p))
1163 for (q = set; *q; q++)
1165 if (!q[1])
1166 break;
1167 if ((*p == *q) && (p[1] == q[1]))
1168 break;
1169 q++;
1171 if (*++p == '\0')
1172 break;
1174 else
1175 for (q = set; *q; q++)
1176 if (*p == *q)
1177 break;
1179 return p - string;
1182 /*********************************************************************
1183 * _mbscspn(MSVCRT.@)
1185 MSVCRT_size_t _mbscspn(const unsigned char* str, const unsigned char* cmp)
1187 if (MSVCRT___mb_cur_max > 1)
1188 FIXME("don't handle double character case\n");
1189 return u_strcspn(str, cmp);
1192 /*********************************************************************
1193 * _mbsrev (MSVCRT.@)
1195 unsigned char* _mbsrev(unsigned char* str)
1197 int i, len = _mbslen(str);
1198 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1200 if(!temp)
1201 return str;
1203 /* unpack multibyte string to temp buffer */
1204 p=str;
1205 for(i=0; i<len; i++)
1207 if (MSVCRT_isleadbyte(*p))
1209 temp[i*2]=*p++;
1210 temp[i*2+1]=*p++;
1212 else
1214 temp[i*2]=*p++;
1215 temp[i*2+1]=0;
1219 /* repack it in the reverse order */
1220 p=str;
1221 for(i=len-1; i>=0; i--)
1223 if(MSVCRT_isleadbyte(temp[i*2]))
1225 *p++=temp[i*2];
1226 *p++=temp[i*2+1];
1228 else
1230 *p++=temp[i*2];
1234 MSVCRT_free(temp);
1236 return str;
1239 /*********************************************************************
1240 * _mbspbrk (MSVCRT.@)
1242 unsigned char* _mbspbrk(const unsigned char* str, const unsigned char* accept)
1244 const unsigned char* p;
1246 while(*str)
1248 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1250 if (*p == *str)
1251 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1252 return (unsigned char*)str;
1254 str += (MSVCRT_isleadbyte(*str)?2:1);
1256 return NULL;