commit 50ba0182235be67f79a1d088f279d69efd06b5d2 (tree)
parent 909aae8153e7ac71197bc9f864e4ceb2793317f2
Author: Isaac Freund <ifreund@ifreund.xyz>
Date: Mon, 2 Nov 2020 19:06:31 +0100
std/ascii: add spaces array
This may be combined with std.mem.trim to form a proper replacement for
the now deprecated std.fmt.trimWhitespace().
Diffstat:
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/lib/std/ascii.zig b/lib/std/ascii.zig
@@ -230,6 +230,20 @@ pub fn isSpace(c: u8) bool {
return inTable(c, tIndex.Space);
}
+/// All the values for which isSpace() returns true. This may be used with
+/// e.g. std.mem.trim() to trim whiteSpace.
+pub const spaces = [_]u8{ ' ', '\t', '\n', '\r', control_code.VT, control_code.FF };
+
+test "spaces" {
+ const testing = std.testing;
+ for (spaces) |space| testing.expect(isSpace(space));
+
+ var i: u8 = 0;
+ while (isASCII(i)) : (i += 1) {
+ if (isSpace(i)) testing.expect(std.mem.indexOfScalar(u8, &spaces, i) != null);
+ }
+}
+
pub fn isUpper(c: u8) bool {
return inTable(c, tIndex.Upper);
}
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
@@ -1703,8 +1703,8 @@ fn testFmt(expected: []const u8, comptime template: []const u8, args: anytype) !
return error.TestFailed;
}
-pub const trim = @compileError("deprecated; use std.mem.trim instead");
-pub const isWhiteSpace = @compileError("deprecated; use std.mem.isWhiteSpace instead");
+pub const trim = @compileError("deprecated; use std.mem.trim with std.ascii.spaces instead");
+pub const isWhiteSpace = @compileError("deprecated; use std.ascii.isSpace instead");
pub fn hexToBytes(out: []u8, input: []const u8) !void {
if (out.len * 2 < input.len)
@@ -1890,4 +1890,3 @@ test "null" {
const inst = null;
try testFmt("null", "{}", .{inst});
}
-