~sircmpwn/hare

hare/strings/contains.ha -rw-r--r-- 526 bytes
06e7f5f7Alexey Yerin types+test: use '&' on literals instead of temporary variable 22 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use bytes;
use encoding::utf8;

// Returns true if a string contains a rune or a sub-string.
export fn contains(haystack: str, needle: (str | rune)) bool = match (needle) {
case s: str  =>
	yield bytes::contains(toutf8(haystack), toutf8(s));
case r: rune =>
	yield bytes::contains(toutf8(haystack), utf8::encoderune(r));
};

@test fn contains() void = {
	assert(contains("hello world", "hello"));
	assert(contains("hello world", "world"));
	assert(contains("hello world", ""));
	assert(!contains("hello world", "foobar"));
};