| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This code was written by Rich Felker in 2010; no copyright is claimed. | 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 | 3 * This code is in the public domain. Attribution is appreciated but |
| 4 * unnecessary. | 4 * unnecessary. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <wchar.h> | 7 #include <wchar.h> |
| 8 | 8 |
| 9 size_t mbsnrtowcs(wchar_t *restrict wcs, const char **restrict src, size_t n, si
ze_t wn, mbstate_t *restrict st) | 9 size_t mbsnrtowcs(wchar_t* restrict wcs, |
| 10 { | 10 const char** restrict src, |
| 11 » size_t l, cnt=0, n2; | 11 size_t n, |
| 12 » wchar_t *ws, wbuf[256]; | 12 size_t wn, |
| 13 » const char *s = *src; | 13 mbstate_t* restrict st) { |
| 14 size_t l, cnt = 0, n2; |
| 15 wchar_t *ws, wbuf[256]; |
| 16 const char* s = *src; |
| 14 | 17 |
| 15 » if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; | 18 if (!wcs) |
| 16 » else ws = wcs; | 19 ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; |
| 20 else |
| 21 ws = wcs; |
| 17 | 22 |
| 18 » /* making sure output buffer size is at most n/4 will ensure | 23 /* making sure output buffer size is at most n/4 will ensure |
| 19 » * that mbsrtowcs never reads more than n input bytes. thus | 24 * that mbsrtowcs never reads more than n input bytes. thus |
| 20 » * we can use mbsrtowcs as long as it's practical.. */ | 25 * we can use mbsrtowcs as long as it's practical.. */ |
| 21 | 26 |
| 22 » while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) { | 27 while (s && wn && ((n2 = n / 4) >= wn || n2 > 32)) { |
| 23 » » if (n2>=wn) n2=wn; | 28 if (n2 >= wn) |
| 24 » » n -= n2; | 29 n2 = wn; |
| 25 » » l = mbsrtowcs(ws, &s, n2, st); | 30 n -= n2; |
| 26 » » if (!(l+1)) { | 31 l = mbsrtowcs(ws, &s, n2, st); |
| 27 » » » cnt = l; | 32 if (!(l + 1)) { |
| 28 » » » wn = 0; | 33 cnt = l; |
| 29 » » » break; | 34 wn = 0; |
| 30 » » } | 35 break; |
| 31 » » if (ws != wbuf) { | 36 } |
| 32 » » » ws += l; | 37 if (ws != wbuf) { |
| 33 » » » wn -= l; | 38 ws += l; |
| 34 » » } | 39 wn -= l; |
| 35 » » cnt += l; | 40 } |
| 36 » } | 41 cnt += l; |
| 37 » if (s) while (wn && n) { | 42 } |
| 38 » » l = mbrtowc(ws, s, n, st); | 43 if (s) |
| 39 » » if (l+2<=2) { | 44 while (wn && n) { |
| 40 » » » if (!(l+1)) { | 45 l = mbrtowc(ws, s, n, st); |
| 41 » » » » cnt = l; | 46 if (l + 2 <= 2) { |
| 42 » » » » break; | 47 if (!(l + 1)) { |
| 43 » » » } | 48 cnt = l; |
| 44 » » » if (!l) { | 49 break; |
| 45 » » » » s = 0; | 50 } |
| 46 » » » » break; | 51 if (!l) { |
| 47 » » » } | 52 s = 0; |
| 48 » » » /* have to roll back partial character */ | 53 break; |
| 49 » » » *(unsigned *)st = 0; | 54 } |
| 50 » » » break; | 55 /* have to roll back partial character */ |
| 51 » » } | 56 *(unsigned*)st = 0; |
| 52 » » s += l; n -= l; | 57 break; |
| 53 » » /* safe - this loop runs fewer than sizeof(wbuf)/8 times */ | 58 } |
| 54 » » ws++; wn--; | 59 s += l; |
| 55 » » cnt++; | 60 n -= l; |
| 56 » } | 61 /* safe - this loop runs fewer than sizeof(wbuf)/8 times */ |
| 57 » if (wcs) *src = s; | 62 ws++; |
| 58 » return cnt; | 63 wn--; |
| 64 cnt++; |
| 65 } |
| 66 if (wcs) |
| 67 *src = s; |
| 68 return cnt; |
| 59 } | 69 } |
| OLD | NEW |