diff --git a/qrcode.zig b/qrcode.zig index a0822ba0968f37225ef565e1091a491cc92aaa8f..a7e76deb85c29dcb6de8560f94e18c7c3a093e78 100644 --- a/qrcode.zig +++ b/qrcode.zig @@ -729,7 +729,7 @@ fn generatorPolynomial(comptime n: usize) [n + 1]u8 { fn to_alpha(array: anytype) [array.len]u8 { var result: [array.len]u8 = array; - for (&result) |*x| x.* = @intCast(std.mem.indexOfScalar(u8, &GF, x.*).?); + for (&result) |*x| x.* = @intCast(std.mem.indexOfScalar(u8, &GF, x.*) orelse 0); return result; } diff --git a/test.zig b/test.zig index 71cb85e06e526568bf39f5443f88a6632f816699..4fe7f52abf3e3e42523a48d30098b1dc951df9ba 100644 --- a/test.zig +++ b/test.zig @@ -1070,3 +1070,113 @@ test { else => unreachable, } } + +test { + const allocator = std.testing.allocator; + const input = "otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&algorithm=SHA256&digits=6&period=30&issuer=ACME%20Co"; + const version = qrcode.getMinVersion(.L, .latin1, input.len).?; + switch (version) { + inline 1...40 => |v| { + const grid = qrcode.encode(.latin1, .L, v, input); + const blocks = try grid.asBlockString2(allocator); + defer allocator.free(blocks); + try expect(blocks).toEqualString( + \\█▀▀▀▀▀█ ▀█▄▄ ▀ █ █ ▄▀▀ ▀ ██▄█▄▀▀ ▄▀ █▀▀▀▀▀█| + \\█ ███ █ █▄▄█▀▄ ▄▀█▄▀ █▀█▄█▄█ ▄ ▄▀ █▄ █ ███ █| + \\█ ▀▀▀ █ ▀█▄ ▄▀██ █▄██▀▀▀█ ██ █▄ ▀ ▀▀▀ █ ▀▀▀ █| + \\▀▀▀▀▀▀▀ ▀ ▀▄▀ ▀▄▀ █ █ ▀ █ █ █ █ ▀▄▀▄█ ▀▀▀▀▀▀▀| + \\██▀█ ▄▀ ▀█ █▀ ▀ █ ▄ █▀▀█▀▄▀▄▀▄█ █▀▄▄█ ▄▀▀█ █| + \\███▀▄ ▀ ▄▄ ▄▄▄▀█▄ ▀ ▄▀▄▄ █▀██ ▄ █▄▄▄ █ █▀█▀| + \\▀▀ ▄▀ ▄▀▀█ █▄▄ █▀ ▄ ▀▄█ ▄▀ █▀█▄ ▀▄█▄▄▄▀ ▄| + \\▄▀█▄ ▀▀███ ██ ▄█▀▀█ ▀▀▀ ██████▀█▀ ▀███▀▄█▄ | + \\▀ ▀▀█▀▀█▀▀▀ █ ▀▀▄ ▀ ▄ █▄▀ █▄█ ▀█▀▄█ ▀ █ ▄█ ▄| + \\█▄▄ ▀▄▄▀▀▄▀ ▄▄▀█▄ ▀▀▄█▀█ ▀▀ ▀█ ▀▀ █▀ ▀▄| + \\█ ▄█▀▀▀█ ▀▀▀█▄ ▀▀▄██▀▀▀██▀▀█ █▄▄▄ ▀█▀▀▀██▄▄ | + \\██▀▀█ ▀ █▀▀▄ █▄▀██ ▄█ ▀ █ ▄▀▀████▀█ █ ▀ █▄ ▀▀| + \\▀█▀▀▀█▀▀█ █▀▀██▄ ▀▀▀▀▀▀▀▀▄▄ ▀ ▀ ▀▀█████▀█▄█▀| + \\ ▄▀██▀▀▄▀█ ▀▄ ▄ ▀▀█ ███ █▄█▄█▀█▀ ▀ ▄▄▀ █▄▄| + \\ █▀▄█▀ ▀ █ █▀▀█▄ ▀ ▀▄ ▀▀▀█▄█▀▀▀▀▄▀▀█▀ ██▀█ | + \\██ ▄▄█▀█ ██▀ ▄▄▀▀█▄ ▀▀▀ █▀▀█▀ ▀ ▀ █ ▄█ ▀▀▄| + \\ ▄ █ ▄▀▄▄ ▄ █ █▀▀█▀▄▀▀█▄▄▀▀▀█▄ ▄ ▀ ▀ ▄█ ▄| + \\ ▄▄▄█ ▀█ ▄▀██ ▀▄▄▄▄████▀█ ▀▀▀ ▄▄▄▄ ▄██▄ █▀█▀| + \\▀ ▀▀ ▀ █▄▄ ▀▀▄█ ▀▀ █▀▀▀███ ▄ ▀▀▄▀▀ █▀▀▀█▄▄▄█| + \\█▀▀▀▀▀█ ▄ █▄ █▄ ▄▄ █ ▀ █ ▀▄█ █▄▄▄ ██ ▀ █ ▄ ▄| + \\█ ███ █ ▄▄█ █ ▄ █ ▀███▀▀ █▄█ ███ ██▀▀▀ ▀█▀| + \\█ ▀▀▀ █ █▄▄█▀▀ ██ █▀▄▀▄▀█ █▄▄ ▀▀█▀█▀ ▄▀▄▀▀ █| + \\▀▀▀▀▀▀▀ ▀ ▀▀ ▀▀▀ ▀▀ ▀▀▀▀ ▀▀▀ ▀ | + \\ + ); + const braille = try grid.asBrailleString(allocator); + defer allocator.free(braille); + try expect(braille).toEqualString( + \\⡏⣭⡍⡇⣝⣲⢌⢘⢤⡸⢰⢭⣨⣸⢓⢓⠍⢰⡁⡏⣭⡍⡇| + \\⠧⠭⠥⠇⠝⢆⠎⢟⠜⡞⡏⠍⡇⡟⡜⡆⢅⢍⡅⠧⠭⠥⠇| + \\⣿⡽⡐⠅⠙⣘⢁⣁⢧⡂⠏⡹⣑⢱⢵⡇⡘⣕⣚⢰⢩⢧⠇| + \\⡩⣄⠐⠥⣮⡝⣼⢒⡼⢥⠢⠵⢣⣮⣼⡽⡦⠠⣵⡷⣲⡁⠂| + \\⣅⡉⠋⢝⡩⢅⠇⠉⢂⡡⣔⠸⢎⡼⡞⠨⠏⢾⠨⠜⡴⠣⡂| + \\⣧⠴⡏⠍⡧⢍⢹⡢⣭⢚⡏⠍⡟⡩⢧⣷⡶⡌⡏⠍⣟⠲⠄| + \\⢙⢭⡽⢍⢧⠸⡉⠛⡂⠭⡍⣭⡍⣖⣌⡬⡬⠹⢛⡻⠙⣞⡁| + \\⣤⢋⣺⢥⢡⡼⢘⡩⢷⡈⠬⠆⢩⠽⡾⠉⠍⠎⢹⢁⡟⠽⡀| + \\⢐⣘⡔⢵⢂⢴⡜⠸⣉⣋⣮⣽⢶⠩⠽⢂⣂⡁⣡⣄⢰⢧⠆| + \\⡥⠬⠥⡅⢓⢢⡉⣞⢈⡁⡏⠍⡟⢇⡆⣍⣊⢡⡏⠍⡗⡒⡃| + \\⡇⠿⠇⡇⣖⣣⠼⢰⡜⡤⡹⡻⡍⣜⡚⠠⢿⢧⠟⡩⡡⠝⡅| + \\⠉⠉⠉⠁⠁⠉⠀⠈⠉⠀⠀⠉⠀⠉⠉⠀⠀⠀⠉⠁⠀⠁⠀| + \\ + ); + const svg = try grid.asSvg(allocator); + defer allocator.free(svg); + try expect(svg).toEqualString( + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + \\ + ); + }, + else => unreachable, + } +}