| ... | ... | @@ -12,7 +12,7 @@ const std = @import("std"); |
| 12 | 12 | |
| 13 | 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 | 16 | pub const control_code = struct { |
| 17 | 17 | /// Null. |
| 18 | 18 | pub const nul = 0x00; |
| ... | ... | @@ -240,24 +240,26 @@ pub const spaces = whitespace; |
| 240 | 240 | /// DEPRECATED: use `isHex`. |
| 241 | 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 | 244 | pub fn isAlphanumeric(c: u8) bool { |
| 245 | 245 | return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) | |
| 246 | 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 | 250 | pub fn isAlphabetic(c: u8) bool { |
| 251 | 251 | return inTable(c, tIndex.Alpha); |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | /// Returns whether the character is a control character. |
| 255 | /// This is the same as `!isPrint(c)`. |
| 255 | 256 | /// |
| 256 | 257 | /// See also: `control_code`. |
| 257 | 258 | pub fn isControl(c: u8) bool { |
| 258 | 259 | return c <= control_code.us or c == control_code.del; |
| 259 | 260 | } |
| 260 | 261 | |
| 262 | /// Returns whether the character is a digit. |
| 261 | 263 | pub fn isDigit(c: u8) bool { |
| 262 | 264 | return inTable(c, tIndex.Digit); |
| 263 | 265 | } |
| ... | ... | @@ -267,13 +269,14 @@ pub fn isGraph(c: u8) bool { |
| 267 | 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 | 273 | pub fn isLower(c: u8) bool { |
| 272 | 274 | return inTable(c, tIndex.Lower); |
| 273 | 275 | } |
| 274 | 276 | |
| 275 | 277 | /// Returns whether the character has some graphical representation and can be printed. |
| 276 | 278 | /// This also returns `true` for the space character. |
| 279 | /// This is the same as `!isControl(c)`. |
| 277 | 280 | pub fn isPrint(c: u8) bool { |
| 278 | 281 | return inTable(c, tIndex.Graph) or c == ' '; |
| 279 | 282 | } |
| ... | ... | @@ -290,7 +293,8 @@ pub fn isWhitespace(c: u8) bool { |
| 290 | 293 | |
| 291 | 294 | /// Whitespace for general use. |
| 292 | 295 | /// This may be used with e.g. `std.mem.trim` to trim whitespace. |
| 293 | | /// See also: `isSpace`. |
| 296 | /// |
| 297 | /// See also: `isWhitespace`. |
| 294 | 298 | pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff }; |
| 295 | 299 | |
| 296 | 300 | 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 | 310 | pub fn isUpper(c: u8) bool { |
| 307 | 311 | return inTable(c, tIndex.Upper); |
| 308 | 312 | } |
| ... | ... | @@ -312,6 +316,7 @@ pub fn isHex(c: u8) bool { |
| 312 | 316 | return inTable(c, tIndex.Hex); |
| 313 | 317 | } |
| 314 | 318 | |
| 319 | /// Returns whether the character is a 7-bit ASCII character. |
| 315 | 320 | pub fn isASCII(c: u8) bool { |
| 316 | 321 | return c < 128; |
| 317 | 322 | } |
| ... | ... | @@ -321,7 +326,7 @@ pub fn isBlank(c: u8) bool { |
| 321 | 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 | 330 | pub fn toUpper(c: u8) u8 { |
| 326 | 331 | if (isLower(c)) { |
| 327 | 332 | return c & 0b11011111; |
| ... | ... | @@ -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 | 339 | pub fn toLower(c: u8) u8 { |
| 335 | 340 | if (isUpper(c)) { |
| 336 | 341 | return c | 0b00100000; |
| ... | ... | @@ -505,7 +510,7 @@ test "indexOfIgnoreCase" { |
| 505 | 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 | 514 | pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { |
| 510 | 515 | const n = std.math.min(lhs.len, rhs.len); |
| 511 | 516 | var i: usize = 0; |
| ... | ... | @@ -519,7 +524,7 @@ pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order { |
| 519 | 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 | 528 | pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool { |
| 524 | 529 | return orderIgnoreCase(lhs, rhs) == .lt; |
| 525 | 530 | } |