2022-07-30 05:46:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-06-09 05:54:41 +00:00
|
|
|
pub const ciutf8 = @import("./ciutf8.zig");
|
|
|
|
pub const Uuid = @import("./Uuid.zig");
|
2022-07-18 07:03:29 +00:00
|
|
|
pub const DateTime = @import("./DateTime.zig");
|
2022-06-02 18:33:16 +00:00
|
|
|
pub const PathIter = @import("./PathIter.zig");
|
2022-09-10 22:29:08 +00:00
|
|
|
pub const Url = @import("./Url.zig");
|
2022-06-09 05:54:41 +00:00
|
|
|
|
2022-10-02 05:18:24 +00:00
|
|
|
/// Joins an array of strings, prefixing every entry with `prefix`,
|
|
|
|
/// and putting `separator` in between each pair
|
|
|
|
pub fn comptimeJoinWithPrefix(
|
|
|
|
comptime separator: []const u8,
|
|
|
|
comptime prefix: []const u8,
|
|
|
|
comptime strs: []const []const u8,
|
|
|
|
) []const u8 {
|
2022-09-29 21:52:01 +00:00
|
|
|
comptime {
|
2022-10-02 05:18:24 +00:00
|
|
|
if (strs.len == 0) return "";
|
|
|
|
|
|
|
|
var size: usize = 0;
|
|
|
|
for (strs) |str| size += prefix.len + str.len + separator.len;
|
|
|
|
size -= separator.len;
|
2022-09-29 21:52:01 +00:00
|
|
|
|
|
|
|
var buf = std.mem.zeroes([size]u8);
|
|
|
|
|
2022-10-02 05:18:24 +00:00
|
|
|
// can't use std.mem.join because of problems with comptime allocation
|
|
|
|
// https://github.com/ziglang/zig/issues/5873#issuecomment-1001778218
|
|
|
|
//var fba = std.heap.FixedBufferAllocator.init(&buf);
|
|
|
|
//return (std.mem.join(fba.allocator(), separator, fields) catch unreachable) ++ " ";
|
|
|
|
|
|
|
|
var buf_idx = 0;
|
|
|
|
for (strs) |str, i| {
|
|
|
|
std.mem.copy(u8, buf[buf_idx..], prefix);
|
|
|
|
buf_idx += prefix.len;
|
|
|
|
std.mem.copy(u8, buf[buf_idx..], str);
|
|
|
|
buf_idx += str.len;
|
|
|
|
|
|
|
|
if (i != strs.len - 1) {
|
|
|
|
std.mem.copy(u8, buf[buf_idx..], separator);
|
|
|
|
buf_idx += separator.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &buf;
|
2022-09-29 21:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 05:18:24 +00:00
|
|
|
/// Joins an array of strings, putting `separator` in between each pair
|
|
|
|
pub fn comptimeJoin(
|
|
|
|
comptime separator: []const u8,
|
|
|
|
comptime strs: []const []const u8,
|
|
|
|
) []const u8 {
|
|
|
|
return comptimeJoinWithPrefix(separator, "", strs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to serialize a runtime enum value as a string inside JSON.
|
|
|
|
/// To use, add
|
|
|
|
/// ```
|
|
|
|
/// pub const jsonStringify = util.jsonSerializeEnumAsString;
|
|
|
|
/// ```
|
|
|
|
/// to your enum type.
|
|
|
|
pub fn jsonSerializeEnumAsString(
|
|
|
|
enum_value: anytype,
|
|
|
|
_: std.json.StringifyOptions,
|
|
|
|
writer: anytype,
|
|
|
|
) !void {
|
|
|
|
switch (@typeInfo(@TypeOf(enum_value))) {
|
|
|
|
.Enum => |info| if (!info.is_exhaustive) @compileError("Enum must be exhaustive"),
|
|
|
|
else => @compileError("Must be enum type"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return std.fmt.format(writer, "\"{s}\"", .{@tagName(enum_value)});
|
2022-09-08 06:56:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 05:18:24 +00:00
|
|
|
/// Recursively frees a struct/array/slice/etc using the given allocator
|
|
|
|
/// by freeing any slices or pointers inside. Assumes that every pointer-like
|
|
|
|
/// object within points to its own allocation that must be free'd separately.
|
|
|
|
/// Do *not* use on self-referential types or structs that contain duplicate
|
|
|
|
/// slices.
|
|
|
|
/// Meant to be the inverse of `deepClone` below
|
2022-09-11 11:38:46 +00:00
|
|
|
pub fn deepFree(alloc: ?std.mem.Allocator, val: anytype) void {
|
2022-09-10 22:29:22 +00:00
|
|
|
const T = @TypeOf(val);
|
|
|
|
switch (@typeInfo(T)) {
|
|
|
|
.Pointer => |ptr| switch (ptr.size) {
|
2022-10-02 05:18:24 +00:00
|
|
|
.One => {
|
|
|
|
deepFree(alloc, val.*);
|
|
|
|
alloc.?.destroy(val);
|
|
|
|
},
|
2022-09-10 22:29:22 +00:00
|
|
|
.Slice => {
|
|
|
|
for (val) |v| deepFree(alloc, v);
|
2022-09-11 11:38:46 +00:00
|
|
|
alloc.?.free(val);
|
2022-09-10 22:29:22 +00:00
|
|
|
},
|
|
|
|
else => @compileError("Many and C-style pointers not supported by deepfree"),
|
|
|
|
},
|
|
|
|
.Optional => if (val) |v| deepFree(alloc, v) else {},
|
2022-09-15 01:12:07 +00:00
|
|
|
.Struct => |struct_info| inline for (struct_info.fields) |field| deepFree(alloc, @field(val, field.name)),
|
2022-09-10 22:29:22 +00:00
|
|
|
.Union, .ErrorUnion => @compileError("TODO: Unions not yet supported by deepFree"),
|
|
|
|
.Array => for (val) |v| deepFree(alloc, v),
|
|
|
|
|
2022-09-15 01:12:07 +00:00
|
|
|
.Enum, .Int, .Float, .Bool, .Void, .Type => {},
|
2022-09-10 22:29:22 +00:00
|
|
|
|
|
|
|
else => @compileError("Type " ++ @typeName(T) ++ " not supported by deepFree"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 05:18:24 +00:00
|
|
|
/// Clones a struct/array/slice/etc and all its submembers.
|
|
|
|
/// Assumes that there are no self-refrential pointers within and that
|
|
|
|
/// every pointer should be followed.
|
2022-09-11 08:55:20 +00:00
|
|
|
pub fn deepClone(alloc: std.mem.Allocator, val: anytype) !@TypeOf(val) {
|
2022-09-10 22:29:22 +00:00
|
|
|
const T = @TypeOf(val);
|
|
|
|
var result: T = undefined;
|
|
|
|
switch (@typeInfo(T)) {
|
|
|
|
.Pointer => |ptr| switch (ptr.size) {
|
|
|
|
.One => {
|
|
|
|
result = try alloc.create(ptr.child);
|
|
|
|
errdefer alloc.free(result);
|
|
|
|
result.* = try deepClone(alloc, val.*);
|
|
|
|
},
|
|
|
|
.Slice => {
|
|
|
|
const slice = try alloc.alloc(ptr.child, val.len);
|
|
|
|
errdefer alloc.free(slice);
|
|
|
|
|
|
|
|
var count: usize = 0;
|
|
|
|
errdefer for (slice[0..count]) |v| deepFree(alloc, v);
|
|
|
|
|
|
|
|
for (val) |v, i| {
|
|
|
|
slice[i] = try deepClone(alloc, v);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = slice;
|
|
|
|
},
|
|
|
|
else => @compileError("Many and C-style pointers not supported"),
|
|
|
|
},
|
|
|
|
.Optional => {
|
|
|
|
result = if (val) |v| try deepClone(alloc, v) else null;
|
|
|
|
},
|
|
|
|
.Struct => {
|
|
|
|
const fields = std.meta.fields(T);
|
|
|
|
var count: usize = 0;
|
|
|
|
errdefer {
|
|
|
|
inline for (fields) |f, i| {
|
|
|
|
if (i < count) deepFree(alloc, @field(result, f.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline for (fields) |f| {
|
|
|
|
@field(result, f.name) = try deepClone(alloc, @field(val, f.name));
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.Array => {
|
|
|
|
var count: usize = 0;
|
|
|
|
errdefer for (result[0..count]) |v| deepFree(alloc, v);
|
|
|
|
|
|
|
|
for (val) |v, i| {
|
|
|
|
result[i] = try deepClone(alloc, v);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 01:12:07 +00:00
|
|
|
.Enum, .Int, .Float, .Bool, .Void, .Type => {
|
2022-09-10 22:29:22 +00:00
|
|
|
result = val;
|
|
|
|
},
|
|
|
|
else => @compileError("Type " ++ @typeName(T) ++ " not supported"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-09 05:54:41 +00:00
|
|
|
test {
|
|
|
|
_ = ciutf8;
|
|
|
|
_ = Uuid;
|
2022-06-02 18:33:16 +00:00
|
|
|
_ = PathIter;
|
2022-07-18 07:03:29 +00:00
|
|
|
_ = DateTime;
|
2022-06-09 05:54:41 +00:00
|
|
|
}
|