aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-12-15 17:26:22 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-12-15 17:26:22 -0500
commit39e96d933ec3611017edccfdd443fece826a7207 (patch)
tree05910a5b956428afb3abb9425184467490962e29 /std
parent68f63323437e1b974be7a9982f5d70b95624878b (diff)
downloadzig-39e96d933ec3611017edccfdd443fece826a7207.tar.gz
zig-39e96d933ec3611017edccfdd443fece826a7207.zip
change mem.cmp to mem.lessThan and add test
Diffstat (limited to 'std')
-rw-r--r--std/mem.zig23
1 files changed, 14 insertions, 9 deletions
diff --git a/std/mem.zig b/std/mem.zig
index 10d2221f9a..7edca350a0 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -3,8 +3,6 @@ const assert = debug.assert;
const math = @import("math/index.zig");
const builtin = @import("builtin");
-pub const Cmp = math.Cmp;
-
pub const Allocator = struct {
/// Allocate byte_count bytes and return them in a slice, with the
/// slice's pointer aligned at least to alignment bytes.
@@ -166,17 +164,24 @@ pub fn set(comptime T: type, dest: []T, value: T) {
for (dest) |*d| *d = value;
}
-/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
-/// memory b, respectively.
-pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
- const n = math.min(a.len, b.len);
+/// Returns true if lhs < rhs, false otherwise
+pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) -> bool {
+ const n = math.min(lhs.len, rhs.len);
var i: usize = 0;
while (i < n) : (i += 1) {
- if (a[i] == b[i]) continue;
- return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;
+ if (lhs[i] == rhs[i]) continue;
+ return lhs[i] < rhs[i];
}
- return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;
+ return lhs.len < rhs.len;
+}
+
+test "mem.lessThan" {
+ assert(lessThan(u8, "abcd", "bee"));
+ assert(!lessThan(u8, "abc", "abc"));
+ assert(lessThan(u8, "abc", "abc0"));
+ assert(!lessThan(u8, "", ""));
+ assert(lessThan(u8, "", "a"));
}
/// Compares two slices and returns whether they are equal.