Remove redundant ASCII functions

The `matchCase` variants are simply duplicates of the `eql` and
`indexOf` functions found in std.mem.
master
Greg Anders 2020-05-12 07:14:50 -06:00
parent 9f496c0777
commit c6420820b0
1 changed files with 6 additions and 48 deletions

View File

@ -259,7 +259,7 @@ test "allocUpperString" {
std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result));
}
/// Return `true` if `a` and `b` are equal, case insensitive.
/// Compares strings `a` and `b` case insensitively and returns whether they are equal.
pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
for (a) |a_c, i| {
@ -274,43 +274,20 @@ test "eqlIgnoreCase" {
std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
}
/// Return `true` if `a` and `b` are equal, case sensitive.
pub fn eqlMatchCase(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
for (a) |a_c, i| {
if (a_c != b[i]) return false;
}
return true;
}
test "eqlMatchCase" {
std.testing.expect(!eqlMatchCase("HEl💩Lo!", "hel💩lo!"));
std.testing.expect(eqlMatchCase("Hello! ", "Hello! "));
std.testing.expect(eqlMatchCase("hElLo!", "hElLo!"));
}
fn indexOfPos(container: []const u8, start_index: usize, substr: []const u8, comptime case_sensitive: bool) ?usize {
/// 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 {
if (substr.len > container.len) return null;
var i: usize = start_index;
const end = container.len - substr.len;
while (i <= end) : (i += 1) {
if (case_sensitive) {
if (eqlMatchCase(container[i .. i + substr.len], substr)) return i;
} else {
if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i;
}
if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i;
}
return null;
}
/// Finds `substr` in `container`, case insensitive, starting at `start_index`.
/// TODO boyer-moore algorithm
pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
return indexOfPos(container, start_index, substr, false);
}
/// Finds `substr` in `container`, case insensitive, starting at index 0.
/// Finds `substr` in `container`, ignoring case, starting at index 0.
pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
return indexOfIgnoreCasePos(container, 0, substr);
}
@ -323,22 +300,3 @@ test "indexOfIgnoreCase" {
std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
}
/// Finds `substr` in `container`, case sensitive, starting at `start_index`.
pub fn indexOfMatchCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
return indexOfPos(container, start_index, substr, true);
}
/// Finds `substr` in `container`, case sensitive, starting at index 0.
pub fn indexOfMatchCase(container: []const u8, substr: []const u8) ?usize {
return indexOfMatchCasePos(container, 0, substr);
}
test "indexOfMatchCase" {
std.testing.expect(indexOfMatchCase("one Two Three Four", "Four").? == 14);
std.testing.expect(indexOfMatchCase("one two three FouR", "fOur") == null);
std.testing.expect(indexOfMatchCase("foO", "foO").? == 0);
std.testing.expect(indexOfMatchCase("foo", "fool") == null);
std.testing.expect(indexOfMatchCase("FOO foo", "FOO").? == 0);
}