| ... | ... | @@ -0,0 +1,232 @@ |
| 1 | // Does NOT look at the locale the way C89's toupper(3), isspace() et cetera does. |
| 2 | // I could have taken only a u7 to make this clear, but it would be slower |
| 3 | // It is my opinion that encodings other than UTF-8 should not be supported. |
| 4 | // |
| 5 | // (and 128 bytes is not much to pay). |
| 6 | // Also does not handle Unicode character classes. |
| 7 | // |
| 8 | // https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/USASCII_code_chart.png/1200px-USASCII_code_chart.png |
| 9 | |
| 10 | const tIndex = enum(u3) { |
| 11 | Alpha, |
| 12 | Hex, |
| 13 | Space, |
| 14 | Digit, |
| 15 | Lower, |
| 16 | Upper, |
| 17 | // Ctrl, < 0x20 || == DEL |
| 18 | // Print, = Graph || == ' '. NOT '\t' et cetera |
| 19 | Punct, |
| 20 | Graph, |
| 21 | //ASCII, | ~0b01111111 |
| 22 | //isBlank, == ' ' || == '\x09' |
| 23 | }; |
| 24 | |
| 25 | const combinedTable = init: { |
| 26 | comptime var table: [256]u8 = undefined; |
| 27 | |
| 28 | const std = @import("std"); |
| 29 | const mem = std.mem; |
| 30 | |
| 31 | const alpha = []u1{ |
| 32 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 |
| 33 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 34 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 35 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 36 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 37 | |
| 38 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 39 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 40 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 0, 1, 1, 1, 1, 1, |
| 112 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 113 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 114 | }; |
| 115 | const graph = []u1{ |
| 116 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 |
| 117 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 118 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 119 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 120 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 121 | |
| 122 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 123 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 124 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 125 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, |
| 126 | }; |
| 127 | |
| 128 | comptime var i = 0; |
| 129 | inline while (i < 128) : (i += 1) { |
| 130 | table[i] = |
| 131 | u8(alpha[i]) << @enumToInt(tIndex.Alpha) | |
| 132 | u8(hex[i]) << @enumToInt(tIndex.Hex) | |
| 133 | u8(space[i]) << @enumToInt(tIndex.Space) | |
| 134 | u8(digit[i]) << @enumToInt(tIndex.Digit) | |
| 135 | u8(lower[i]) << @enumToInt(tIndex.Lower) | |
| 136 | u8(upper[i]) << @enumToInt(tIndex.Upper) | |
| 137 | u8(punct[i]) << @enumToInt(tIndex.Punct) | |
| 138 | u8(graph[i]) << @enumToInt(tIndex.Graph); |
| 139 | } |
| 140 | mem.set(u8, table[128..256], 0); |
| 141 | break :init table; |
| 142 | }; |
| 143 | |
| 144 | fn inTable(c: u8, t: tIndex) bool { |
| 145 | return (combinedTable[c] & (u8(1) << @enumToInt(t))) != 0; |
| 146 | } |
| 147 | |
| 148 | pub fn isAlNum(c: u8) bool { |
| 149 | return (combinedTable[c] & ((u8(1) << @enumToInt(tIndex.Alpha)) | |
| 150 | u8(1) << @enumToInt(tIndex.Digit))) != 0; |
| 151 | } |
| 152 | |
| 153 | pub fn isAlpha(c: u8) bool { |
| 154 | return inTable(c, tIndex.Alpha); |
| 155 | } |
| 156 | |
| 157 | pub fn isCtrl(c: u8) bool { |
| 158 | return c < 0x20 or c == 127; //DEL |
| 159 | } |
| 160 | |
| 161 | pub fn isCntrl(c: u8) bool { |
| 162 | return isCtrl(c); |
| 163 | } |
| 164 | |
| 165 | pub fn isDigit(c: u8) bool { |
| 166 | return inTable(c, tIndex.Digit); |
| 167 | } |
| 168 | |
| 169 | pub fn isGraph(c: u8) bool { |
| 170 | return inTable(c, tIndex.Graph); |
| 171 | } |
| 172 | |
| 173 | pub fn isLower(c: u8) bool { |
| 174 | return inTable(c, tIndex.Lower); |
| 175 | } |
| 176 | |
| 177 | pub fn isPrint(c: u8) bool { |
| 178 | return inTable(c, tIndex.Graph) or c == ' '; |
| 179 | } |
| 180 | |
| 181 | pub fn isPunct(c: u8) bool { |
| 182 | return inTable(c, tIndex.Punct); |
| 183 | } |
| 184 | |
| 185 | pub fn isSpace(c: u8) bool { |
| 186 | return inTable(c, tIndex.Space); |
| 187 | } |
| 188 | |
| 189 | pub fn isUpper(c: u8) bool { |
| 190 | return inTable(c, tIndex.Upper); |
| 191 | } |
| 192 | |
| 193 | pub fn isXDigit(c: u8) bool { |
| 194 | return inTable(c, tIndex.Hex); |
| 195 | } |
| 196 | |
| 197 | pub fn isASCII(c: u8) bool { |
| 198 | return c < 128; |
| 199 | } |
| 200 | |
| 201 | pub fn isBlank(c: u8) bool { |
| 202 | return (c == ' ') or (c == '\x09'); |
| 203 | } |
| 204 | |
| 205 | pub fn toUpper(c: u8) u8 { |
| 206 | if (isLower(c)) { |
| 207 | return c - 0x20; |
| 208 | } else { |
| 209 | return c; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | pub fn toLower(c: u8) u8 { |
| 214 | if (isUpper(c)) { |
| 215 | return c + 0x20; |
| 216 | } else { |
| 217 | return c; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | test "ascii character classes" { |
| 222 | const std = @import("std"); |
| 223 | const testing = std.testing; |
| 224 | |
| 225 | testing.expect('C' == toUpper('c')); |
| 226 | testing.expect(':' == toUpper(':')); |
| 227 | testing.expect('\xab' == toUpper('\xab')); |
| 228 | testing.expect('c' == toLower('C')); |
| 229 | testing.expect(isAlpha('c')); |
| 230 | testing.expect(!isAlpha('5')); |
| 231 | testing.expect(isSpace(' ')); |
| 232 | } |