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

Unified Diff: fusl/src/multibyte/mbsnrtowcs.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « fusl/src/multibyte/mbsinit.c ('k') | fusl/src/multibyte/mbsrtowcs.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: fusl/src/multibyte/mbsnrtowcs.c
diff --git a/fusl/src/multibyte/mbsnrtowcs.c b/fusl/src/multibyte/mbsnrtowcs.c
new file mode 100644
index 0000000000000000000000000000000000000000..68b9960f375e3b3dcbe5f4f5de9d6a8a543be104
--- /dev/null
+++ b/fusl/src/multibyte/mbsnrtowcs.c
@@ -0,0 +1,59 @@
+/*
+ * This code was written by Rich Felker in 2010; no copyright is claimed.
+ * This code is in the public domain. Attribution is appreciated but
+ * unnecessary.
+ */
+
+#include <wchar.h>
+
+size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, size_t wn, mbstate_t *restrict st)
+{
+ size_t l, cnt=0, n2;
+ wchar_t *ws, wbuf[256];
+ const char *s = *src;
+
+ if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf;
+ else ws = wcs;
+
+ /* making sure output buffer size is at most n/4 will ensure
+ * that mbsrtowcs never reads more than n input bytes. thus
+ * we can use mbsrtowcs as long as it's practical.. */
+
+ while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) {
+ if (n2>=wn) n2=wn;
+ n -= n2;
+ l = mbsrtowcs(ws, &s, n2, st);
+ if (!(l+1)) {
+ cnt = l;
+ wn = 0;
+ break;
+ }
+ if (ws != wbuf) {
+ ws += l;
+ wn -= l;
+ }
+ cnt += l;
+ }
+ if (s) while (wn && n) {
+ l = mbrtowc(ws, s, n, st);
+ if (l+2<=2) {
+ if (!(l+1)) {
+ cnt = l;
+ break;
+ }
+ if (!l) {
+ s = 0;
+ break;
+ }
+ /* have to roll back partial character */
+ *(unsigned *)st = 0;
+ break;
+ }
+ s += l; n -= l;
+ /* safe - this loop runs fewer than sizeof(wbuf)/8 times */
+ ws++; wn--;
+ cnt++;
+ }
+ if (wcs) *src = s;
+ return cnt;
+}
« no previous file with comments | « fusl/src/multibyte/mbsinit.c ('k') | fusl/src/multibyte/mbsrtowcs.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698