aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-03-06 16:41:59 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-03-06 16:43:45 +0100
commit5ecd0fc61a1a292426538fcb1e791342e04953b5 (patch)
tree6cdaaf37b22cf834dc1f6089693183d6e6504878 /lib
Genesis
Diffstat (limited to 'lib')
-rw-r--r--lib/alloc/bufalloc.c29
-rw-r--r--lib/bob/u8strfit.c9
-rw-r--r--lib/bob/u8strfree.c9
-rw-r--r--lib/bob/u8strgrow.c39
-rw-r--r--lib/bob/u8strinit.c16
-rw-r--r--lib/bob/u8strpushr.c11
-rw-r--r--lib/bob/u8strpushstr.c9
-rw-r--r--lib/bob/u8strpushu8.c14
-rw-r--r--lib/errors/cerr.c14
-rw-r--r--lib/errors/cerrx.c14
-rw-r--r--lib/errors/err.c14
-rw-r--r--lib/errors/errx.c14
-rw-r--r--lib/errors/setprogname.c12
-rw-r--r--lib/errors/vwarn.c15
-rw-r--r--lib/errors/vwarnx.c12
-rw-r--r--lib/errors/warn.c12
-rw-r--r--lib/errors/warnx.c12
-rw-r--r--lib/mbstring/rtou8.c37
-rw-r--r--lib/mbstring/u8chk.c19
-rw-r--r--lib/mbstring/u8chr.c97
-rw-r--r--lib/mbstring/u8cmp.c11
-rw-r--r--lib/mbstring/u8cspn.c18
-rw-r--r--lib/mbstring/u8len.c10
-rw-r--r--lib/mbstring/u8next.c16
-rw-r--r--lib/mbstring/u8prev.c45
-rw-r--r--lib/mbstring/u8rchr.c87
-rw-r--r--lib/mbstring/u8spn.c22
-rw-r--r--lib/mbstring/u8tor.c25
28 files changed, 642 insertions, 0 deletions
diff --git a/lib/alloc/bufalloc.c b/lib/alloc/bufalloc.c
new file mode 100644
index 0000000..a81f5b9
--- /dev/null
+++ b/lib/alloc/bufalloc.c
@@ -0,0 +1,29 @@
+#include <errno.h>
+#if __has_include(<stdckdint.h>)
+# include <stdckdint.h>
+# warning "stdckdint.h now available; remove manual ckd_*() implementations"
+#elifdef __GNUC__
+# define ckd_add(r, a, b) ((bool)__builtin_add_overflow(a, b, r))
+# define ckd_mul(r, a, b) ((bool)__builtin_mul_overflow(a, b, r))
+#else
+# define ckd_add(r, a, b) (*(r) = (a) + (b))
+# define ckd_mul(r, a, b) (*(r) = (a) * (b))
+# warning "ckd_*() not supported on the current platform"
+#endif
+#include <stdlib.h>
+
+#include "alloc.h"
+#include "errors.h"
+
+void *
+bufalloc(void *p, size_t n, size_t m)
+{
+ if (ckd_mul(&n, n, m)) {
+ errno = EOVERFLOW;
+ err(__func__);
+ }
+
+ if (!(p = realloc(p, n)))
+ err(__func__);
+ return p;
+}
diff --git a/lib/bob/u8strfit.c b/lib/bob/u8strfit.c
new file mode 100644
index 0000000..332a5d7
--- /dev/null
+++ b/lib/bob/u8strfit.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strfit(struct u8str *b)
+{
+ return (b->p = realloc(b->p, b->len)) ? b : nullptr;
+}
diff --git a/lib/bob/u8strfree.c b/lib/bob/u8strfree.c
new file mode 100644
index 0000000..7e25ca8
--- /dev/null
+++ b/lib/bob/u8strfree.c
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+void
+u8strfree(struct u8str b)
+{
+ free(b.p);
+}
diff --git a/lib/bob/u8strgrow.c b/lib/bob/u8strgrow.c
new file mode 100644
index 0000000..f1f86d3
--- /dev/null
+++ b/lib/bob/u8strgrow.c
@@ -0,0 +1,39 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+static size_t nextpow2(size_t);
+
+struct u8str *
+u8strgrow(struct u8str *b, size_t n)
+{
+ if (n > b->cap) {
+ b->cap = nextpow2(n);
+ if (!(b->p = realloc(b->p, b->cap)))
+ return nullptr;
+ }
+ return b;
+}
+
+size_t
+nextpow2(size_t x)
+{
+#if defined(__has_builtin) && __has_builtin(__builtin_clzl)
+ x = x <= 1 ? 1 : 1 << (64 - __builtin_clzl(x - 1));
+#else
+ if (x) {
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ if (sizeof(size_t) >= 4)
+ x |= x >> 16;
+ if (sizeof(size_t) >= 8)
+ x |= x >> 32;
+ }
+ x++;
+#endif
+
+ return x;
+}
diff --git a/lib/bob/u8strinit.c b/lib/bob/u8strinit.c
new file mode 100644
index 0000000..60423c1
--- /dev/null
+++ b/lib/bob/u8strinit.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strinit(struct u8str *b, size_t n)
+{
+ if (n) {
+ if (!(b->p = malloc(n)))
+ return nullptr;
+ } else
+ b->p = nullptr;
+ b->len = 0;
+ b->cap = n;
+ return b;
+}
diff --git a/lib/bob/u8strpushr.c b/lib/bob/u8strpushr.c
new file mode 100644
index 0000000..6fe5fd9
--- /dev/null
+++ b/lib/bob/u8strpushr.c
@@ -0,0 +1,11 @@
+#include "bob.h"
+#include "mbstring.h"
+
+struct u8str *
+u8strpushr(struct u8str *b, rune ch)
+{
+ if (!u8strgrow(b, b->len + rtou8(nullptr, ch, 0)))
+ return nullptr;
+ b->len += rtou8(b->p + b->len, ch, b->cap - b->len);
+ return b;
+}
diff --git a/lib/bob/u8strpushstr.c b/lib/bob/u8strpushstr.c
new file mode 100644
index 0000000..64b123d
--- /dev/null
+++ b/lib/bob/u8strpushstr.c
@@ -0,0 +1,9 @@
+#include <string.h>
+
+#include "bob.h"
+
+struct u8str *
+u8strpushstr(struct u8str *b, const char *s)
+{
+ return u8strpushu8(b, (struct u8view){.p = s, .len = strlen(s)});
+}
diff --git a/lib/bob/u8strpushu8.c b/lib/bob/u8strpushu8.c
new file mode 100644
index 0000000..8358e01
--- /dev/null
+++ b/lib/bob/u8strpushu8.c
@@ -0,0 +1,14 @@
+#include <string.h>
+
+#include "bob.h"
+#include "mbstring.h"
+
+struct u8str *
+u8strpushu8(struct u8str *b, struct u8view v)
+{
+ if (!u8strgrow(b, b->len + v.len))
+ return nullptr;
+ memcpy(b->p + b->len, v.p, v.len);
+ b->len += v.len;
+ return b;
+}
diff --git a/lib/errors/cerr.c b/lib/errors/cerr.c
new file mode 100644
index 0000000..bf5a98d
--- /dev/null
+++ b/lib/errors/cerr.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+cerr(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+ exit(code);
+}
diff --git a/lib/errors/cerrx.c b/lib/errors/cerrx.c
new file mode 100644
index 0000000..b614f47
--- /dev/null
+++ b/lib/errors/cerrx.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+cerrx(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+ exit(code);
+}
diff --git a/lib/errors/err.c b/lib/errors/err.c
new file mode 100644
index 0000000..906e661
--- /dev/null
+++ b/lib/errors/err.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+err(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
diff --git a/lib/errors/errx.c b/lib/errors/errx.c
new file mode 100644
index 0000000..1fd55af
--- /dev/null
+++ b/lib/errors/errx.c
@@ -0,0 +1,14 @@
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "errors.h"
+
+void
+errx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
diff --git a/lib/errors/setprogname.c b/lib/errors/setprogname.c
new file mode 100644
index 0000000..ab2de3e
--- /dev/null
+++ b/lib/errors/setprogname.c
@@ -0,0 +1,12 @@
+#include <string.h>
+
+#include "errors.h"
+
+const char *__mlib_errors_progname;
+
+void
+setprogname(const char *s)
+{
+ const char *p = strrchr(s, '/');
+ __mlib_errors_progname = p ? p + 1 : s;
+}
diff --git a/lib/errors/vwarn.c b/lib/errors/vwarn.c
new file mode 100644
index 0000000..bc6bd16
--- /dev/null
+++ b/lib/errors/vwarn.c
@@ -0,0 +1,15 @@
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "errors.h"
+
+void
+vwarn(const char *fmt, va_list ap)
+{
+ int save = errno;
+ fprintf(stderr, "%s: ", progname());
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, ": %s\n", strerror(save));
+}
diff --git a/lib/errors/vwarnx.c b/lib/errors/vwarnx.c
new file mode 100644
index 0000000..72d8fa7
--- /dev/null
+++ b/lib/errors/vwarnx.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "errors.h"
+
+void
+vwarnx(const char *fmt, va_list ap)
+{
+ fprintf(stderr, "%s: ", progname());
+ vfprintf(stderr, fmt, ap);
+ fputc('\n', stderr);
+}
diff --git a/lib/errors/warn.c b/lib/errors/warn.c
new file mode 100644
index 0000000..78f3e36
--- /dev/null
+++ b/lib/errors/warn.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+
+#include "errors.h"
+
+void
+warn(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarn(fmt, ap);
+ va_end(ap);
+}
diff --git a/lib/errors/warnx.c b/lib/errors/warnx.c
new file mode 100644
index 0000000..ea50299
--- /dev/null
+++ b/lib/errors/warnx.c
@@ -0,0 +1,12 @@
+#include <stdarg.h>
+
+#include "errors.h"
+
+void
+warnx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+}
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;
+}