aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-09-26 03:03:12 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-09-26 03:03:12 -0400
commit5c2db5a942a7f3af52fa1a46df0afa7013d65dd9 (patch)
tree18aa452f5837d03fe5688510b6f03a3ccc014a90 /std
parentfd2d502e411c45828ebdf1008c5060db8749ef31 (diff)
downloadzig-5c2db5a942a7f3af52fa1a46df0afa7013d65dd9.tar.gz
zig-5c2db5a942a7f3af52fa1a46df0afa7013d65dd9.zip
add std.fmt.trim
Diffstat (limited to 'std')
-rw-r--r--std/fmt/index.zig34
1 files changed, 34 insertions, 0 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 0993bd5e2b..050914d2c4 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -489,3 +489,37 @@ test "fmt.format" {
assert(mem.eql(u8, result, "error union: error.InvalidChar\n"));
}
}
+
+pub fn trim(buf: []const u8) -> []const u8 {
+ var start: usize = 0;
+ while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) { }
+
+ var end: usize = buf.len;
+ while (true) {
+ if (end > start) {
+ const new_end = end - 1;
+ if (isWhiteSpace(buf[new_end])) {
+ end = new_end;
+ continue;
+ }
+ }
+ break;
+
+ }
+ return buf[start..end];
+}
+
+test "fmt.trim" {
+ assert(mem.eql(u8, "abc", trim("\n abc \t")));
+ assert(mem.eql(u8, "", trim(" ")));
+ assert(mem.eql(u8, "", trim("")));
+ assert(mem.eql(u8, "abc", trim(" abc")));
+ assert(mem.eql(u8, "abc", trim("abc ")));
+}
+
+pub fn isWhiteSpace(byte: u8) -> bool {
+ return switch (byte) {
+ ' ', '\t', '\n', '\r' => true,
+ else => false,
+ };
+}