Skip to content

Commit 39e96d9

Browse files
committedDec 15, 2017
change mem.cmp to mem.lessThan and add test
·
0.15.10.2.0
1 parent 68f6332 commit 39e96d9

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed
 

‎std/mem.zig‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const assert = debug.assert;
33
const math = @import("math/index.zig");
44
const builtin = @import("builtin");
55

6-
pub const Cmp = math.Cmp;
7-
86
pub const Allocator = struct {
97
/// Allocate byte_count bytes and return them in a slice, with the
108
/// slice's pointer aligned at least to alignment bytes.
@@ -166,17 +164,24 @@ pub fn set(comptime T: type, dest: []T, value: T) {
166164
for (dest) |*d| *d = value;
167165
}
168166

169-
/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
170-
/// memory b, respectively.
171-
pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
172-
const n = math.min(a.len, b.len);
167+
/// Returns true if lhs < rhs, false otherwise
168+
pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) -> bool {
169+
const n = math.min(lhs.len, rhs.len);
173170
var i: usize = 0;
174171
while (i < n) : (i += 1) {
175-
if (a[i] == b[i]) continue;
176-
return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;
172+
if (lhs[i] == rhs[i]) continue;
173+
return lhs[i] < rhs[i];
177174
}
178175

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"));
180185
}
181186

182187
/// Compares two slices and returns whether they are equal.

2 commit comments

Comments
 (2)

shawnl commented on Dec 16, 2017

@shawnl
Contributor

whats was wrong with mem.cmp? Are you just trying to be differn't from libc?

Hejsil commented on Dec 16, 2017

@Hejsil
Contributor

@shawnl
@andrewrk and I had a similar discussion one the commit where he refactored std.sort:
75ecfdf#diff-495fa39e543ad33fe8a074ec43e49d0d

Please sign in to comment.