authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-27 01:21:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-27 01:21:01-07:00
log490654c332f2d8eaf7edffa35ea0523800df998d
tree5398465a81ab6432997c76e5b31c3116a02568d1
parent4a548002afcff3850ab117a263f20ef34259c884

std.ascii: add lessThanIgnoreCase and orderIgnoreCase

For sorting ascii strings, case insensitively.

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

lib/std/ascii.zig+20
...@@ -379,3 +379,23 @@ test "indexOfIgnoreCase" {...@@ -379,3 +379,23 @@ test "indexOfIgnoreCase" {
379379
380 std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);380 std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
381}381}
382
383/// Compares two slices of numbers lexicographically. O(n).
384pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order {
385 const n = std.math.min(lhs.len, rhs.len);
386 var i: usize = 0;
387 while (i < n) : (i += 1) {
388 switch (std.math.order(toLower(lhs[i]), toLower(rhs[i]))) {
389 .eq => continue,
390 .lt => return .lt,
391 .gt => return .gt,
392 }
393 }
394 return std.math.order(lhs.len, rhs.len);
395}
396
397/// Returns true if lhs < rhs, false otherwise
398/// TODO rename "IgnoreCase" to "Insensitive" in this entire file.
399pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool {
400 return orderIgnoreCase(lhs, rhs) == .lt;
401}