hare

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

commit 8369d72918381cb6243e67bcd05f3ae4a85992df
parent dcce9fe9d7835c531569c61d587818051ba8fc0a
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri,  9 Apr 2021 13:50:21 -0400

ascii: add strcasecmp

Diffstat:
Mascii/strcmp.ha | 28++++++++++++++++++++++++++++
1 file changed, 28 insertions(+), 0 deletions(-)

diff --git a/ascii/strcmp.ha b/ascii/strcmp.ha @@ -27,10 +27,38 @@ export fn strcmp(a: str, b: str) (int | void) = { }; }; +// Similar to [strcmp], but compares strings irrespective of their uppercase or +// lowercase classification within the ASCII character set. +export fn strcasecmp(a: str, b: str) (int | void) = { + let a = strings::iter(a), b = strings::iter(b); + for (true) { + let ra = match (strings::next(&a)) { + void => return match (strings::next(&b)) { + void => 0, + rune => -1, + }, + r: rune => r, + }; + let rb = match (strings::next(&b)) { + void => return 1, + r: rune => r, + }; + if (!isascii(ra) || !isascii(rb)) { + return; + }; + let ra = tolower(ra), rb = tolower(rb); + if (ra != rb) { + return ra: u32: int - rb: u32: int; + }; + }; +}; + @test fn strcmp() void = { assert(strcmp("ABC", "ABC") as int == 0); assert(strcmp("ABC", "AB") as int == 1); assert(strcmp("AB", "ABC") as int == -1); assert(strcmp("BCD", "ABC") as int == 1); assert(strcmp("ABC", "こんにちは") is void); + assert(strcmp("ABC", "abc") as int != 0); + assert(strcasecmp("ABC", "abc") as int == 0); };