1
0

wip
Some checks failed
Build and test / test (push) Failing after 2m6s

This commit is contained in:
2023-12-16 17:37:04 +11:00
parent a684d9b238
commit 6065242de9
2 changed files with 187 additions and 0 deletions

View File

@ -35,3 +35,31 @@ pub fn splitLines(buffer: []const u8) mem.SplitIterator(u8, .scalar) {
pub fn splitSpace(buffer: []const u8) mem.SplitIterator(u8, .scalar) {
return split(buffer, ' ');
}
pub fn allocGrid(comptime T: type, alloc: mem.Allocator, w: usize, h: usize, init: T) ![][]T {
var grid = try alloc.alloc([]T, h);
for (0..h) |i| {
grid[i] = try alloc.alloc(T, w);
@memset(grid[i], init);
}
return grid;
}
pub fn toGridOwned(alloc: mem.Allocator, buffer: []const u8, sep: u8) ![][]u8 {
if (buffer.len == 0) return error.Empty;
const w = mem.indexOfScalar(u8, buffer, sep) orelse unreachable;
var h = mem.count(u8, buffer, &[_]u8{sep});
if (buffer[buffer.len - 1] != sep) h += 1;
var grid = try alloc.alloc([]u8, h);
var it = split(buffer, sep);
var i: usize = 0;
while (it.next()) |line| {
grid[i] = try alloc.alloc(u8, w);
@memcpy(grid[i], line);
i += 1;
}
return grid;
}