hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 654ef204a274cccc27328d100175390b72ec40d6
parent 34a6ab32985cdf6916c447cc7eec28214c683681
Author: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
Date:   Wed, 17 Nov 2021 09:14:39 +0100

bytes: Add hasprefix() and hassuffix()

Basically copies the byte slice comparison implementation from
strings::has_prefix() and strings::has_suffix(), and have the string
functions just call the byte functions.

Signed-off-by: Thomas Bracht Laumann Jespersen <t@laumann.xyz>

Diffstat:
Mbytes/contains.ha | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mstrings/suffix.ha | 49++++---------------------------------------------
2 files changed, 54 insertions(+), 45 deletions(-)

diff --git a/bytes/contains.ha b/bytes/contains.ha @@ -5,3 +5,53 @@ case b: u8 => case b: []u8 => yield !(index_slice(haystack, b) is void); }; + +// Returns true if "in" has the given prefix, false otherwise +export fn hasprefix(in: []u8, prefix: []u8) bool = { + return if (len(in) < len(prefix)) false + else equal(in[..len(prefix)], prefix); +}; + +@test fn hasprefix() void = { + const cases: []([]u8, []u8, bool) = [ + ([], [], true), + ([0], [], true), + ([], [0], false), + ([1, 2, 3], [1, 2], true), + ([1, 2, 3], [1, 1], false), + ([1, 2, 3], [1, 2, 3, 4], false), + ]; + + for (let i = 0z; i < len(cases); i += 1) { + let s = cases[i].0; + let prefix = cases[i].1; + let expected = cases[i].2; + let actual = hasprefix(s, prefix); + assert(actual == expected); + }; +}; + +// Returns true if "in" has the given suffix, false otherwise +export fn hassuffix(in: []u8, suffix: []u8) bool = { + return if (len(in) < len(suffix)) false + else equal(in[len(in) - len(suffix)..], suffix); +}; + +@test fn hassuffix() void = { + const cases: []([]u8, []u8, bool) = [ + ([], [], true), + ([0], [], true), + ([], [0], false), + ([1, 2, 3], [2, 3], true), + ([1, 2, 3], [2, 2], false), + ([1, 2, 3, 4], [2, 3, 4], true), + ]; + + for (let i = 0z; i < len(cases); i += 1) { + let s = cases[i].0; + let prefix = cases[i].1; + let expected = cases[i].2; + let actual = hassuffix(s, prefix); + assert(actual == expected); + }; +}; diff --git a/strings/suffix.ha b/strings/suffix.ha @@ -1,48 +1,7 @@ -// Returns true if 'in' has the given prefix. -export fn has_prefix(in: str, prefix: str) bool = { - let a = toutf8(in), b = toutf8(prefix); - if (len(a) < len(b)) { - return false; - }; - for (let i = 0z; i < len(b); i += 1) { - if (a[i] != b[i]) { - return false; - }; - }; - return true; -}; - -@test fn prefix() void = { - assert(has_prefix("abcde", "abc")); - assert(has_prefix("abcde", "abcde")); - assert(has_prefix("abcde", "")); - assert(has_prefix("", "")); - assert(!has_prefix("abcde", "cde")); - assert(!has_prefix("abcde", "abcdefg")); - assert(!has_prefix("", "abc")); -}; +use bytes; // Returns true if 'in' has the given prefix. -export fn has_suffix(in: str, suff: str) bool = { - let a = toutf8(in), b = toutf8(suff); - if (len(a) < len(b)) { - return false; - }; - for (let i = 0z; i < len(b); i += 1) { - if (a[len(a) - len(b) + i] != b[i]) { - return false; - }; - }; - return true; -}; +export fn has_prefix(in: str, prefix: str) bool = bytes::hasprefix(toutf8(in), toutf8(prefix)); -@test fn suffix() void = { - assert(has_suffix("abcde", "cde")); - assert(has_suffix("abcde", "abcde")); - assert(has_suffix("abcde", "")); - assert(has_suffix("", "")); - assert(has_suffix("abcde", "")); - assert(!has_suffix("abcde", "abc")); - assert(!has_suffix("abcde", "fabcde")); - assert(!has_suffix("", "abc")); -}; +// Returns true if 'in' has the given prefix. +export fn has_suffix(in: str, suff: str) bool = bytes::hassuffix(toutf8(in), toutf8(suff));