hare

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

commit b5fb7b94c644fe2b89885f221993f99ee1d632d8
parent b41a3b9cbad400925a54790f265179c36926553f
Author: Sebastian <sebastian@sebsite.pw>
Date:   Tue, 17 Jan 2023 01:04:46 -0500

bytes::contains: accept multiple needles

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mbytes/contains.ha | 18+++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/bytes/contains.ha b/bytes/contains.ha @@ -4,11 +4,19 @@ // (c) 2021 Thomas Bracht Laumann Jespersen <t@laumann.xyz> // Returns true if a byte slice contains a byte or a sequence of bytes. -export fn contains(haystack: []u8, needle: (u8 | []u8)) bool = match (needle) { -case let b: u8 => - yield !(index_byte(haystack, b) is void); -case let b: []u8 => - yield !(index_slice(haystack, b) is void); +export fn contains(haystack: []u8, needles: (u8 | []u8)...) bool = { + for (let i = 0z; i < len(needles); i += 1) { + const matched = match (needles[i]) { + case let b: u8 => + yield index_byte(haystack, b) is size; + case let b: []u8 => + yield index_slice(haystack, b) is size; + }; + if (matched) { + return true; + }; + }; + return false; }; // Returns true if "in" has the given prefix, false otherwise