diff options
author | 2021-05-20 02:34:42 -0700 | |
---|---|---|
committer | 2021-05-20 02:34:42 -0700 | |
commit | 8b75f56577abc3ad2479b87fc59b7243add6a29a (patch) | |
tree | 5501729e5f81a6aa65ed986148956882e207d656 /src/string_immutable.zig | |
parent | 9541d268ded6ed38d739e62cb8b4cf597cf69193 (diff) | |
download | bun-8b75f56577abc3ad2479b87fc59b7243add6a29a.tar.gz bun-8b75f56577abc3ad2479b87fc59b7243add6a29a.tar.zst bun-8b75f56577abc3ad2479b87fc59b7243add6a29a.zip |
pico
Former-commit-id: cee857ac4e25638e5f93d838d54124ffaca938b7
Diffstat (limited to 'src/string_immutable.zig')
-rw-r--r-- | src/string_immutable.zig | 45 |
1 files changed, 1 insertions, 44 deletions
diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 663b714ea..ab19f4dc9 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -360,47 +360,4 @@ test "sortDesc" { std.testing.expectEqualStrings(sorted_join, string_join); } -pub fn ExactSizeMatcher(comptime max_bytes: usize) type { - const T = std.meta.Int( - .unsigned, - max_bytes * 8, - ); - - return struct { - pub fn match(str: anytype) T { - return hash(str) orelse std.math.maxInt(T); - } - - pub fn case(comptime str: []const u8) T { - return hash(str) orelse std.math.maxInt(T); - } - - fn hash(str: anytype) ?T { - if (str.len > max_bytes) return null; - var tmp = [_]u8{0} ** max_bytes; - std.mem.copy(u8, &tmp, str[0..str.len]); - return std.mem.readIntNative(T, &tmp); - } - - fn hashUnsafe(str: anytype) T { - var tmp = [_]u8{0} ** max_bytes; - std.mem.copy(u8, &tmp, str[0..str.len]); - return std.mem.readIntNative(T, &tmp); - } - }; -} - -const eight = ExactSizeMatcher(8); - -test "ExactSizeMatcher 5 letter" { - const word = "yield"; - expect(eight.match(word) == eight.case("yield")); - expect(eight.match(word) != eight.case("yields")); -} - -test "ExactSizeMatcher 4 letter" { - const Four = ExactSizeMatcher(4); - const word = "from"; - expect(Four.match(word) == Four.case("from")); - expect(Four.match(word) != Four.case("fro")); -} +pub usingnamespace @import("exact_size_matcher.zig"); |