feat: Use fastb64z for encoding and decoding

This commit is contained in:
Kalle Carlbark 2024-11-19 22:13:32 +01:00
parent 74536957b0
commit a85a352c66
No known key found for this signature in database
3 changed files with 13 additions and 5 deletions

View file

@ -6,9 +6,11 @@ pub fn build(b: *std.Build) void {
const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize }); const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize });
const clap = clap_dep.module("clap"); const clap = clap_dep.module("clap");
const fastb64z_dep = b.dependency("fastb64z", .{ .target = target, .optimize = optimize });
const fastb64z = fastb64z_dep.module("fastb64z");
var opt = b.addOptions(); var opt = b.addOptions();
opt.addOption([]const u8, "version", "0.0.2"); opt.addOption([]const u8, "version", "0.0.3");
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "b64", .name = "b64",
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
@ -17,6 +19,7 @@ pub fn build(b: *std.Build) void {
}); });
exe.root_module.addImport("build_info", opt.createModule()); exe.root_module.addImport("build_info", opt.createModule());
exe.root_module.addImport("clap", clap); exe.root_module.addImport("clap", clap);
exe.root_module.addImport("fastb64z", fastb64z);
b.installArtifact(exe); b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());

View file

@ -4,6 +4,10 @@
.paths = .{""}, .paths = .{""},
.minimum_zig_version = "0.13.0", .minimum_zig_version = "0.13.0",
.dependencies = .{ .dependencies = .{
.fastb64z = .{
.url = "https://github.com/joadnacer/fastb64z/archive/17f00616b661efa6e02065c4c9a0e7b4682e0a16.tar.gz",
.hash = "1220e16c6b41749367d8663ba8acc356fdafce8b0fef40edb59ce0525e0683392eda",
},
.clap = .{ .clap = .{
.url = "https://github.com/Hejsil/zig-clap/archive/master/latest.tar.gz", .url = "https://github.com/Hejsil/zig-clap/archive/master/latest.tar.gz",
.hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05", .hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05",

View file

@ -2,6 +2,7 @@ const std = @import("std");
const clap = @import("clap"); const clap = @import("clap");
const build_info = @import("build_info"); const build_info = @import("build_info");
const base64 = std.base64; const base64 = std.base64;
const fastb64z = @import("fastb64z");
const debug = std.debug; const debug = std.debug;
const io = std.io; const io = std.io;
@ -62,7 +63,7 @@ pub fn main() !void {
const encoded = try encode(allocator, input.items); const encoded = try encode(allocator, input.items);
defer allocator.free(encoded); defer allocator.free(encoded);
try stdout.writer().print("{s}", .{encoded}); try stdout.writer().print("{s}\n", .{encoded});
return; return;
} }
@ -121,7 +122,7 @@ fn version() void {
fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.ArrayList(u8) { fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.ArrayList(u8) {
var input = std.ArrayList(u8).init(allocator); var input = std.ArrayList(u8).init(allocator);
var fifo = std.fifo.LinearFifo(u8, .{ .Static = 1024 }).init(); var fifo = std.fifo.LinearFifo(u8, .{ .Static = 8192 }).init();
try fifo.pump(stream.reader(), input.writer()); try fifo.pump(stream.reader(), input.writer());
defer fifo.deinit(); defer fifo.deinit();
@ -130,7 +131,7 @@ fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.Arra
} }
fn encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 { fn encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 {
const encoder = std.base64.standard.Encoder; const encoder = fastb64z.standard.Encoder;
const encoded = try allocator.alloc(u8, encoder.calcSize(string.len)); const encoded = try allocator.alloc(u8, encoder.calcSize(string.len));
defer allocator.free(encoded); defer allocator.free(encoded);
@ -141,7 +142,7 @@ fn encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 {
} }
fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 { fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
const decoder = std.base64.standard.Decoder; const decoder = fastb64z.standard.Decoder;
const decodeSize = decoder.calcSizeForSlice(b64string) catch |err| { const decodeSize = decoder.calcSizeForSlice(b64string) catch |err| {
return err; return err;
}; };