Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Created January 27, 2022 11:21
Show Gist options
  • Save matu3ba/d90f103b233465a8d2e028f9bca9331d to your computer and use it in GitHub Desktop.
Save matu3ba/d90f103b233465a8d2e028f9bca9331d to your computer and use it in GitHub Desktop.
comptime append strings to buffer
const std = @import("std");
const io = std.io;
//try stdout.print("All your base belong to use!\n", .{});
pub fn main() !void {
comptime var names = [_]u8{0} ** 5000;
comptime var name_offsets = [_]u64{0} ** 100; // no comptime writing to uninitialized values
comptime var offset: usize = 0;
comptime var index_offsets = 0;
name_offsets[index_offsets] = offset;
index_offsets += 1;
const s1 = "test123";
comptime {
for (s1) |char, index| {
names[offset + index] = char;
}
}
offset += s1.len; // usize
name_offsets[index_offsets] = offset;
index_offsets += 1;
const s2 = "456";
comptime {
for (s2) |char, index| {
names[offset + index] = char;
}
}
offset += s2.len; // usize
name_offsets[index_offsets] = offset;
index_offsets += 1;
// print stuff
const stdout = io.getStdOut().writer();
var index: u16 = 0;
while (index < index_offsets) : (index += 1) {
try stdout.print("{d}\n", .{name_offsets[index]});
}
index = 0;
std.debug.assert(index_offsets > 0);
while (index < index_offsets - 1) : (index += 1) {
try stdout.print("{s}\n", .{names[name_offsets[index]..name_offsets[index + 1]]});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment