raz / commit f703dc3f

commit
f703dc3f72698ee89b12fb25b1d172f2f68d5f26
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4fb20c27
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.

patch · log

4 files changed, +120 -55

PROPOSALS.md modified +16 -36

@@ -621,26 +621,6 @@
621621
622622 Met in rwalk's `Rwfiles` and rrem's `rre`.
623623
624-## Character tests in `Str`
625-
626-`Str` has `lower` and `upper`, ASCII, and inside it the private
627-`small` and `space`. Every parser writes its own `isDigit`,
628-`isLetter`, `isSpace`, `isPunct` and `hexVal` beside them.
629-
630-The proposal, in `Str`, all ASCII as `lower` and `upper` are:
631-
632-```
633-proc isDigit*(c: U8): BOOL; (* 0 to 9 *)
634-proc isLetter*(c: U8): BOOL; (* A to Z, a to z *)
635-proc isSpace*(c: U8): BOOL; (* space, tab, line feed, carriage return, form feed, vertical tab *)
636-proc isPunct*(c: U8): BOOL; (* the printable characters that are neither letters, digits nor space *)
637-proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *)
638-```
639-
640-`isSpace` is today's `space`, exported. It breaks no program.
641-
642-Met in rrem's parser.
643-
644624 ## The text of an errno
645625
646626 `Sys` returns minus an errno, and a tool that tells the user what
@@ -674,23 +654,7 @@
674654 It breaks no program.
675655
676656 Met in rwalk's tests.
677-
678-## Finding from a place
679-
680-`Str.find(s, sub)` searches the whole of `s`. To search from `i`, a
681-program writes `Str.find(s[i..LEN(s)], sub)` and adds `i` back when
682-the result isn't -1, which is clumsy and easy to get off by one.
683657
684-The proposal, in `Str`:
685-
686-```
687-proc findFrom*(s, sub: []U8; from: INT): INT; (* the first place at or after from where sub is in s, or -1 *)
688-```
689-
690-It breaks no program.
691-
692-Met in rwalk's `Rwglob`.
693-
694658 ## A hint for a hexadecimal name
695659
696660 `FFFDH`, meant as a number, is an identifier, and the error is "FFFDH
@@ -701,3 +665,19 @@
701665 program means changes.
702666
703667 Met in rrem's parser, writing U+FFFD.
668+
669+## Code fences that say they are raz
670+
671+The report, the library's book and the README fence their code with
672+bare lines of ``` (about 200 fences), so nothing that renders them can
673+tell raz from a shell session, a program's output or a grammar. rless
674+colours a fence marked `raz`, ```` ```raz ````, as the editors colour
675+raz; GitHub would too, given a grammar. A bare fence is one colour.
676+
677+The proposal: a fence around raz code says `raz`; one around anything
678+else stays bare. `tools/Doc` finds the report's examples by a fence
679+line that is exactly ```, so it takes ```` ```raz ```` as well, and
680+finds the same examples as before. Nothing in the language or the
681+library changes; the documents read the same as text.
682+
683+Met in rless, whose pager shows the report.

doc/raz-lib.md (history) modified +8 -1

@@ -537,6 +537,7 @@
537537 proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *)
538538 proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
539539 proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
540+proc findFrom*(s, sub: []U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *)
540541 proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)
541542 proc hasPrefix*(s, p: []U8): BOOL;
542543 proc hasSuffix*(s, x: []U8): BOOL;
@@ -545,6 +546,11 @@
545546 proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *)
546547 proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *)
547548 proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *)
549+proc isDigit*(c: U8): BOOL; (* 0 to 9 *)
550+proc isLetter*(c: U8): BOOL; (* A to Z, a to z *)
551+proc isSpace*(c: U8): BOOL; (* a space, as trim takes it *)
552+proc isPunct*(c: U8): BOOL; (* printable, and neither a letter, a digit nor the blank *)
553+proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *)
548554 proc lower*(s: []U8); (* s's ASCII capitals made small, in place *)
549555 proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *)
550556 proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *)
@@ -684,10 +690,11 @@
684690
685691 A string is bytes, and `Str` takes no more of Unicode than UTF-8:
686692 `lower`, `upper` and `eqFold` change and match ASCII letters alone,
693+the character tests know ASCII alone,
687694 and nothing says how wide a character is on a terminal, nor which
688695 spellings of a text are the same.
689696
690-A space, to `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a
697+A space, to `isSpace`, `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a
691698 tab, a line feed, a carriage return, a vertical tab or a form feed.
692699 `cut` gives what comes before the first `sep` in `s`, what comes after
693700 it, and `true`; or `s`, an empty string and `false` if `s` has no

