mirror of
https://github.com/ziglang/zig.git
synced 2025-12-10 08:13:07 +00:00
Remove redundant ASCII functions
The `matchCase` variants are simply duplicates of the `eql` and `indexOf` functions found in std.mem.
This commit is contained in:
parent
9f496c0777
commit
c6420820b0
@ -259,7 +259,7 @@ test "allocUpperString" {
|
|||||||
std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result));
|
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 {
|
pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
|
||||||
if (a.len != b.len) return false;
|
if (a.len != b.len) return false;
|
||||||
for (a) |a_c, i| {
|
for (a) |a_c, i| {
|
||||||
@ -274,43 +274,20 @@ test "eqlIgnoreCase" {
|
|||||||
std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
|
std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `true` if `a` and `b` are equal, case sensitive.
|
/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
|
||||||
pub fn eqlMatchCase(a: []const u8, b: []const u8) bool {
|
/// TODO boyer-moore algorithm
|
||||||
if (a.len != b.len) return false;
|
pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
|
||||||
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 {
|
|
||||||
if (substr.len > container.len) return null;
|
if (substr.len > container.len) return null;
|
||||||
|
|
||||||
var i: usize = start_index;
|
var i: usize = start_index;
|
||||||
const end = container.len - substr.len;
|
const end = container.len - substr.len;
|
||||||
while (i <= end) : (i += 1) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds `substr` in `container`, case insensitive, starting at `start_index`.
|
/// Finds `substr` in `container`, ignoring case, starting at index 0.
|
||||||
/// 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.
|
|
||||||
pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
|
pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
|
||||||
return indexOfIgnoreCasePos(container, 0, substr);
|
return indexOfIgnoreCasePos(container, 0, substr);
|
||||||
}
|
}
|
||||||
@ -323,22 +300,3 @@ test "indexOfIgnoreCase" {
|
|||||||
|
|
||||||
std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
|
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);
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user