| ... | @@ -549,8 +549,8 @@ fn expand_variables_cmake( | ... | @@ -549,8 +549,8 @@ fn expand_variables_cmake( |
| 549 | contents: []const u8, | 549 | contents: []const u8, |
| 550 | values: std.StringArrayHashMap(Value), | 550 | values: std.StringArrayHashMap(Value), |
| 551 | ) ![]const u8 { | 551 | ) ![]const u8 { |
| 552 | var content_buf = allocator.alloc(u8, 0) catch @panic("OOM"); | 552 | var result = allocator.alloc(u8, 0) catch @panic("OOM"); |
| 553 | errdefer allocator.free(content_buf); | 553 | errdefer allocator.free(result); |
| 554 | | 554 | |
| 555 | const valid_varname_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/_.+-"; | 555 | const valid_varname_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/_.+-"; |
| 556 | const open_var = "${"; | 556 | const open_var = "${"; |
| ... | @@ -580,9 +580,9 @@ fn expand_variables_cmake( | ... | @@ -580,9 +580,9 @@ fn expand_variables_cmake( |
| 580 | const key = contents[curr + 1 .. close_pos]; | 580 | const key = contents[curr + 1 .. close_pos]; |
| 581 | const value = values.get(key) orelse .undef; | 581 | const value = values.get(key) orelse .undef; |
| 582 | const missing = contents[source_offset..curr]; | 582 | const missing = contents[source_offset..curr]; |
| 583 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}{}", .{ content_buf, missing, fmtValueCMake(value) }); | 583 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}{}", .{ result, missing, fmtValueCMake(value) }); |
| 584 | allocator.free(content_buf); | 584 | allocator.free(result); |
| 585 | content_buf = buf; | 585 | result = buf; |
| 586 | | 586 | |
| 587 | curr = close_pos; | 587 | curr = close_pos; |
| 588 | source_offset = close_pos + 1; | 588 | source_offset = close_pos + 1; |
| ... | @@ -597,15 +597,15 @@ fn expand_variables_cmake( | ... | @@ -597,15 +597,15 @@ fn expand_variables_cmake( |
| 597 | break :blk; | 597 | break :blk; |
| 598 | } | 598 | } |
| 599 | const missing = contents[source_offset..curr]; | 599 | const missing = contents[source_offset..curr]; |
| 600 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ content_buf, missing, open_var }); | 600 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ result, missing, open_var }); |
| 601 | allocator.free(content_buf); | 601 | allocator.free(result); |
| 602 | content_buf = buf; | 602 | result = buf; |
| 603 | | 603 | |
| 604 | source_offset = curr + open_var.len; | 604 | source_offset = curr + open_var.len; |
| 605 | curr = next; | 605 | curr = next; |
| 606 | try var_stack.append(Position{ | 606 | try var_stack.append(Position{ |
| 607 | .source = curr, | 607 | .source = curr, |
| 608 | .target = content_buf.len - open_var.len, | 608 | .target = result.len - open_var.len, |
| 609 | }); | 609 | }); |
| 610 | | 610 | |
| 611 | continue :loop; | 611 | continue :loop; |
| ... | @@ -621,13 +621,13 @@ fn expand_variables_cmake( | ... | @@ -621,13 +621,13 @@ fn expand_variables_cmake( |
| 621 | } | 621 | } |
| 622 | const missing = contents[source_offset..curr]; | 622 | const missing = contents[source_offset..curr]; |
| 623 | const key_start = open_pos.target + open_var.len; | 623 | const key_start = open_pos.target + open_var.len; |
| 624 | const key = try std.fmt.allocPrint(allocator, "{s}{s}", .{ content_buf[key_start..], missing }); | 624 | const key = try std.fmt.allocPrint(allocator, "{s}{s}", .{ result[key_start..], missing }); |
| 625 | defer allocator.free(key); | 625 | defer allocator.free(key); |
| 626 | | 626 | |
| 627 | const value = values.get(key) orelse .undef; | 627 | const value = values.get(key) orelse .undef; |
| 628 | const buf = try std.fmt.allocPrint(allocator, "{s}{}", .{ content_buf[0..open_pos.target], fmtValueCMake(value) }); | 628 | const buf = try std.fmt.allocPrint(allocator, "{s}{}", .{ result[0..open_pos.target], fmtValueCMake(value) }); |
| 629 | allocator.free(content_buf); | 629 | allocator.free(result); |
| 630 | content_buf = buf; | 630 | result = buf; |
| 631 | | 631 | |
| 632 | source_offset = curr + 1; | 632 | source_offset = curr + 1; |
| 633 | | 633 | |
| ... | @@ -646,12 +646,12 @@ fn expand_variables_cmake( | ... | @@ -646,12 +646,12 @@ fn expand_variables_cmake( |
| 646 | } | 646 | } |
| 647 | | 647 | |
| 648 | if (source_offset != contents.len) { | 648 | if (source_offset != contents.len) { |
| 649 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}", .{ content_buf, contents[source_offset..] }); | 649 | const buf = try std.fmt.allocPrint(allocator, "{s}{s}", .{ result, contents[source_offset..] }); |
| 650 | allocator.free(content_buf); | 650 | allocator.free(result); |
| 651 | content_buf = buf; | 651 | result = buf; |
| 652 | } | 652 | } |
| 653 | | 653 | |
| 654 | return content_buf; | 654 | return result; |
| 655 | } | 655 | } |
| 656 | | 656 | |
| 657 | fn testReplaceVariables( | 657 | fn testReplaceVariables( |
| ... | @@ -681,7 +681,6 @@ test "expand_variables_cmake simple cases" { | ... | @@ -681,7 +681,6 @@ test "expand_variables_cmake simple cases" { |
| 681 | | 681 | |
| 682 | // empty strings are preserved | 682 | // empty strings are preserved |
| 683 | try testReplaceVariables(allocator, "", "", values); | 683 | try testReplaceVariables(allocator, "", "", values); |
| 684 | try testReplaceVariables(allocator, "", "", values); | | |
| 685 | | 684 | |
| 686 | // line with misc content is preserved | 685 | // line with misc content is preserved |
| 687 | try testReplaceVariables(allocator, "no substitution", "no substitution", values); | 686 | try testReplaceVariables(allocator, "no substitution", "no substitution", values); |