From 8b923ba5e5bb37ea26350b4c1c688b8697706609 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 4 May 2024 02:21:49 +0200 Subject: Add u8split() --- include/mbstring.h | 2 ++ lib/mbstring/u8split.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 lib/mbstring/u8split.c diff --git a/include/mbstring.h b/include/mbstring.h index 06e32ae..d908284 100644 --- a/include/mbstring.h +++ b/include/mbstring.h @@ -52,6 +52,8 @@ int u8prev(rune *, const char8_t **, const char8_t *); [[nodiscard]] size_t u8len(const char8_t *, size_t); +struct u8view u8split(const char8_t **, size_t *, rune); + #define u8chk(s, n) _MLIB_Q_PTR(char8_t, u8chk, (s), (s), (n)) #define u8chr(s, n, ch) _MLIB_Q_PTR(char8_t, u8chr, (s), (s), (n), (ch)) #define u8rchr(s, n, ch) _MLIB_Q_PTR(char8_t, u8rchr, (s), (s), (n), (ch)) diff --git a/lib/mbstring/u8split.c b/lib/mbstring/u8split.c new file mode 100644 index 0000000..5ee3bc0 --- /dev/null +++ b/lib/mbstring/u8split.c @@ -0,0 +1,16 @@ +#include "mbstring.h" + +struct u8view +u8split(const char8_t **p, size_t *n, rune ch) +{ + struct u8view lhs = {.p = *p}; + if ((*p = u8chr(*p, *n, ch)) == nullptr) { + lhs.len = *n; + *n = 0; + } else { + lhs.len = *p - lhs.p; + *n -= lhs.len; + u8next(nullptr, p, n); + } + return lhs; +} -- cgit v1.2.3