Use u128 as backing field for Uuid
This commit is contained in:
parent
ccf79febe4
commit
20af2c82c8
1 changed files with 30 additions and 29 deletions
|
@ -2,33 +2,34 @@ const Uuid = @This();
|
|||
|
||||
const std = @import("std");
|
||||
|
||||
data: [16]u8,
|
||||
data: u128,
|
||||
|
||||
pub const Nil = Uuid{ .data = @bitCast([16]u8, @as(u128, 0)) };
|
||||
pub const Nil = Uuid{ .data = @as(u128, 0) };
|
||||
pub const StringLen = 36;
|
||||
|
||||
pub fn eql(lhs: Uuid, rhs: Uuid) bool {
|
||||
return std.mem.eql(u8, &lhs.data, &rhs.data);
|
||||
return lhs.data == rhs.data;
|
||||
}
|
||||
|
||||
pub fn format(value: Uuid, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
|
||||
const arr = @bitCast([16]u8, value.data);
|
||||
try std.fmt.format(writer, "{x:0>2}{x:0>2}{x:0>2}{x:0>2}-{x:0>2}{x:0>2}-{x:0>2}{x:0>2}-{x:0>2}{x:0>2}-{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}", .{
|
||||
value.data[0],
|
||||
value.data[1],
|
||||
value.data[2],
|
||||
value.data[3],
|
||||
value.data[4],
|
||||
value.data[5],
|
||||
value.data[6],
|
||||
value.data[7],
|
||||
value.data[8],
|
||||
value.data[9],
|
||||
value.data[10],
|
||||
value.data[11],
|
||||
value.data[12],
|
||||
value.data[13],
|
||||
value.data[14],
|
||||
value.data[15],
|
||||
arr[0],
|
||||
arr[1],
|
||||
arr[2],
|
||||
arr[3],
|
||||
arr[4],
|
||||
arr[5],
|
||||
arr[6],
|
||||
arr[7],
|
||||
arr[8],
|
||||
arr[9],
|
||||
arr[10],
|
||||
arr[11],
|
||||
arr[12],
|
||||
arr[13],
|
||||
arr[14],
|
||||
arr[15],
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -40,14 +41,14 @@ pub const ParseError = error{
|
|||
pub fn parse(str: []const u8) ParseError!Uuid {
|
||||
if (str.len != StringLen) return error.InvalidLength;
|
||||
|
||||
var uuid: Uuid = undefined;
|
||||
var data: [16]u8 = undefined;
|
||||
var str_i: usize = 0;
|
||||
var i: usize = 0;
|
||||
while (i < 16 and str_i < str.len) : ({
|
||||
i += 1;
|
||||
str_i += 2;
|
||||
}) {
|
||||
uuid.data[i] = std.fmt.parseInt(u8, str[str_i .. str_i + 2], 16) catch |err| switch (err) {
|
||||
data[i] = std.fmt.parseInt(u8, str[str_i .. str_i + 2], 16) catch |err| switch (err) {
|
||||
error.InvalidCharacter => return error.InvalidCharacter,
|
||||
else => unreachable,
|
||||
};
|
||||
|
@ -58,18 +59,18 @@ pub fn parse(str: []const u8) ParseError!Uuid {
|
|||
}
|
||||
}
|
||||
|
||||
return uuid;
|
||||
return Uuid{ .data = @bitCast(u128, data) };
|
||||
}
|
||||
|
||||
pub fn randV4(rand: std.rand.Random) Uuid {
|
||||
var ret: Uuid = undefined;
|
||||
rand.bytes(&ret.data);
|
||||
var ret: [16]u8 = undefined;
|
||||
rand.bytes(&ret);
|
||||
|
||||
// signify that this is a random v4 uuid
|
||||
ret.data[7] = (0b0100_0000) | (ret.data[7] & 0b1111);
|
||||
ret.data[9] = (0b1000_0000) | (ret.data[9] & 0b11_1111);
|
||||
ret[7] = (0b0100_0000) | (ret[7] & 0b1111);
|
||||
ret[9] = (0b1000_0000) | (ret[9] & 0b11_1111);
|
||||
|
||||
return ret;
|
||||
return Uuid{ .data = @bitCast(u128, ret) };
|
||||
}
|
||||
|
||||
test "parse uuid" {
|
||||
|
@ -80,7 +81,7 @@ test "parse uuid" {
|
|||
|
||||
try std.testing.expectEqual(
|
||||
Uuid{
|
||||
.data = @bitCast([16]u8, @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7)),
|
||||
.data = @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7),
|
||||
},
|
||||
try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b"),
|
||||
);
|
||||
|
@ -90,7 +91,7 @@ test "format uuid" {
|
|||
try std.testing.expectFmt("00000000-0000-0000-0000-000000000000", "{}", .{Uuid.Nil});
|
||||
|
||||
const uuid = Uuid{
|
||||
.data = @bitCast([16]u8, @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7)),
|
||||
.data = @as(u128, 0x4ba7b74522ad_1da8_c242_d312_60515ff7),
|
||||
};
|
||||
try std.testing.expectFmt("f75f5160-12d3-42c2-a81d-ad2245b7a74b", "{}", .{uuid});
|
||||
|
||||
|
|
Loading…
Reference in a new issue