authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-03-27 19:32:36+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-08 14:27:44-04:00
log143688e26644b64609410a1be378b349dce983fc
treea4026eefe10760a9026e0710b0c885e200b5e632
parentccfa16828445ebf3634c2a0f27ec359af1628efc

add allocation free versions of lower/upper string

Co-authored-by: LemonBoy <thatlemon@gmail.com> Co-authored-by: Andrew Kelley <andrew@ziglang.org>

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

lib/std/ascii.zig+36-10
...@@ -288,36 +288,62 @@ test "ascii character classes" {...@@ -288,36 +288,62 @@ test "ascii character classes" {
288 try testing.expect(isSpace(' '));288 try testing.expect(isSpace(' '));
289}289}
290290
291/// Writes a lower case copy of `ascii_string` to `output`.
292/// Asserts `output.len >= ascii_string.len`.
293pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
294 std.debug.assert(output.len >= ascii_string.len);
295 for (ascii_string) |c, i| {
296 output[i] = toLower(c);
297 }
298 return output[0..ascii_string.len];
299}
300
301test "lowerString" {
302 var buf: [1024]u8 = undefined;
303 const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
304 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
305}
306
291/// Allocates a lower case copy of `ascii_string`.307/// Allocates a lower case copy of `ascii_string`.
292/// Caller owns returned string and must free with `allocator`.308/// Caller owns returned string and must free with `allocator`.
293pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {309pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {
294 const result = try allocator.alloc(u8, ascii_string.len);310 const result = try allocator.alloc(u8, ascii_string.len);
295 for (result) |*c, i| {311 return lowerString(result, ascii_string);
296 c.* = toLower(ascii_string[i]);
297 }
298 return result;
299}312}
300313
301test "allocLowerString" {314test "allocLowerString" {
302 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");315 const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
303 defer std.testing.allocator.free(result);316 defer std.testing.allocator.free(result);
304 try std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));317 try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
318}
319
320/// Writes an upper case copy of `ascii_string` to `output`.
321/// Asserts `output.len >= ascii_string.len`.
322pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
323 std.debug.assert(output.len >= ascii_string.len);
324 for (ascii_string) |c, i| {
325 output[i] = toUpper(c);
326 }
327 return output[0..ascii_string.len];
328}
329
330test "upperString" {
331 var buf: [1024]u8 = undefined;
332 const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
333 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
305}334}
306335
307/// Allocates an upper case copy of `ascii_string`.336/// Allocates an upper case copy of `ascii_string`.
308/// Caller owns returned string and must free with `allocator`.337/// Caller owns returned string and must free with `allocator`.
309pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {338pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {
310 const result = try allocator.alloc(u8, ascii_string.len);339 const result = try allocator.alloc(u8, ascii_string.len);
311 for (result) |*c, i| {340 return upperString(result, ascii_string);
312 c.* = toUpper(ascii_string[i]);
313 }
314 return result;
315}341}
316342
317test "allocUpperString" {343test "allocUpperString" {
318 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");344 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
319 defer std.testing.allocator.free(result);345 defer std.testing.allocator.free(result);
320 try std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result));346 try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
321}347}
322348
323/// Compares strings `a` and `b` case insensitively and returns whether they are equal.349/// Compares strings `a` and `b` case insensitively and returns whether they are equal.