std.ascii: add lessThanIgnoreCase and orderIgnoreCase

For sorting ascii strings, case insensitively.
This commit is contained in:
Andrew Kelley 2021-02-27 01:21:01 -07:00
parent 4a548002af
commit 490654c332

View File

@ -379,3 +379,23 @@ test "indexOfIgnoreCase" {
std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
}
/// Compares two slices of numbers lexicographically. O(n).
pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order {
const n = std.math.min(lhs.len, rhs.len);
var i: usize = 0;
while (i < n) : (i += 1) {
switch (std.math.order(toLower(lhs[i]), toLower(rhs[i]))) {
.eq => continue,
.lt => return .lt,
.gt => return .gt,
}
}
return std.math.order(lhs.len, rhs.len);
}
/// Returns true if lhs < rhs, false otherwise
/// TODO rename "IgnoreCase" to "Insensitive" in this entire file.
pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool {
return orderIgnoreCase(lhs, rhs) == .lt;
}