Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: fusl/src/multibyte/mbtowc.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/multibyte/mbstowcs.c ('k') | fusl/src/multibyte/wcrtomb.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This code was written by Rich Felker in 2010; no copyright is claimed.
3 * This code is in the public domain. Attribution is appreciated but
4 * unnecessary.
5 */
6
7 #include <stdlib.h>
8 #include <wchar.h>
9 #include <errno.h>
10 #include "internal.h"
11
12 int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
13 {
14 unsigned c;
15 const unsigned char *s = (const void *)src;
16 wchar_t dummy;
17
18 if (!s) return 0;
19 if (!n) goto ilseq;
20 if (!wc) wc = &dummy;
21
22 if (*s < 0x80) return !!(*wc = *s);
23 if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
24 if (*s-SA > SB-SA) goto ilseq;
25 c = bittab[*s++-SA];
26
27 /* Avoid excessive checks against n: If shifting the state n-1
28 * times does not clear the high bit, then the value of n is
29 * insufficient to read a character */
30 if (n<4 && ((c<<(6*n-6)) & (1U<<31))) goto ilseq;
31
32 if (OOB(c,*s)) goto ilseq;
33 c = c<<6 | *s++-0x80;
34 if (!(c&(1U<<31))) {
35 *wc = c;
36 return 2;
37 }
38
39 if (*s-0x80u >= 0x40) goto ilseq;
40 c = c<<6 | *s++-0x80;
41 if (!(c&(1U<<31))) {
42 *wc = c;
43 return 3;
44 }
45
46 if (*s-0x80u >= 0x40) goto ilseq;
47 *wc = c<<6 | *s++-0x80;
48 return 4;
49
50 ilseq:
51 errno = EILSEQ;
52 return -1;
53 }
OLDNEW
« no previous file with comments | « fusl/src/multibyte/mbstowcs.c ('k') | fusl/src/multibyte/wcrtomb.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698