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.
| @@ -621,26 +621,6 @@ | ||
| 621 | 621 | |
| 622 | 622 | Met in rwalk's `Rwfiles` and rrem's `rre`. |
| 623 | 623 | |
| 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 | - | |
| 644 | 624 | ## The text of an errno |
| 645 | 625 | |
| 646 | 626 | `Sys` returns minus an errno, and a tool that tells the user what |
| @@ -674,23 +654,7 @@ | ||
| 674 | 654 | It breaks no program. |
| 675 | 655 | |
| 676 | 656 | 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. | |
| 683 | 657 | |
| 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 | - | |
| 694 | 658 | ## A hint for a hexadecimal name |
| 695 | 659 | |
| 696 | 660 | `FFFDH`, meant as a number, is an identifier, and the error is "FFFDH |
| @@ -701,3 +665,19 @@ | ||
| 701 | 665 | program means changes. |
| 702 | 666 | |
| 703 | 667 | 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. |
| @@ -537,6 +537,7 @@ | ||
| 537 | 537 | proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) |
| 538 | 538 | proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 539 | 539 | 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 *) | |
| 540 | 541 | proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) |
| 541 | 542 | proc hasPrefix*(s, p: []U8): BOOL; |
| 542 | 543 | proc hasSuffix*(s, x: []U8): BOOL; |
| @@ -545,6 +546,11 @@ | ||
| 545 | 546 | proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *) |
| 546 | 547 | proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *) |
| 547 | 548 | 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 *) | |
| 548 | 554 | proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) |
| 549 | 555 | proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) |
| 550 | 556 | proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) |
| @@ -684,10 +690,11 @@ | ||
| 684 | 690 | |
| 685 | 691 | A string is bytes, and `Str` takes no more of Unicode than UTF-8: |
| 686 | 692 | `lower`, `upper` and `eqFold` change and match ASCII letters alone, |
| 693 | +the character tests know ASCII alone, | |
| 687 | 694 | and nothing says how wide a character is on a terminal, nor which |
| 688 | 695 | spellings of a text are the same. |
| 689 | 696 | |
| 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 | |
| 691 | 698 | tab, a line feed, a carriage return, a vertical tab or a form feed. |
| 692 | 699 | `cut` gives what comes before the first `sep` in `s`, what comes after |
| 693 | 700 | it, and `true`; or `s`, an empty string and `false` if `s` has no |
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | (* Operations on strings, which are []U8. = and # compare them; this |
| 2 | 2 | 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. *) | |
| 7 | 8 | module Str; |
| 8 | 9 | import Mem, Heap, Rt, Bytes, Buf; |
| 9 | 10 | |
| @@ -86,6 +87,16 @@ | ||
| 86 | 87 | return Bytes.find(s, sub); |
| 87 | 88 | end; |
| 88 | 89 | |
| 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 | + | |
| 89 | 100 | (* Where sub last starts in s, or -1. *) |
| 90 | 101 | proc findLast*(s, sub: []U8): INT; |
| 91 | 102 | var i: INT; |
| @@ -110,17 +121,47 @@ | ||
| 110 | 121 | |
| 111 | 122 | (* Whether c is a space: a blank, a tab, a line feed, a vertical tab, a |
| 112 | 123 | form feed or a carriage return. *) |
| 113 | -proc space(c: U8): BOOL; | |
| 124 | +proc isSpace*(c: U8): BOOL; | |
| 114 | 125 | begin |
| 115 | 126 | return (c = ' ') or (c >= 9) and (c <= 13); |
| 116 | 127 | end; |
| 117 | 128 | |
| 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 | + | |
| 118 | 159 | (* s without the spaces at its start. *) |
| 119 | 160 | proc trimLeft*(s: []U8): []U8; |
| 120 | 161 | var i: INT; |
| 121 | 162 | begin |
| 122 | 163 | 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; | |
| 124 | 165 | return s[i..LEN(s)]; |
| 125 | 166 | end; |
| 126 | 167 | |
| @@ -129,7 +170,7 @@ | ||
| 129 | 170 | var n: INT; |
| 130 | 171 | begin |
| 131 | 172 | 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; | |
| 133 | 174 | return s[0..n]; |
| 134 | 175 | end; |
| 135 | 176 | |
| @@ -157,7 +198,7 @@ | ||
| 157 | 198 | begin |
| 158 | 199 | s := trimLeft(s); |
| 159 | 200 | 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; | |
| 161 | 202 | return s[0..i], s[i..LEN(s)]; |
| 162 | 203 | end; |
| 163 | 204 | |
| @@ -336,12 +377,8 @@ | ||
| 336 | 377 | begin |
| 337 | 378 | if LEN(s) = 0 then return 0, false; end; |
| 338 | 379 | 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; | |
| 345 | 382 | if v shr 60 # 0 then return 0, false; end; |
| 346 | 383 | v := v shl 4 | U64(d); |
| 347 | 384 | end; |
| @@ -524,6 +561,22 @@ | ||
| 524 | 561 | Buf.done(ADR(b)); |
| 525 | 562 | end; |
| 526 | 563 | |
| 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 | + | |
| 527 | 580 | proc testToInt; |
| 528 | 581 | var n: I64; ok: BOOL; |
| 529 | 582 | begin |
| @@ -592,6 +645,9 @@ | ||
| 592 | 645 | if (find("hello", "ll") # 2) or (find("hello", "x") # -1) or (find("", "") # 0) |
| 593 | 646 | or (find("ab", "abc") # -1) then Rt.stop("Str.find"); end; |
| 594 | 647 | 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; | |
| 595 | 651 | testParts; |
| 596 | 652 | testUtf8; |
| 597 | 653 | testToInt; |
| @@ -1,10 +1,11 @@ | ||
| 1 | 1 | module Str; |
| 2 | 2 | Operations on strings, which are []U8. = and # compare them; this |
| 3 | 3 | 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. | |
| 8 | 9 | |
| 9 | 10 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); |
| 10 | 11 | s's bytes, copied into memory from a, to keep after s's own are |
| @@ -29,6 +30,10 @@ | ||
| 29 | 30 | proc find*(s, sub: []U8): INT; |
| 30 | 31 | Where sub first starts in s, or -1. |
| 31 | 32 | |
| 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 | + | |
| 32 | 37 | proc findLast*(s, sub: []U8): INT; |
| 33 | 38 | Where sub last starts in s, or -1. |
| 34 | 39 | |
| @@ -37,6 +42,23 @@ | ||
| 37 | 42 | |
| 38 | 43 | proc hasSuffix*(s, x: []U8): BOOL; |
| 39 | 44 | 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. | |
| 40 | 62 | |
| 41 | 63 | proc trimLeft*(s: []U8): []U8; |
| 42 | 64 | s without the spaces at its start. |