hare

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

suffix.ha (1071B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use encoding::utf8;
      6 
      7 // Returns true if 'in' has the given prefix.
      8 export fn hasprefix(in: str, prefix: (str | rune)) bool = {
      9 	let prefix = match (prefix) {
     10 	case let r: rune =>
     11 		yield utf8::encoderune(r);
     12 	case let s: str =>
     13 		yield toutf8(s);
     14 	};
     15 	return bytes::hasprefix(toutf8(in), prefix);
     16 };
     17 
     18 @test fn hasprefix() void = {
     19 	assert(hasprefix("hello world", "hello"));
     20 	assert(hasprefix("hello world", 'h'));
     21 	assert(!hasprefix("hello world", "world"));
     22 	assert(!hasprefix("hello world", 'q'));
     23 };
     24 
     25 // Returns true if 'in' has the given suffix.
     26 export fn hassuffix(in: str, suff: (str | rune)) bool = {
     27 	let suff = match (suff) {
     28 	case let r: rune =>
     29 		yield utf8::encoderune(r);
     30 	case let s: str =>
     31 		yield toutf8(s);
     32 	};
     33 	return bytes::hassuffix(toutf8(in), suff);
     34 };
     35 
     36 @test fn hassuffix() void = {
     37 	assert(hassuffix("hello world", "world"));
     38 	assert(hassuffix("hello world", 'd'));
     39 	assert(!hassuffix("hello world", "hello"));
     40 	assert(!hassuffix("hello world", 'h'));
     41 };