Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
4feb810d93 | |||
8484063764 |
4 changed files with 47 additions and 3 deletions
|
@ -7,6 +7,9 @@ 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 opts = .{ .target = target, .optimize = optimize };
|
||||||
|
const zbench_module = b.dependency("zbench", opts).module("zbench");
|
||||||
|
|
||||||
var opt = b.addOptions();
|
var opt = b.addOptions();
|
||||||
opt.addOption([]const u8, "version", "0.0.2");
|
opt.addOption([]const u8, "version", "0.0.2");
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
|
@ -17,6 +20,8 @@ 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("zbench", zbench_module);
|
||||||
|
|
||||||
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());
|
||||||
|
|
|
@ -8,5 +8,9 @@
|
||||||
.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",
|
||||||
},
|
},
|
||||||
|
.zbench = .{
|
||||||
|
.url = "https://github.com/hendriknielaender/zbench/archive/0.13.0/latest.tar.gz",
|
||||||
|
.hash = "1220b7219ed9b700aaac2a5f84af3c2b3a7c8f714f701511912434525f41735f950b",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
31
src/main.zig
31
src/main.zig
|
@ -1,12 +1,14 @@
|
||||||
const std = @import("std");
|
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 debug = std.debug;
|
const debug = std.debug;
|
||||||
const io = std.io;
|
const io = std.io;
|
||||||
|
const zbench = @import("zbench");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const params = comptime clap.parseParamsComptime(
|
const params = comptime clap.parseParamsComptime(
|
||||||
|
\\-b, --benchmark Benchmark
|
||||||
\\-h, --help Display this help and exit.
|
\\-h, --help Display this help and exit.
|
||||||
\\-d, --decode Decode base64 to string.
|
\\-d, --decode Decode base64 to string.
|
||||||
\\-e, --encode Encode string to base64.
|
\\-e, --encode Encode string to base64.
|
||||||
|
@ -32,9 +34,17 @@ pub fn main() !void {
|
||||||
|
|
||||||
const stdout = std.io.getStdOut();
|
const stdout = std.io.getStdOut();
|
||||||
|
|
||||||
|
if (res.args.benchmark != 0) {
|
||||||
|
var bench = zbench.Benchmark.init(allocator, .{});
|
||||||
|
defer bench.deinit();
|
||||||
|
|
||||||
|
try bench.add("decode", bench_decode, .{});
|
||||||
|
try bench.run(io.getStdOut().writer());
|
||||||
|
}
|
||||||
|
|
||||||
if (res.args.help != 0) {
|
if (res.args.help != 0) {
|
||||||
usage();
|
usage();
|
||||||
try clap.help(std.io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
try clap.help(io.getStdErr().writer(), clap.Help, ¶ms, .{});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -115,7 +125,8 @@ fn usage() void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version() void {
|
fn version() void {
|
||||||
std.debug.print("b64 - {s}\n", .{build_info.version});
|
// std.debug.print("b64 - {s}\n", .{build_info.version});
|
||||||
|
std.debug.print("b64 - {s}\n", .{"0.3.0"});
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -161,6 +172,20 @@ fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
|
||||||
return decodedString;
|
return decodedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bench_decode(allocator: std.mem.Allocator) void {
|
||||||
|
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
||||||
|
|
||||||
|
try decode(allocator, base64String);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "bench decode" {
|
||||||
|
var bench = try zbench.Benchmark.init(std.testing.allocator, .{});
|
||||||
|
defer bench.deinit();
|
||||||
|
|
||||||
|
try bench.add("bench decode", bench_decode, .{});
|
||||||
|
try bench.run(std.io.getStdout().Writer());
|
||||||
|
}
|
||||||
|
|
||||||
test "base64_decode() returns decoded string" {
|
test "base64_decode() returns decoded string" {
|
||||||
const test_allocator = std.testing.allocator;
|
const test_allocator = std.testing.allocator;
|
||||||
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
|
||||||
|
|
10
src/root.zig
Normal file
10
src/root.zig
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
export fn add(a: i32, b: i32) i32 {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "basic add functionality" {
|
||||||
|
try testing.expect(add(3, 7) == 10);
|
||||||
|
}
|
Loading…
Reference in a new issue