Skip to content

Commit

Permalink
Merge pull request #7 from redmaner/zon-support
Browse files Browse the repository at this point in the history
Fix build with 0.13.0 and add support for zon
  • Loading branch information
gernest authored Jul 31, 2024
2 parents 0414979 + 137dae2 commit 934231c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
zig-out/
zig-cache/
kcov-output/
.zig-cache/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

This implements `base32` `encoding` and `decoding` for the zig programming language (ziglang)

# Installation
First fetch dependency by running:
```
zig fetch --save git+https://github.com/gernest/base32
```
Then update your `build.zig` file to load the module:
```
const base32 = b.dependency("base32", .{});
exe.root_module.addImport("base32", base32.module("base32"));
```

# Usage

`example.zig`
Expand Down
21 changes: 16 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const lib = b.addStaticLibrary(
.{
.name = "base32",
.root_source_file = .{ .path = "src/base32.zig" },
.root_source_file = b.path("src/base32.zig"),
.target = target,
.optimize = optimize,
},
);
lib.install();

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);

// This adds a module which allows it to be imported as module with Zon
_ = b.addModule("base32", .{
.root_source_file = b.path("src/base32.zig"),
});

const coverage = b.option(bool, "coverage", "Generate test coverage") orelse false;

var main_tests = b.addTest(
.{
.root_source_file = .{ .path = "src/base32.zig" },
.root_source_file = b.path("src/base32.zig"),
.optimize = optimize,
},
);

const run_main_tests = b.addRunArtifact(main_tests);

if (coverage) {
main_tests.setExecCmd(&[_]?[]const u8{
"kcov",
Expand All @@ -34,5 +45,5 @@ pub fn build(b: *std.build.Builder) void {
}

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.run().step);
test_step.dependOn(&run_main_tests.step);
}
35 changes: 35 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = "base32",

// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.2.0",

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{},

.paths = .{
"build.zig",
"build.zig.zon",
"src",
// For example...
//"LICENSE",
//"README.md",
},
}
16 changes: 8 additions & 8 deletions src/base32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub const Encoding = struct {
return Encoding{
.buf = blk: {
var a: [32]u8 = undefined;
std.mem.copy(u8, a[0..], encoder_string);
std.mem.copyForwards(u8, a[0..], encoder_string);
break :blk a;
},
.decode_map = blk: {
var a = [_]u8{0xFF} ** 256;
for (encoder_string, 0..) |c, i| {
a[@intCast(usize, c)] = @intCast(u8, i);
a[@intCast(c)] = @intCast(i);
}
break :blk a;
},
Expand Down Expand Up @@ -147,12 +147,12 @@ pub const Encoding = struct {
if (dest.len < self.decodeLen(source.len)) {
return error.NotEnoughSpace;
}
var dst = dest;
const dst = dest;
var src = source;
var end: bool = false;
var n: usize = 0;
var dsti: usize = 0;
var olen = src.len;
const olen = src.len;
while (src.len > 0 and !end) {
var dbuf = [_]u8{0} ** 8;
var dlen: usize = 8;
Expand Down Expand Up @@ -340,7 +340,7 @@ test "Decoding" {
var buf: [1024]u8 = undefined;
for (pairs) |ts| {
const size = std_encoding.decodeLen(ts.encoded.len);
var result = try std_encoding.decode(buf[0..size], ts.encoded);
const result = try std_encoding.decode(buf[0..size], ts.encoded);
try testing.expectEqualSlices(u8, ts.decoded, result);
}
}
Expand All @@ -351,7 +351,7 @@ test "Encoding no padding" {
for (pairs) |ts| {
const size = std_encoding_no_padding.encodeLen(ts.decoded.len);
const result = std_encoding_no_padding.encode(buf[0..size], ts.decoded);
var expected_end = std.mem.indexOf(u8, ts.encoded, "=") orelse ts.encoded.len;
const expected_end = std.mem.indexOf(u8, ts.encoded, "=") orelse ts.encoded.len;
const expected = ts.encoded[0..expected_end];
try testing.expectEqualSlices(u8, expected, result);
}
Expand All @@ -362,9 +362,9 @@ test "Decoding no padding" {
const std_encoding_no_padding = Encoding.initWithPadding(encode_std, null);
for (pairs) |ts| {
const size = std_encoding_no_padding.decodeLen(ts.encoded.len);
var end_without_padding = std.mem.indexOf(u8, ts.encoded, "=") orelse ts.encoded.len;
const end_without_padding = std.mem.indexOf(u8, ts.encoded, "=") orelse ts.encoded.len;
const encoded_no_pad = ts.encoded[0..end_without_padding];
var result = try std_encoding_no_padding.decode(buf[0..size], encoded_no_pad);
const result = try std_encoding_no_padding.decode(buf[0..size], encoded_no_pad);
try testing.expectEqualSlices(u8, ts.decoded, result);
}
}

0 comments on commit 934231c

Please sign in to comment.