| ... | @@ -12,7 +12,7 @@ const std = @import("std"); | ... | @@ -12,7 +12,7 @@ const std = @import("std"); |
| 12 | | 12 | |
| 13 | /// The C0 control codes of the ASCII encoding. | 13 | /// The C0 control codes of the ASCII encoding. |
| 14 | /// | 14 | /// |
| 15 | /// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `is_control`. | 15 | /// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `isControl`. |
| 16 | pub const control_code = struct { | 16 | pub const control_code = struct { |
| 17 | /// Null. | 17 | /// Null. |
| 18 | pub const nul = 0x00; | 18 | pub const nul = 0x00; |
| ... | @@ -240,24 +240,26 @@ pub const spaces = whitespace; | ... | @@ -240,24 +240,26 @@ pub const spaces = whitespace; |
| 240 | /// DEPRECATED: use `isHex`. | 240 | /// DEPRECATED: use `isHex`. |
| 241 | pub const isXDigit = isHex; | 241 | pub const isXDigit = isHex; |
| 242 | | 242 | |
| 243 | /// Returns whether the character is alphanumeric. This is case-insensitive. | 243 | /// Returns whether the character is alphanumeric. |
| 244 | pub fn isAlphanumeric(c: u8) bool { | 244 | pub fn isAlphanumeric(c: u8) bool { |
| 245 | return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) | | 245 | return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) | |
| 246 | @as(u8, 1) << @enumToInt(tIndex.Digit))) != 0; | 246 | @as(u8, 1) << @enumToInt(tIndex.Digit))) != 0; |
| 247 | } | 247 | } |
| 248 | | 248 | |
| 249 | /// Returns whether the character is alphabetic. This is case-insensitive. | 249 | /// Returns whether the character is alphabetic. |
| 250 | pub fn isAlphabetic(c: u8) bool { | 250 | pub fn isAlphabetic(c: u8) bool { |
| 251 | return inTable(c, tIndex.Alpha); | 251 | return inTable(c, tIndex.Alpha); |
| 252 | } | 252 | } |
| 253 | | 253 | |
| 254 | /// Returns whether the character is a control character. | 254 | /// Returns whether the character is a control character. |
| | 255 | /// This is the same as `!isPrint(c)`. |
| 255 | /// | 256 | /// |
| 256 | /// See also: `control_code`. | 257 | /// See also: `control_code`. |
| 257 | pub fn isControl(c: u8) bool { | 258 | pub fn isControl(c: u8) bool { |
| 258 | return c <= control_code.us or c == control_code.del; | 259 | return c <= control_code.us or c == control_code.del; |
| 259 | } | 260 | } |
| 260 | | 261 | |
| | 262 | /// Returns whether the character is a digit. |
| 261 | pub fn isDigit(c: u8) bool { | 263 | pub fn isDigit(c: u8) bool { |
| 262 | return inTable(c, tIndex.Digit); | 264 | return inTable(c, tIndex.Digit); |
| 263 | } | 265 | } |
| ... | @@ -267,13 +269,14 @@ pub fn isGraph(c: u8) bool { | ... | @@ -267,13 +269,14 @@ pub fn isGraph(c: u8) bool { |
| 267 | return inTable(c, tIndex.Graph); | 269 | return inTable(c, tIndex.Graph); |
| 268 | } | 270 | } |
| 269 | | 271 | |
| 270 | /// Returns whether the character is lowercased. | 272 | /// Returns whether the character is a lowercased letter. |
| 271 | pub fn isLower(c: u8) bool { | 273 | pub fn isLower(c: u8) bool { |
| 272 | return inTable(c, tIndex.Lower); | 274 | return inTable(c, tIndex.Lower); |
| 273 | } | 275 | } |
| 274 | | 276 | |
| 275 | /// Returns whether the character has some graphical representation and can be printed. | 277 | /// Returns whether the character has some graphical representation and can be printed. |
| 276 | /// This also returns `true` for the space character. | 278 | /// This also returns `true` for the space character. |
| | 279 | /// This is the same as `!isControl(c)`. |
| 277 | pub fn isPrint(c: u8) bool { | 280 | pub fn isPrint(c: u8) bool { |
| 278 | return inTable(c, tIndex.Graph) or c == ' '; | 281 | return inTable(c, tIndex.Graph) or c == ' '; |
| 279 | } | 282 | } |
| ... | @@ -290,7 +293,8 @@ pub fn isWhitespace(c: u8) bool { | ... | @@ -290,7 +293,8 @@ pub fn isWhitespace(c: u8) bool { |
| 290 | | 293 | |
| 291 | /// Whitespace for general use. | 294 | /// Whitespace for general use. |
| 292 | /// This may be used with e.g. `std.mem.trim` to trim whitespace. | 295 | /// This may be used with e.g. `std.mem.trim` to trim whitespace. |
| 293 | /// See also: `isSpace`. | 296 | /// |
| | 297 | /// See also: `isWhitespace`. |
| 294 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; | 298 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; |
| 295 | | 299 | |
| 296 | test "whitespace" { | 300 | test "whitespace" { |
| ... | @@ -302,7 +306,7 @@ test "whitespace" { | ... | @@ -302,7 +306,7 @@ test "whitespace" { |
| 302 | } | 306 | } |
| 303 | } | 307 | } |
| 304 | | 308 | |
| 305 | /// Returns whether the character is uppercased. | 309 | /// Returns whether the character is an uppercased letter. |
| 306 | pub fn isUpper(c: u8) bool { | 310 | pub fn isUpper(c: u8) bool { |
| 307 | return inTable(c, tIndex.Upper); | 311 | return inTable(c, tIndex.Upper); |
| 308 | } | 312 | } |
| ... | @@ -312,6 +316,7 @@ pub fn isHex(c: u8) bool { | ... | @@ -312,6 +316,7 @@ pub fn isHex(c: u8) bool { |
| 312 | return inTable(c, tIndex.Hex); | 316 | return inTable(c, tIndex.Hex); |
| 313 | } | 317 | } |
| 314 | | 318 | |
| | 319 | /// Returns whether the character is a 7-bit ASCII character. |
| 315 | pub fn isASCII(c: u8) bool { | 320 | pub fn isASCII(c: u8) bool { |
| 316 | return c < 128; | 321 | return c < 128; |
| 317 | } | 322 | } |
| ... | @@ -321,7 +326,7 @@ pub fn isBlank(c: u8) bool { | ... | @@ -321,7 +326,7 @@ pub fn isBlank(c: u8) bool { |
| 321 | return (c == ' ') or (c == '\x09'); | 326 | return (c == ' ') or (c == '\x09'); |
| 322 | } | 327 | } |
| 323 | | 328 | |
| 324 | /// Uppercases the character and returns it as-is if it's already uppercased. | 329 | /// Uppercases the character and returns it as-is if it's already uppercased or not a letter. |
| 325 | pub fn toUpper(c: u8) u8 { | 330 | pub fn toUpper(c: u8) u8 { |
| 326 | if (isLower(c)) { | 331 | if (isLower(c)) { |
| 327 | return c & 0b11011111; | 332 | return c & 0b11011111; |
| ... | @@ -330,7 +335,7 @@ pub fn toUpper(c: u8) u8 { | ... | @@ -330,7 +335,7 @@ pub fn toUpper(c: u8) u8 { |
| 330 | } | 335 | } |
| 331 | } | 336 | } |
| 332 | | 337 | |
| 333 | /// Lowercases the character and returns it as-is if it's already lowercased. | 338 | /// Lowercases the character and returns it as-is if it's already lowercased or not a letter. |
| 334 | pub fn toLower(c: u8) u8 { | 339 | pub fn toLower(c: u8) u8 { |
| 335 | if (isUpper(c)) { | 340 | if (isUpper(c)) { |
| 336 | return c | 0b00100000; | 341 | return c | 0b00100000; |
| ... | @@ -505,7 +510,7 @@ test "indexOfIgnoreCase" { | ... | @@ -505,7 +510,7 @@ test "indexOfIgnoreCase" { |
| 505 | try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0); | 510 | try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0); |
| 506 | } | 511 | } |
| 507 | | 512 | |
| 508 | /// Compares two slices of numbers lexicographically. O(n). | 513 | /// Returns the lexicographical order of two slices. O(n). |
| 509 | pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { | 514 | pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { |
| 510 | const n = std.math.min(lhs.len, rhs.len); | 515 | const n = std.math.min(lhs.len, rhs.len); |
| 511 | var i: usize = 0; | 516 | var i: usize = 0; |
| ... | @@ -519,7 +524,7 @@ pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { | ... | @@ -519,7 +524,7 @@ pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { |
| 519 | return std.math.order(lhs.len, rhs.len); | 524 | return std.math.order(lhs.len, rhs.len); |
| 520 | } | 525 | } |
| 521 | | 526 | |
| 522 | /// Returns whether `lhs` < `rhs`. | 527 | /// Returns whether the lexicographical order of `lhs` is lower than `rhs`. |
| 523 | pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool { | 528 | pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool { |
| 524 | return orderIgnoreCase(lhs, rhs) == .lt; | 529 | return orderIgnoreCase(lhs, rhs) == .lt; |
| 525 | } | 530 | } |