| 1 | module Str; |
| 2 | Operations on strings, which are []const U8. = and # compare them; this |
| 3 | module orders, searches and hashes them, trims them and takes them |
| 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. |
| 9 | |
| 10 | proc copy*(a: ^Mem.Allocator; s: []const U8): ([]U8, BOOL); |
| 11 | s's bytes, copied into memory from a, to keep after s's own are |
| 12 | gone; "" and false if a has no room. |
| 13 | |
| 14 | proc free*(a: ^Mem.Allocator; s: []U8); |
| 15 | Gives back to a a copy that copy made from it; "", which copy gives |
| 16 | for an empty string, was never a's, and is left alone. |
| 17 | |
| 18 | proc cmp*(a, b: []const U8): INT; |
| 19 | -1, 0 or 1 as a sorts before, with or after b, byte by byte. |
| 20 | |
| 21 | proc eq*(a, b: []const U8): BOOL; |
| 22 | Whether a and b hold the same bytes: a = b, as a procedure value. |
| 23 | |
| 24 | proc eqFold*(a, b: []const U8): BOOL; |
| 25 | Whether a and b are the same but for the case of ASCII letters. |
| 26 | |
| 27 | proc hash*(s: []const U8): U64; |
| 28 | FNV-1a, 64 bits. |
| 29 | |
| 30 | proc find*(s, sub: []const U8): INT; |
| 31 | Where sub first starts in s, or -1. |
| 32 | |
| 33 | proc findFrom*(s, sub: []const 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 | |
| 37 | proc findLast*(s, sub: []const U8): INT; |
| 38 | Where sub last starts in s, or -1. |
| 39 | |
| 40 | proc hasPrefix*(s, p: []const U8): BOOL; |
| 41 | Whether s starts with p. |
| 42 | |
| 43 | proc hasSuffix*(s, x: []const U8): BOOL; |
| 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. |
| 62 | |
| 63 | proc trimLeft*(s: []const U8): []const U8; |
| 64 | s without the spaces at its start. |
| 65 | |
| 66 | proc trimRight*(s: []const U8): []const U8; |
| 67 | s without the spaces at its end. |
| 68 | |
| 69 | proc trim*(s: []const U8): []const U8; |
| 70 | s without the spaces at either end. |
| 71 | |
| 72 | proc cut*(s, sep: []const U8): ([]const U8, []const U8, BOOL); |
| 73 | What comes before the first sep in s, what comes after it, and true; |
| 74 | or s, an empty string and false if s has no sep. |
| 75 | |
| 76 | proc field*(s: []const U8): ([]const U8, []const U8); |
| 77 | The first word of s, the bytes after the spaces at its start up to |
| 78 | the next space or the end, and the rest of s from there; two empty |
| 79 | strings if s holds only spaces. |
| 80 | |
| 81 | proc lower*(s: []U8); |
| 82 | s's ASCII capitals made small, in place. |
| 83 | |
| 84 | proc upper*(s: []U8); |
| 85 | s's small ASCII letters made capitals, in place. |
| 86 | |
| 87 | proc join*(var b: Buf.Buf; parts: [][]const U8; sep: []const U8); |
| 88 | The parts added to b, with sep between each two. |
| 89 | |
| 90 | proc replace*(var b: Buf.Buf; s, old, new: []const U8); |
| 91 | s added to b, with new for each old in it, from the left, none |
| 92 | overlapping another; s as it is if old is empty. |
| 93 | |
| 94 | proc encode*(c: I32): ([4]U8, INT); |
| 95 | c in UTF-8: its bytes, and how many. A number that isn't a |
| 96 | character, a surrogate or one past 10FFFFH, becomes FFFDH, the |
| 97 | character that stands for a wrong one. |
| 98 | |
| 99 | proc decode*(s: []const U8): (I32, INT); |
| 100 | The character s starts with, and its length in bytes: FFFDH and 1 |
| 101 | if its first byte doesn't start one rightly, being a continuation, |
| 102 | or followed by too few continuations, or spelling a surrogate or a |
| 103 | character in more bytes than it needs; 0 and 0 if s is empty. |
| 104 | |
| 105 | proc decodeLast*(s: []const U8): (I32, INT); |
| 106 | The character s ends with, and its length in bytes, read as decode |
| 107 | reads one: FFFDH and 1 if the last byte doesn't end one rightly; 0 |
| 108 | and 0 if s is empty. |
| 109 | |
| 110 | proc next*(s: []const U8; i: INT): (I32, INT); |
| 111 | The character at i in s, as decode reads it, and where the one after |
| 112 | it starts; 0 and -1 if i is at the end or past it. |
| 113 | |
| 114 | proc chars*(s: []const U8): INT; |
| 115 | The characters in s, as decode reads them, each byte that doesn't |
| 116 | start one rightly counted as one. |
| 117 | |
| 118 | proc valid*(s: []const U8): BOOL; |
| 119 | Whether s is UTF-8: decode finds no byte that doesn't start a |
| 120 | character rightly. |
| 121 | |
| 122 | proc toInt*(s: []const U8): (I64, BOOL); |
| 123 | s as a decimal number, with a - or + before it if any, and nothing |
| 124 | else; false if it isn't one, or an I64 can't hold it. |
| 125 | |
| 126 | proc toHex*(s: []const U8): (U64, BOOL); |
| 127 | s in hexadecimal, digits only, capitals or not; false if it isn't |
| 128 | that, or a U64 can't hold it. |
| 129 | |
| 130 | const Base64* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 131 | The digits of RFC 4648: section 4's base64, and section 5's, which |
| 132 | has - and _ for + and / and is safe in a URL or a file name. |
| 133 | |
| 134 | const Base64Url* = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 135 | |
| 136 | proc base64Size*(n: INT): INT; |
| 137 | The bytes base64 makes of n bytes: 4 for each 3, or part of 3. |
| 138 | |
| 139 | proc base64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): INT; |
| 140 | s in base64 into out, which holds base64Size(LEN(s)) bytes at least; |
| 141 | a shorter out stops the program at the byte it lacks, as any slice |
| 142 | does. alphabet holds the 64 digits (Base64 or Base64Url, or one's |
| 143 | own). A last group of 1 or 2 bytes is padded with = to four if pad, |
| 144 | and else has 2 or 3 digits. The bytes written. |
| 145 | |
| 146 | proc base64*(s: []const U8; out: []U8): INT; |
| 147 | s in base64 (RFC 4648, section 4), padded, into out as base64With |
| 148 | writes it. |
| 149 | |
| 150 | proc unbase64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): (INT, BOOL); |
| 151 | s, base64 with the digits of alphabet, decoded into out, which holds |
| 152 | (LEN(s) + 3) div 4 * 3 bytes at least; a shorter out stops the |
| 153 | program at the byte it lacks, as any slice does. The bytes written, |
| 154 | and true; or 0 and false, with out left in no state to use, if s is |
| 155 | not base64 as the RFC writes it and in no other way. With pad: its |
| 156 | length a multiple of 4, every byte a digit but the last one or two, |
| 157 | which may be = (and then both of the last two are, or the last |
| 158 | alone). Without: no =, and a length that is not 1 more than a |
| 159 | multiple of 4. Either way the bits of the last digit that no byte |
| 160 | uses are 0, so a byte string has the one spelling that base64 gives |
| 161 | it (RFC 4648, section 3.5 allows a decoder to take that). Line |
| 162 | breaks, and padding or its lack, are the caller's to mend first. |
| 163 | |
| 164 | proc unbase64*(s: []const U8; out: []U8): (INT, BOOL); |
| 165 | s, base64 (RFC 4648, section 4), padded, decoded into out as |
| 166 | unbase64With reads it; out holds LEN(s) div 4 * 3 bytes at least. |
| 167 | |
| 168 | proc toU64*(s: []const U8): (U64, BOOL); |
| 169 | s as a decimal number, digits only; false if it isn't one, or a U64 |
| 170 | can't hold it. |
| 171 | |
| 172 | proc fromC*(p: ^U8): []const U8; |
| 173 | The bytes at p up to its zero byte, not copied; "" for nil. |
| 174 | |
| 175 | proc toC*(s: []const U8; out: []U8): ^U8; |
| 176 | s copied into out with a zero byte after it, for C: out holds |
| 177 | LEN(s) + 1 bytes at least, or the program stops at the byte it |
| 178 | lacks, as any slice does. The pointer C is given, SYSTEM.PTR(out). |
| 179 | |
| 180 | proc fromInt*(n: I64): ([20]U8, INT); |
| 181 | n in decimal, as Out.int writes it: its bytes, the first of the 20, |
| 182 | and how many. |
| 183 | |
| 184 | proc le*(s: []const U8): U64; |
| 185 | The bytes of s, at most 8, as an unsigned number, the first byte |
| 186 | the lowest: how a file or a message in little-endian order is read, |
| 187 | whatever the machine's own order. |
| 188 | |
| 189 | proc be*(s: []const U8): U64; |
| 190 | As le, the first byte the highest: big-endian, network order. |
| 191 | |
| 192 | proc putLE*(s: []U8; v: U64); |
| 193 | v into the bytes of s, at most 8, the lowest first; what doesn't |
| 194 | fit is left out. |
| 195 | |
| 196 | proc putBE*(s: []U8; v: U64); |
| 197 | As putLE, the highest first. |
| 198 | |
| 199 | proc main*; |
| 200 | The module's test: stops the program if something is wrong. |