Commit fd8fa55129

LemonBoy <LemonBoy@users.noreply.github.com>
2020-12-04 21:27:28
std: Add a few helpers for matching ascii strings (#7300)
* startsWithIgnoreCase * endsWithIgnoreCase
1 parent ac443b9
Changed files (1)
lib
lib/std/ascii.zig
@@ -335,6 +335,24 @@ test "eqlIgnoreCase" {
     std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
 }
 
+pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
+    return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
+}
+
+test "ascii.startsWithIgnoreCase" {
+    std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
+    std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
+}
+
+pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
+    return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
+}
+
+test "ascii.endsWithIgnoreCase" {
+    std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
+    std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
+}
+
 /// Finds `substr` in `container`, ignoring case, starting at `start_index`.
 /// TODO boyer-moore algorithm
 pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {