1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-03 06:11:46 +02:00

Deduplicate string literal rendering, fix 4909

This commit is contained in:
Robert Hensing 2023-04-09 22:42:20 +02:00
parent 8f0ec323ea
commit 4e0804c920
7 changed files with 86 additions and 39 deletions

View file

@ -0,0 +1,26 @@
#include "value/print.hh"
namespace nix {
std::ostream &
printLiteral(std::ostream & str, const std::string_view string) {
str << "\"";
for (auto i = string.begin(); i != string.end(); ++i) {
if (*i == '\"' || *i == '\\') str << "\\" << *i;
else if (*i == '\n') str << "\\n";
else if (*i == '\r') str << "\\r";
else if (*i == '\t') str << "\\t";
else if (*i == '$' && *(i+1) == '{') str << "\\" << *i;
else str << *i;
}
str << "\"";
return str;
}
std::ostream &
printLiteral(std::ostream & str, bool boolean) {
str << (boolean ? "true" : "false");
return str;
}
}