only allocate if mutation is neded

This commit is contained in:
Emil Lerch 2021-05-13 10:10:12 -07:00
parent dd5ad72b5a
commit 1d10ec3e17
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -1456,18 +1456,19 @@ pub const ParseOptions = struct {
}; };
fn camelCaseComp(field: []const u8, key: []const u8, options: ParseOptions) !bool { fn camelCaseComp(field: []const u8, key: []const u8, options: ParseOptions) !bool {
const allocator = options.allocator orelse return error.AllocatorRequired; var utf8_source_key = (std.unicode.Utf8View.init(key) catch unreachable).iterator();
const source_key_camel_case = try allocator.dupeZ(u8, key);
defer allocator.free(source_key_camel_case);
var utf8_source_key = (std.unicode.Utf8View.init(source_key_camel_case) catch unreachable).iterator();
if (utf8_source_key.nextCodepoint()) |codepoint| { if (utf8_source_key.nextCodepoint()) |codepoint| {
if (codepoint >= 'A' and codepoint <= 'Z') { if (codepoint >= 'A' and codepoint <= 'Z') {
const allocator = options.allocator orelse return error.AllocatorRequired;
const source_key_camel_case = try allocator.dupeZ(u8, key);
defer allocator.free(source_key_camel_case);
// First codepoint is uppercase Latin char, which is all we're handling atm // First codepoint is uppercase Latin char, which is all we're handling atm
source_key_camel_case[0] = source_key_camel_case[0] + ('a' - 'A'); source_key_camel_case[0] = source_key_camel_case[0] + ('a' - 'A');
// We will assume the target field is in camelCase // We will assume the target field is in camelCase
return std.mem.eql(u8, field, source_key_camel_case);
} }
} }
return std.mem.eql(u8, field, source_key_camel_case); return std.mem.eql(u8, field, key);
} }
fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: ParseOptions) !T { fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options: ParseOptions) !T {