diff options
Diffstat (limited to '_tests/switchtable.zig')
| -rw-r--r-- | _tests/switchtable.zig | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/_tests/switchtable.zig b/_tests/switchtable.zig new file mode 100644 index 0000000..722ecdd --- /dev/null +++ b/_tests/switchtable.zig @@ -0,0 +1,93 @@ +const std = @import("std"); + +const Reader = std.io.AnyReader; + +pub fn main() !u8 { + return f(); +} + +fn f() !u8 { + const file = try std.fs.cwd().openFile("string", .{}); + defer file.close(); + + var br = std.io.bufferedReader(file.reader()); + const r = br.reader().any(); + + var n: u8 = 0; + for (0..1_000_000) |i| { + _ = i; + while (r.readByte() catch null) |c| { + n +%= try f1(r, c); + } + br.start = 0; + br.end = 0; + try file.seekTo(0); + } + return n; +} + +fn f1(r: Reader, c1: u8) !u8 { + if (c1 != '\\') return c1; + const c = try r.readByte(); + if (c == 'u') return unknown1(); + return switch (c) { + '\\', '"' => c, + '0' => 0, + 'a' => 7, + 'b' => 8, + 't' => 9, + 'n' => 10, + 'v' => 11, + 'f' => 12, + 'r' => 13, + 'e' => 27, + 'x' => unknown2(), + else => unknown3(), + }; +} + +fn f2(r: Reader, c1: u8) !u8 { + if (c1 != '\\') return c1; + const c = try r.readByte(); + if (c == 'u') return unknown1(); + if (c == 'x') return unknown2(); + if (c == '\\' or c == '"') return c; + const itable = .{ '0', 'a', 'b', 't', 'n', 'v', 'f', 'r', 'e' }; + const ctable: []const u8 = &.{ 0, 7, 8, 9, 10, 11, 12, 13, 27 }; + const i = std.mem.indexOfScalar(u8, &itable, c) orelse return unknown3(); + return ctable[i]; +} + +fn f3(r: Reader, c1: u8) !u8 { + if (c1 != '\\') return c1; + const c = try r.readByte(); + if (c == 'u') return unknown1(); + if (c == 'x') return unknown2(); + if (c == '\\' or c == '"') return c; + if (c == '0') return 0; + const table = comptime t: { + var table: [26]u8 = .{0} ** 26; + table['a' - 'a'] = 7; + table['b' - 'a'] = 8; + table['t' - 'a'] = 9; + table['n' - 'a'] = 10; + table['v' - 'a'] = 11; + table['f' - 'a'] = 12; + table['r' - 'a'] = 13; + table['e' - 'a'] = 27; + break :t table; + }; + if (c < 'a') return unknown3(); + const result = table[c - 'a']; + return if (result != 0) result else unknown3(); +} + +fn unknown1() u8 { + return 0; +} +fn unknown2() u8 { + return 0; +} +fn unknown3() u8 { + return 0; +} |
