From 143688e26644b64609410a1be378b349dce983fc Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Sat, 27 Mar 2021 19:32:36 +0100 Subject: [PATCH] add allocation free versions of lower/upper string Co-authored-by: LemonBoy Co-authored-by: Andrew Kelley --- lib/std/ascii.zig | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/std/ascii.zig b/lib/std/ascii.zig index ea7b61aa9e..afaf9f8ca4 100644 --- a/lib/std/ascii.zig +++ b/lib/std/ascii.zig @@ -288,36 +288,62 @@ test "ascii character classes" { try testing.expect(isSpace(' ')); } +/// Writes a lower case copy of `ascii_string` to `output`. +/// Asserts `output.len >= ascii_string.len`. +pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 { + std.debug.assert(output.len >= ascii_string.len); + for (ascii_string) |c, i| { + output[i] = toLower(c); + } + return output[0..ascii_string.len]; +} + +test "lowerString" { + var buf: [1024]u8 = undefined; + const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!"); + try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result); +} + /// Allocates a lower case copy of `ascii_string`. /// Caller owns returned string and must free with `allocator`. pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { const result = try allocator.alloc(u8, ascii_string.len); - for (result) |*c, i| { - c.* = toLower(ascii_string[i]); - } - return result; + return lowerString(result, ascii_string); } test "allocLowerString" { const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); defer std.testing.allocator.free(result); - try std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result)); + try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result); +} + +/// Writes an upper case copy of `ascii_string` to `output`. +/// Asserts `output.len >= ascii_string.len`. +pub fn upperString(output: []u8, ascii_string: []const u8) []u8 { + std.debug.assert(output.len >= ascii_string.len); + for (ascii_string) |c, i| { + output[i] = toUpper(c); + } + return output[0..ascii_string.len]; +} + +test "upperString" { + var buf: [1024]u8 = undefined; + const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!"); + try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result); } /// Allocates an upper case copy of `ascii_string`. /// Caller owns returned string and must free with `allocator`. pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { const result = try allocator.alloc(u8, ascii_string.len); - for (result) |*c, i| { - c.* = toUpper(ascii_string[i]); - } - return result; + return upperString(result, ascii_string); } test "allocUpperString" { const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); defer std.testing.allocator.free(result); - try std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result)); + try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result); } /// Compares strings `a` and `b` case insensitively and returns whether they are equal.