hare

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

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

ascii: rename isascii to valid

For consistency with e.g. encoding::utf8::valid

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

Diffstat:
Mascii/ctype.ha | 27+++++++++++----------------
Mascii/strcmp.ha | 6+++---
Aascii/valid.ha | 11+++++++++++
Mencoding/base32/base32.ha | 2+-
Mencoding/base64/base64.ha | 2+-
Mfnmatch/fnmatch.ha | 2+-
Mmime/parse.ha | 2+-
7 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/ascii/ctype.ha b/ascii/ctype.ha @@ -34,56 +34,53 @@ const cclass: []u8 = [ // Returns true if an ASCII character is a letter. export fn isalpha(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&(U|L) > 0; + if (!valid(c)) false else cclass[c: u32]&(U|L) > 0; // Returns true if an ASCII character is uppercase. export fn isupper(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&U > 0; + if (!valid(c)) false else cclass[c: u32]&U > 0; // Returns true if an ASCII character is lowercase. export fn islower(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&L > 0; + if (!valid(c)) false else cclass[c: u32]&L > 0; // Returns true if an ASCII character is a digit. export fn isdigit(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&N > 0; + if (!valid(c)) false else cclass[c: u32]&N > 0; // Returns true if an ASCII character is a hexadecimal digit. export fn isxdigit(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&X > 0; + if (!valid(c)) false else cclass[c: u32]&X > 0; // Returns true if an ASCII character is a white-space character - // one of '\f', '\n', '\r', '\t', '\v', ' '. export fn isspace(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&S > 0; + if (!valid(c)) false else cclass[c: u32]&S > 0; // Returns true if an ASCII character is punctuation. export fn ispunct(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&P > 0; + if (!valid(c)) false else cclass[c: u32]&P > 0; // Returns true if an ASCII character is alphanumeric. export fn isalnum(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&(U|L|N) > 0; + if (!valid(c)) false else cclass[c: u32]&(U|L|N) > 0; // Returns true if an ASCII character is printable. export fn isprint(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&(P|U|L|N|B) > 0; + if (!valid(c)) false else cclass[c: u32]&(P|U|L|N|B) > 0; // Returns true if an ASCII character is any printable character other than // space. export fn isgraph(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&(P|U|L|N) > 0; + if (!valid(c)) false else cclass[c: u32]&(P|U|L|N) > 0; // Returns true if an ASCII character is a control character. export fn iscntrl(c: rune) bool = - if (!isascii(c)) false else cclass[c: u32]&C > 0; + if (!valid(c)) false else cclass[c: u32]&C > 0; // Returns true if a rune is a space or a tab. export fn isblank(c: rune) bool = (c == ' ' || c == '\t'); -// Returns true if a rune is a valid ASCII character. -export fn isascii(c: rune) bool = c: u32 <= 0o177; - // Returns the uppercase form of an ASCII character, or the original character // if it was not a lowercase letter (or was not ASCII). export fn toupper(c: rune) rune = { @@ -105,8 +102,6 @@ export fn tolower(c: rune) rune = { assert(isspace(' ') && !isspace('x') && !isspace('こ')); assert(isalnum('a') && isalnum('8') && !isalnum('こ')); assert(!ispunct('\0') && iscntrl('\b')); - assert(isascii('a') && isascii('\0') && isascii('\x7F')); - assert(!isascii('\x80') && !isascii('こ')); assert(tolower('A') == 'a' && tolower('こ') == 'こ'); assert(isblank(' ') && isblank('\t') && !isblank('6')); }; diff --git a/ascii/strcmp.ha b/ascii/strcmp.ha @@ -20,7 +20,7 @@ export fn strcmp(a: str, b: str) (int | void) = { case void => break; case let r: rune => - if (!isascii(r)) return; + if (!valid(r)) return; }; return -1; }; @@ -39,7 +39,7 @@ export fn strcmp(a: str, b: str) (int | void) = { case let r: rune => yield r; }; - if (!isascii(ra) || !isascii(rb)) { + if (!valid(ra) || !valid(rb)) { return; }; if (ra != rb) { @@ -67,7 +67,7 @@ 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; }; diff --git a/ascii/valid.ha b/ascii/valid.ha @@ -0,0 +1,11 @@ +// License: MPL-2.0 +// (c) 2022 Sebastian <sebastian@sebsite.pw> +// (c) 2021 Drew Devault <sir@cmpwn.com> + +// Returns true if a rune is a valid ASCII character. +export fn valid(c: rune) bool = c: u32 <= 0o177; + +@test fn valid() void = { + assert(valid('a') && valid('\0') && valid('\x7F')); + assert(!valid('\x80') && !valid('こ')); +}; diff --git a/encoding/base32/base32.ha b/encoding/base32/base32.ha @@ -29,7 +29,7 @@ export fn encoding_init(enc: *encoding, alphabet: str) void = { assert(len(alphabet) == 32); for (let i: u8 = 0; i < 32; i += 1) { const ch = alphabet[i]; - assert(ascii::isascii(ch: u32: rune)); + assert(ascii::valid(ch: u32: rune)); enc.encmap[i] = ch; enc.decmap[ch] = i; enc.valid[ch] = true; diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -34,7 +34,7 @@ export fn encoding_init(enc: *encoding, alphabet: str) void = { assert(len(alphabet) == 64); for (let i: u8 = 0; i < 64; i += 1) { const ch = alphabet[i]; - assert(ascii::isascii(ch: u32: rune)); + assert(ascii::valid(ch: u32: rune)); enc.encmap[i] = ch; enc.decmap[ch] = i; enc.valid[ch] = true; diff --git a/fnmatch/fnmatch.ha b/fnmatch/fnmatch.ha @@ -312,7 +312,7 @@ fn match_ctype(it: *strings::iterator, c: rune) (bool | errors::invalid) = { let i = 0z; for (let r = '\0'; r != ':'; i+= 1) { r = advance_or_err(it)?; - if (!ascii::isascii(r)) { + if (!ascii::valid(r)) { return errors::invalid; }; }; diff --git a/mime/parse.ha b/mime/parse.ha @@ -87,7 +87,7 @@ fn typevalid(in: str) (void | errors::invalid) = { case void => break; }; - if (!ascii::isascii(rn) || rn == ' ' + if (!ascii::valid(rn) || rn == ' ' || ascii::iscntrl(rn) || strings::contains(tspecial, rn)) { return errors::invalid;