authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-12-04 21:27:28+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-04 15:27:28-05:00
logfd8fa551295cdf8eac2f524bda835dce731a4c0a
tree742b683ea8b638dfc7e159f6b9ad29db66529175
parentac443b941df08bacb5a37e17d1285cb9f973c4f5
signature Signed by PGP key 4AEE18F83AFDEB23

std: Add a few helpers for matching ascii strings (#7300)

* startsWithIgnoreCase * endsWithIgnoreCase

1 files changed, 18 insertions(+), 0 deletions(-)

lib/std/ascii.zig+18
...@@ -335,6 +335,24 @@ test "eqlIgnoreCase" {...@@ -335,6 +335,24 @@ test "eqlIgnoreCase" {
335 std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));335 std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
336}336}
337337
338pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
339 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
340}
341
342test "ascii.startsWithIgnoreCase" {
343 std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
344 std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
345}
346
347pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
348 return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
349}
350
351test "ascii.endsWithIgnoreCase" {
352 std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
353 std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
354}
355
338/// Finds `substr` in `container`, ignoring case, starting at `start_index`.356/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
339/// TODO boyer-moore algorithm357/// TODO boyer-moore algorithm
340pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {358pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {