diff options
Diffstat (limited to 'lib/mbstring')
-rw-r--r-- | lib/mbstring/rtou8.c | 37 | ||||
-rw-r--r-- | lib/mbstring/u8chk.c | 19 | ||||
-rw-r--r-- | lib/mbstring/u8chr.c | 97 | ||||
-rw-r--r-- | lib/mbstring/u8cmp.c | 11 | ||||
-rw-r--r-- | lib/mbstring/u8cspn.c | 18 | ||||
-rw-r--r-- | lib/mbstring/u8len.c | 10 | ||||
-rw-r--r-- | lib/mbstring/u8next.c | 16 | ||||
-rw-r--r-- | lib/mbstring/u8prev.c | 45 | ||||
-rw-r--r-- | lib/mbstring/u8rchr.c | 87 | ||||
-rw-r--r-- | lib/mbstring/u8spn.c | 22 | ||||
-rw-r--r-- | lib/mbstring/u8tor.c | 25 |
11 files changed, 387 insertions, 0 deletions
diff --git a/lib/mbstring/rtou8.c b/lib/mbstring/rtou8.c new file mode 100644 index 0000000..0ddac89 --- /dev/null +++ b/lib/mbstring/rtou8.c @@ -0,0 +1,37 @@ +#include <stddef.h> + +#include "mbstring.h" +#include "rune.h" + +int +rtou8(char8_t *s, rune ch, size_t n) +{ + if (ch <= U8_1B_MAX) { + if (n >= 1) + s[0] = ch; + return 1; + } else if (ch <= U8_2B_MAX) { + if (n >= 2) { + s[0] = (ch >> 6) | 0xC0; + s[1] = (ch & 0x3F) | 0x80; + } + return 2; + } else if (ch <= U8_3B_MAX) { + if (n >= 3) { + s[0] = (ch >> 12) | 0xE0; + s[1] = ((ch >> 6) & 0x3F) | 0x80; + s[2] = (ch & 0x3F) | 0x80; + } + return 3; + } else if (ch <= U8_4B_MAX) { + if (n >= 4) { + s[0] = (ch >> 18) | 0xF0; + s[1] = ((ch >> 12) & 0x3F) | 0x80; + s[2] = ((ch >> 6) & 0x3F) | 0x80; + s[3] = (ch & 0x3F) | 0x80; + } + return 4; + } + + return rtou8(s, RUNE_ERROR, n); +} diff --git a/lib/mbstring/u8chk.c b/lib/mbstring/u8chk.c new file mode 100644 index 0000000..79ef5ee --- /dev/null +++ b/lib/mbstring/u8chk.c @@ -0,0 +1,19 @@ +#include "rune.h" +#define _RUNE_NO_MACRO_WRAPPER 1 +#include "mbstring.h" + +char8_t * +u8chk(const char8_t *s, size_t n) +{ + while (n) { + rune ch; + int m = u8tor(&ch, s); + + if (ch == RUNE_ERROR) + return (char8_t *)s; + n -= m; + s += m; + } + + return nullptr; +} diff --git a/lib/mbstring/u8chr.c b/lib/mbstring/u8chr.c new file mode 100644 index 0000000..008b7d7 --- /dev/null +++ b/lib/mbstring/u8chr.c @@ -0,0 +1,97 @@ +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#define _RUNE_NO_MACRO_WRAPPER 1 +#include "mbstring.h" + +/* NOTE: The memmem*() functions were taken directly from the memmem() + implementation on OpenBSD. As a result, these functions are licensed under + OpenBSDs 2-Clause BSD License instead of this libraries 0-Clause BSD License. + + The license for these functions is as follows: + + Copyright © 2005–2020 Rich Felker, et al. + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + “Software”), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +static char8_t * +memmem2(const char8_t *h, size_t k, const char8_t *n) +{ + uint16_t hw, nw; + hw = h[0] << 8 | h[1]; + nw = n[0] << 8 | n[1]; + + for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++) { + if (hw == nw) + return (char8_t *)h - 2; + } + return hw == nw ? (char8_t *)h - 2 : nullptr; +} + +static char8_t * +memmem3(const char8_t *h, size_t k, const char8_t *n) +{ + uint32_t hw, nw; + hw = h[0] << 24 | h[1] << 16 | h[2] << 8; + nw = n[0] << 24 | n[1] << 16 | n[2] << 8; + + for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8) { + if (hw == nw) + return (char8_t *)h - 3; + } + return hw == nw ? (char8_t *)h - 3 : nullptr; +} + +static char8_t * +memmem4(const char8_t *h, size_t k, const char8_t *n) +{ + uint32_t hw, nw; + hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; + nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; + + for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++) { + if (hw == nw) + return (char8_t *)h - 4; + } + return hw == nw ? (char8_t *)h - 4 : nullptr; +} + +char8_t * +u8chr(const char8_t *s, rune ch, size_t n) +{ + char8_t buf[U8_LEN_MAX]; + int m = rtou8(buf, ch, sizeof(buf)); + + if (n < (size_t)m) + return nullptr; + switch (m) { + case 1: + return memchr(s, ch, n); + case 2: + return memmem2(s, n, buf); + case 3: + return memmem3(s, n, buf); + case 4: + return memmem4(s, n, buf); + } + + unreachable(); +} diff --git a/lib/mbstring/u8cmp.c b/lib/mbstring/u8cmp.c new file mode 100644 index 0000000..732896c --- /dev/null +++ b/lib/mbstring/u8cmp.c @@ -0,0 +1,11 @@ +#include <string.h> + +#include "mbstring.h" + +int +u8cmp(struct u8view x, struct u8view y) +{ + if (x.len != y.len) + return x.len > y.len ? +1 : -1; + return memcmp(x.p, y.p, x.len); +} diff --git a/lib/mbstring/u8cspn.c b/lib/mbstring/u8cspn.c new file mode 100644 index 0000000..4892de4 --- /dev/null +++ b/lib/mbstring/u8cspn.c @@ -0,0 +1,18 @@ +#include "mbstring.h" + +size_t +u8cspn(const char8_t *s, size_t n, const rune *p, size_t m) +{ + rune ch; + size_t k, l; + + for (k = 0; (l = u8next(&ch, &s, &n)); k += l) { + for (size_t i = 0; i < m; i++) { + if (p[i] == ch) + goto found; + } + } + +found: + return k; +} diff --git a/lib/mbstring/u8len.c b/lib/mbstring/u8len.c new file mode 100644 index 0000000..217ab66 --- /dev/null +++ b/lib/mbstring/u8len.c @@ -0,0 +1,10 @@ +#include "mbstring.h" + +size_t +u8len(const char8_t *s, size_t n) +{ + size_t m = 0; + while (u8next(nullptr, &s, &n)) + m++; + return m; +} diff --git a/lib/mbstring/u8next.c b/lib/mbstring/u8next.c new file mode 100644 index 0000000..82d2ad7 --- /dev/null +++ b/lib/mbstring/u8next.c @@ -0,0 +1,16 @@ +#include "mbstring.h" + +int +u8next(rune *ch, const char8_t **s, size_t *n) +{ + rune _; + int m = 0; + + if (*n) { + m = u8tor(ch ? ch : &_, *s); + *n -= m; + *s += m; + } + + return m; +} diff --git a/lib/mbstring/u8prev.c b/lib/mbstring/u8prev.c new file mode 100644 index 0000000..507f7e1 --- /dev/null +++ b/lib/mbstring/u8prev.c @@ -0,0 +1,45 @@ +#include <stddef.h> + +#include "mbstring.h" +#include "rune.h" + +int +u8prev(rune *ch, const char8_t **p, const char8_t *start) +{ + int off; + const char8_t *s = *p; + ptrdiff_t d = s - start; + + if (d <= 0) { + return 0; + } else if (U8_BYTE_1(s[-1])) { + if (ch) + *ch = s[-1]; + off = 1; + } else if (d > 1 && U8_BYTE_C(s[-1]) && U8_BYTE_2(s[-2])) { + if (ch) + *ch = ((s[-2] & 0x1F) << 6) | (s[-1] & 0x3F); + off = 2; + } else if (d > 2 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2]) + && U8_BYTE_3(s[-3])) { + if (ch) { + *ch = + ((s[-3] & 0x0F) << 12) | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F); + } + off = 3; + } else if (d > 3 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2]) && U8_BYTE_C(s[-3]) + && U8_BYTE_4(s[-4])) { + if (ch) { + *ch = ((s[-4] & 0x07) << 18) | ((s[-3] & 0x3F) << 12) + | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F); + } + off = 4; + } else { + if (ch) + *ch = RUNE_ERROR; + off = 1; + } + + *p -= off; + return off; +} diff --git a/lib/mbstring/u8rchr.c b/lib/mbstring/u8rchr.c new file mode 100644 index 0000000..b92c323 --- /dev/null +++ b/lib/mbstring/u8rchr.c @@ -0,0 +1,87 @@ +#include <stddef.h> +#include <stdint.h> + +#define _RUNE_NO_MACRO_WRAPPER 1 +#include "mbstring.h" + +static char8_t * +memrchr1(const char8_t *s, size_t k, const char8_t *n) +{ + for (const char8_t *p = s + k - 1; k-- > 0; p--) { + if (*p == *n) + return (char8_t *)p; + } + return nullptr; +} + +static char8_t * +memrchr2(const char8_t *h, size_t k, const char8_t *n) +{ + uint16_t hw, nw; + const char8_t *H = h + k - 1; + hw = H[-1] << 8 | H[-0]; + nw = n[+0] << 8 | n[+1]; + + for (H -= 2, k -= 2; k; k--, hw = hw >> 8 | (*H-- << 8)) { + if (hw == nw) + return (char8_t *)H + 1; + } + + return hw == nw ? (char8_t *)H + 1 : nullptr; +} + +static char8_t * +memrchr3(const char8_t *h, size_t k, const char8_t *n) +{ + uint32_t hw, nw; + const char8_t *H = h + k - 1; + hw = H[-2] << 24 | H[-1] << 16 | H[-0] << 8; + nw = n[+0] << 24 | n[+1] << 16 | n[+2] << 8; + + for (H -= 3, k -= 3; k; + k--, hw = (hw >> 8 | (*H-- << 24)) & UINT32_C(0xFFFFFF00)) + { + if (hw == nw) + return (char8_t *)H + 1; + } + + return hw == nw ? (char8_t *)H + 1 : nullptr; +} + +static char8_t * +memrchr4(const char8_t *h, size_t k, const char8_t *n) +{ + uint32_t hw, nw; + const char8_t *H = h + k - 1; + hw = H[-3] << 24 | H[-2] << 16 | H[-1] << 8 | H[-0]; + nw = n[+0] << 24 | n[+1] << 16 | n[+2] << 8 | n[+3]; + + for (H -= 4, k -= 4; k; k--, hw = hw >> 8 | (*H-- << 24)) { + if (hw == nw) + return (char8_t *)H + 1; + } + + return hw == nw ? (char8_t *)H + 1 : nullptr; +} + +char8_t * +u8rchr(const char8_t *s, rune ch, size_t n) +{ + char8_t buf[U8_LEN_MAX]; + int m = rtou8(buf, ch, sizeof(buf)); + + if (n < (size_t)m) + return nullptr; + switch (m) { + case 1: + return (char8_t *)memrchr1(s, n, buf); + case 2: + return (char8_t *)memrchr2(s, n, buf); + case 3: + return (char8_t *)memrchr3(s, n, buf); + case 4: + return (char8_t *)memrchr4(s, n, buf); + } + + unreachable(); +} diff --git a/lib/mbstring/u8spn.c b/lib/mbstring/u8spn.c new file mode 100644 index 0000000..1cf45f2 --- /dev/null +++ b/lib/mbstring/u8spn.c @@ -0,0 +1,22 @@ +#include "mbstring.h" + +size_t +u8spn(const char8_t *s, size_t n, const rune *p, size_t m) +{ + rune ch; + size_t k = 0, l; + + while ((l = u8next(&ch, &s, &n))) { + for (size_t i = 0; i < m; i++) { + if (p[i] == ch) { + k += l; + goto found; + } + } + + break; +found:; + } + + return k; +} diff --git a/lib/mbstring/u8tor.c b/lib/mbstring/u8tor.c new file mode 100644 index 0000000..85c4c19 --- /dev/null +++ b/lib/mbstring/u8tor.c @@ -0,0 +1,25 @@ +#include "mbstring.h" +#include "rune.h" + +int +u8tor(rune *ch, const char8_t *s) +{ + if (U8_BYTE_1(s[0])) { + *ch = s[0]; + return 1; + } else if (U8_BYTE_2(s[0]) && U8_BYTE_C(s[1])) { + *ch = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F); + return 2; + } else if (U8_BYTE_3(s[0]) && U8_BYTE_C(s[1]) && U8_BYTE_C(s[2])) { + *ch = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); + return 3; + } else if (U8_BYTE_4(s[0]) && U8_BYTE_C(s[1]) && U8_BYTE_C(s[2]) + && U8_BYTE_C(s[3])) { + *ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) + | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); + return 4; + } + + *ch = RUNE_ERROR; + return 1; +} |