| ... | @@ -10,83 +10,10 @@ | ... | @@ -10,83 +10,10 @@ |
| 10 | | 10 | |
| 11 | const std = @import("std"); | 11 | const std = @import("std"); |
| 12 | | 12 | |
| 13 | // TODO: remove all decls marked as DEPRECATED after 0.10.0's release | | |
| 14 | | | |
| 15 | /// The C0 control codes of the ASCII encoding. | 13 | /// The C0 control codes of the ASCII encoding. |
| 16 | /// | 14 | /// |
| 17 | /// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `isControl`. | 15 | /// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `isControl` |
| 18 | pub const control_code = struct { | 16 | pub const control_code = struct { |
| 19 | // DEPRECATED: use the lowercase variant | | |
| 20 | pub const NUL = 0x00; | | |
| 21 | // DEPRECATED: use the lowercase variant | | |
| 22 | pub const SOH = 0x01; | | |
| 23 | // DEPRECATED: use the lowercase variant | | |
| 24 | pub const STX = 0x02; | | |
| 25 | // DEPRECATED: use the lowercase variant | | |
| 26 | pub const ETX = 0x03; | | |
| 27 | // DEPRECATED: use the lowercase variant | | |
| 28 | pub const EOT = 0x04; | | |
| 29 | // DEPRECATED: use the lowercase variant | | |
| 30 | pub const ENQ = 0x05; | | |
| 31 | // DEPRECATED: use the lowercase variant | | |
| 32 | pub const ACK = 0x06; | | |
| 33 | // DEPRECATED: use the lowercase variant | | |
| 34 | pub const BEL = 0x07; | | |
| 35 | // DEPRECATED: use the lowercase variant | | |
| 36 | pub const BS = 0x08; | | |
| 37 | // DEPRECATED: use `ht` | | |
| 38 | pub const TAB = 0x09; | | |
| 39 | // DEPRECATED: use the lowercase variant | | |
| 40 | pub const LF = 0x0A; | | |
| 41 | // DEPRECATED: use the lowercase variant | | |
| 42 | pub const VT = 0x0B; | | |
| 43 | // DEPRECATED: use the lowercase variant | | |
| 44 | pub const FF = 0x0C; | | |
| 45 | // DEPRECATED: use the lowercase variant | | |
| 46 | pub const CR = 0x0D; | | |
| 47 | // DEPRECATED: use the lowercase variant | | |
| 48 | pub const SO = 0x0E; | | |
| 49 | // DEPRECATED: use the lowercase variant | | |
| 50 | pub const SI = 0x0F; | | |
| 51 | // DEPRECATED: use the lowercase variant | | |
| 52 | pub const DLE = 0x10; | | |
| 53 | // DEPRECATED: use the lowercase variant | | |
| 54 | pub const DC1 = 0x11; | | |
| 55 | // DEPRECATED: use the lowercase variant | | |
| 56 | pub const DC2 = 0x12; | | |
| 57 | // DEPRECATED: use the lowercase variant | | |
| 58 | pub const DC3 = 0x13; | | |
| 59 | // DEPRECATED: use the lowercase variant | | |
| 60 | pub const DC4 = 0x14; | | |
| 61 | // DEPRECATED: use the lowercase variant | | |
| 62 | pub const NAK = 0x15; | | |
| 63 | // DEPRECATED: use the lowercase variant | | |
| 64 | pub const SYN = 0x16; | | |
| 65 | // DEPRECATED: use the lowercase variant | | |
| 66 | pub const ETB = 0x17; | | |
| 67 | // DEPRECATED: use the lowercase variant | | |
| 68 | pub const CAN = 0x18; | | |
| 69 | // DEPRECATED: use the lowercase variant | | |
| 70 | pub const EM = 0x19; | | |
| 71 | // DEPRECATED: use the lowercase variant | | |
| 72 | pub const SUB = 0x1A; | | |
| 73 | // DEPRECATED: use the lowercase variant | | |
| 74 | pub const ESC = 0x1B; | | |
| 75 | // DEPRECATED: use the lowercase variant | | |
| 76 | pub const FS = 0x1C; | | |
| 77 | // DEPRECATED: use the lowercase variant | | |
| 78 | pub const GS = 0x1D; | | |
| 79 | // DEPRECATED: use the lowercase variant | | |
| 80 | pub const RS = 0x1E; | | |
| 81 | // DEPRECATED: use the lowercase variant | | |
| 82 | pub const US = 0x1F; | | |
| 83 | // DEPRECATED: use the lowercase variant | | |
| 84 | pub const DEL = 0x7F; | | |
| 85 | // DEPRECATED: use the lowercase variant | | |
| 86 | pub const XON = 0x11; | | |
| 87 | // DEPRECATED: use the lowercase variant | | |
| 88 | pub const XOFF = 0x13; | | |
| 89 | | | |
| 90 | /// Null. | 17 | /// Null. |
| 91 | pub const nul = 0x00; | 18 | pub const nul = 0x00; |
| 92 | /// Start of Heading. | 19 | /// Start of Heading. |
| ... | @@ -161,211 +88,63 @@ pub const control_code = struct { | ... | @@ -161,211 +88,63 @@ pub const control_code = struct { |
| 161 | pub const xoff = dc3; | 88 | pub const xoff = dc3; |
| 162 | }; | 89 | }; |
| 163 | | 90 | |
| 164 | const tIndex = enum(u3) { | 91 | /// Returns whether the character is alphanumeric: A-Z, a-z, or 0-9. |
| 165 | Alpha, | | |
| 166 | Hex, | | |
| 167 | Space, | | |
| 168 | Digit, | | |
| 169 | Lower, | | |
| 170 | Upper, | | |
| 171 | // Ctrl, < 0x20 || == DEL | | |
| 172 | // Print, = Graph || == ' '. NOT '\t' et cetera | | |
| 173 | Punct, | | |
| 174 | Graph, | | |
| 175 | //ASCII, | ~0b01111111 | | |
| 176 | //isBlank, == ' ' || == '\x09' | | |
| 177 | }; | | |
| 178 | | | |
| 179 | const combinedTable = init: { | | |
| 180 | comptime var table: [256]u8 = undefined; | | |
| 181 | | | |
| 182 | const mem = std.mem; | | |
| 183 | | | |
| 184 | const alpha = [_]u1{ | | |
| 185 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 186 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 190 | | | |
| 191 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 192 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | | |
| 193 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 194 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | | |
| 195 | }; | | |
| 196 | const lower = [_]u1{ | | |
| 197 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 198 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 199 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 200 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 201 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 202 | | | |
| 203 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 204 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 205 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 206 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | | |
| 207 | }; | | |
| 208 | const upper = [_]u1{ | | |
| 209 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 210 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 211 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 212 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 213 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 214 | | | |
| 215 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 216 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, | | |
| 217 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 218 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 219 | }; | | |
| 220 | const digit = [_]u1{ | | |
| 221 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 222 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 223 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 224 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 225 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, | | |
| 226 | | | |
| 227 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 228 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 229 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 230 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 231 | }; | | |
| 232 | const hex = [_]u1{ | | |
| 233 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 234 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 235 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 236 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 237 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, | | |
| 238 | | | |
| 239 | 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 240 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 241 | 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 242 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 243 | }; | | |
| 244 | const space = [_]u1{ | | |
| 245 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 246 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, | | |
| 247 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 248 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 249 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 250 | | | |
| 251 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 252 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 254 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 255 | }; | | |
| 256 | const punct = [_]u1{ | | |
| 257 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 258 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 259 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 260 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 261 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, | | |
| 262 | | | |
| 263 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 264 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, | | |
| 265 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 266 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, | | |
| 267 | }; | | |
| 268 | const graph = [_]u1{ | | |
| 269 | // 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9,10,11,12,13,14,15 | | |
| 270 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 271 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | | |
| 272 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 273 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 274 | | | |
| 275 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 276 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 277 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | | |
| 278 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, | | |
| 279 | }; | | |
| 280 | | | |
| 281 | comptime var i = 0; | | |
| 282 | inline while (i < 128) : (i += 1) { | | |
| 283 | table[i] = | | |
| 284 | @as(u8, alpha[i]) << @enumToInt(tIndex.Alpha) | | | |
| 285 | @as(u8, hex[i]) << @enumToInt(tIndex.Hex) | | | |
| 286 | @as(u8, space[i]) << @enumToInt(tIndex.Space) | | | |
| 287 | @as(u8, digit[i]) << @enumToInt(tIndex.Digit) | | | |
| 288 | @as(u8, lower[i]) << @enumToInt(tIndex.Lower) | | | |
| 289 | @as(u8, upper[i]) << @enumToInt(tIndex.Upper) | | | |
| 290 | @as(u8, punct[i]) << @enumToInt(tIndex.Punct) | | | |
| 291 | @as(u8, graph[i]) << @enumToInt(tIndex.Graph); | | |
| 292 | } | | |
| 293 | mem.set(u8, table[128..256], 0); | | |
| 294 | break :init table; | | |
| 295 | }; | | |
| 296 | | | |
| 297 | fn inTable(c: u8, t: tIndex) bool { | | |
| 298 | return (combinedTable[c] & (@as(u8, 1) << @enumToInt(t))) != 0; | | |
| 299 | } | | |
| 300 | | | |
| 301 | /// DEPRECATED: use `isAlphanumeric` | | |
| 302 | pub const isAlNum = isAlphanumeric; | | |
| 303 | /// DEPRECATED: use `isAlphabetic` | | |
| 304 | pub const isAlpha = isAlphabetic; | | |
| 305 | /// DEPRECATED: use `isControl` | | |
| 306 | pub const isCntrl = isControl; | | |
| 307 | /// DEPRECATED: use `isWhitespace`. | | |
| 308 | pub const isSpace = isWhitespace; | | |
| 309 | /// DEPRECATED: use `whitespace`. | | |
| 310 | pub const spaces = whitespace; | | |
| 311 | /// DEPRECATED: use `isHex`. | | |
| 312 | pub const isXDigit = isHex; | | |
| 313 | | | |
| 314 | /// Returns whether the character is alphanumeric. | | |
| 315 | pub fn isAlphanumeric(c: u8) bool { | 92 | pub fn isAlphanumeric(c: u8) bool { |
| 316 | return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) | | 93 | return switch (c) { |
| 317 | @as(u8, 1) << @enumToInt(tIndex.Digit))) != 0; | 94 | '0'...'9', 'A'...'Z', 'a'...'z' => true, |
| | 95 | else => false, |
| | 96 | }; |
| 318 | } | 97 | } |
| 319 | | 98 | |
| 320 | /// Returns whether the character is alphabetic. | 99 | /// Returns whether the character is alphabetic: A-Z or a-z. |
| 321 | pub fn isAlphabetic(c: u8) bool { | 100 | pub fn isAlphabetic(c: u8) bool { |
| 322 | return inTable(c, tIndex.Alpha); | 101 | return switch (c) { |
| | 102 | 'A'...'Z', 'a'...'z' => true, |
| | 103 | else => false, |
| | 104 | }; |
| 323 | } | 105 | } |
| 324 | | 106 | |
| 325 | /// Returns whether the character is a control character. | 107 | /// Returns whether the character is a control character. |
| 326 | /// This is the same as `!isPrint(c)`. | | |
| 327 | /// | 108 | /// |
| 328 | /// See also: `control_code`. | 109 | /// See also: `control_code` |
| 329 | pub fn isControl(c: u8) bool { | 110 | pub fn isControl(c: u8) bool { |
| 330 | return c <= control_code.us or c == control_code.del; | 111 | return c <= control_code.us or c == control_code.del; |
| 331 | } | 112 | } |
| 332 | | 113 | |
| 333 | /// Returns whether the character is a digit. | 114 | /// Returns whether the character is a digit. |
| 334 | pub fn isDigit(c: u8) bool { | 115 | pub fn isDigit(c: u8) bool { |
| 335 | return inTable(c, tIndex.Digit); | 116 | return switch (c) { |
| 336 | } | 117 | '0'...'9' => true, |
| 337 | | 118 | else => false, |
| 338 | /// DEPRECATED: use `isPrint(c) and c != ' '` instead | 119 | }; |
| 339 | pub fn isGraph(c: u8) bool { | | |
| 340 | return inTable(c, tIndex.Graph); | | |
| 341 | } | 120 | } |
| 342 | | 121 | |
| 343 | /// Returns whether the character is a lowercased letter. | 122 | /// Returns whether the character is a lowercase letter. |
| 344 | pub fn isLower(c: u8) bool { | 123 | pub fn isLower(c: u8) bool { |
| 345 | return inTable(c, tIndex.Lower); | 124 | return switch (c) { |
| | 125 | 'a'...'z' => true, |
| | 126 | else => false, |
| | 127 | }; |
| 346 | } | 128 | } |
| 347 | | 129 | |
| 348 | /// Returns whether the character has some graphical representation and can be printed. | 130 | /// Returns whether the character is printable and has some graphical representation, |
| 349 | /// This also returns `true` for the space character. | 131 | /// including the space character. |
| 350 | /// This is the same as `!isControl(c)`. | | |
| 351 | pub fn isPrint(c: u8) bool { | 132 | pub fn isPrint(c: u8) bool { |
| 352 | return inTable(c, tIndex.Graph) or c == ' '; | 133 | return isASCII(c) and !isControl(c); |
| 353 | } | | |
| 354 | | | |
| 355 | /// DEPRECATED: create your own function based on your needs and what you want to do. | | |
| 356 | pub fn isPunct(c: u8) bool { | | |
| 357 | return inTable(c, tIndex.Punct); | | |
| 358 | } | 134 | } |
| 359 | | 135 | |
| 360 | /// Returns whether this character is included in `whitespace`. | 136 | /// Returns whether this character is included in `whitespace`. |
| 361 | pub fn isWhitespace(c: u8) bool { | 137 | pub fn isWhitespace(c: u8) bool { |
| 362 | return inTable(c, tIndex.Space); | 138 | return for (whitespace) |other| { |
| | 139 | if (c == other) |
| | 140 | break true; |
| | 141 | } else false; |
| 363 | } | 142 | } |
| 364 | | 143 | |
| 365 | /// Whitespace for general use. | 144 | /// Whitespace for general use. |
| 366 | /// This may be used with e.g. `std.mem.trim` to trim whitespace. | 145 | /// This may be used with e.g. `std.mem.trim` to trim whitespace. |
| 367 | /// | 146 | /// |
| 368 | /// See also: `isWhitespace`. | 147 | /// See also: `isWhitespace` |
| 369 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; | 148 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; |
| 370 | | 149 | |
| 371 | test "whitespace" { | 150 | test "whitespace" { |
| ... | @@ -377,14 +156,20 @@ test "whitespace" { | ... | @@ -377,14 +156,20 @@ test "whitespace" { |
| 377 | } | 156 | } |
| 378 | } | 157 | } |
| 379 | | 158 | |
| 380 | /// Returns whether the character is an uppercased letter. | 159 | /// Returns whether the character is an uppercase letter. |
| 381 | pub fn isUpper(c: u8) bool { | 160 | pub fn isUpper(c: u8) bool { |
| 382 | return inTable(c, tIndex.Upper); | 161 | return switch (c) { |
| | 162 | 'A'...'Z' => true, |
| | 163 | else => false, |
| | 164 | }; |
| 383 | } | 165 | } |
| 384 | | 166 | |
| 385 | /// Returns whether the character is a hexadecimal digit. This is case-insensitive. | 167 | /// Returns whether the character is a hexadecimal digit: A-F, a-f, or 0-9. |
| 386 | pub fn isHex(c: u8) bool { | 168 | pub fn isHex(c: u8) bool { |
| 387 | return inTable(c, tIndex.Hex); | 169 | return switch (c) { |
| | 170 | '0'...'9', 'A'...'F', 'a'...'f' => true, |
| | 171 | else => false, |
| | 172 | }; |
| 388 | } | 173 | } |
| 389 | | 174 | |
| 390 | /// Returns whether the character is a 7-bit ASCII character. | 175 | /// Returns whether the character is a 7-bit ASCII character. |
| ... | @@ -392,12 +177,7 @@ pub fn isASCII(c: u8) bool { | ... | @@ -392,12 +177,7 @@ pub fn isASCII(c: u8) bool { |
| 392 | return c < 128; | 177 | return c < 128; |
| 393 | } | 178 | } |
| 394 | | 179 | |
| 395 | /// DEPRECATED: use `c == ' ' or c == '\t'` or try `isWhitespace` | 180 | /// Uppercases the character and returns it as-is if already uppercase or not a letter. |
| 396 | pub fn isBlank(c: u8) bool { | | |
| 397 | return (c == ' ') or (c == '\x09'); | | |
| 398 | } | | |
| 399 | | | |
| 400 | /// Uppercases the character and returns it as-is if it's already uppercased or not a letter. | | |
| 401 | pub fn toUpper(c: u8) u8 { | 181 | pub fn toUpper(c: u8) u8 { |
| 402 | if (isLower(c)) { | 182 | if (isLower(c)) { |
| 403 | return c & 0b11011111; | 183 | return c & 0b11011111; |
| ... | @@ -406,7 +186,7 @@ pub fn toUpper(c: u8) u8 { | ... | @@ -406,7 +186,7 @@ pub fn toUpper(c: u8) u8 { |
| 406 | } | 186 | } |
| 407 | } | 187 | } |
| 408 | | 188 | |
| 409 | /// Lowercases the character and returns it as-is if it's already lowercased or not a letter. | 189 | /// Lowercases the character and returns it as-is if already lowercase or not a letter. |
| 410 | pub fn toLower(c: u8) u8 { | 190 | pub fn toLower(c: u8) u8 { |
| 411 | if (isUpper(c)) { | 191 | if (isUpper(c)) { |
| 412 | return c | 0b00100000; | 192 | return c | 0b00100000; |
| ... | @@ -415,53 +195,77 @@ pub fn toLower(c: u8) u8 { | ... | @@ -415,53 +195,77 @@ pub fn toLower(c: u8) u8 { |
| 415 | } | 195 | } |
| 416 | } | 196 | } |
| 417 | | 197 | |
| 418 | test "ascii character classes" { | 198 | test "ASCII character classes" { |
| 419 | const testing = std.testing; | 199 | const testing = std.testing; |
| 420 | | 200 | |
| 421 | try testing.expect(!isControl('a')); | 201 | try testing.expect(!isControl('a')); |
| 422 | try testing.expect(!isControl('z')); | 202 | try testing.expect(!isControl('z')); |
| | 203 | try testing.expect(!isControl(' ')); |
| 423 | try testing.expect(isControl(control_code.nul)); | 204 | try testing.expect(isControl(control_code.nul)); |
| 424 | try testing.expect(isControl(control_code.ff)); | 205 | try testing.expect(isControl(control_code.ff)); |
| 425 | try testing.expect(isControl(control_code.us)); | 206 | try testing.expect(isControl(control_code.us)); |
| | 207 | try testing.expect(isControl(control_code.del)); |
| | 208 | try testing.expect(!isControl(0x80)); |
| | 209 | try testing.expect(!isControl(0xff)); |
| 426 | | 210 | |
| 427 | try testing.expect('C' == toUpper('c')); | 211 | try testing.expect('C' == toUpper('c')); |
| 428 | try testing.expect(':' == toUpper(':')); | 212 | try testing.expect(':' == toUpper(':')); |
| 429 | try testing.expect('\xab' == toUpper('\xab')); | 213 | try testing.expect('\xab' == toUpper('\xab')); |
| 430 | try testing.expect(!isUpper('z')); | 214 | try testing.expect(!isUpper('z')); |
| | 215 | try testing.expect(!isUpper(0x80)); |
| | 216 | try testing.expect(!isUpper(0xff)); |
| 431 | | 217 | |
| 432 | try testing.expect('c' == toLower('C')); | 218 | try testing.expect('c' == toLower('C')); |
| 433 | try testing.expect(':' == toLower(':')); | 219 | try testing.expect(':' == toLower(':')); |
| 434 | try testing.expect('\xab' == toLower('\xab')); | 220 | try testing.expect('\xab' == toLower('\xab')); |
| 435 | try testing.expect(!isLower('Z')); | 221 | try testing.expect(!isLower('Z')); |
| | 222 | try testing.expect(!isLower(0x80)); |
| | 223 | try testing.expect(!isLower(0xff)); |
| 436 | | 224 | |
| 437 | try testing.expect(isAlphanumeric('Z')); | 225 | try testing.expect(isAlphanumeric('Z')); |
| 438 | try testing.expect(isAlphanumeric('z')); | 226 | try testing.expect(isAlphanumeric('z')); |
| 439 | try testing.expect(isAlphanumeric('5')); | 227 | try testing.expect(isAlphanumeric('5')); |
| 440 | try testing.expect(isAlphanumeric('5')); | 228 | try testing.expect(isAlphanumeric('a')); |
| 441 | try testing.expect(!isAlphanumeric('!')); | 229 | try testing.expect(!isAlphanumeric('!')); |
| | 230 | try testing.expect(!isAlphanumeric(0x80)); |
| | 231 | try testing.expect(!isAlphanumeric(0xff)); |
| 442 | | 232 | |
| 443 | try testing.expect(!isAlpha('5')); | 233 | try testing.expect(!isAlphabetic('5')); |
| 444 | try testing.expect(isAlpha('c')); | 234 | try testing.expect(isAlphabetic('c')); |
| 445 | try testing.expect(!isAlpha('5')); | 235 | try testing.expect(!isAlphabetic('@')); |
| | 236 | try testing.expect(isAlphabetic('Z')); |
| | 237 | try testing.expect(!isAlphabetic(0x80)); |
| | 238 | try testing.expect(!isAlphabetic(0xff)); |
| 446 | | 239 | |
| 447 | try testing.expect(isWhitespace(' ')); | 240 | try testing.expect(isWhitespace(' ')); |
| 448 | try testing.expect(isWhitespace('\t')); | 241 | try testing.expect(isWhitespace('\t')); |
| 449 | try testing.expect(isWhitespace('\r')); | 242 | try testing.expect(isWhitespace('\r')); |
| 450 | try testing.expect(isWhitespace('\n')); | 243 | try testing.expect(isWhitespace('\n')); |
| | 244 | try testing.expect(isWhitespace(control_code.ff)); |
| 451 | try testing.expect(!isWhitespace('.')); | 245 | try testing.expect(!isWhitespace('.')); |
| | 246 | try testing.expect(!isWhitespace(control_code.us)); |
| | 247 | try testing.expect(!isWhitespace(0x80)); |
| | 248 | try testing.expect(!isWhitespace(0xff)); |
| 452 | | 249 | |
| 453 | try testing.expect(!isHex('g')); | 250 | try testing.expect(!isHex('g')); |
| 454 | try testing.expect(isHex('b')); | 251 | try testing.expect(isHex('b')); |
| | 252 | try testing.expect(isHex('F')); |
| 455 | try testing.expect(isHex('9')); | 253 | try testing.expect(isHex('9')); |
| | 254 | try testing.expect(!isHex(0x80)); |
| | 255 | try testing.expect(!isHex(0xff)); |
| 456 | | 256 | |
| 457 | try testing.expect(!isDigit('~')); | 257 | try testing.expect(!isDigit('~')); |
| 458 | try testing.expect(isDigit('0')); | 258 | try testing.expect(isDigit('0')); |
| 459 | try testing.expect(isDigit('9')); | 259 | try testing.expect(isDigit('9')); |
| | 260 | try testing.expect(!isDigit(0x80)); |
| | 261 | try testing.expect(!isDigit(0xff)); |
| 460 | | 262 | |
| 461 | try testing.expect(isPrint(' ')); | 263 | try testing.expect(isPrint(' ')); |
| 462 | try testing.expect(isPrint('@')); | 264 | try testing.expect(isPrint('@')); |
| 463 | try testing.expect(isPrint('~')); | 265 | try testing.expect(isPrint('~')); |
| 464 | try testing.expect(!isPrint(control_code.esc)); | 266 | try testing.expect(!isPrint(control_code.esc)); |
| | 267 | try testing.expect(!isPrint(0x80)); |
| | 268 | try testing.expect(!isPrint(0xff)); |
| 465 | } | 269 | } |
| 466 | | 270 | |
| 467 | /// Writes a lower case copy of `ascii_string` to `output`. | 271 | /// Writes a lower case copy of `ascii_string` to `output`. |
| ... | @@ -541,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool { | ... | @@ -541,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool { |
| 541 | return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle); | 345 | return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle); |
| 542 | } | 346 | } |
| 543 | | 347 | |
| 544 | test "ascii.startsWithIgnoreCase" { | 348 | test "startsWithIgnoreCase" { |
| 545 | try std.testing.expect(startsWithIgnoreCase("boB", "Bo")); | 349 | try std.testing.expect(startsWithIgnoreCase("boB", "Bo")); |
| 546 | try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack")); | 350 | try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack")); |
| 547 | } | 351 | } |
| ... | @@ -550,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool { | ... | @@ -550,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool { |
| 550 | return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle); | 354 | return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle); |
| 551 | } | 355 | } |
| 552 | | 356 | |
| 553 | test "ascii.endsWithIgnoreCase" { | 357 | test "endsWithIgnoreCase" { |
| 554 | try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack")); | 358 | try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack")); |
| 555 | try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo")); | 359 | try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo")); |
| 556 | } | 360 | } |