Commit 490654c332

Andrew Kelley <andrew@ziglang.org>
2021-02-27 09:21:01
std.ascii: add lessThanIgnoreCase and orderIgnoreCase
For sorting ascii strings, case insensitively.
1 parent 4a54800
Changed files (1)
lib
lib/std/ascii.zig
@@ -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;
+}