| ... | ... | @@ -175,16 +175,46 @@ pub fn set(comptime T: type, dest: []T, value: T) void { |
| 175 | 175 | d.* = value; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | | /// Returns true if lhs < rhs, false otherwise |
| 179 | | pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool { |
| 178 | pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare { |
| 180 | 179 | const n = math.min(lhs.len, rhs.len); |
| 181 | 180 | var i: usize = 0; |
| 182 | 181 | while (i < n) : (i += 1) { |
| 183 | | if (lhs[i] == rhs[i]) continue; |
| 184 | | return lhs[i] < rhs[i]; |
| 182 | if (lhs[i] == rhs[i]) { |
| 183 | continue; |
| 184 | } else if (lhs[i] < rhs[i]) { |
| 185 | return Compare.LessThan; |
| 186 | } else if (lhs[i] > rhs[i]) { |
| 187 | return Compare.GreaterThan; |
| 188 | } else { |
| 189 | unreachable; |
| 190 | } |
| 185 | 191 | } |
| 186 | 192 | |
| 187 | | return lhs.len < rhs.len; |
| 193 | if (lhs.len == rhs.len) { |
| 194 | return Compare.Equal; |
| 195 | } else if (lhs.len < rhs.len) { |
| 196 | return Compare.LessThan; |
| 197 | } else if (lhs.len > rhs.len) { |
| 198 | return Compare.GreaterThan; |
| 199 | } |
| 200 | unreachable; |
| 201 | } |
| 202 | |
| 203 | test "mem.compare" { |
| 204 | assert(compare(u8, "abcd", "bee") == Compare.LessThan); |
| 205 | assert(compare(u8, "abc", "abc") == Compare.Equal); |
| 206 | assert(compare(u8, "abc", "abc0") == Compare.LessThan); |
| 207 | assert(compare(u8, "", "") == Compare.Equal); |
| 208 | assert(compare(u8, "", "a") == Compare.LessThan); |
| 209 | } |
| 210 | |
| 211 | /// Returns true if lhs < rhs, false otherwise |
| 212 | pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool { |
| 213 | var result = compare(T, lhs, rhs); |
| 214 | if (result == Compare.LessThan) { |
| 215 | return true; |
| 216 | } else |
| 217 | return false; |
| 188 | 218 | } |
| 189 | 219 | |
| 190 | 220 | test "mem.lessThan" { |