Files
browser/src/id.zig
T
Karl Seguin 8e42d63c1c zig: Zig 0.16
Built against https://github.com/lightpanda-io/zig-v8-fork/tree/zig-0.16 but
it doesn't require a new v8 build.

Built against https://github.com/lightpanda-io/boringssl-zig/tree/zig-0.16
since the current fork we point to isn't updated.

A global std.Io instance, lp.io. Way easier this way and requires 0 changes to
our libcurl integration / event loop.

Network code uses a new layer that does what Zig 0.15's posix package used to
do. Again, quicker migration that way. But, as long as we have the global IO,
and given the half-baked nature of networking in std.Io 0.16, this just makes
sense. Things can be migrated as needed.

The std.time.* -> std.Io.Timestamp/Clock/Duration resulted in _a lot_ of
changes. ArrayList = .{} -> ArrayList -> .empty also resulted in a lot of
changes, but that's obviously superficial. As is the trimLeft/trimRight ->
trimStart/trimEnd rename.

Locking adopt the `Uncancelable` variants, e.g. mutex.lockUncancelable() to
preserve the error-free signature (and, because cancellation would be something
we'd have to put more thought into).

std.json.ObjectMap is now unmanaged, so the allocator had to be passed along.
However, there's still a deprecated managed variant of MemoryPool, so I switched
to it (we can do a small follow up PR to move to the unmanaged after).

I tried use_llvm = false, but it locks my computer, consuming RAM until MacOS
gives me a popup I've never seen before, begging me to start killing processes.

Agent and the networking stuff saw the most significant changes.
2026-07-22 13:26:03 +08:00

75 lines
2.4 KiB
Zig

// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
pub fn uuidv4(hex: []u8) void {
lp.assert(hex.len == 36, "uuidv4.len", .{ .len = hex.len });
var bin: [16]u8 = undefined;
lp.io.random(&bin);
bin[6] = (bin[6] & 0x0f) | 0x40;
bin[8] = (bin[8] & 0x3f) | 0x80;
const alphabet = "0123456789abcdef";
hex[8] = '-';
hex[13] = '-';
hex[18] = '-';
hex[23] = '-';
const encoded_pos = [16]u8{ 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 };
inline for (encoded_pos, 0..) |i, j| {
hex[i + 0] = alphabet[bin[j] >> 4];
hex[i + 1] = alphabet[bin[j] & 0x0f];
}
}
const testing = std.testing;
test "id: uuiv4" {
const expectUUID = struct {
fn expect(uuid: [36]u8) !void {
for (uuid, 0..) |b, i| {
switch (b) {
'0'...'9', 'a'...'z' => {},
'-' => {
if (i != 8 and i != 13 and i != 18 and i != 23) {
return error.InvalidEncoding;
}
},
else => return error.InvalidHexEncoding,
}
}
}
}.expect;
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
var seen = std.StringHashMapUnmanaged(void){};
for (0..100) |_| {
var hex: [36]u8 = undefined;
uuidv4(&hex);
try expectUUID(hex);
try seen.put(allocator, try allocator.dupe(u8, &hex), {});
}
try testing.expectEqual(100, seen.count());
}