From f703dc3f72698ee89b12fb25b1d172f2f68d5f26 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:58:08 +0100 Subject: [PATCH] Str: isDigit, isLetter, isSpace, isPunct, hexVal and findFrom ASCII tests every parser wrote for itself; isSpace is the private space exported, toHex reads its digits by hexVal. findFrom searches from a place without the slice and the sum. Library growth that breaks no program, met in rrem and rwalk. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 7fb2207..95c113f 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -621,26 +621,6 @@ Met in rwalk's `Rwfiles` and rrem's `rre`. -## Character tests in `Str` - -`Str` has `lower` and `upper`, ASCII, and inside it the private -`small` and `space`. Every parser writes its own `isDigit`, -`isLetter`, `isSpace`, `isPunct` and `hexVal` beside them. - -The proposal, in `Str`, all ASCII as `lower` and `upper` are: - -``` -proc isDigit*(c: U8): BOOL; (* 0 to 9 *) -proc isLetter*(c: U8): BOOL; (* A to Z, a to z *) -proc isSpace*(c: U8): BOOL; (* space, tab, line feed, carriage return, form feed, vertical tab *) -proc isPunct*(c: U8): BOOL; (* the printable characters that are neither letters, digits nor space *) -proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) -``` - -`isSpace` is today's `space`, exported. It breaks no program. - -Met in rrem's parser. - ## The text of an errno `Sys` returns minus an errno, and a tool that tells the user what @@ -674,23 +654,7 @@ It breaks no program. Met in rwalk's tests. - -## Finding from a place - -`Str.find(s, sub)` searches the whole of `s`. To search from `i`, a -program writes `Str.find(s[i..LEN(s)], sub)` and adds `i` back when -the result isn't -1, which is clumsy and easy to get off by one. -The proposal, in `Str`: - -``` -proc findFrom*(s, sub: []U8; from: INT): INT; (* the first place at or after from where sub is in s, or -1 *) -``` - -It breaks no program. - -Met in rwalk's `Rwglob`. - ## A hint for a hexadecimal name `FFFDH`, meant as a number, is an identifier, and the error is "FFFDH @@ -701,3 +665,19 @@ program means changes. Met in rrem's parser, writing U+FFFD. + +## Code fences that say they are raz + +The report, the library's book and the README fence their code with +bare lines of ``` (about 200 fences), so nothing that renders them can +tell raz from a shell session, a program's output or a grammar. rless +colours a fence marked `raz`, ```` ```raz ````, as the editors colour +raz; GitHub would too, given a grammar. A bare fence is one colour. + +The proposal: a fence around raz code says `raz`; one around anything +else stays bare. `tools/Doc` finds the report's examples by a fence +line that is exactly ```, so it takes ```` ```raz ```` as well, and +finds the same examples as before. Nothing in the language or the +library changes; the documents read the same as text. + +Met in rless, whose pager shows the report. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 729fae8..e1f5acb 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -537,6 +537,7 @@ proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) +proc findFrom*(s, sub: []U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *) proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) proc hasPrefix*(s, p: []U8): BOOL; proc hasSuffix*(s, x: []U8): BOOL; @@ -545,6 +546,11 @@ proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *) proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *) proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *) +proc isDigit*(c: U8): BOOL; (* 0 to 9 *) +proc isLetter*(c: U8): BOOL; (* A to Z, a to z *) +proc isSpace*(c: U8): BOOL; (* a space, as trim takes it *) +proc isPunct*(c: U8): BOOL; (* printable, and neither a letter, a digit nor the blank *) +proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) @@ -684,10 +690,11 @@ A string is bytes, and `Str` takes no more of Unicode than UTF-8: `lower`, `upper` and `eqFold` change and match ASCII letters alone, +the character tests know ASCII alone, and nothing says how wide a character is on a terminal, nor which spellings of a text are the same. -A space, to `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a +A space, to `isSpace`, `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a tab, a line feed, a carriage return, a vertical tab or a form feed. `cut` gives what comes before the first `sep` in `s`, what comes after it, and `true`; or `s`, an empty string and `false` if `s` has no diff --git a/lib/Str.raz b/lib/Str.raz index 04aad28..4896ce6 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -1,9 +1,10 @@ (* Operations on strings, which are []U8. = and # compare them; this module orders, searches and hashes them, trims them and takes them - apart, changes the case of their ASCII letters, joins them and - replaces in them, reads and writes their UTF-8, reads a number from - one and writes one, and copies one to keep. A part it gives is a - slice of the string it was given, with no memory of its own. *) + apart, tells an ASCII character's kind and changes a letter's case, + joins them and replaces in them, reads and writes their UTF-8, reads + a number from one and writes one, and copies one to keep. A part it + gives is a slice of the string it was given, with no memory of its + own. *) module Str; import Mem, Heap, Rt, Bytes, Buf; @@ -86,6 +87,16 @@ return Bytes.find(s, sub); end; +(* Where sub first starts in s at or after from, or -1; from is at + most LEN(s). *) +proc findFrom*(s, sub: []U8; from: INT): INT; +var i: INT; +begin + i := Bytes.find(s[from..LEN(s)], sub); + if i < 0 then return -1; end; + return from + i; +end; + (* Where sub last starts in s, or -1. *) proc findLast*(s, sub: []U8): INT; var i: INT; @@ -110,17 +121,47 @@ (* Whether c is a space: a blank, a tab, a line feed, a vertical tab, a form feed or a carriage return. *) -proc space(c: U8): BOOL; +proc isSpace*(c: U8): BOOL; begin return (c = ' ') or (c >= 9) and (c <= 13); end; +(* Whether c is an ASCII digit, 0 to 9. *) +proc isDigit*(c: U8): BOOL; +begin + return (c >= '0') and (c <= '9'); +end; + +(* Whether c is an ASCII letter, A to Z or a to z. *) +proc isLetter*(c: U8): BOOL; +begin + return (small(c) >= 'a') and (small(c) <= 'z'); +end; + +(* Whether c is printable ASCII and neither a letter, a digit nor the + blank: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. *) +proc isPunct*(c: U8): BOOL; +begin + return (c > ' ') and (c < 127) and not isLetter(c) and not isDigit(c); +end; + +(* c's value as a hexadecimal digit, capital or small; -1 if it isn't one. *) +proc hexVal*(c: U8): INT; +begin + match c of + | '0'..'9': return INT(c) - '0'; + | 'A'..'F': return INT(c) - 'A' + 10; + | 'a'..'f': return INT(c) - 'a' + 10; + | else: return -1; + end; +end; + (* s without the spaces at its start. *) proc trimLeft*(s: []U8): []U8; var i: INT; begin i := 0; - while (i < LEN(s)) and space(s[i]) do INC(i); end; + while (i < LEN(s)) and isSpace(s[i]) do INC(i); end; return s[i..LEN(s)]; end; @@ -129,7 +170,7 @@ var n: INT; begin n := LEN(s); - while (n > 0) and space(s[n - 1]) do DEC(n); end; + while (n > 0) and isSpace(s[n - 1]) do DEC(n); end; return s[0..n]; end; @@ -157,7 +198,7 @@ begin s := trimLeft(s); i := 0; - while (i < LEN(s)) and not space(s[i]) do INC(i); end; + while (i < LEN(s)) and not isSpace(s[i]) do INC(i); end; return s[0..i], s[i..LEN(s)]; end; @@ -336,12 +377,8 @@ begin if LEN(s) = 0 then return 0, false; end; for i := 0 to LEN(s) - 1 do - match s[i] of - | '0'..'9': d := INT(s[i]) - '0'; - | 'A'..'F': d := INT(s[i]) - 'A' + 10; - | 'a'..'f': d := INT(s[i]) - 'a' + 10; - | else: return 0, false; - end; + d := hexVal(s[i]); + if d < 0 then return 0, false; end; if v shr 60 # 0 then return 0, false; end; v := v shl 4 | U64(d); end; @@ -524,6 +561,22 @@ Buf.done(ADR(b)); end; +proc testChars; +var c: INT; +begin + for c := 0 to 255 do + if isDigit(U8(c)) # ((c >= 48) and (c <= 57)) then Rt.stop("Str.isDigit"); end; + if isLetter(U8(c)) # ((c >= 65) and (c <= 90) or (c >= 97) and (c <= 122)) then Rt.stop("Str.isLetter"); end; + if isPunct(U8(c)) # ((c > 32) and (c < 127) and not isLetter(U8(c)) and not isDigit(U8(c))) then + Rt.stop("Str.isPunct"); + end; + end; + if not isSpace(' ') or not isSpace(9) or not isSpace(13) or isSpace(14) or isSpace('a') then Rt.stop("Str.isSpace"); end; + if isPunct(' ') or not isPunct('~') or not isPunct('!') or isPunct(127) then Rt.stop("Str.isPunct: ends"); end; + if (hexVal('0') # 0) or (hexVal('9') # 9) or (hexVal('a') # 10) or (hexVal('F') # 15) or (hexVal('g') # -1) + or (hexVal('@') # -1) then Rt.stop("Str.hexVal"); end; +end; + proc testToInt; var n: I64; ok: BOOL; begin @@ -592,6 +645,9 @@ if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; + if (findFrom("abcabc", "bc", 1) # 1) or (findFrom("abcabc", "bc", 2) # 4) or (findFrom("abc", "c", 3) # -1) + or (findFrom("abc", "", 3) # 3) then Rt.stop("Str.findFrom"); end; + testChars; testParts; testUtf8; testToInt; diff --git a/test/doc/Str.expect b/test/doc/Str.expect index 5b46c08..dffd532 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -1,10 +1,11 @@ module Str; Operations on strings, which are []U8. = and # compare them; this module orders, searches and hashes them, trims them and takes them - apart, changes the case of their ASCII letters, joins them and - replaces in them, reads and writes their UTF-8, reads a number from - one and writes one, and copies one to keep. A part it gives is a - slice of the string it was given, with no memory of its own. + apart, tells an ASCII character's kind and changes a letter's case, + joins them and replaces in them, reads and writes their UTF-8, reads + a number from one and writes one, and copies one to keep. A part it + gives is a slice of the string it was given, with no memory of its + own. proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); s's bytes, copied into memory from a, to keep after s's own are @@ -29,6 +30,10 @@ proc find*(s, sub: []U8): INT; Where sub first starts in s, or -1. +proc findFrom*(s, sub: []U8; from: INT): INT; + Where sub first starts in s at or after from, or -1; from is at + most LEN(s). + proc findLast*(s, sub: []U8): INT; Where sub last starts in s, or -1. @@ -37,6 +42,23 @@ proc hasSuffix*(s, x: []U8): BOOL; Whether s ends with x. + +proc isSpace*(c: U8): BOOL; + Whether c is a space: a blank, a tab, a line feed, a vertical tab, a + form feed or a carriage return. + +proc isDigit*(c: U8): BOOL; + Whether c is an ASCII digit, 0 to 9. + +proc isLetter*(c: U8): BOOL; + Whether c is an ASCII letter, A to Z or a to z. + +proc isPunct*(c: U8): BOOL; + Whether c is printable ASCII and neither a letter, a digit nor the + blank: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. + +proc hexVal*(c: U8): INT; + c's value as a hexadecimal digit, capital or small; -1 if it isn't one. proc trimLeft*(s: []U8): []U8; s without the spaces at its start.