I’m a bit confused about this scenario: Shouldn’t the compiler or even the LSP warn about a line like const fields = fields_array[0..index];
since the lifetime of fields_array
ends with the function, as shown below?
I don’t have experience with manual memory management, so trying to stay alert.
const HttpMessage = struct {
start_line: [] const u8,
fields: [][] const u8,
message: ?[] const u8,
pub fn init(buf: []const u8) HttpMessage {
var tokenizer = std.mem.tokenizeSequence(u8, buf,"\r\n");
const start_line = tokenizer.next().?;
var fields_array: [30][]const u8 = undefined;
var index: u32 = 0;
while(tokenizer.next()) |header|: (index += 1) {
if (header.len == 0) {
break;
}
fields_array[index] = header[0..];
}
const fields = fields_array[0..index];
var message: ?[] const u8 = undefined;
if (tokenizer.next()) |text| {
message = text;
} else {
message = null;
}
return HttpMessage{
.start_line = start_line,
.fields = fields[0..],
.message = message,
};
}
};