lib/Str.raz (history) modified +70 -14

@@ -1,9 +1,10 @@
11 (* Operations on strings, which are []U8. = and # compare them; this
22 module orders, searches and hashes them, trims them and takes them
3- apart, changes the case of their ASCII letters, joins them and
4- replaces in them, reads and writes their UTF-8, reads a number from
5- one and writes one, and copies one to keep. A part it gives is a
6- slice of the string it was given, with no memory of its own. *)
3+ apart, tells an ASCII character's kind and changes a letter's case,
4+ joins them and replaces in them, reads and writes their UTF-8, reads
5+ a number from one and writes one, and copies one to keep. A part it
6+ gives is a slice of the string it was given, with no memory of its
7+ own. *)
78 module Str;
89 import Mem, Heap, Rt, Bytes, Buf;
910
@@ -86,6 +87,16 @@
8687 return Bytes.find(s, sub);
8788 end;
8889
90+(* Where sub first starts in s at or after from, or -1; from is at
91+ most LEN(s). *)
92+proc findFrom*(s, sub: []U8; from: INT): INT;
93+var i: INT;
94+begin
95+ i := Bytes.find(s[from..LEN(s)], sub);
96+ if i < 0 then return -1; end;
97+ return from + i;
98+end;
99+
89100 (* Where sub last starts in s, or -1. *)
90101 proc findLast*(s, sub: []U8): INT;
91102 var i: INT;
@@ -110,17 +121,47 @@
110121
111122 (* Whether c is a space: a blank, a tab, a line feed, a vertical tab, a
112123 form feed or a carriage return. *)
113-proc space(c: U8): BOOL;
124+proc isSpace*(c: U8): BOOL;
114125 begin
115126 return (c = ' ') or (c >= 9) and (c <= 13);
116127 end;
117128
129+(* Whether c is an ASCII digit, 0 to 9. *)
130+proc isDigit*(c: U8): BOOL;
131+begin
132+ return (c >= '0') and (c <= '9');
133+end;
134+
135+(* Whether c is an ASCII letter, A to Z or a to z. *)
136+proc isLetter*(c: U8): BOOL;
137+begin
138+ return (small(c) >= 'a') and (small(c) <= 'z');
139+end;
140+
141+(* Whether c is printable ASCII and neither a letter, a digit nor the
142+ blank: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. *)
143+proc isPunct*(c: U8): BOOL;
144+begin
145+ return (c > ' ') and (c < 127) and not isLetter(c) and not isDigit(c);
146+end;
147+
148+(* c's value as a hexadecimal digit, capital or small; -1 if it isn't one. *)
149+proc hexVal*(c: U8): INT;
150+begin
151+ match c of
152+ | '0'..'9': return INT(c) - '0';
153+ | 'A'..'F': return INT(c) - 'A' + 10;
154+ | 'a'..'f': return INT(c) - 'a' + 10;
155+ | else: return -1;
156+ end;
157+end;
158+
118159 (* s without the spaces at its start. *)
119160 proc trimLeft*(s: []U8): []U8;
120161 var i: INT;
121162 begin
122163 i := 0;
123- while (i < LEN(s)) and space(s[i]) do INC(i); end;
164+ while (i < LEN(s)) and isSpace(s[i]) do INC(i); end;
124165 return s[i..LEN(s)];
125166 end;
126167
@@ -129,7 +170,7 @@
129170 var n: INT;
130171 begin
131172 n := LEN(s);
132- while (n > 0) and space(s[n - 1]) do DEC(n); end;
173+ while (n > 0) and isSpace(s[n - 1]) do DEC(n); end;
133174 return s[0..n];
134175 end;
135176
@@ -157,7 +198,7 @@
157198 begin
158199 s := trimLeft(s);
159200 i := 0;
160- while (i < LEN(s)) and not space(s[i]) do INC(i); end;
201+ while (i < LEN(s)) and not isSpace(s[i]) do INC(i); end;
161202 return s[0..i], s[i..LEN(s)];
162203 end;
163204
@@ -336,12 +377,8 @@
336377 begin
337378 if LEN(s) = 0 then return 0, false; end;
338379 for i := 0 to LEN(s) - 1 do
339- match s[i] of
340- | '0'..'9': d := INT(s[i]) - '0';
341- | 'A'..'F': d := INT(s[i]) - 'A' + 10;
342- | 'a'..'f': d := INT(s[i]) - 'a' + 10;
343- | else: return 0, false;
344- end;
380+ d := hexVal(s[i]);
381+ if d < 0 then return 0, false; end;
345382 if v shr 60 # 0 then return 0, false; end;
346383 v := v shl 4 | U64(d);
347384 end;
@@ -524,6 +561,22 @@
524561 Buf.done(ADR(b));
525562 end;
526563
564+proc testChars;
565+var c: INT;
566+begin
567+ for c := 0 to 255 do
568+ if isDigit(U8(c)) # ((c >= 48) and (c <= 57)) then Rt.stop("Str.isDigit"); end;
569+ if isLetter(U8(c)) # ((c >= 65) and (c <= 90) or (c >= 97) and (c <= 122)) then Rt.stop("Str.isLetter"); end;
570+ if isPunct(U8(c)) # ((c > 32) and (c < 127) and not isLetter(U8(c)) and not isDigit(U8(c))) then
571+ Rt.stop("Str.isPunct");
572+ end;
573+ end;
574+ if not isSpace(' ') or not isSpace(9) or not isSpace(13) or isSpace(14) or isSpace('a') then Rt.stop("Str.isSpace"); end;
575+ if isPunct(' ') or not isPunct('~') or not isPunct('!') or isPunct(127) then Rt.stop("Str.isPunct: ends"); end;
576+ if (hexVal('0') # 0) or (hexVal('9') # 9) or (hexVal('a') # 10) or (hexVal('F') # 15) or (hexVal('g') # -1)
577+ or (hexVal('@') # -1) then Rt.stop("Str.hexVal"); end;
578+end;
579+
527580 proc testToInt;
528581 var n: I64; ok: BOOL;
529582 begin
@@ -592,6 +645,9 @@
592645 if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0)
593646 or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end;
594647 if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end;
648+ if (findFrom("abcabc", "bc", 1) # 1) or (findFrom("abcabc", "bc", 2) # 4) or (findFrom("abc", "c", 3) # -1)
649+ or (findFrom("abc", "", 3) # 3) then Rt.stop("Str.findFrom"); end;
650+ testChars;
595651 testParts;
596652 testUtf8;
597653 testToInt;

test/doc/Str.expect (history) modified +26 -4

@@ -1,10 +1,11 @@
11 module Str;
22 Operations on strings, which are []U8. = and # compare them; this
33 module orders, searches and hashes them, trims them and takes them
4- apart, changes the case of their ASCII letters, joins them and
5- replaces in them, reads and writes their UTF-8, reads a number from
6- one and writes one, and copies one to keep. A part it gives is a
7- slice of the string it was given, with no memory of its own.
4+ apart, tells an ASCII character's kind and changes a letter's case,
5+ joins them and replaces in them, reads and writes their UTF-8, reads
6+ a number from one and writes one, and copies one to keep. A part it
7+ gives is a slice of the string it was given, with no memory of its
8+ own.
89
910 proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL);
1011 s's bytes, copied into memory from a, to keep after s's own are
@@ -29,6 +30,10 @@
2930 proc find*(s, sub: []U8): INT;
3031 Where sub first starts in s, or -1.
3132
33+proc findFrom*(s, sub: []U8; from: INT): INT;
34+ Where sub first starts in s at or after from, or -1; from is at
35+ most LEN(s).
36+
3237 proc findLast*(s, sub: []U8): INT;
3338 Where sub last starts in s, or -1.
3439
@@ -37,6 +42,23 @@
3742
3843 proc hasSuffix*(s, x: []U8): BOOL;
3944 Whether s ends with x.
45+
46+proc isSpace*(c: U8): BOOL;
47+ Whether c is a space: a blank, a tab, a line feed, a vertical tab, a
48+ form feed or a carriage return.
49+
50+proc isDigit*(c: U8): BOOL;
51+ Whether c is an ASCII digit, 0 to 9.
52+
53+proc isLetter*(c: U8): BOOL;
54+ Whether c is an ASCII letter, A to Z or a to z.
55+
56+proc isPunct*(c: U8): BOOL;
57+ Whether c is printable ASCII and neither a letter, a digit nor the
58+ blank: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~.
59+
60+proc hexVal*(c: U8): INT;
61+ c's value as a hexadecimal digit, capital or small; -1 if it isn't one.
4062
4163 proc trimLeft*(s: []U8): []U8;
4264 s without the spaces at its start.