When writing WML, output preprocessor-safe strings

Basically, this means that the output will parse to the original value even if run through the preprocessor.
This commit is contained in:
Celtic Minstrel 2021-08-01 23:33:55 -04:00 committed by Celtic Minstrel
parent 55d12404aa
commit a9906226a7

View file

@ -557,7 +557,43 @@ public:
void operator()(const std::string& s) const
{
indent();
out_ << key_ << '=' << '"' << escaped_string(s) << '"' << '\n';
out_ << key_ << '=';
if(s.find("{") == std::string::npos) {
out_ << '"' << escaped_string(s) << '"' << '\n';
} else if(s.find(">>") == std::string::npos) {
out_ << "<<" << s << ">>" << '\n';
} else {
for(size_t i = 0, brace = s.find("{"); i < s.size(); brace = s.find("{", i + 1)) {
out_ << '"' << escaped_string(s.substr(i, brace - i)) << '"';
if(brace == std::string::npos) break;
out_ << "<<" << s[brace];
// Now go until the matching closing brace
size_t brace_nesting = 1;
bool in_shift = false;
for(i = brace + 1; brace_nesting > 0 && i < s.size(); i++) {
if(in_shift) {
if(s[i] == '>') {
out_ << R"""(>>">>"<<)""";
} else {
out_ << s[i-1];
}
in_shift = false;
continue;
}
if(s[i] == '{') {
brace_nesting++;
} else if(s[i] == '}') {
brace_nesting--;
} else if(s[i] == '>') {
in_shift = true;
continue;
}
out_ << s[i];
}
out_ << ">>";
}
out_ << '\n';
}
}
void operator()(const t_string& s) const;