string.ha (3198B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use encoding::utf8; 5 use sort::cmp; 6 use strings; 7 8 // Converts all ASCII uppercase characters in a string to their lowercase 9 // representation, returning a new string. The return value must be freed by the 10 // caller. 11 export fn strlower(s: str) str = { 12 let new: []u8 = alloc([], len(s)); 13 return strlower_buf(s, new); 14 }; 15 16 // Converts all ASCII uppercase characters in a string to their lowercase 17 // representation, returning a new string. The new string data is stored in the 18 // supplied buffer. This function will abort if the buffer's length is too small 19 // to fit the entire string. 20 export fn strlower_buf(s: str, buf: []u8) str = { 21 let it = strings::iter(s); 22 for (let r => strings::next(&it)) { 23 static append(buf, utf8::encoderune(tolower(r))...); 24 }; 25 return strings::fromutf8(buf)!; 26 }; 27 28 // Converts all ASCII lowercase characters in a string to their uppercase 29 // representation, returning a new string. The return value must be freed by the 30 // caller. 31 export fn strupper(s: str) str = { 32 let new: []u8 = alloc([], len(s)); 33 return strupper_buf(s, new); 34 }; 35 36 // Converts all ASCII lowercase characters in a string to their uppercase 37 // representation, returning a new string. The new string data is stored in the 38 // supplied buffer. This function will abort if the buffer's length is too small 39 // to fit the entire string. 40 export fn strupper_buf(s: str, buf: []u8) str = { 41 let it = strings::iter(s); 42 for (let r => strings::next(&it)) { 43 static append(buf, utf8::encoderune(toupper(r))...); 44 }; 45 return strings::fromutf8(buf)!; 46 }; 47 48 // Compares two strings by their sort order, treating all ASCII capital letters 49 // as their lowercase counterpart (i.e. an ASCII-case-insensitive comparison is 50 // performed). Zero is returned if the strings are equal, a negative value if a 51 // is less than b, or a positive value if a is greater than b. 52 export fn strcasecmp(a: str, b: str) int = { 53 let abs = strings::toutf8(a); 54 let bbs = strings::toutf8(b); 55 for (let i = 0z; i < len(abs) && i < len(bbs); i += 1) { 56 // you know that i am called "the Cast"... 57 // because i *really* love to cast... 58 // sometimes i sit and cast all day... ha ha, but 59 // sometimes i get carried away! 60 let cmp = tolower(abs[i]: rune): u32: int - tolower(bbs[i]: rune): u32: int; 61 if (cmp != 0) return cmp; 62 }; 63 return cmp::sizes(&len(abs), &len(bbs)); 64 }; 65 66 @test fn strcasecmp() void = { 67 let s = strupper("ABC"); 68 defer free(s); 69 assert(s == "ABC"); 70 71 let s = strlower("ABC"); 72 defer free(s); 73 assert(s == "abc"); 74 75 let s = strupper("abc"); 76 defer free(s); 77 assert(s == "ABC"); 78 79 let s = strlower("abc"); 80 defer free(s); 81 assert(s == "abc"); 82 83 let s = strupper("[[["); 84 defer free(s); 85 assert(s == "[[["); 86 87 let s = strlower("[[["); 88 defer free(s); 89 assert(s == "[[["); 90 91 let s = strupper("こ"); 92 defer free(s); 93 assert(s == "こ"); 94 95 let s = strlower("こ"); 96 defer free(s); 97 assert(s == "こ"); 98 99 assert(strcasecmp("ABC", "ABC") == 0); 100 assert(strcasecmp("ABC", "abc") == 0); 101 assert(strcasecmp("ABC", "aB") > 0); 102 assert(strcasecmp("ab", "Abc") < 0); 103 assert(strcasecmp("bcd", "ABC") > 0); 104 assert(strcasecmp("ABC", "[[[") > 0); 105 };