authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-01 18:08:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-01 18:08:40-05:00
log5575e2a168c07d2dcc0e58146231e490ef8a898e
tree7cd93a54d6012066a9daf63465c40b342946ecdd
parent7b62d5dfd872de8719cc05c2486f77b261e863e9
signaturelock-open Commit is signed but in an unrecognized format.

std.mem.compare: breaking API changes

* `std.mem.Compare` is now `std.math.Order` and the enum tags renamed to follow new style convention. * `std.mem.compare` is renamed to `std.mem.order`. * new function `std.math.order`

5 files changed, 63 insertions(+), 61 deletions(-)

lib/std/crypto/chacha20.zig+1-1
......@@ -224,7 +224,7 @@ test "crypto.chacha20 test vector sunscreen" {
224224 // Chacha20 is self-reversing.
225225 var plaintext: [114]u8 = undefined;
226226 chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce);
227 testing.expect(mem.compare(u8, input, &plaintext) == mem.Compare.Equal);
227 testing.expect(mem.order(u8, input, &plaintext) == .eq);
228228}
229229
230230// https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
lib/std/http/headers.zig+2-2
......@@ -70,12 +70,12 @@ const HeaderEntry = struct {
7070 }
7171
7272 // Sort lexicographically on header name
73 return mem.compare(u8, a.name, b.name) == mem.Compare.LessThan;
73 return mem.order(u8, a.name, b.name) == .lt;
7474 }
7575
7676 // Sort lexicographically on header value
7777 if (!mem.eql(u8, a.value, b.value)) {
78 return mem.compare(u8, a.value, b.value) == mem.Compare.LessThan;
78 return mem.order(u8, a.value, b.value) == .lt;
7979 }
8080
8181 // Doesn't matter here; need to pick something for sort consistency
lib/std/math.zig+27-5
......@@ -926,9 +926,6 @@ test "minInt and maxInt" {
926926}
927927
928928test "max value type" {
929 // If the type of maxInt(i32) was i32 then this implicit cast to
930 // u32 would not work. But since the value is a number literal,
931 // it works fine.
932929 const x: u32 = maxInt(i32);
933930 testing.expect(x == 2147483647);
934931}
......@@ -944,7 +941,32 @@ test "math.mulWide" {
944941 testing.expect(mulWide(u8, 100, 100) == 10000);
945942}
946943
947/// Not to be confused with `std.mem.Compare`.
944/// See also `CompareOperator`.
945pub const Order = enum {
946 /// Less than (`<`)
947 lt,
948
949 /// Equal (`==`)
950 eq,
951
952 /// Greater than (`>`)
953 gt,
954};
955
956/// Given two numbers, this function returns the order they are with respect to each other.
957pub fn order(a: var, b: var) Order {
958 if (a == b) {
959 return .eq;
960 } else if (a < b) {
961 return .lt;
962 } else if (a > b) {
963 return .gt;
964 } else {
965 unreachable;
966 }
967}
968
969/// See also `Order`.
948970pub const CompareOperator = enum {
949971 /// Less than (`<`)
950972 lt,
......@@ -979,7 +1001,7 @@ pub fn compare(a: var, op: CompareOperator, b: var) bool {
9791001 };
9801002}
9811003
982test "math.lt, et al < <= > >= between signed and unsigned" {
1004test "compare between signed and unsigned" {
9831005 testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255)));
9841006 testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1)));
9851007 testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255)));
lib/std/mem.zig+13-35
......@@ -239,12 +239,6 @@ pub const Allocator = struct {
239239 }
240240};
241241
242pub const Compare = enum {
243 LessThan,
244 Equal,
245 GreaterThan,
246};
247
248242/// Copy all of source into dest at position 0.
249243/// dest.len must be >= source.len.
250244/// dest.ptr must be <= src.ptr.
......@@ -297,46 +291,30 @@ test "mem.secureZero" {
297291 testing.expectEqualSlices(u8, a[0..], b[0..]);
298292}
299293
300pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare {
294pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
301295 const n = math.min(lhs.len, rhs.len);
302296 var i: usize = 0;
303297 while (i < n) : (i += 1) {
304 if (lhs[i] == rhs[i]) {
305 continue;
306 } else if (lhs[i] < rhs[i]) {
307 return Compare.LessThan;
308 } else if (lhs[i] > rhs[i]) {
309 return Compare.GreaterThan;
310 } else {
311 unreachable;
298 switch (math.order(lhs[i], rhs[i])) {
299 .eq => continue,
300 .lt => return .lt,
301 .gt => return .gt,
312302 }
313303 }
314
315 if (lhs.len == rhs.len) {
316 return Compare.Equal;
317 } else if (lhs.len < rhs.len) {
318 return Compare.LessThan;
319 } else if (lhs.len > rhs.len) {
320 return Compare.GreaterThan;
321 }
322 unreachable;
304 return math.order(lhs.len, rhs.len);
323305}
324306
325test "mem.compare" {
326 testing.expect(compare(u8, "abcd", "bee") == Compare.LessThan);
327 testing.expect(compare(u8, "abc", "abc") == Compare.Equal);
328 testing.expect(compare(u8, "abc", "abc0") == Compare.LessThan);
329 testing.expect(compare(u8, "", "") == Compare.Equal);
330 testing.expect(compare(u8, "", "a") == Compare.LessThan);
307test "order" {
308 testing.expect(order(u8, "abcd", "bee") == .lt);
309 testing.expect(order(u8, "abc", "abc") == .eq);
310 testing.expect(order(u8, "abc", "abc0") == .lt);
311 testing.expect(order(u8, "", "") == .eq);
312 testing.expect(order(u8, "", "a") == .lt);
331313}
332314
333315/// Returns true if lhs < rhs, false otherwise
334316pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool {
335 var result = compare(T, lhs, rhs);
336 if (result == Compare.LessThan) {
337 return true;
338 } else
339 return false;
317 return order(T, lhs, rhs) == .lt;
340318}
341319
342320test "mem.lessThan" {
lib/std/rb.zig+20-18
......@@ -1,7 +1,7 @@
11const std = @import("std.zig");
22const assert = std.debug.assert;
33const testing = std.testing;
4const mem = std.mem; // For mem.Compare
4const Order = std.math.Order;
55
66const Color = enum(u1) {
77 Black,
......@@ -132,7 +132,7 @@ pub const Node = struct {
132132
133133pub const Tree = struct {
134134 root: ?*Node,
135 compareFn: fn (*Node, *Node) mem.Compare,
135 compareFn: fn (*Node, *Node) Order,
136136
137137 /// If you have a need for a version that caches this, please file a bug.
138138 pub fn first(tree: *Tree) ?*Node {
......@@ -389,7 +389,7 @@ pub const Tree = struct {
389389 var new = newconst;
390390
391391 // I assume this can get optimized out if the caller already knows.
392 if (tree.compareFn(old, new) != mem.Compare.Equal) return ReplaceError.NotEqual;
392 if (tree.compareFn(old, new) != .eq) return ReplaceError.NotEqual;
393393
394394 if (old.getParent()) |parent| {
395395 parent.setChild(new, parent.left == old);
......@@ -404,7 +404,7 @@ pub const Tree = struct {
404404 new.* = old.*;
405405 }
406406
407 pub fn init(tree: *Tree, f: fn (*Node, *Node) mem.Compare) void {
407 pub fn init(tree: *Tree, f: fn (*Node, *Node) Order) void {
408408 tree.root = null;
409409 tree.compareFn = f;
410410 }
......@@ -469,19 +469,21 @@ fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node {
469469 is_left.* = false;
470470
471471 while (maybe_node) |node| {
472 var res: mem.Compare = tree.compareFn(node, key);
473 if (res == mem.Compare.Equal) {
472 const res = tree.compareFn(node, key);
473 if (res == .eq) {
474474 return node;
475475 }
476476 pparent.* = node;
477 if (res == mem.Compare.GreaterThan) {
478 is_left.* = true;
479 maybe_node = node.left;
480 } else if (res == mem.Compare.LessThan) {
481 is_left.* = false;
482 maybe_node = node.right;
483 } else {
484 unreachable;
477 switch (res) {
478 .gt => {
479 is_left.* = true;
480 maybe_node = node.left;
481 },
482 .lt => {
483 is_left.* = false;
484 maybe_node = node.right;
485 },
486 .eq => unreachable, // handled above
485487 }
486488 }
487489 return null;
......@@ -496,16 +498,16 @@ fn testGetNumber(node: *Node) *testNumber {
496498 return @fieldParentPtr(testNumber, "node", node);
497499}
498500
499fn testCompare(l: *Node, r: *Node) mem.Compare {
501fn testCompare(l: *Node, r: *Node) Order {
500502 var left = testGetNumber(l);
501503 var right = testGetNumber(r);
502504
503505 if (left.value < right.value) {
504 return mem.Compare.LessThan;
506 return .lt;
505507 } else if (left.value == right.value) {
506 return mem.Compare.Equal;
508 return .eq;
507509 } else if (left.value > right.value) {
508 return mem.Compare.GreaterThan;
510 return .gt;
509511 }
510512 unreachable;
511513}