From a85a352c669438abcbbb85af620e279bfa38623f Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Tue, 19 Nov 2024 22:13:32 +0100 Subject: [PATCH] feat: Use fastb64z for encoding and decoding --- build.zig | 5 ++++- build.zig.zon | 4 ++++ src/main.zig | 9 +++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index b35660b..d80a261 100644 --- a/build.zig +++ b/build.zig @@ -6,9 +6,11 @@ pub fn build(b: *std.Build) void { const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize }); 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(); - opt.addOption([]const u8, "version", "0.0.2"); + opt.addOption([]const u8, "version", "0.0.3"); const exe = b.addExecutable(.{ .name = "b64", .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("clap", clap); + exe.root_module.addImport("fastb64z", fastb64z); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); diff --git a/build.zig.zon b/build.zig.zon index 3ba1ebb..053da10 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,6 +4,10 @@ .paths = .{""}, .minimum_zig_version = "0.13.0", .dependencies = .{ + .fastb64z = .{ + .url = "https://github.com/joadnacer/fastb64z/archive/17f00616b661efa6e02065c4c9a0e7b4682e0a16.tar.gz", + .hash = "1220e16c6b41749367d8663ba8acc356fdafce8b0fef40edb59ce0525e0683392eda", + }, .clap = .{ .url = "https://github.com/Hejsil/zig-clap/archive/master/latest.tar.gz", .hash = "1220c900c70daf3e715fad6f266ec14b1d0f5e6c2d3f34b32142f60306cb9b5e5f05", diff --git a/src/main.zig b/src/main.zig index 9a92fda..172c05e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const clap = @import("clap"); const build_info = @import("build_info"); const base64 = std.base64; +const fastb64z = @import("fastb64z"); const debug = std.debug; const io = std.io; @@ -62,7 +63,7 @@ pub fn main() !void { const encoded = try encode(allocator, input.items); defer allocator.free(encoded); - try stdout.writer().print("{s}", .{encoded}); + try stdout.writer().print("{s}\n", .{encoded}); return; } @@ -121,7 +122,7 @@ fn version() void { fn read_from_stream(allocator: std.mem.Allocator, stream: std.fs.File) !std.ArrayList(u8) { 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()); 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 { - const encoder = std.base64.standard.Encoder; + const encoder = fastb64z.standard.Encoder; const encoded = try allocator.alloc(u8, encoder.calcSize(string.len)); 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 { - const decoder = std.base64.standard.Decoder; + const decoder = fastb64z.standard.Decoder; const decodeSize = decoder.calcSizeForSlice(b64string) catch |err| { return err; };