| 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 wcsrtombs(char *restrict s, const wchar_t **restrict ws, size_t n, mbstat
e_t *restrict st) | 9 size_t wcsrtombs(char* restrict s, |
| 10 { | 10 const wchar_t** restrict ws, |
| 11 » const wchar_t *ws2; | 11 size_t n, |
| 12 » char buf[4]; | 12 mbstate_t* restrict st) { |
| 13 » size_t N = n, l; | 13 const wchar_t* ws2; |
| 14 » if (!s) { | 14 char buf[4]; |
| 15 » » for (n=0, ws2=*ws; *ws2; ws2++) { | 15 size_t N = n, l; |
| 16 » » » if (*ws2 >= 0x80u) { | 16 if (!s) { |
| 17 » » » » l = wcrtomb(buf, *ws2, 0); | 17 for (n = 0, ws2 = *ws; *ws2; ws2++) { |
| 18 » » » » if (!(l+1)) return -1; | 18 if (*ws2 >= 0x80u) { |
| 19 » » » » n += l; | 19 l = wcrtomb(buf, *ws2, 0); |
| 20 » » » } else n++; | 20 if (!(l + 1)) |
| 21 » » } | 21 return -1; |
| 22 » » return n; | 22 n += l; |
| 23 » } | 23 } else |
| 24 » while (n>=4) { | 24 n++; |
| 25 » » if (**ws-1u >= 0x7fu) { | 25 } |
| 26 » » » if (!**ws) { | 26 return n; |
| 27 » » » » *s = 0; | 27 } |
| 28 » » » » *ws = 0; | 28 while (n >= 4) { |
| 29 » » » » return N-n; | 29 if (**ws - 1u >= 0x7fu) { |
| 30 » » » } | 30 if (!**ws) { |
| 31 » » » l = wcrtomb(s, **ws, 0); | 31 *s = 0; |
| 32 » » » if (!(l+1)) return -1; | 32 *ws = 0; |
| 33 » » » s += l; | 33 return N - n; |
| 34 » » » n -= l; | 34 } |
| 35 » » } else { | 35 l = wcrtomb(s, **ws, 0); |
| 36 » » » *s++ = **ws; | 36 if (!(l + 1)) |
| 37 » » » n--; | 37 return -1; |
| 38 » » } | 38 s += l; |
| 39 » » (*ws)++; | 39 n -= l; |
| 40 » } | 40 } else { |
| 41 » while (n) { | 41 *s++ = **ws; |
| 42 » » if (**ws-1u >= 0x7fu) { | 42 n--; |
| 43 » » » if (!**ws) { | 43 } |
| 44 » » » » *s = 0; | 44 (*ws)++; |
| 45 » » » » *ws = 0; | 45 } |
| 46 » » » » return N-n; | 46 while (n) { |
| 47 » » » } | 47 if (**ws - 1u >= 0x7fu) { |
| 48 » » » l = wcrtomb(buf, **ws, 0); | 48 if (!**ws) { |
| 49 » » » if (!(l+1)) return -1; | 49 *s = 0; |
| 50 » » » if (l>n) return N-n; | 50 *ws = 0; |
| 51 » » » wcrtomb(s, **ws, 0); | 51 return N - n; |
| 52 » » » s += l; | 52 } |
| 53 » » » n -= l; | 53 l = wcrtomb(buf, **ws, 0); |
| 54 » » } else { | 54 if (!(l + 1)) |
| 55 » » » *s++ = **ws; | 55 return -1; |
| 56 » » » n--; | 56 if (l > n) |
| 57 » » } | 57 return N - n; |
| 58 » » (*ws)++; | 58 wcrtomb(s, **ws, 0); |
| 59 » } | 59 s += l; |
| 60 » return N; | 60 n -= l; |
| 61 } else { |
| 62 *s++ = **ws; |
| 63 n--; |
| 64 } |
| 65 (*ws)++; |
| 66 } |
| 67 return N; |
| 61 } | 68 } |
| OLD | NEW |