diff options
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 48d591eff..acf9d057c 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -324,6 +324,43 @@ pub fn copyLowercase(in: string, out: []u8) string { return out[0..in.len]; } +pub fn copyLowercaseIfNeeded(in: string, out: []u8) string { + var in_slice: string = in; + var out_slice: []u8 = out[0..in.len]; + var any = false; + + begin: while (out_slice.len > 0) { + for (in_slice) |c, i| { + switch (c) { + 'A'...'Z' => { + @memcpy(out_slice.ptr, in_slice.ptr, i); + out_slice[i] = std.ascii.toLower(c); + const end = i + 1; + if (end >= out_slice.len) break :begin; + in_slice = in_slice[end..]; + out_slice = out_slice[end..]; + any = true; + continue :begin; + }, + else => {}, + } + } + + if (!any) { + return in; + } + + @memcpy(out_slice.ptr, in_slice.ptr, in_slice.len); + break :begin; + } + + if (!any) { + return in; + } + + return out[0..in.len]; +} + test "indexOf" { const fixtures = .{ .{ |