2024-03-28 23:26:42 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "ansicolor.hh"
|
|
|
|
#include "escape-char.hh"
|
|
|
|
#include "english.hh"
|
|
|
|
#include "escape-string.hh"
|
|
|
|
#include "print-elided.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
std::ostream &
|
2024-03-28 23:26:42 +00:00
|
|
|
escapeString(std::ostream & output, std::string_view string, EscapeStringOptions options)
|
2024-03-28 23:26:42 +00:00
|
|
|
{
|
|
|
|
size_t charsPrinted = 0;
|
2024-03-28 23:26:42 +00:00
|
|
|
if (options.outputAnsiColors) {
|
|
|
|
output << ANSI_MAGENTA;
|
|
|
|
}
|
|
|
|
output << "\"";
|
2024-03-28 23:26:42 +00:00
|
|
|
for (auto i = string.begin(); i != string.end(); ++i) {
|
2024-03-28 23:26:42 +00:00
|
|
|
if (charsPrinted >= options.maxLength) {
|
|
|
|
output << "\" ";
|
|
|
|
printElided(
|
|
|
|
output, string.length() - charsPrinted, "byte", "bytes", options.outputAnsiColors
|
|
|
|
);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*i == '\"' || *i == '\\') {
|
|
|
|
output << "\\" << *i;
|
|
|
|
} else if (*i == '\n') {
|
|
|
|
output << "\\n";
|
|
|
|
} else if (*i == '\r') {
|
|
|
|
output << "\\r";
|
|
|
|
} else if (*i == '\t') {
|
|
|
|
output << "\\t";
|
|
|
|
} else if (*i == '$' && *(i + 1) == '{') {
|
|
|
|
output << "\\" << *i;
|
|
|
|
} else if (options.escapeNonPrinting && !isprint(*i)) {
|
|
|
|
output << MaybeHexEscapedChar{*i};
|
|
|
|
} else {
|
|
|
|
output << *i;
|
2024-03-28 23:26:42 +00:00
|
|
|
}
|
|
|
|
charsPrinted++;
|
|
|
|
}
|
2024-03-28 23:26:42 +00:00
|
|
|
output << "\"";
|
|
|
|
if (options.outputAnsiColors) {
|
|
|
|
output << ANSI_NORMAL;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string escapeString(std::string_view s, EscapeStringOptions options)
|
|
|
|
{
|
|
|
|
std::ostringstream output;
|
|
|
|
escapeString(output, s, options);
|
|
|
|
return output.str();
|
2024-03-28 23:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace nix
|