aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2023-01-19 18:44:09 +0200
committerVeikka Tuominen <git@vexu.eu>2023-01-22 00:12:37 +0200
commit5f5ab491684f0f5dd235189416f9c3d85e8e9be0 (patch)
tree51d450c61c1ece9b6208d0904ab306f660502251 /src/value.zig
parenta492a607d5410b1136db3a63fabd01c10827144c (diff)
downloadzig-5f5ab491684f0f5dd235189416f9c3d85e8e9be0.tar.gz
zig-5f5ab491684f0f5dd235189416f9c3d85e8e9be0.zip
Value: implement `compareAllWithZero` for `bytes` and `str_lit`
Closes #10692
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig34
1 files changed, 29 insertions, 5 deletions
diff --git a/src/value.zig b/src/value.zig
index 5d38bcb7a9..306e31c0a7 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -2076,13 +2076,22 @@ pub const Value = extern union {
/// For vectors, returns true if comparison is true for ALL elements.
///
/// Note that `!compareAllWithZero(.eq, ...) != compareAllWithZero(.neq, ...)`
- pub fn compareAllWithZero(lhs: Value, op: std.math.CompareOperator) bool {
- return compareAllWithZeroAdvanced(lhs, op, null) catch unreachable;
+ pub fn compareAllWithZero(lhs: Value, op: std.math.CompareOperator, mod: *Module) bool {
+ return compareAllWithZeroAdvancedExtra(lhs, op, mod, null) catch unreachable;
}
pub fn compareAllWithZeroAdvanced(
lhs: Value,
op: std.math.CompareOperator,
+ sema: *Sema,
+ ) Module.CompileError!bool {
+ return compareAllWithZeroAdvancedExtra(lhs, op, sema.mod, sema);
+ }
+
+ pub fn compareAllWithZeroAdvancedExtra(
+ lhs: Value,
+ op: std.math.CompareOperator,
+ mod: *Module,
opt_sema: ?*Sema,
) Module.CompileError!bool {
if (lhs.isInf()) {
@@ -2095,10 +2104,25 @@ pub const Value = extern union {
}
switch (lhs.tag()) {
- .repeated => return lhs.castTag(.repeated).?.data.compareAllWithZeroAdvanced(op, opt_sema),
+ .repeated => return lhs.castTag(.repeated).?.data.compareAllWithZeroAdvancedExtra(op, mod, opt_sema),
.aggregate => {
for (lhs.castTag(.aggregate).?.data) |elem_val| {
- if (!(try elem_val.compareAllWithZeroAdvanced(op, opt_sema))) return false;
+ if (!(try elem_val.compareAllWithZeroAdvancedExtra(op, mod, opt_sema))) return false;
+ }
+ return true;
+ },
+ .str_lit => {
+ const str_lit = lhs.castTag(.str_lit).?.data;
+ const bytes = mod.string_literal_bytes.items[str_lit.index..][0..str_lit.len];
+ for (bytes) |byte| {
+ if (!std.math.compare(byte, op, 0)) return false;
+ }
+ return true;
+ },
+ .bytes => {
+ const bytes = lhs.castTag(.bytes).?.data;
+ for (bytes) |byte| {
+ if (!std.math.compare(byte, op, 0)) return false;
}
return true;
},
@@ -3103,7 +3127,7 @@ pub const Value = extern union {
.int_i64,
.int_big_positive,
.int_big_negative,
- => compareAllWithZero(self, .eq),
+ => self.orderAgainstZero().compare(.eq),
.undef => unreachable,
.unreachable_value => unreachable,