authorgravatar for greg@gpanders.comGreg Anders <greg@gpanders.com> 2020-05-12 07:14:50-06:00
committergravatar for greg@gpanders.comGreg Anders <greg@gpanders.com> 2020-05-12 08:38:15-06:00
logc6420820b0aa5df8db801c9511305b009c8920e9
treebb7fed6e23b85a9becc54b44455d80f78de5b3d6
parent9f496c077766172465a065eafba56dd5280021a6

Remove redundant ASCII functions

The `matchCase` variants are simply duplicates of the `eql` and `indexOf` functions found in std.mem.

1 files changed, 6 insertions(+), 48 deletions(-)

lib/std/ascii.zig+6-48
......@@ -259,7 +259,7 @@ test "allocUpperString" {
259259 std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result));
260260}
261261
262/// Return `true` if `a` and `b` are equal, case insensitive.
262/// Compares strings `a` and `b` case insensitively and returns whether they are equal.
263263pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
264264 if (a.len != b.len) return false;
265265 for (a) |a_c, i| {
......@@ -274,43 +274,20 @@ test "eqlIgnoreCase" {
274274 std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
275275}
276276
277/// Return `true` if `a` and `b` are equal, case sensitive.
278pub fn eqlMatchCase(a: []const u8, b: []const u8) bool {
279 if (a.len != b.len) return false;
280 for (a) |a_c, i| {
281 if (a_c != b[i]) return false;
282 }
283 return true;
284}
285
286test "eqlMatchCase" {
287 std.testing.expect(!eqlMatchCase("HEl💩Lo!", "hel💩lo!"));
288 std.testing.expect(eqlMatchCase("Hello! ", "Hello! "));
289 std.testing.expect(eqlMatchCase("hElLo!", "hElLo!"));
290}
291
292fn indexOfPos(container: []const u8, start_index: usize, substr: []const u8, comptime case_sensitive: bool) ?usize {
277/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
278/// TODO boyer-moore algorithm
279pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
293280 if (substr.len > container.len) return null;
294281
295282 var i: usize = start_index;
296283 const end = container.len - substr.len;
297284 while (i <= end) : (i += 1) {
298 if (case_sensitive) {
299 if (eqlMatchCase(container[i .. i + substr.len], substr)) return i;
300 } else {
301 if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i;
302 }
285 if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i;
303286 }
304287 return null;
305288}
306289
307/// Finds `substr` in `container`, case insensitive, starting at `start_index`.
308/// TODO boyer-moore algorithm
309pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
310 return indexOfPos(container, start_index, substr, false);
311}
312
313/// Finds `substr` in `container`, case insensitive, starting at index 0.
290/// Finds `substr` in `container`, ignoring case, starting at index 0.
314291pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
315292 return indexOfIgnoreCasePos(container, 0, substr);
316293}
......@@ -323,22 +300,3 @@ test "indexOfIgnoreCase" {
323300
324301 std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
325302}
326
327/// Finds `substr` in `container`, case sensitive, starting at `start_index`.
328pub fn indexOfMatchCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize {
329 return indexOfPos(container, start_index, substr, true);
330}
331
332/// Finds `substr` in `container`, case sensitive, starting at index 0.
333pub fn indexOfMatchCase(container: []const u8, substr: []const u8) ?usize {
334 return indexOfMatchCasePos(container, 0, substr);
335}
336
337test "indexOfMatchCase" {
338 std.testing.expect(indexOfMatchCase("one Two Three Four", "Four").? == 14);
339 std.testing.expect(indexOfMatchCase("one two three FouR", "fOur") == null);
340 std.testing.expect(indexOfMatchCase("foO", "foO").? == 0);
341 std.testing.expect(indexOfMatchCase("foo", "fool") == null);
342
343 std.testing.expect(indexOfMatchCase("FOO foo", "FOO").? == 0);
344}