aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/build.zig7
-rw-r--r--std/cstr.zig54
2 files changed, 61 insertions, 0 deletions
diff --git a/std/build.zig b/std/build.zig
index 3a2079db15..bb5280837f 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -784,6 +784,13 @@ const Target = union(enum) {
};
}
+ pub fn libFileExt(self: &const Target) -> []const u8 {
+ return switch (self.getOs()) {
+ builtin.Os.windows => ".lib",
+ else => ".a",
+ };
+ }
+
pub fn getOs(self: &const Target) -> builtin.Os {
return switch (*self) {
Target.Native => builtin.os,
diff --git a/std/cstr.zig b/std/cstr.zig
index d17b69c7a1..1610f12481 100644
--- a/std/cstr.zig
+++ b/std/cstr.zig
@@ -48,3 +48,57 @@ pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) -> %[]u8 {
result[slice.len] = 0;
return result;
}
+
+pub const NullTerminated2DArray = struct {
+ allocator: &mem.Allocator,
+ byte_count: usize,
+ ptr: ?&?&u8,
+
+ /// Takes N lists of strings, concatenates the lists together, and adds a null terminator
+ /// Caller must deinit result
+ pub fn fromSlices(allocator: &mem.Allocator, slices: []const []const []const u8) -> %NullTerminated2DArray {
+ var new_len: usize = 1; // 1 for the list null
+ var byte_count: usize = 0;
+ for (slices) |slice| {
+ new_len += slice.len;
+ for (slice) |inner| {
+ byte_count += inner.len;
+ }
+ byte_count += slice.len; // for the null terminators of inner
+ }
+
+ const index_size = @sizeOf(usize) * new_len; // size of the ptrs
+ byte_count += index_size;
+
+ const buf = %return allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count);
+ %defer allocator.free(buf);
+
+ var write_index = index_size;
+ const index_buf = ([]?&u8)(buf);
+
+ var i: usize = 0;
+ for (slices) |slice| {
+ for (slice) |inner| {
+ index_buf[i] = &buf[write_index];
+ i += 1;
+ mem.copy(u8, buf[write_index..], inner);
+ write_index += inner.len;
+ buf[write_index] = 0;
+ write_index += 1;
+ }
+ }
+ index_buf[i] = null;
+
+ return NullTerminated2DArray {
+ .allocator = allocator,
+ .byte_count = byte_count,
+ .ptr = @ptrCast(?&?&u8, buf.ptr),
+ };
+ }
+
+ pub fn deinit(self: &NullTerminated2DArray) {
+ const buf = @ptrCast(&u8, self.ptr);
+ self.allocator.free(buf[0..self.byte_count]);
+ }
+};
+