string: add string.isOneOf and deduplicate helpers

This commit is contained in:
Adrià Arrufat
2026-06-08 09:04:36 +02:00
parent 7bcab1d840
commit 2a7bda3608
3 changed files with 18 additions and 22 deletions

View File

@@ -311,6 +311,13 @@ pub fn isAllWhitespace(text: []const u8) bool {
} else true;
}
/// True when `needle` is byte-equal to one of the strings in `haystack`.
pub fn isOneOf(needle: []const u8, haystack: []const []const u8) bool {
return for (haystack) |s| {
if (std.mem.eql(u8, s, needle)) break true;
} else false;
}
/// Largest prefix of `bytes` whose length is at most `max_bytes` and
/// ends on a UTF-8 codepoint boundary. Invalid sequences count as one
/// byte each so the function never loops.