I'd like to get a substring of a string, i.e. the characters contained between two indexes. I tried to slice it, but since the original string is an array this doesn't work and gives error: expected type '*const [0:0]u8', found '[]const u8'.
Basically I'd like to do what this library does, but with a regular string: https://github.com/JakubSzark/zig-string/blob/e842e89f2c18eecda2721f4a3114f63c98bd5b87/zig-string.zig#L371-L387
Here is my code:
const std = @import("std");
const MAX_SUB_DIRS = 10;
pub fn main() !void {
const out = std.io.getStdOut().writer();
const input_cur_dir = try std.fs.cwd().openDir("input", .{});
const file = try input_cur_dir.openFile("day07.txt", .{});
defer file.close();
var buffer: [100]u8 = undefined;
var output: ?[]const u8 = try nextLine(file.reader(), &buffer);
var cur_dir = "";
var buf: [100]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
while (output != null) : (output = try nextLine(file.reader(), &buffer)) {
if (output.?[0] == '$') {
if (output.?[2] == 'c') {
if (output.?[5] == '.') {
const index = std.mem.lastIndexOf(u8, cur_dir, "/").?;
cur_dir = cur_dir[0..index];
} else if (output.?[5] == '/') {
cur_dir = "";
} else {
const dir = output.?[5..output.len];
const to_concat: []const []const u8 = [_][]u8{ cur_dir, "/", dir };
cur_dir = std.mem.concat(fba.allocator(), u8, &to_concat);
}
}
} else {
// sum file sizes
}
}
try out.print("Answer: TODO\n", .{});
}
pub fn nextLine(reader: anytype, buffer: []u8) !?[]u8 {
// omitted due to character limit
}