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