diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-05-04 04:01:45 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-05-04 04:01:45 +0200 |
commit | ac1b4bcbaeaee7d2ef9132dcdc254f2d08691650 (patch) | |
tree | 90250966629653f0462cf17bc0b6f2476fb6d1fc /lib/mbstring | |
parent | 8b923ba5e5bb37ea26350b4c1c688b8697706609 (diff) |
Go all in on string views, and fix manuals
Diffstat (limited to 'lib/mbstring')
-rw-r--r-- | lib/mbstring/u8chk.c | 14 | ||||
-rw-r--r-- | lib/mbstring/u8chr.c | 32 | ||||
-rw-r--r-- | lib/mbstring/u8cmp.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8cspn.c | 8 | ||||
-rw-r--r-- | lib/mbstring/u8haspfx.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8hassfx.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8len.c | 4 | ||||
-rw-r--r-- | lib/mbstring/u8next.c | 15 | ||||
-rw-r--r-- | lib/mbstring/u8rchr.c | 40 | ||||
-rw-r--r-- | lib/mbstring/u8split.c | 16 | ||||
-rw-r--r-- | lib/mbstring/u8spn.c | 10 |
11 files changed, 74 insertions, 77 deletions
diff --git a/lib/mbstring/u8chk.c b/lib/mbstring/u8chk.c index 2566bac..20c4f3f 100644 --- a/lib/mbstring/u8chk.c +++ b/lib/mbstring/u8chk.c @@ -1,17 +1,15 @@ #include "rune.h" #include "mbstring.h" -char8_t * -(u8chk)(const char8_t *s, size_t n) +const char8_t * +u8chk(struct u8view sv) { - while (n) { - rune ch; - int m = u8tor(&ch, s); + int w; + rune ch; + while (w = u8next(&ch, &sv)) { if (ch == RUNE_ERROR) - return (char8_t *)s; - n -= m; - s += m; + return sv.p - w; } return nullptr; diff --git a/lib/mbstring/u8chr.c b/lib/mbstring/u8chr.c index 395a328..4831695 100644 --- a/lib/mbstring/u8chr.c +++ b/lib/mbstring/u8chr.c @@ -31,7 +31,7 @@ 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 * +static const char8_t * memmem2(const char8_t *h, size_t k, const char8_t *n) { uint16_t hw, nw; @@ -40,12 +40,12 @@ memmem2(const char8_t *h, size_t k, const char8_t *n) for (h += 2, k -= 2; k; k--, hw = hw << 8 | *h++) { if (hw == nw) - return (char8_t *)h - 2; + return h - 2; } - return hw == nw ? (char8_t *)h - 2 : nullptr; + return hw == nw ? h - 2 : nullptr; } -static char8_t * +static const char8_t * memmem3(const char8_t *h, size_t k, const char8_t *n) { uint32_t hw, nw; @@ -54,12 +54,12 @@ memmem3(const char8_t *h, size_t k, const char8_t *n) for (h += 3, k -= 3; k; k--, hw = (hw | *h++) << 8) { if (hw == nw) - return (char8_t *)h - 3; + return h - 3; } - return hw == nw ? (char8_t *)h - 3 : nullptr; + return hw == nw ? h - 3 : nullptr; } -static char8_t * +static const char8_t * memmem4(const char8_t *h, size_t k, const char8_t *n) { uint32_t hw, nw; @@ -68,28 +68,28 @@ memmem4(const char8_t *h, size_t k, const char8_t *n) for (h += 4, k -= 4; k; k--, hw = hw << 8 | *h++) { if (hw == nw) - return (char8_t *)h - 4; + return h - 4; } - return hw == nw ? (char8_t *)h - 4 : nullptr; + return hw == nw ? h - 4 : nullptr; } -char8_t * -(u8chr)(const char8_t *s, size_t n, rune ch) +const char8_t * +u8chr(struct u8view sv, rune ch) { char8_t buf[U8_LEN_MAX]; int m = rtou8(buf, sizeof(buf), ch); - if (n < (size_t)m) + if (sv.len < (size_t)m) return nullptr; switch (m) { case 1: - return memchr(s, ch, n); + return memchr(sv.p, ch, sv.len); case 2: - return memmem2(s, n, buf); + return memmem2(sv.p, sv.len, buf); case 3: - return memmem3(s, n, buf); + return memmem3(sv.p, sv.len, buf); case 4: - return memmem4(s, n, buf); + return memmem4(sv.p, sv.len, buf); } unreachable(); diff --git a/lib/mbstring/u8cmp.c b/lib/mbstring/u8cmp.c index 8bd2400..0059020 100644 --- a/lib/mbstring/u8cmp.c +++ b/lib/mbstring/u8cmp.c @@ -3,7 +3,7 @@ #include "mbstring.h" int -u8cmp(const char8_t *x, size_t n, const char8_t *y, size_t m) +u8cmp(struct u8view x, struct u8view y) { - return n != m ? (n > m ? +1 : -1) : memcmp(x, y, n); + return x.len != y.len ? (x.len > y.len ? +1 : -1) : memcmp(x.p, y.p, x.len); } diff --git a/lib/mbstring/u8cspn.c b/lib/mbstring/u8cspn.c index 4892de4..827373f 100644 --- a/lib/mbstring/u8cspn.c +++ b/lib/mbstring/u8cspn.c @@ -1,13 +1,13 @@ #include "mbstring.h" size_t -u8cspn(const char8_t *s, size_t n, const rune *p, size_t m) +u8cspn(struct u8view sv, const rune *p, size_t n) { rune ch; - size_t k, l; + size_t k, w; - for (k = 0; (l = u8next(&ch, &s, &n)); k += l) { - for (size_t i = 0; i < m; i++) { + for (k = 0; w = u8next(&ch, &sv); k += w) { + for (size_t i = 0; i < n; i++) { if (p[i] == ch) goto found; } diff --git a/lib/mbstring/u8haspfx.c b/lib/mbstring/u8haspfx.c index b6cea50..c61efbb 100644 --- a/lib/mbstring/u8haspfx.c +++ b/lib/mbstring/u8haspfx.c @@ -4,7 +4,7 @@ #include "mbstring.h" bool -u8haspfx(const char8_t *s, size_t n, const char8_t *pfx, size_t m) +u8haspfx(struct u8view sv, struct u8view pfx) { - return n >= m && memeq(s, pfx, m); + return sv.len >= pfx.len && memeq(sv.p, pfx.p, pfx.len); } diff --git a/lib/mbstring/u8hassfx.c b/lib/mbstring/u8hassfx.c index e31bb4b..8ea4456 100644 --- a/lib/mbstring/u8hassfx.c +++ b/lib/mbstring/u8hassfx.c @@ -4,7 +4,7 @@ #include "mbstring.h" bool -u8hassfx(const char8_t *s, size_t n, const char8_t *sfx, size_t m) +u8hassfx(struct u8view sv, struct u8view sfx) { - return n >= m && memeq(s + n - m, sfx, m); + return sv.len >= sfx.len && memeq(sv.p + sv.len - sfx.len, sfx.p, sfx.len); } diff --git a/lib/mbstring/u8len.c b/lib/mbstring/u8len.c index 217ab66..23c55c5 100644 --- a/lib/mbstring/u8len.c +++ b/lib/mbstring/u8len.c @@ -1,10 +1,10 @@ #include "mbstring.h" size_t -u8len(const char8_t *s, size_t n) +u8len(struct u8view sv) { size_t m = 0; - while (u8next(nullptr, &s, &n)) + while (u8next(nullptr, &sv)) m++; return m; } diff --git a/lib/mbstring/u8next.c b/lib/mbstring/u8next.c index 82d2ad7..518de49 100644 --- a/lib/mbstring/u8next.c +++ b/lib/mbstring/u8next.c @@ -1,16 +1,15 @@ #include "mbstring.h" int -u8next(rune *ch, const char8_t **s, size_t *n) +u8next(rune *ch, struct u8view *sv) { - rune _; - int m = 0; + int n = 0; - if (*n) { - m = u8tor(ch ? ch : &_, *s); - *n -= m; - *s += m; + if (sv->len) { + rune _; + n = u8tor(ch ? ch : &_, sv->p); + VSHFT(sv, n); } - return m; + return n; } diff --git a/lib/mbstring/u8rchr.c b/lib/mbstring/u8rchr.c index 09aa111..825f8fd 100644 --- a/lib/mbstring/u8rchr.c +++ b/lib/mbstring/u8rchr.c @@ -3,17 +3,17 @@ #include "mbstring.h" -static char8_t * +static const 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 p; } return nullptr; } -static char8_t * +static const char8_t * memrchr2(const char8_t *h, size_t k, const char8_t *n) { uint16_t hw, nw; @@ -23,13 +23,13 @@ memrchr2(const char8_t *h, size_t k, const char8_t *n) for (H -= 2, k -= 2; k; k--, hw = hw >> 8 | (*H-- << 8)) { if (hw == nw) - return (char8_t *)H + 1; + return H + 1; } - return hw == nw ? (char8_t *)H + 1 : nullptr; + return hw == nw ? H + 1 : nullptr; } -static char8_t * +static const char8_t * memrchr3(const char8_t *h, size_t k, const char8_t *n) { uint32_t hw, nw; @@ -41,13 +41,13 @@ memrchr3(const char8_t *h, size_t k, const char8_t *n) k--, hw = (hw >> 8 | (*H-- << 24)) & UINT32_C(0xFFFFFF00)) { if (hw == nw) - return (char8_t *)H + 1; + return H + 1; } - return hw == nw ? (char8_t *)H + 1 : nullptr; + return hw == nw ? H + 1 : nullptr; } -static char8_t * +static const char8_t * memrchr4(const char8_t *h, size_t k, const char8_t *n) { uint32_t hw, nw; @@ -57,29 +57,29 @@ memrchr4(const char8_t *h, size_t k, const char8_t *n) for (H -= 4, k -= 4; k; k--, hw = hw >> 8 | (*H-- << 24)) { if (hw == nw) - return (char8_t *)H + 1; + return H + 1; } - return hw == nw ? (char8_t *)H + 1 : nullptr; + return hw == nw ? H + 1 : nullptr; } -char8_t * -(u8rchr)(const char8_t *s, size_t n, rune ch) +const char8_t * +u8rchr(struct u8view sv, rune ch) { char8_t buf[U8_LEN_MAX]; - int m = rtou8(buf, ch, sizeof(buf)); + int n = rtou8(buf, ch, sizeof(buf)); - if (n < (size_t)m) + if (sv.len < (size_t)n) return nullptr; - switch (m) { + switch (n) { case 1: - return (char8_t *)memrchr1(s, n, buf); + return memrchr1(sv.p, sv.len, buf); case 2: - return (char8_t *)memrchr2(s, n, buf); + return memrchr2(sv.p, sv.len, buf); case 3: - return (char8_t *)memrchr3(s, n, buf); + return memrchr3(sv.p, sv.len, buf); case 4: - return (char8_t *)memrchr4(s, n, buf); + return memrchr4(sv.p, sv.len, buf); } unreachable(); diff --git a/lib/mbstring/u8split.c b/lib/mbstring/u8split.c index 5ee3bc0..c26f48b 100644 --- a/lib/mbstring/u8split.c +++ b/lib/mbstring/u8split.c @@ -1,16 +1,16 @@ #include "mbstring.h" struct u8view -u8split(const char8_t **p, size_t *n, rune ch) +u8split(struct u8view *rhs, rune ch) { - struct u8view lhs = {.p = *p}; - if ((*p = u8chr(*p, *n, ch)) == nullptr) { - lhs.len = *n; - *n = 0; + struct u8view lhs = {.p = rhs->p}; + if ((rhs->p = u8chr(*rhs, ch)) == nullptr) { + lhs.len = rhs->len; + rhs->len = 0; } else { - lhs.len = *p - lhs.p; - *n -= lhs.len; - u8next(nullptr, p, n); + lhs.len = rhs->p - lhs.p; + rhs->len -= lhs.len; + u8next(nullptr, rhs); } return lhs; } diff --git a/lib/mbstring/u8spn.c b/lib/mbstring/u8spn.c index 1cf45f2..d41fcbc 100644 --- a/lib/mbstring/u8spn.c +++ b/lib/mbstring/u8spn.c @@ -1,15 +1,15 @@ #include "mbstring.h" size_t -u8spn(const char8_t *s, size_t n, const rune *p, size_t m) +u8spn(struct u8view sv, const rune *p, size_t n) { rune ch; - size_t k = 0, l; + size_t k = 0, w; - while ((l = u8next(&ch, &s, &n))) { - for (size_t i = 0; i < m; i++) { + while (w = u8next(&ch, &sv)) { + for (size_t i = 0; i < n; i++) { if (p[i] == ch) { - k += l; + k += w; goto found; } } |