20class SourceCodeBuilder {
22 unsigned indentAmount;
26 bool supressSemi =
false;
29 SourceCodeBuilder() : indentLevel(0), indentAmount(4), endsInSpace(
false) {}
31 void increaseIndent() { indentLevel += indentAmount; }
32 void decreaseIndent() {
33 indentLevel -= indentAmount;
34 if (indentLevel < 0) BUG(
"Negative indent");
41 if (!endsInSpace) buffer.Append(
" ");
45 void append(
cstring str) { append(str.c_str()); }
46 void appendLine(
const char *str) {
54 void append(
const std::string &str) {
55 if (str.empty())
return;
56 endsInSpace = ::isspace(str.back());
59 [[deprecated(
"use string / char* version instead")]]
61 std::string str(1, c);
64 void append(
const char *str) {
65 if (str ==
nullptr) BUG(
"Null argument to append");
66 if (strlen(str) == 0)
return;
67 endsInSpace = ::isspace(str[strlen(str) - 1]);
71 template <
typename... Args>
72 void appendFormat(
const absl::FormatSpec<Args...> &format, Args &&...args) {
74 append(absl::StrFormat(format, std::forward<Args>(args)...));
76 void append(
unsigned u) { appendFormat(
"%d", u); }
77 void append(
int u) { appendFormat(
"%d", u); }
79 void endOfStatement(
bool addNl =
false) {
80 if (!supressSemi) append(
";");
84 void supressStatementSemi() { supressSemi =
true; }
93 buffer.Append(std::string(indentLevel,
' '));
94 if (indentLevel > 0) endsInSpace =
true;
97 void blockEnd(
bool nl) {
104 std::string toString()
const {
return std::string(buffer); }
105 void commentStart() { append(
"/* "); }
106 void commentEnd() { append(
" */"); }
107 bool lastIsSpace()
const {
return endsInSpace; }