#Cleanly iteratively concatenate a string
1 messages · Page 1 of 1 (latest)
fn colorize_line(allocator: Allocator, s: []const u8, match_it: *mvzr.Regex.RegexIterator) ![]const u8 {
var start: usize = 0;
var line = try std.ArrayList(u8).initCapacity(allocator, s.len);
while (match_it.next()) |m| {
// Color the current match
const colorized_match = try colorize(allocator, m.slice);
// Append everything from the end of the last match to the start of the current
try line.appendSlice(s[start..m.start]);
// Append the colorized match
try line.appendSlice(colorized_match);
allocator.free(colorized_match);
// Update the cursor to the end of the match
start = m.end;
}
// Append everything after the last match
try line.appendSlice(s[start..]);
return line.items;
}
is probably a lot more reasonable