//https://github.com/dmgk/zig-uuid const std = @import("std"); const crypto = std.crypto; const fmt = std.fmt; const testing = std.testing; pub const Error = error{InvalidUUID}; const UUID = @This(); bytes: [16]u8, pub fn init() UUID { var uuid = UUID{ .bytes = undefined }; crypto.random.bytes(&uuid.bytes); // Version 4 uuid.bytes[6] = (uuid.bytes[6] & 0x0f) | 0x40; // Variant 1 uuid.bytes[8] = (uuid.bytes[8] & 0x3f) | 0x80; return uuid; } // 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 }; // Hex to nibble mapping. const hex_to_nibble = [256]u8{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 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 if (layout.len != 0 and layout[0] != 's') @compileError("Unsupported format specifier for UUID type: '" ++ layout ++ "'."); var buf: [36]u8 = undefined; const hex = "0123456789abcdef"; buf[8] = '-'; buf[13] = '-'; buf[18] = '-'; buf[23] = '-'; inline for (encoded_pos) |i, j| { buf[i + 0] = hex[self.bytes[j] >> 4]; buf[i + 1] = hex[self.bytes[j] & 0x0f]; } try fmt.format(writer, "{s}", .{buf}); } pub fn parse(buf: []const u8) Error!UUID { var uuid = UUID{ .bytes = undefined }; if (buf.len != 36 or buf[8] != '-' or buf[13] != '-' or buf[18] != '-' or buf[23] != '-') return Error.InvalidUUID; inline for (encoded_pos) |i, j| { const hi = hex_to_nibble[buf[i + 0]]; const lo = hex_to_nibble[buf[i + 1]]; if (hi == 0xff or lo == 0xff) { return Error.InvalidUUID; } uuid.bytes[j] = hi << 4 | lo; } return uuid; } // Zero UUID pub const zero: UUID = .{ .bytes = .{0} ** 16 }; // Convenience function to return a new v4 UUID. pub fn newV4() UUID { return UUID.init(); } option> Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/integration (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-03-26Auto-set content-typeGravatar Jarred Sumner 1-1/+4
2022-03-26Fix clonefile() so it actually does runGravatar Jarred Sumner 1-1/+4
2022-03-26Remove imports of <WTF/FileSystem.h>Gravatar Jarred Sumner 2-2/+0
2022-03-26Move .d.ts into a better folderGravatar Jarred Sumner 2-4/+109
2022-03-25Update response.file.test.jsGravatar Jarred Sumner 1-0/+8
2022-03-25fix error hanldingGravatar Jarred Sumner 1-6/+22
2022-03-25[bun.js] configuration and error handling for HTTP serverGravatar Jarred Sumner 16-1582/+2172
2022-03-24Auto-detect MimeType based on file extensionGravatar Jarred Sumner 3-48/+3603
2022-03-24Bun.openInEditorGravatar Jarred Sumner 4-13/+131
2022-03-24Update MakefileGravatar Jarred Sumner 1-1/+2
2022-03-24Implement error page for HTTP serverGravatar Jarred Sumner 14-171/+894
2022-03-24clang-formatGravatar Jarred Sumner 7-278/+363
2022-03-24Update response.zigGravatar Jarred Sumner 1-6/+6
2022-03-24[bun.js] Enable SharedArrayBuffer and expose internal LoaderGravatar Jarred Sumner 1-7/+16
2022-03-23handle bodies of 0 length betterGravatar Jarred Sumner 6-42/+86
2022-03-23Update server.zigGravatar Jarred Sumner 1-1/+2
2022-03-23Implement Request body support!Gravatar Jarred Sumner 3-30/+132
2022-03-23Support `Request.headers` and `Request.url` in http serverGravatar Jarred Sumner 5-30/+134
2022-03-23Update MakefileGravatar Jarred Sumner 1-1/+1
2022-03-23:scissors: testGravatar Jarred Sumner 1-14/+2
2022-03-23[bun.js] Bun.write for macOSGravatar Jarred Sumner 14-76/+232
2022-03-23[bun.js] Implement Bun.write()Gravatar Jarred SUmner 11-63/+959
2022-03-22Handle integer sizes greater than i32Gravatar Jarred SUmner 14-74/+142
2022-03-22Linux-specific tweaksGravatar Jarred SUmner 6-43/+52
2022-03-22`Response.file` -> `Bun.file`Gravatar Jarred Sumner 3-72/+65
2022-03-21sendfile worksGravatar Jarred Sumner 7-62/+174
2022-03-21[bun.js] 2/? Implement `Response.file`, sendfile editionGravatar Jarred Sumner 8-161/+506
2022-03-21[bun.js] 1/? Implement `Response.file`Gravatar Jarred Sumner 13-103/+1103