summaryrefslogtreecommitdiff
path: root/string32.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-04-19 22:51:33 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2021-04-19 22:53:35 -0400
commitda61efabb1b28c5500824a560c960a720a628de0 (patch)
tree618127733dd0a2923536f52b920671723e6471db /string32.c
parent61cd687d858b7a3811a168393f6b698c24cfff17 (diff)
markdown highlighting
Diffstat (limited to 'string32.c')
-rw-r--r--string32.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/string32.c b/string32.c
index 9b88f0e..1b4f1ac 100644
--- a/string32.c
+++ b/string32.c
@@ -148,6 +148,25 @@ size_t str32_remove_all_instances_of_char(String32 *s, char32_t c) {
return ndeleted;
}
+// returns the length of the longest prefix of `s` containing only
+// ASCII characters in the C-string `charset`.
+size_t str32_ascii_spn(String32 s, char const *charset) {
+ for (u32 i = 0; i < s.len; ++i) {
+ if (s.str[i] >= 128)
+ return i; // non-ASCII character in s, so that can't be in charset.
+ bool found = false;
+ for (char const *p = charset; *p; ++p) {
+ assert((char32_t)*p < 128);
+ if ((char32_t)*p == s.str[i]) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) return i;
+ }
+ return s.len;
+}
+
bool is32_space(char32_t c) {
return c <= WINT_MAX && iswspace((wint_t)c);
}