| ... | @@ -647,12 +647,14 @@ pub fn sortUnstableContext(a: usize, b: usize, context: anytype) void { | ... | @@ -647,12 +647,14 @@ pub fn sortUnstableContext(a: usize, b: usize, context: anytype) void { |
| 647 | | 647 | |
| 648 | /// Compares two slices of numbers lexicographically. O(n). | 648 | /// Compares two slices of numbers lexicographically. O(n). |
| 649 | pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { | 649 | pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { |
| 650 | const n = @min(lhs.len, rhs.len); | 650 | if (lhs.ptr != rhs.ptr) { |
| 651 | for (lhs[0..n], rhs[0..n]) |lhs_elem, rhs_elem| { | 651 | const n = @min(lhs.len, rhs.len); |
| 652 | switch (math.order(lhs_elem, rhs_elem)) { | 652 | for (lhs[0..n], rhs[0..n]) |lhs_elem, rhs_elem| { |
| 653 | .eq => continue, | 653 | switch (math.order(lhs_elem, rhs_elem)) { |
| 654 | .lt => return .lt, | 654 | .eq => continue, |
| 655 | .gt => return .gt, | 655 | .lt => return .lt, |
| | 656 | .gt => return .gt, |
| | 657 | } |
| 656 | } | 658 | } |
| 657 | } | 659 | } |
| 658 | return math.order(lhs.len, rhs.len); | 660 | return math.order(lhs.len, rhs.len); |
| ... | @@ -660,6 +662,7 @@ pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { | ... | @@ -660,6 +662,7 @@ pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { |
| 660 | | 662 | |
| 661 | /// Compares two many-item pointers with NUL-termination lexicographically. | 663 | /// Compares two many-item pointers with NUL-termination lexicographically. |
| 662 | pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order { | 664 | pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order { |
| | 665 | if (lhs == rhs) return .eq; |
| 663 | var i: usize = 0; | 666 | var i: usize = 0; |
| 664 | while (lhs[i] == rhs[i] and lhs[i] != 0) : (i += 1) {} | 667 | while (lhs[i] == rhs[i] and lhs[i] != 0) : (i += 1) {} |
| 665 | return math.order(lhs[i], rhs[i]); | 668 | return math.order(lhs[i], rhs[i]); |
| ... | @@ -671,6 +674,10 @@ test order { | ... | @@ -671,6 +674,10 @@ test order { |
| 671 | try testing.expect(order(u8, "abc", "abc0") == .lt); | 674 | try testing.expect(order(u8, "abc", "abc0") == .lt); |
| 672 | try testing.expect(order(u8, "", "") == .eq); | 675 | try testing.expect(order(u8, "", "") == .eq); |
| 673 | try testing.expect(order(u8, "", "a") == .lt); | 676 | try testing.expect(order(u8, "", "a") == .lt); |
| | 677 | |
| | 678 | const s: []const u8 = "abc"; |
| | 679 | try testing.expect(order(u8, s, s) == .eq); |
| | 680 | try testing.expect(order(u8, s[0..2], s) == .lt); |
| 674 | } | 681 | } |
| 675 | | 682 | |
| 676 | test orderZ { | 683 | test orderZ { |
| ... | @@ -679,6 +686,9 @@ test orderZ { | ... | @@ -679,6 +686,9 @@ test orderZ { |
| 679 | try testing.expect(orderZ(u8, "abc", "abc0") == .lt); | 686 | try testing.expect(orderZ(u8, "abc", "abc0") == .lt); |
| 680 | try testing.expect(orderZ(u8, "", "") == .eq); | 687 | try testing.expect(orderZ(u8, "", "") == .eq); |
| 681 | try testing.expect(orderZ(u8, "", "a") == .lt); | 688 | try testing.expect(orderZ(u8, "", "a") == .lt); |
| | 689 | |
| | 690 | const s: [*:0]const u8 = "abc"; |
| | 691 | try testing.expect(orderZ(u8, s, s) == .eq); |
| 682 | } | 692 | } |
| 683 | | 693 | |
| 684 | /// Returns true if lhs < rhs, false otherwise | 694 | /// Returns true if lhs < rhs, false otherwise |