commit 8ffa53f262a4d612ce9a6f700c1e6a154d3ea2a1
parent 900234ef10b3d17b84430fee266a5b40249d5173
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 18 Nov 2021 10:24:35 +0100
strings: accept rune in hasprefix, hassuffix
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/strings/suffix.ha b/strings/suffix.ha
@@ -1,7 +1,38 @@
use bytes;
+use encoding::utf8;
// Returns true if 'in' has the given prefix.
-export fn hasprefix(in: str, prefix: str) bool = bytes::hasprefix(toutf8(in), toutf8(prefix));
+export fn hasprefix(in: str, prefix: (str | rune)) bool = {
+ let prefix = match (prefix) {
+ case r: rune =>
+ yield utf8::encoderune(r);
+ case s: str =>
+ yield toutf8(s);
+ };
+ return bytes::hasprefix(toutf8(in), prefix);
+};
+
+@test fn hasprefix() void = {
+ assert(hasprefix("hello world", "hello"));
+ assert(hasprefix("hello world", 'h'));
+ assert(!hasprefix("hello world", "world"));
+ assert(!hasprefix("hello world", 'q'));
+};
// Returns true if 'in' has the given prefix.
-export fn hassuffix(in: str, suff: str) bool = bytes::hassuffix(toutf8(in), toutf8(suff));
+export fn hassuffix(in: str, suff: (str | rune)) bool = {
+ let suff = match (suff) {
+ case r: rune =>
+ yield utf8::encoderune(r);
+ case s: str =>
+ yield toutf8(s);
+ };
+ return bytes::hassuffix(toutf8(in), suff);
+};
+
+@test fn hassuffix() void = {
+ assert(hassuffix("hello world", "world"));
+ assert(hassuffix("hello world", 'd'));
+ assert(!hassuffix("hello world", "hello"));
+ assert(!hassuffix("hello world", 'h'));
+};