authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-08-16 21:37:02+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2022-08-16 21:37:02+02:00
log298062897da5de0766f180ebb7a66a6f1e91af88
treeba22d6bce42c108fa6972d3ec7868801b6dee8d4
parentee97fbc199a6b5cf04637d044650d407ce44f883

docs: fixes and improvements


1 files changed, 15 insertions(+), 10 deletions(-)

lib/std/ascii.zig+15-10
...@@ -12,7 +12,7 @@ const std = @import("std");...@@ -12,7 +12,7 @@ const std = @import("std");
1212
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`.
16pub const control_code = struct {16pub 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`.
241pub const isXDigit = isHex;241pub const isXDigit = isHex;
242242
243/// Returns whether the character is alphanumeric. This is case-insensitive.243/// Returns whether the character is alphanumeric.
244pub fn isAlphanumeric(c: u8) bool {244pub 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}
248248
249/// Returns whether the character is alphabetic. This is case-insensitive.249/// Returns whether the character is alphabetic.
250pub fn isAlphabetic(c: u8) bool {250pub fn isAlphabetic(c: u8) bool {
251 return inTable(c, tIndex.Alpha);251 return inTable(c, tIndex.Alpha);
252}252}
253253
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`.
257pub fn isControl(c: u8) bool {258pub 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}
260261
262/// Returns whether the character is a digit.
261pub fn isDigit(c: u8) bool {263pub 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}
269271
270/// Returns whether the character is lowercased.272/// Returns whether the character is a lowercased letter.
271pub fn isLower(c: u8) bool {273pub fn isLower(c: u8) bool {
272 return inTable(c, tIndex.Lower);274 return inTable(c, tIndex.Lower);
273}275}
274276
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)`.
277pub fn isPrint(c: u8) bool {280pub 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 {
290293
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`.
294pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };298pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
295299
296test "whitespace" {300test "whitespace" {
...@@ -302,7 +306,7 @@ test "whitespace" {...@@ -302,7 +306,7 @@ test "whitespace" {
302 }306 }
303}307}
304308
305/// Returns whether the character is uppercased.309/// Returns whether the character is an uppercased letter.
306pub fn isUpper(c: u8) bool {310pub 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}
314318
319/// Returns whether the character is a 7-bit ASCII character.
315pub fn isASCII(c: u8) bool {320pub 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}
323328
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.
325pub fn toUpper(c: u8) u8 {330pub 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}
332337
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.
334pub fn toLower(c: u8) u8 {339pub 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}
507512
508/// Compares two slices of numbers lexicographically. O(n).513/// Returns the lexicographical order of two slices. O(n).
509pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order {514pub 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}
521526
522/// Returns whether `lhs` < `rhs`.527/// Returns whether the lexicographical order of `lhs` is lower than `rhs`.
523pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool {528pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool {
524 return orderIgnoreCase(lhs, rhs) == .lt;529 return orderIgnoreCase(lhs, rhs) == .lt;
525}530}