authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-13 19:04:53+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-13 19:04:53+03:00
log118db892bea5222a509a9f91abe560b52a5f08eb
tree744b8c8ce8e597b05c5858b1da387cb722dff114
parent76681e6b9689c4df78b6451da1aa857dc08a84d2
parentc6420820b0aa5df8db801c9511305b009c8920e9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5321 from gpanders/ascii-case-sensitive

Add helper functions and docstrings to ascii.zig

1 files changed, 21 insertions(+), 2 deletions(-)

lib/std/ascii.zig+21-2
......@@ -227,6 +227,8 @@ test "ascii character classes" {
227227 testing.expect(isSpace(' '));
228228}
229229
230/// Allocates a lower case copy of `ascii_string`.
231/// Caller owns returned string and must free with `allocator`.
230232pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {
231233 const result = try allocator.alloc(u8, ascii_string.len);
232234 for (result) |*c, i| {
......@@ -241,6 +243,23 @@ test "allocLowerString" {
241243 std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result));
242244}
243245
246/// Allocates an upper case copy of `ascii_string`.
247/// Caller owns returned string and must free with `allocator`.
248pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 {
249 const result = try allocator.alloc(u8, ascii_string.len);
250 for (result) |*c, i| {
251 c.* = toUpper(ascii_string[i]);
252 }
253 return result;
254}
255
256test "allocUpperString" {
257 const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
258 defer std.testing.allocator.free(result);
259 std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result));
260}
261
262/// Compares strings `a` and `b` case insensitively and returns whether they are equal.
244263pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
245264 if (a.len != b.len) return false;
246265 for (a) |a_c, i| {
......@@ -255,7 +274,7 @@ test "eqlIgnoreCase" {
255274 std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
256275}
257276
258/// Finds `substr` in `container`, starting at `start_index`.
277/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
259278/// TODO boyer-moore algorithm
260279pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
261280 if (substr.len > container.len) return null;
......@@ -268,7 +287,7 @@ pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: [
268287 return null;
269288}
270289
271/// Finds `substr` in `container`, starting at `start_index`.
290/// Finds `substr` in `container`, ignoring case, starting at index 0.
272291pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
273292 return indexOfIgnoreCasePos(container, 0, substr);
274293}