From 50ba0182235be67f79a1d088f279d69efd06b5d2 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 2 Nov 2020 19:06:31 +0100 Subject: [PATCH] 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(). --- lib/std/ascii.zig | 14 ++++++++++++++ lib/std/fmt.zig | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/std/ascii.zig b/lib/std/ascii.zig index 25c34ef0cc..d0f4b5758b 100644 --- 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 index 626b586bd5..bd6f4c7800 100644 --- 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}); } -