@@ -14,7 +14,7 @@ pub fn StaticStringMapIgnoreCase(comptime V: type) type {
14
14
/// Same as StaticStringMap, but allows you to provide the `eql` function yourself.
15
15
pub fn StaticStringMapWithEql (
16
16
comptime V : type ,
17
- comptime eql : fn (comptime usize , comptime anytype , anytype ) bool ,
17
+ comptime eql : fn (comptime anytype , anytype ) bool ,
18
18
) type {
19
19
return StaticArrayMapWithEql (V , u8 , eql );
20
20
}
@@ -34,7 +34,11 @@ pub fn defaultEql(comptime expected: anytype, actual: anytype) bool {
34
34
// which exceed 65535 bits (there are no integer types that large).
35
35
const unique = std .meta .hasUniqueRepresentation (Array );
36
36
const too_many_bits = child_bits > 65535 ;
37
- if (comptime ! unique or too_many_bits ) {
37
+
38
+ // TODO: riscv64 backend can't airBitCast [7]u8 to u56
39
+ const limited_backend = @import ("builtin" ).zig_backend == .stage2_riscv64 ;
40
+
41
+ if (! unique or too_many_bits or limited_backend ) {
38
42
var match : bool = true ;
39
43
for (expected , actual ) | a , b | {
40
44
match = match and a == b ;
@@ -49,7 +53,7 @@ pub fn defaultEql(comptime expected: anytype, actual: anytype) bool {
49
53
}
50
54
51
55
fn ignoreCaseEql (comptime expected : anytype , actual : anytype ) bool {
52
- const lower_expected = comptime toLowerSimd (expected );
56
+ const lower_expected = toLowerSimd (expected );
53
57
54
58
// TODO: x86_64 self hosted backend hasn't implemented genBinOp for cmp_gte
55
59
const lower_actual = blk : {
@@ -129,7 +133,7 @@ pub fn StaticArrayMapWithEql(
129
133
130
134
/// Returns the value for the key if any, else null.
131
135
pub fn get (comptime self : Self , key : []const T ) ? V {
132
- switch (comptime self .kvs .len ) {
136
+ switch (self .kvs .len ) {
133
137
0 = > return null ,
134
138
else = > return self .filterLength (key ),
135
139
}
@@ -461,6 +465,31 @@ test "array elements that are padded" {
461
465
try testing .expectEqual (2 , map .get (&.{ 0 , 1 , 2 , 126 , 4 , 5 }));
462
466
}
463
467
468
+ fn lastElementEql (comptime expected : anytype , actual : anytype ) bool {
469
+ if (expected .len == 0 ) {
470
+ return false ;
471
+ } else {
472
+ const last_idx = expected .len - 1 ;
473
+ return expected [last_idx ] == actual [last_idx ];
474
+ }
475
+ }
476
+
477
+ test "custom equal function" {
478
+ const map = StaticStringMapWithEql (u2 , lastElementEql ).initComptime (.{
479
+ .{ "last byte is a t" , 0 },
480
+ .{ "last byte is a b" , 1 },
481
+ .{ "last byte is a s" , 2 },
482
+ .{ "last byte is a c" , 3 },
483
+ });
484
+
485
+ // limitation: eql functions only are called on same-length inputs
486
+ try testing .expectEqual (false , map .has ("t" ));
487
+
488
+ try testing .expectEqual (1 , map .get ("my magic byte: b" ));
489
+ try testing .expectEqual (0 , map .get ("my magic byte: t" ));
490
+ try testing .expectEqual (2 , map .get ("my magic byte: s" ));
491
+ }
492
+
464
493
test "single string StaticStringMap" {
465
494
const map = StaticStringMap (void ).initComptime (.{.{"o kama pona" }});
466
495
0 commit comments