diff --git a/build.zig b/build.zig index 0e57f1d..be8fafe 100644 --- a/build.zig +++ b/build.zig @@ -1,22 +1,18 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); _ = b.addModule("uuid", .{ - .source_file = .{ - .path = "uuid.zig", - }, - }); - - const tests = b.addTest(.{ - .root_source_file = .{ .path = "uuid.zig" }, + .root_source_file = .{ .path = "src/root.zig" }, .target = target, .optimize = optimize, }); + + const tests = b.addTest(.{ .root_source_file = .{ .path = "src/root.zig" } }); const run_tests = b.addRunArtifact(tests); + const tests_step = b.step("test", "Run tests"); - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_tests.step); + tests_step.dependOn(&run_tests.step); } diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..8af2ff2 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,12 @@ +.{ + .name = "zig-uuid", + .version = "0.0.1", + + .paths = .{ + "src", + "build.zig", + "build.zig.zon", + "LICENSE", + "README.md", + }, +} diff --git a/example.zig b/example.zig deleted file mode 100644 index 46e856c..0000000 --- a/example.zig +++ /dev/null @@ -1,12 +0,0 @@ -const std = @import("std"); -const UUID = @import("uuid.zig").UUID; - -pub fn main() !void { - // generate - const uuid1 = UUID.init(); - std.debug.print("{}\n", .{uuid1}); - - // parse - const uuid2 = try UUID.parse("3df6f0e4-f9b1-4e34-ad70-33206069b995"); - std.debug.print("{}\n", .{uuid2}); -} diff --git a/uuid.zig b/src/root.zig similarity index 75% rename from uuid.zig rename to src/root.zig index 09c6af0..8430da9 100644 --- a/uuid.zig +++ b/src/root.zig @@ -8,8 +8,11 @@ const testing = std.testing; pub const Error = error{InvalidUUID}; +pub const bytes_len = 16; +pub const slice_len = 36; + pub const UUID = struct { - bytes: [16]u8, + bytes: [bytes_len]u8, pub fn init() UUID { var uuid = UUID{ .bytes = undefined }; @@ -22,13 +25,8 @@ pub const UUID = struct { return uuid; } - fn to_string(self: UUID,slice: []u8) void { - var string:[36]u8 = format_uuid(self); - std.mem.copy(u8, slice, &string); - } - - fn format_uuid(self: UUID) [36]u8 { - var buf: [36]u8 = undefined; + pub fn toSlice(self: UUID) [36]u8 { + var buf: [slice_len]u8 = undefined; buf[8] = '-'; buf[13] = '-'; buf[18] = '-'; @@ -40,6 +38,11 @@ pub const UUID = struct { return buf; } + pub fn copy(self: UUID, dest: *[slice_len]u8) void { + const src = self.toSlice(); + @memcpy(dest, &src); + } + // Indices in the UUID string representation for each byte. const encoded_pos = [16]u8{ 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 }; @@ -82,47 +85,39 @@ pub const UUID = struct { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; - pub fn format( - self: UUID, - comptime layout: []const u8, - options: fmt.FormatOptions, - writer: anytype, - ) !void { - _ = options; // currently unused - + pub fn format(self: UUID, comptime layout: []const u8, _: fmt.FormatOptions, writer: anytype) !void { if (layout.len != 0 and layout[0] != 's') @compileError("Unsupported format specifier for UUID type: '" ++ layout ++ "'."); - const buf = format_uuid(self); + const buf = self.toSlice(); try fmt.format(writer, "{s}", .{buf}); } - pub fn parse(buf: []const u8) Error!UUID { - var uuid = UUID{ .bytes = undefined }; + pub fn isValid(s: []const u8) bool { + return s.len == slice_len and s[8] == '-' and s[13] == '-' and s[18] == '-' and s[23] == '-'; + } + + pub fn parse(s: []const u8) Error!UUID { + var buf: [bytes_len]u8 = [_]u8{0} ** bytes_len; - if (buf.len != 36 or buf[8] != '-' or buf[13] != '-' or buf[18] != '-' or buf[23] != '-') + if (!isValid(s)) return Error.InvalidUUID; inline for (encoded_pos, 0..) |i, j| { - const hi = hex_to_nibble[buf[i + 0]]; - const lo = hex_to_nibble[buf[i + 1]]; + const hi = hex_to_nibble[s[i + 0]]; + const lo = hex_to_nibble[s[i + 1]]; + if (hi == 0xff or lo == 0xff) { return Error.InvalidUUID; } - uuid.bytes[j] = hi << 4 | lo; + buf[j] = hi << 4 | lo; } - - return uuid; + return .{ .bytes = buf }; } }; // Zero UUID -pub const zero: UUID = .{ .bytes = .{0} ** 16 }; - -// Convenience function to return a new v4 UUID. -pub fn newV4() UUID { - return UUID.init(); -} +pub const zero: UUID = .{ .bytes = .{0} ** bytes_len }; test "parse and format" { const uuids = [_][]const u8{ @@ -152,17 +147,8 @@ test "invalid UUID" { } } -test "check to_string works" { - const uuid1 = UUID.init(); - - var string1: [36]u8 = undefined; - var string2: [36]u8 = undefined; - - uuid1.to_string(&string1); - uuid1.to_string(&string2); - - std.debug.print("\nUUID {s} \n", .{uuid1}); - std.debug.print("\nFirst call to_string {s} \n", .{string1}); - std.debug.print("Second call to_string {s} \n", .{string2}); - try testing.expectEqual(string1, string2); +test "UUID.copy()" { + const uuid = UUID.init(); + var s: [36]u8 = undefined; + uuid.copy(&s); }