| ... | ... | @@ -10,83 +10,10 @@ |
| 10 | 10 | |
| 11 | 11 | const std = @import("std"); |
| 12 | 12 | |
| 13 | | // TODO: remove all decls marked as DEPRECATED after 0.10.0's release |
| 14 | | |
| 15 | 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 | 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 | 17 | /// Null. |
| 91 | 18 | pub const nul = 0x00; |
| 92 | 19 | /// Start of Heading. |
| ... | ... | @@ -161,211 +88,63 @@ pub const control_code = struct { |
| 161 | 88 | pub const xoff = dc3; |
| 162 | 89 | }; |
| 163 | 90 | |
| 164 | | const tIndex = enum(u3) { |
| 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. |
| 91 | /// Returns whether the character is alphanumeric: A-Z, a-z, or 0-9. |
| 315 | 92 | pub fn isAlphanumeric(c: u8) bool { |
| 316 | | return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) | |
| 317 | | @as(u8, 1) << @enumToInt(tIndex.Digit))) != 0; |
| 93 | return switch (c) { |
| 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 | 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 | 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 | 110 | pub fn isControl(c: u8) bool { |
| 330 | 111 | return c <= control_code.us or c == control_code.del; |
| 331 | 112 | } |
| 332 | 113 | |
| 333 | 114 | /// Returns whether the character is a digit. |
| 334 | 115 | pub fn isDigit(c: u8) bool { |
| 335 | | return inTable(c, tIndex.Digit); |
| 336 | | } |
| 337 | | |
| 338 | | /// DEPRECATED: use `isPrint(c) and c != ' '` instead |
| 339 | | pub fn isGraph(c: u8) bool { |
| 340 | | return inTable(c, tIndex.Graph); |
| 116 | return switch (c) { |
| 117 | '0'...'9' => true, |
| 118 | else => false, |
| 119 | }; |
| 341 | 120 | } |
| 342 | 121 | |
| 343 | | /// Returns whether the character is a lowercased letter. |
| 122 | /// Returns whether the character is a lowercase letter. |
| 344 | 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. |
| 349 | | /// This also returns `true` for the space character. |
| 350 | | /// This is the same as `!isControl(c)`. |
| 130 | /// Returns whether the character is printable and has some graphical representation, |
| 131 | /// including the space character. |
| 351 | 132 | pub fn isPrint(c: u8) bool { |
| 352 | | return inTable(c, tIndex.Graph) or 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); |
| 133 | return isASCII(c) and !isControl(c); |
| 358 | 134 | } |
| 359 | 135 | |
| 360 | 136 | /// Returns whether this character is included in `whitespace`. |
| 361 | 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 | 144 | /// Whitespace for general use. |
| 366 | 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 | 148 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; |
| 370 | 149 | |
| 371 | 150 | 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 | 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 | 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 | 175 | /// Returns whether the character is a 7-bit ASCII character. |
| ... | ... | @@ -392,12 +177,7 @@ pub fn isASCII(c: u8) bool { |
| 392 | 177 | return c < 128; |
| 393 | 178 | } |
| 394 | 179 | |
| 395 | | /// DEPRECATED: use `c == ' ' or c == '\t'` or try `isWhitespace` |
| 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. |
| 180 | /// Uppercases the character and returns it as-is if already uppercase or not a letter. |
| 401 | 181 | pub fn toUpper(c: u8) u8 { |
| 402 | 182 | if (isLower(c)) { |
| 403 | 183 | return c & 0b11011111; |
| ... | ... | @@ -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 | 190 | pub fn toLower(c: u8) u8 { |
| 411 | 191 | if (isUpper(c)) { |
| 412 | 192 | return c | 0b00100000; |
| ... | ... | @@ -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 | 199 | const testing = std.testing; |
| 420 | 200 | |
| 421 | 201 | try testing.expect(!isControl('a')); |
| 422 | 202 | try testing.expect(!isControl('z')); |
| 203 | try testing.expect(!isControl(' ')); |
| 423 | 204 | try testing.expect(isControl(control_code.nul)); |
| 424 | 205 | try testing.expect(isControl(control_code.ff)); |
| 425 | 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 | 211 | try testing.expect('C' == toUpper('c')); |
| 428 | 212 | try testing.expect(':' == toUpper(':')); |
| 429 | 213 | try testing.expect('\xab' == toUpper('\xab')); |
| 430 | 214 | try testing.expect(!isUpper('z')); |
| 215 | try testing.expect(!isUpper(0x80)); |
| 216 | try testing.expect(!isUpper(0xff)); |
| 431 | 217 | |
| 432 | 218 | try testing.expect('c' == toLower('C')); |
| 433 | 219 | try testing.expect(':' == toLower(':')); |
| 434 | 220 | try testing.expect('\xab' == toLower('\xab')); |
| 435 | 221 | try testing.expect(!isLower('Z')); |
| 222 | try testing.expect(!isLower(0x80)); |
| 223 | try testing.expect(!isLower(0xff)); |
| 436 | 224 | |
| 437 | 225 | try testing.expect(isAlphanumeric('Z')); |
| 438 | 226 | try testing.expect(isAlphanumeric('z')); |
| 439 | 227 | try testing.expect(isAlphanumeric('5')); |
| 440 | | try testing.expect(isAlphanumeric('5')); |
| 228 | try testing.expect(isAlphanumeric('a')); |
| 441 | 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')); |
| 444 | | try testing.expect(isAlpha('c')); |
| 445 | | try testing.expect(!isAlpha('5')); |
| 233 | try testing.expect(!isAlphabetic('5')); |
| 234 | try testing.expect(isAlphabetic('c')); |
| 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 | 240 | try testing.expect(isWhitespace(' ')); |
| 448 | 241 | try testing.expect(isWhitespace('\t')); |
| 449 | 242 | try testing.expect(isWhitespace('\r')); |
| 450 | 243 | try testing.expect(isWhitespace('\n')); |
| 244 | try testing.expect(isWhitespace(control_code.ff)); |
| 451 | 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 | 250 | try testing.expect(!isHex('g')); |
| 454 | 251 | try testing.expect(isHex('b')); |
| 252 | try testing.expect(isHex('F')); |
| 455 | 253 | try testing.expect(isHex('9')); |
| 254 | try testing.expect(!isHex(0x80)); |
| 255 | try testing.expect(!isHex(0xff)); |
| 456 | 256 | |
| 457 | 257 | try testing.expect(!isDigit('~')); |
| 458 | 258 | try testing.expect(isDigit('0')); |
| 459 | 259 | try testing.expect(isDigit('9')); |
| 260 | try testing.expect(!isDigit(0x80)); |
| 261 | try testing.expect(!isDigit(0xff)); |
| 460 | 262 | |
| 461 | 263 | try testing.expect(isPrint(' ')); |
| 462 | 264 | try testing.expect(isPrint('@')); |
| 463 | 265 | try testing.expect(isPrint('~')); |
| 464 | 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 | 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 | 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 | 349 | try std.testing.expect(startsWithIgnoreCase("boB", "Bo")); |
| 546 | 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 | 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 | 358 | try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack")); |
| 555 | 359 | try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo")); |
| 556 | 360 | } |