commit e21e8aa8a24fb0341ca6e2916d7909713a13f773 parent 66e538a14b032987c71188c37f4e04dc33a24028 Author: Drew DeVault <sir@cmpwn.com> Date: Tue, 2 Feb 2021 13:57:05 -0500 encoding::utf8: new function: valid Diffstat:
M | encoding/utf8/decode.ha | | | 13 | +++++++++++++ |
1 file changed, 13 insertions(+), 0 deletions(-)
diff --git a/encoding/utf8/decode.ha b/encoding/utf8/decode.ha @@ -60,6 +60,19 @@ export fn next(d: *decoder) (rune | void | more | invalid) = { return r: rune; }; +export fn valid(src: (str | []u8)) bool = { + let decoder = decode(src); + for (true) { + match (next(&decoder)) { + void => return true, + invalid => return false, + more => return false, + rune => void, + }; + }; + abort(); +}; + fn utf8sz(src: []u8) size = { assert(len(src) > 0z);