| ... | ... | @@ -601,9 +601,8 @@ fn renderFieldName(writer: anytype, operands: []const g.Operand, field_index: us |
| 601 | 601 | const operand = operands[field_index]; |
| 602 | 602 | |
| 603 | 603 | // Should be enough for all names - adjust as needed. |
| 604 | | var name_buffer = std.BoundedArray(u8, 64){ |
| 605 | | .buffer = undefined, |
| 606 | | }; |
| 604 | var name_backing_buffer: [64]u8 = undefined; |
| 605 | var name_buffer = std.ArrayListUnmanaged(u8).initBuffer(&name_backing_buffer); |
| 607 | 606 | |
| 608 | 607 | derive_from_kind: { |
| 609 | 608 | // Operand names are often in the json encoded as "'Name'" (with two sets of quotes). |
| ... | ... | @@ -617,33 +616,33 @@ fn renderFieldName(writer: anytype, operands: []const g.Operand, field_index: us |
| 617 | 616 | // Use the same loop to transform to snake-case. |
| 618 | 617 | for (name) |c| { |
| 619 | 618 | switch (c) { |
| 620 | | 'a'...'z', '0'...'9' => try name_buffer.append(c), |
| 621 | | 'A'...'Z' => try name_buffer.append(std.ascii.toLower(c)), |
| 622 | | ' ', '~' => try name_buffer.append('_'), |
| 619 | 'a'...'z', '0'...'9' => name_buffer.appendAssumeCapacity(c), |
| 620 | 'A'...'Z' => name_buffer.appendAssumeCapacity(std.ascii.toLower(c)), |
| 621 | ' ', '~' => name_buffer.appendAssumeCapacity('_'), |
| 623 | 622 | else => break :derive_from_kind, |
| 624 | 623 | } |
| 625 | 624 | } |
| 626 | 625 | |
| 627 | 626 | // Assume there are no duplicate 'name' fields. |
| 628 | | try writer.print("{}", .{std.zig.fmtId(name_buffer.slice())}); |
| 627 | try writer.print("{}", .{std.zig.fmtId(name_buffer.items)}); |
| 629 | 628 | return; |
| 630 | 629 | } |
| 631 | 630 | |
| 632 | 631 | // Translate to snake case. |
| 633 | | name_buffer.len = 0; |
| 632 | name_buffer.items.len = 0; |
| 634 | 633 | for (operand.kind, 0..) |c, i| { |
| 635 | 634 | switch (c) { |
| 636 | | 'a'...'z', '0'...'9' => try name_buffer.append(c), |
| 635 | 'a'...'z', '0'...'9' => name_buffer.appendAssumeCapacity(c), |
| 637 | 636 | 'A'...'Z' => if (i > 0 and std.ascii.isLower(operand.kind[i - 1])) { |
| 638 | | try name_buffer.appendSlice(&[_]u8{ '_', std.ascii.toLower(c) }); |
| 637 | name_buffer.appendSliceAssumeCapacity(&[_]u8{ '_', std.ascii.toLower(c) }); |
| 639 | 638 | } else { |
| 640 | | try name_buffer.append(std.ascii.toLower(c)); |
| 639 | name_buffer.appendAssumeCapacity(std.ascii.toLower(c)); |
| 641 | 640 | }, |
| 642 | 641 | else => unreachable, // Assume that the name is valid C-syntax (and contains no underscores). |
| 643 | 642 | } |
| 644 | 643 | } |
| 645 | 644 | |
| 646 | | try writer.print("{}", .{std.zig.fmtId(name_buffer.slice())}); |
| 645 | try writer.print("{}", .{std.zig.fmtId(name_buffer.items)}); |
| 647 | 646 | |
| 648 | 647 | // For fields derived from type name, there could be any amount. |
| 649 | 648 | // Simply check against all other fields, and if another similar one exists, add a number. |