~subsetpark/erasmus

4de79645422633c2160b224102c563b629d89d0e — Zach Smith 2 months ago 4f85fb3
Free headroom of escaped strings
1 files changed, 6 insertions(+), 1 deletions(-)

M src/note.zig
M src/note.zig => src/note.zig +6 -1
@@ 14,7 14,10 @@ fn escapeAndLower(s: []const u8, allocator: mem.Allocator) !util.CharBuffer {
        return EscapeError.TerminalPeriod;
    }

    var escaped = try util.CharBuffer.initCapacity(allocator, s.len * 2);
    // Initialize double the length of `s`, covering the pessimal case where
    // every character of `s` needs escaping.
    const total_capacity = s.len * 2;
    var escaped = try util.CharBuffer.initCapacity(allocator, total_capacity);

    for (s) |char| {
        switch (char) {


@@ 26,6 29,8 @@ fn escapeAndLower(s: []const u8, allocator: mem.Allocator) !util.CharBuffer {
        const lower_char = std.ascii.toLower(char);
        escaped.appendAssumeCapacity(lower_char);
    }
    // Free the extra head room allocated for unneeded escape chars.
    escaped.shrinkAndFree(escaped.items.len);

    return escaped;
}