hare

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

commit bd52e831d9fb1024c98ef5a1f501a408a3a8328c
parent 0fa2a28048a20a83813b0ec4ec6b29774d1662d1
Author: Sebastian <sebastian@sebsite.pw>
Date:   Wed, 13 Apr 2022 20:33:46 -0400

ascii: add validstr

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

Diffstat:
Mascii/strcmp.ha | 4++--
Mascii/valid.ha | 15+++++++++++++++
Mnet/dns/decode.ha | 2+-
Mscripts/gen-stdlib | 5+++--
Mstdlib.mk | 10++++++----
5 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/ascii/strcmp.ha b/ascii/strcmp.ha @@ -80,13 +80,13 @@ export fn strcasecmp(a: str, b: str) (int | void) = { case void => break; case let r: rune => - if (!isascii(r)) return; + if (!valid(r)) return; }; return 1; case let r: rune => yield r; }; - if (!isascii(ra) || !isascii(rb)) { + if (!valid(ra) || !valid(rb)) { return; }; let ra = tolower(ra), rb = tolower(rb); diff --git a/ascii/valid.ha b/ascii/valid.ha @@ -1,11 +1,26 @@ // License: MPL-2.0 // (c) 2022 Sebastian <sebastian@sebsite.pw> // (c) 2021 Drew Devault <sir@cmpwn.com> +use strings; // Returns true if a rune is a valid ASCII character. export fn valid(c: rune) bool = c: u32 <= 0o177; +// Returns true if all runes in a string are valid ASCII characters. +export fn validstr(s: str) bool = { + const s = strings::toutf8(s); + for (let i = 0z; i < len(s); i += 1) { + if (s[i] >= 0200) { + return false; + }; + }; + return true; +}; + @test fn valid() void = { assert(valid('a') && valid('\0') && valid('\x7F')); assert(!valid('\x80') && !valid('こ')); + assert(validstr("abc\0")); + assert(!validstr("こんにちは")); + assert(!validstr("ABCこんにちは")); }; diff --git a/net/dns/decode.ha b/net/dns/decode.ha @@ -112,7 +112,7 @@ fn decode_name(dec: *decoder) ([]str | format) = { const name = dec.cur[..z]; dec.cur = dec.cur[z..]; for (let i = 0z; i < len(name); i += 1) { - if (!ascii::isascii(name[i]: u32: rune)) { + if (!ascii::valid(name[i]: u32: rune)) { return format; }; }; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -157,7 +157,8 @@ EOF ascii() { gen_srcs ascii \ ctype.ha \ - strcmp.ha + strcmp.ha \ + valid.ha gen_ssa ascii strings } @@ -601,7 +602,7 @@ hare_types() { else gensrcs_hare_types fi - gen_ssa hare::types hare::ast hare::parse hash hash::fnv endian io + gen_ssa hare::types hare::ast hare::parse hash hash::fnv endian io strings } gensrcs_hare_unit() { diff --git a/stdlib.mk b/stdlib.mk @@ -689,7 +689,8 @@ stdlib_uuid_freebsd=$(stdlib_uuid_any) # ascii (+any) stdlib_ascii_any_srcs= \ $(STDLIB)/ascii/ctype.ha \ - $(STDLIB)/ascii/strcmp.ha + $(STDLIB)/ascii/strcmp.ha \ + $(STDLIB)/ascii/valid.ha $(HARECACHE)/ascii/ascii-any.ssa: $(stdlib_ascii_any_srcs) $(stdlib_rt) $(stdlib_strings_$(PLATFORM)) @printf 'HAREC \t$@\n' @@ -1182,7 +1183,7 @@ stdlib_hare_types_any_srcs= \ $(STDLIB)/hare/types/store.ha \ $(STDLIB)/hare/types/types.ha -$(HARECACHE)/hare/types/hare_types-any.ssa: $(stdlib_hare_types_any_srcs) $(stdlib_rt) $(stdlib_hare_ast_$(PLATFORM)) $(stdlib_hare_parse_$(PLATFORM)) $(stdlib_hash_$(PLATFORM)) $(stdlib_hash_fnv_$(PLATFORM)) $(stdlib_endian_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) +$(HARECACHE)/hare/types/hare_types-any.ssa: $(stdlib_hare_types_any_srcs) $(stdlib_rt) $(stdlib_hare_ast_$(PLATFORM)) $(stdlib_hare_parse_$(PLATFORM)) $(stdlib_hash_$(PLATFORM)) $(stdlib_hash_fnv_$(PLATFORM)) $(stdlib_endian_$(PLATFORM)) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/hare/types @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nhare::types \ @@ -2658,7 +2659,8 @@ testlib_uuid_freebsd=$(testlib_uuid_any) # ascii (+any) testlib_ascii_any_srcs= \ $(STDLIB)/ascii/ctype.ha \ - $(STDLIB)/ascii/strcmp.ha + $(STDLIB)/ascii/strcmp.ha \ + $(STDLIB)/ascii/valid.ha $(TESTCACHE)/ascii/ascii-any.ssa: $(testlib_ascii_any_srcs) $(testlib_rt) $(testlib_strings_$(PLATFORM)) @printf 'HAREC \t$@\n' @@ -3180,7 +3182,7 @@ testlib_hare_types_any_srcs= \ $(STDLIB)/hare/types/types.ha \ $(STDLIB)/hare/types/+test.ha -$(TESTCACHE)/hare/types/hare_types-any.ssa: $(testlib_hare_types_any_srcs) $(testlib_rt) $(testlib_hare_ast_$(PLATFORM)) $(testlib_hare_parse_$(PLATFORM)) $(testlib_hash_$(PLATFORM)) $(testlib_hash_fnv_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_io_$(PLATFORM)) +$(TESTCACHE)/hare/types/hare_types-any.ssa: $(testlib_hare_types_any_srcs) $(testlib_rt) $(testlib_hare_ast_$(PLATFORM)) $(testlib_hare_parse_$(PLATFORM)) $(testlib_hash_$(PLATFORM)) $(testlib_hash_fnv_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/hare/types @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nhare::types \