| ... | @@ -3,8 +3,6 @@ const assert = debug.assert; | ... | @@ -3,8 +3,6 @@ const assert = debug.assert; |
| 3 | const math = @import("math/index.zig"); | 3 | const math = @import("math/index.zig"); |
| 4 | const builtin = @import("builtin"); | 4 | const builtin = @import("builtin"); |
| 5 | | 5 | |
| 6 | pub const Cmp = math.Cmp; | | |
| 7 | | | |
| 8 | pub const Allocator = struct { | 6 | pub const Allocator = struct { |
| 9 | /// Allocate byte_count bytes and return them in a slice, with the | 7 | /// Allocate byte_count bytes and return them in a slice, with the |
| 10 | /// slice's pointer aligned at least to alignment bytes. | 8 | /// slice's pointer aligned at least to alignment bytes. |
| ... | @@ -166,17 +164,24 @@ pub fn set(comptime T: type, dest: []T, value: T) { | ... | @@ -166,17 +164,24 @@ pub fn set(comptime T: type, dest: []T, value: T) { |
| 166 | for (dest) |*d| *d = value; | 164 | for (dest) |*d| *d = value; |
| 167 | } | 165 | } |
| 168 | | 166 | |
| 169 | /// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than, | 167 | /// Returns true if lhs < rhs, false otherwise |
| 170 | /// memory b, respectively. | 168 | pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) -> bool { |
| 171 | pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp { | 169 | const n = math.min(lhs.len, rhs.len); |
| 172 | const n = math.min(a.len, b.len); | | |
| 173 | var i: usize = 0; | 170 | var i: usize = 0; |
| 174 | while (i < n) : (i += 1) { | 171 | while (i < n) : (i += 1) { |
| 175 | if (a[i] == b[i]) continue; | 172 | if (lhs[i] == rhs[i]) continue; |
| 176 | return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal; | 173 | return lhs[i] < rhs[i]; |
| 177 | } | 174 | } |
| 178 | | 175 | |
| 179 | return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal; | 176 | return lhs.len < rhs.len; |
| | 177 | } |
| | 178 | |
| | 179 | test "mem.lessThan" { |
| | 180 | assert(lessThan(u8, "abcd", "bee")); |
| | 181 | assert(!lessThan(u8, "abc", "abc")); |
| | 182 | assert(lessThan(u8, "abc", "abc0")); |
| | 183 | assert(!lessThan(u8, "", "")); |
| | 184 | assert(lessThan(u8, "", "a")); |
| 180 | } | 185 | } |
| 181 | | 186 | |
| 182 | /// Compares two slices and returns whether they are equal. | 187 | /// Compares two slices and returns whether they are equal. |