Release 20030408.
[wine/gsoc-2012-control.git] / dlls / msvcrt / mbcs.c
blob2421011eb5cd639333aed2fe668fb74097288f89
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"
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"
34 #include "wine/unicode.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 static MSVCRT_wchar_t msvcrt_mbc_to_wc(unsigned int ch)
44 MSVCRT_wchar_t chW;
45 char mbch[2];
46 int n_chars;
48 if (ch <= 0xff) {
49 mbch[0] = ch;
50 n_chars = 1;
51 } else {
52 mbch[0] = (ch >> 8) & 0xff;
53 mbch[1] = ch & 0xff;
54 n_chars = 2;
56 if (!MultiByteToWideChar(MSVCRT_current_lc_all_cp, 0, mbch, n_chars, &chW, 1))
58 WARN("MultiByteToWideChar failed on %x\n", ch);
59 return 0;
61 return chW;
64 /*********************************************************************
65 * __p__mbctype (MSVCRT.@)
67 unsigned char* __p__mbctype(void)
69 return MSVCRT_mbctype;
72 /*********************************************************************
73 * __p___mb_cur_max(MSVCRT.@)
75 int* __p___mb_cur_max(void)
77 return &MSVCRT___mb_cur_max;
80 /*********************************************************************
81 * _mbsnextc(MSVCRT.@)
83 unsigned int _mbsnextc(const unsigned char* str)
85 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
86 return *str << 8 | str[1];
87 return *str; /* ASCII CP or SB char */
90 /*********************************************************************
91 * _mbctolower(MSVCRT.@)
93 unsigned int _mbctolower(unsigned int c)
95 if (MSVCRT_isleadbyte(c))
97 FIXME("Handle MBC chars\n");
98 return c;
100 return tolower(c); /* ASCII CP or SB char */
103 /*********************************************************************
104 * _mbctoupper(MSVCRT.@)
106 unsigned int _mbctoupper(unsigned int c)
108 if (MSVCRT_isleadbyte(c))
110 FIXME("Handle MBC chars\n");
111 return c;
113 return toupper(c); /* ASCII CP or SB char */
116 /*********************************************************************
117 * _mbsdec(MSVCRT.@)
119 unsigned char* _mbsdec(const unsigned char* start, const unsigned char* cur)
121 if(MSVCRT___mb_cur_max > 1)
122 return (char *)(_ismbstrail(start,cur-1) ? cur - 2 : cur -1);
124 return (char *)cur - 1; /* ASCII CP or SB char */
127 /*********************************************************************
128 * _mbsinc(MSVCRT.@)
130 unsigned char* _mbsinc(const unsigned char* str)
132 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*str))
133 return (unsigned char*)str + 2; /* MB char */
135 return (unsigned char*)str + 1; /* ASCII CP or SB char */
138 /*********************************************************************
139 * _mbsninc(MSVCRT.@)
141 unsigned char* _mbsninc(const unsigned char* str, MSVCRT_size_t num)
143 if(!str || num < 1)
144 return NULL;
145 if(MSVCRT___mb_cur_max > 1)
147 while(num--)
148 str = _mbsinc(str);
149 return (unsigned char*)str;
151 return (unsigned char*)str + num; /* ASCII CP */
154 /*********************************************************************
155 * _mbclen(MSVCRT.@)
157 unsigned int _mbclen(const unsigned char* str)
159 return MSVCRT_isleadbyte(*str) ? 2 : 1;
162 /*********************************************************************
163 * mblen(MSVCRT.@)
165 int MSVCRT_mblen(const char* str, MSVCRT_size_t size)
167 if (str && *str && size)
169 if(MSVCRT___mb_cur_max == 1)
170 return 1; /* ASCII CP */
172 return !MSVCRT_isleadbyte(*str) ? 1 : (size>1 ? 2 : -1);
174 return 0;
177 /*********************************************************************
178 * _mbslen(MSVCRT.@)
180 MSVCRT_size_t _mbslen(const unsigned char* str)
182 if(MSVCRT___mb_cur_max > 1)
184 MSVCRT_size_t len = 0;
185 while(*str)
187 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
188 len++;
190 return len;
192 return strlen(str); /* ASCII CP */
195 /*********************************************************************
196 * _mbstrlen(MSVCRT.@)
198 MSVCRT_size_t _mbstrlen(const char* str)
200 if(MSVCRT___mb_cur_max > 1)
202 MSVCRT_size_t len = 0;
203 while(*str)
205 /* FIXME: According to the documentation we are supposed to test for
206 * multi-byte character validity. Whatever that means
208 str += MSVCRT_isleadbyte(*str) ? 2 : 1;
209 len++;
211 return len;
213 return strlen(str); /* ASCII CP */
216 /*********************************************************************
217 * _mbccpy(MSVCRT.@)
219 void _mbccpy(unsigned char* dest, const unsigned char* src)
221 *dest++ = *src;
222 if(MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(*src))
223 *dest = *++src; /* MB char */
224 else
225 ERR("failure.. is this ok?\n");
228 /*********************************************************************
229 * _mbsncpy(MSVCRT.@)
231 unsigned char* _mbsncpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
233 if(!n)
234 return dst;
235 if(MSVCRT___mb_cur_max > 1)
237 unsigned char* ret = dst;
238 while (*src && n--)
240 *dst++ = *src;
241 if (MSVCRT_isleadbyte(*src++))
242 *dst++ = *src++;
244 while(n--)
245 *dst++ = '\0';
246 return ret;
248 return strncpy(dst, src, n); /* ASCII CP */
251 /*********************************************************************
252 * _mbsnbcpy(MSVCRT.@)
254 unsigned char* _mbsnbcpy(unsigned char* dst, const unsigned char* src, MSVCRT_size_t n)
256 if(!n)
257 return dst;
258 if(MSVCRT___mb_cur_max > 1)
260 unsigned char* ret = dst;
261 while (*src && (n-- > 1))
263 *dst++ = *src;
264 if (MSVCRT_isleadbyte(*src++))
266 *dst++ = *src++;
267 n--;
270 if (*src && n && !MSVCRT_isleadbyte(*src))
272 /* If the last character is a multi-byte character then
273 * we cannot copy it since we have only one byte left
275 *dst++ = *src;
276 n--;
278 while (n--)
279 *dst++ = '\0';
280 return ret;
282 return strncpy(dst, src, n); /* ASCII CP */
285 /*********************************************************************
286 * _mbscmp(MSVCRT.@)
288 int _mbscmp(const unsigned char* str, const unsigned char* cmp)
290 if(MSVCRT___mb_cur_max > 1)
292 unsigned int strc, cmpc;
293 do {
294 if(!*str)
295 return *cmp ? -1 : 0;
296 if(!*cmp)
297 return 1;
298 strc = _mbsnextc(str);
299 cmpc = _mbsnextc(cmp);
300 if(strc != cmpc)
301 return strc < cmpc ? -1 : 1;
302 str +=(strc > 255) ? 2 : 1;
303 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
304 } while(1);
306 return strcmp(str, cmp); /* ASCII CP */
309 /*********************************************************************
310 * _mbsicoll(MSVCRT.@)
311 * FIXME: handle locales.
313 int _mbsicoll(const unsigned char* str, const unsigned char* cmp)
315 if(MSVCRT___mb_cur_max > 1)
317 unsigned int strc, cmpc;
318 do {
319 if(!*str)
320 return *cmp ? -1 : 0;
321 if(!*cmp)
322 return 1;
323 strc = _mbctolower(_mbsnextc(str));
324 cmpc = _mbctolower(_mbsnextc(cmp));
325 if(strc != cmpc)
326 return strc < cmpc ? -1 : 1;
327 str +=(strc > 255) ? 2 : 1;
328 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
329 } while(1);
331 return strcasecmp(str, cmp); /* ASCII CP */
335 /*********************************************************************
336 * _mbsicmp(MSVCRT.@)
338 int _mbsicmp(const unsigned char* str, const unsigned char* cmp)
340 if(MSVCRT___mb_cur_max > 1)
342 unsigned int strc, cmpc;
343 do {
344 if(!*str)
345 return *cmp ? -1 : 0;
346 if(!*cmp)
347 return 1;
348 strc = _mbctolower(_mbsnextc(str));
349 cmpc = _mbctolower(_mbsnextc(cmp));
350 if(strc != cmpc)
351 return strc < cmpc ? -1 : 1;
352 str +=(strc > 255) ? 2 : 1;
353 cmp +=(strc > 255) ? 2 : 1; /* equal, use same increment */
354 } while(1);
356 return strcasecmp(str, cmp); /* ASCII CP */
359 /*********************************************************************
360 * _mbsncmp(MSVCRT.@)
362 int _mbsncmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
364 if(!len)
365 return 0;
367 if(MSVCRT___mb_cur_max > 1)
369 unsigned int strc, cmpc;
370 while(len--)
372 int inc;
373 if(!*str)
374 return *cmp ? -1 : 0;
375 if(!*cmp)
376 return 1;
377 strc = _mbsnextc(str);
378 cmpc = _mbsnextc(cmp);
379 if(strc != cmpc)
380 return strc < cmpc ? -1 : 1;
381 inc=(strc > 255) ? 2 : 1; /* Equal, use same increment */
382 str += inc;
383 cmp += inc;
385 return 0; /* Matched len chars */
387 return strncmp(str, cmp, len); /* ASCII CP */
390 /*********************************************************************
391 * _mbsnbcmp(MSVCRT.@)
393 int _mbsnbcmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
395 if (!len)
396 return 0;
397 if(MSVCRT___mb_cur_max > 1)
399 unsigned int strc, cmpc;
400 while (len)
402 int clen;
403 if(!*str)
404 return *cmp ? -1 : 0;
405 if(!*cmp)
406 return 1;
407 if (MSVCRT_isleadbyte(*str))
409 strc=(len>=2)?_mbsnextc(str):0;
410 clen=2;
412 else
414 strc=*str;
415 clen=1;
417 if (MSVCRT_isleadbyte(*cmp))
418 cmpc=(len>=2)?_mbsnextc(cmp):0;
419 else
420 cmpc=*str;
421 if(strc != cmpc)
422 return strc < cmpc ? -1 : 1;
423 len -= clen;
424 str += clen;
425 cmp += clen;
427 return 0; /* Matched len chars */
429 return strncmp(str,cmp,len);
432 /*********************************************************************
433 * _mbsnicmp(MSVCRT.@)
435 * Compare two multibyte strings case insensitively to 'len' characters.
437 int _mbsnicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
439 /* FIXME: No tolower() for mb strings yet */
440 if(MSVCRT___mb_cur_max > 1)
442 unsigned int strc, cmpc;
443 while(len--)
445 if(!*str)
446 return *cmp ? -1 : 0;
447 if(!*cmp)
448 return 1;
449 strc = _mbctolower(_mbsnextc(str));
450 cmpc = _mbctolower(_mbsnextc(cmp));
451 if(strc != cmpc)
452 return strc < cmpc ? -1 : 1;
453 str +=(strc > 255) ? 2 : 1;
454 cmp +=(strc > 255) ? 2 : 1; /* Equal, use same increment */
456 return 0; /* Matched len chars */
458 return strncasecmp(str, cmp, len); /* ASCII CP */
461 /*********************************************************************
462 * _mbsnbicmp(MSVCRT.@)
464 int _mbsnbicmp(const unsigned char* str, const unsigned char* cmp, MSVCRT_size_t len)
466 if (!len)
467 return 0;
468 if(MSVCRT___mb_cur_max > 1)
470 unsigned int strc, cmpc;
471 while (len)
473 int clen;
474 if(!*str)
475 return *cmp ? -1 : 0;
476 if(!*cmp)
477 return 1;
478 if (MSVCRT_isleadbyte(*str))
480 strc=(len>=2)?_mbsnextc(str):0;
481 clen=2;
483 else
485 strc=*str;
486 clen=1;
488 if (MSVCRT_isleadbyte(*cmp))
489 cmpc=(len>=2)?_mbsnextc(cmp):0;
490 else
491 cmpc=*str;
492 strc = _mbctolower(strc);
493 cmpc = _mbctolower(cmpc);
494 if(strc != cmpc)
495 return strc < cmpc ? -1 : 1;
496 len -= clen;
497 str += clen;
498 cmp += clen;
500 return 0; /* Matched len bytes */
502 return strncmp(str,cmp,len);
505 /*********************************************************************
506 * _mbschr(MSVCRT.@)
508 * Find a multibyte character in a multibyte string.
510 unsigned char* _mbschr(const unsigned char* s, unsigned int x)
512 if(MSVCRT___mb_cur_max > 1)
514 unsigned int c;
515 while (1)
517 c = _mbsnextc(s);
518 if (c == x)
519 return (unsigned char*)s;
520 if (!c)
521 return NULL;
522 s += c > 255 ? 2 : 1;
525 return strchr(s, x); /* ASCII CP */
528 /*********************************************************************
529 * _mbsrchr(MSVCRT.@)
531 unsigned char* _mbsrchr(const unsigned char* s, unsigned int x)
533 if(MSVCRT___mb_cur_max > 1)
535 unsigned int c;
536 unsigned char* match=NULL;
537 if(!s)
538 return NULL;
539 while (1) {
540 c = _mbsnextc(s);
541 if (c == x)
542 match=(unsigned char*)s;
543 if (!c)
544 return match;
545 s +=(c > 255) ? 2 : 1;
548 return strrchr(s,x);
551 /*********************************************************************
552 * _mbstok(MSVCRT.@)
554 * Find and extract tokens from strings
556 unsigned char* _mbstok(unsigned char *str, const unsigned char *delim)
558 MSVCRT_thread_data *data = msvcrt_get_thread_data();
559 char *ret;
561 if(MSVCRT___mb_cur_max > 1)
563 unsigned int c;
565 if (!str)
566 if (!(str = data->mbstok_next)) return NULL;
568 while ((c = _mbsnextc(str)) && _mbschr(delim, c)) {
569 str += c > 255 ? 2 : 1;
571 if (!*str) return NULL;
572 ret = str++;
573 while ((c = _mbsnextc(str)) && !_mbschr(delim, c)) {
574 str += c > 255 ? 2 : 1;
576 if (*str) {
577 *str++ = 0;
578 if (c > 255) *str++ = 0;
580 data->mbstok_next = str;
581 return ret;
583 return strtok(str, delim); /* ASCII CP */
586 /*********************************************************************
587 * mbtowc(MSVCRT.@)
589 int MSVCRT_mbtowc(MSVCRT_wchar_t *dst, const char* str, MSVCRT_size_t n)
591 if(n <= 0 || !str)
592 return 0;
593 if(!MultiByteToWideChar(CP_ACP, 0, str, n, dst, 1))
594 return 0;
595 /* return the number of bytes from src that have been used */
596 if(!*str)
597 return 0;
598 if(n >= 2 && MSVCRT_isleadbyte(*str) && str[1])
599 return 2;
600 return 1;
603 /*********************************************************************
604 * _mbbtombc(MSVCRT.@)
606 unsigned int _mbbtombc(unsigned int c)
608 if(MSVCRT___mb_cur_max > 1 &&
609 ((c >= 0x20 && c <=0x7e) ||(c >= 0xa1 && c <= 0xdf)))
611 /* FIXME: I can't get this function to return anything
612 * different to what I pass it...
615 return c; /* ASCII CP or no MB char */
618 /*********************************************************************
619 * _ismbbkana(MSVCRT.@)
621 int _ismbbkana(unsigned int c)
623 /* FIXME: use lc_ctype when supported, not lc_all */
624 if(MSVCRT_current_lc_all_cp == 932)
626 /* Japanese/Katakana, CP 932 */
627 return (c >= 0xa1 && c <= 0xdf);
629 return 0;
632 /*********************************************************************
633 * _ismbcdigit(MSVCRT.@)
635 int _ismbcdigit(unsigned int ch)
637 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
638 return (get_char_typeW( wch ) & C1_DIGIT);
641 /*********************************************************************
642 * _ismbcgraph(MSVCRT.@)
644 int _ismbcgraph(unsigned int ch)
646 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
647 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA));
650 /*********************************************************************
651 * _ismbcalpha (MSVCRT.@)
653 int _ismbcalpha(unsigned int ch)
655 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
656 return (get_char_typeW( wch ) & C1_ALPHA);
659 /*********************************************************************
660 * _ismbclower (MSVCRT.@)
662 int _ismbclower(unsigned int ch)
664 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
665 return (get_char_typeW( wch ) & C1_UPPER);
668 /*********************************************************************
669 * _ismbcupper (MSVCRT.@)
671 int _ismbcupper(unsigned int ch)
673 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
674 return (get_char_typeW( wch ) & C1_LOWER);
677 /*********************************************************************
678 * _ismbcsymbol(MSVCRT.@)
680 int _ismbcsymbol(unsigned int ch)
682 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
683 WORD ctype;
684 if (!GetStringTypeW(CT_CTYPE3, &wch, 1, &ctype))
686 WARN("GetStringTypeW failed on %x\n", ch);
687 return 0;
689 return ((ctype & C3_SYMBOL) != 0);
692 /*********************************************************************
693 * _ismbcalnum (MSVCRT.@)
695 int _ismbcalnum(unsigned int ch)
697 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
698 return (get_char_typeW( wch ) & (C1_ALPHA | C1_DIGIT));
701 /*********************************************************************
702 * _ismbcspace (MSVCRT.@)
704 int _ismbcspace(unsigned int ch)
706 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
707 return (get_char_typeW( wch ) & C1_SPACE);
710 /*********************************************************************
711 * _ismbcprint (MSVCRT.@)
713 int _ismbcprint(unsigned int ch)
715 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
716 return (get_char_typeW( wch ) & (C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_ALPHA | C1_SPACE));
719 /*********************************************************************
720 * _ismbcpunct(MSVCRT.@)
722 int _ismbcpunct(unsigned int ch)
724 MSVCRT_wchar_t wch = msvcrt_mbc_to_wc( ch );
725 return (get_char_typeW( wch ) & C1_PUNCT);
728 /*********************************************************************
729 * _ismbchira(MSVCRT.@)
731 int _ismbchira(unsigned int c)
733 /* FIXME: use lc_ctype when supported, not lc_all */
734 if(MSVCRT_current_lc_all_cp == 932)
736 /* Japanese/Hiragana, CP 932 */
737 return (c >= 0x829f && c <= 0x82f1);
739 return 0;
742 /*********************************************************************
743 * _ismbckata(MSVCRT.@)
745 int _ismbckata(unsigned int c)
747 /* FIXME: use lc_ctype when supported, not lc_all */
748 if(MSVCRT_current_lc_all_cp == 932)
750 if(c < 256)
751 return _ismbbkana(c);
752 /* Japanese/Katakana, CP 932 */
753 return (c >= 0x8340 && c <= 0x8396 && c != 0x837f);
755 return 0;
758 /*********************************************************************
759 * _ismbblead(MSVCRT.@)
761 int _ismbblead(unsigned int c)
763 /* FIXME: should reference MSVCRT_mbctype */
764 return MSVCRT___mb_cur_max > 1 && MSVCRT_isleadbyte(c);
768 /*********************************************************************
769 * _ismbbtrail(MSVCRT.@)
771 int _ismbbtrail(unsigned int c)
773 /* FIXME: should reference MSVCRT_mbctype */
774 return !_ismbblead(c);
777 /*********************************************************************
778 * _ismbslead(MSVCRT.@)
780 int _ismbslead(const unsigned char* start, const unsigned char* str)
782 /* Lead bytes can also be trail bytes if caller messed up
783 * iterating through the string...
785 if(MSVCRT___mb_cur_max > 1)
787 while(start < str)
788 start += MSVCRT_isleadbyte(*str) ? 2 : 1;
790 if(start == str)
791 return MSVCRT_isleadbyte(*str);
793 return 0; /* Must have been a trail, we skipped it */
796 /*********************************************************************
797 * _ismbstrail(MSVCRT.@)
799 int _ismbstrail(const unsigned char* start, const unsigned char* str)
801 /* Must not be a lead, and must be preceeded by one */
802 return !_ismbslead(start, str) && MSVCRT_isleadbyte(str[-1]);
805 /*********************************************************************
806 * _mbsset(MSVCRT.@)
808 unsigned char* _mbsset(unsigned char* str, unsigned int c)
810 unsigned char* ret = str;
812 if(MSVCRT___mb_cur_max == 1 || c < 256)
813 return _strset(str, c); /* ASCII CP or SB char */
815 c &= 0xffff; /* Strip high bits */
817 while(str[0] && str[1])
819 *str++ = c >> 8;
820 *str++ = c & 0xff;
822 if(str[0])
823 str[0] = '\0'; /* FIXME: OK to shorten? */
825 return ret;
828 /*********************************************************************
829 * _mbsnbset(MSVCRT.@)
831 unsigned char* _mbsnbset(unsigned char *str, unsigned int c, MSVCRT_size_t len)
833 unsigned char *ret = str;
835 if(!len)
836 return ret;
838 if(MSVCRT___mb_cur_max == 1 || c < 256)
839 return _strnset(str, c, len); /* ASCII CP or SB char */
841 c &= 0xffff; /* Strip high bits */
843 while(str[0] && str[1] && (len > 1))
845 *str++ = c >> 8;
846 len--;
847 *str++ = c & 0xff;
848 len--;
850 if(len && str[0]) {
851 /* as per msdn pad with a blank character */
852 str[0] = ' ';
855 return ret;
858 /*********************************************************************
859 * _mbsnset(MSVCRT.@)
861 unsigned char* _mbsnset(unsigned char* str, unsigned int c, MSVCRT_size_t len)
863 unsigned char *ret = str;
865 if(!len)
866 return ret;
868 if(MSVCRT___mb_cur_max == 1 || c < 256)
869 return _strnset(str, c, len); /* ASCII CP or SB char */
871 c &= 0xffff; /* Strip high bits */
873 while(str[0] && str[1] && len--)
875 *str++ = c >> 8;
876 *str++ = c & 0xff;
878 if(len && str[0])
879 str[0] = '\0'; /* FIXME: OK to shorten? */
881 return ret;
884 /*********************************************************************
885 * _mbsnccnt(MSVCRT.@)
886 * 'c' is for 'character'.
888 MSVCRT_size_t _mbsnccnt(const unsigned char* str, MSVCRT_size_t len)
890 MSVCRT_size_t ret;
891 if(MSVCRT___mb_cur_max > 1)
893 ret=0;
894 while(*str && len-- > 0)
896 if(MSVCRT_isleadbyte(*str))
898 if (!len)
899 break;
900 len--;
901 str++;
903 str++;
904 ret++;
906 return ret;
908 ret=strlen(str);
909 return min(ret, len); /* ASCII CP */
912 /*********************************************************************
913 * _mbsnbcnt(MSVCRT.@)
914 * 'b' is for byte count.
916 MSVCRT_size_t _mbsnbcnt(const unsigned char* str, MSVCRT_size_t len)
918 MSVCRT_size_t ret;
919 if(MSVCRT___mb_cur_max > 1)
921 const unsigned char* xstr = str;
922 while(*xstr && len-- > 0)
924 if (MSVCRT_isleadbyte(*xstr++))
925 xstr++;
927 return xstr-str;
929 ret=strlen(str);
930 return min(ret, len); /* ASCII CP */
934 /*********************************************************************
935 * _mbsnbcat(MSVCRT.@)
937 unsigned char* _mbsnbcat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
939 if(MSVCRT___mb_cur_max > 1)
941 char *res = dst;
942 while (*dst) {
943 if (MSVCRT_isleadbyte(*dst++)) {
944 if (*dst) {
945 dst++;
946 } else {
947 /* as per msdn overwrite the lead byte in front of '\0' */
948 dst--;
949 break;
953 while (*src && len--) *dst++ = *src++;
954 *dst = '\0';
955 return res;
957 return strncat(dst, src, len); /* ASCII CP */
961 /*********************************************************************
962 * _mbsncat(MSVCRT.@)
964 unsigned char* _mbsncat(unsigned char* dst, const unsigned char* src, MSVCRT_size_t len)
966 if(MSVCRT___mb_cur_max > 1)
968 char *res = dst;
969 while (*dst)
971 if (MSVCRT_isleadbyte(*dst++))
972 dst++;
974 while (*src && len--)
976 *dst++ = *src;
977 if(MSVCRT_isleadbyte(*src++))
978 *dst++ = *src++;
980 *dst = '\0';
981 return res;
983 return strncat(dst, src, len); /* ASCII CP */
987 /*********************************************************************
988 * _mbslwr(MSVCRT.@)
990 unsigned char* _mbslwr(unsigned char* s)
992 if (!s)
993 return NULL;
994 if (MSVCRT___mb_cur_max > 1)
996 unsigned int c;
997 unsigned char* p=s;
998 while (*s)
1000 c = _mbctolower(_mbsnextc(s));
1001 /* Note that I assume that the size of the character is unchanged */
1002 if (c > 255)
1004 *s++=(c>>8);
1005 c=c & 0xff;
1007 *s++=c;
1009 return p;
1011 return _strlwr(s);
1015 /*********************************************************************
1016 * _mbsupr(MSVCRT.@)
1018 unsigned char* _mbsupr(unsigned char* s)
1020 if (!s)
1021 return NULL;
1022 if (MSVCRT___mb_cur_max > 1)
1024 unsigned int c;
1025 unsigned char* p=s;
1026 while (*s)
1028 c = _mbctoupper(_mbsnextc(s));
1029 /* Note that I assume that the size of the character is unchanged */
1030 if (c > 255)
1032 *s++=(c>>8);
1033 c=c & 0xff;
1035 *s++=c;
1037 return p;
1039 return _strupr(s);
1043 /*********************************************************************
1044 * _mbsspn (MSVCRT.@)
1046 MSVCRT_size_t _mbsspn(const unsigned char* string, const unsigned char* set)
1048 const unsigned char *p, *q;
1050 for (p = string; *p; p++)
1052 if (MSVCRT_isleadbyte(*p))
1054 for (q = set; *q; q++)
1056 if (!q[1])
1057 break;
1058 if ((*p == *q) && (p[1] == q[1]))
1059 break;
1060 q++;
1062 if (*++p == '\0')
1063 break;
1065 else
1066 for (q = set; *q; q++)
1067 if (*p == *q)
1068 break;
1070 return p - string;
1073 /*********************************************************************
1074 * _mbscspn(MSVCRT.@)
1076 MSVCRT_size_t _mbscspn(const unsigned char* str, const unsigned char* cmp)
1078 if (MSVCRT___mb_cur_max > 1)
1079 FIXME("don't handle double character case\n");
1080 return strcspn(str, cmp);
1083 /*********************************************************************
1084 * _mbsrev (MSVCRT.@)
1086 unsigned char* _mbsrev(unsigned char* str)
1088 int i, len = _mbslen(str);
1089 unsigned char *p, *temp=MSVCRT_malloc(len*2);
1091 if(!temp)
1092 return str;
1094 /* unpack multibyte string to temp buffer */
1095 p=str;
1096 for(i=0; i<len; i++)
1098 if (MSVCRT_isleadbyte(*p))
1100 temp[i*2]=*p++;
1101 temp[i*2+1]=*p++;
1103 else
1105 temp[i*2]=*p++;
1106 temp[i*2+1]=0;
1110 /* repack it in the reverse order */
1111 p=str;
1112 for(i=len-1; i>=0; i--)
1114 if(MSVCRT_isleadbyte(temp[i*2]))
1116 *p++=temp[i*2];
1117 *p++=temp[i*2+1];
1119 else
1121 *p++=temp[i*2];
1125 MSVCRT_free(temp);
1127 return str;
1130 /*********************************************************************
1131 * _mbspbrk (MSVCRT.@)
1133 unsigned char* _mbspbrk(const unsigned char* str, const unsigned char* accept)
1135 const unsigned char* p;
1137 while(*str)
1139 for(p = accept; *p; p += (MSVCRT_isleadbyte(*p)?2:1) )
1141 if (*p == *str)
1142 if( !MSVCRT_isleadbyte(*p) || ( *(p+1) == *(str+1) ) )
1143 return (unsigned char*)str;
1145 str += (MSVCRT_isleadbyte(*str)?2:1);
1147 return NULL;