zig

fork of https://codeberg.org/ziglang/zig
Log | Tree | Refs | README | LICENSE

commit 490654c332f2d8eaf7edffa35ea0523800df998d (tree)
parent 4a548002afcff3850ab117a263f20ef34259c884
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sat, 27 Feb 2021 01:21:01 -0700

std.ascii: add lessThanIgnoreCase and orderIgnoreCase

For sorting ascii strings, case insensitively.

Diffstat:
Mlib/std/ascii.zig | 20++++++++++++++++++++
1 file changed, 20 insertions(+), 0 deletions(-)

diff --git a/lib/std/ascii.zig b/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; +}