Add EscapeStringOptions and escapeString tests

Change-Id: I86ead2f969c9e03c9edfa51bbc92ee06393fd7d6
This commit is contained in:
Rebecca Turner 2024-03-28 16:26:42 -07:00
parent a5a25894c1
commit c41ec4e64c
5 changed files with 132 additions and 29 deletions

View file

@ -198,12 +198,14 @@ private:
void printString(Value & v) void printString(Value & v)
{ {
// NB: Non-printing characters won't be escaped.
escapeString( escapeString(
output, output,
v.string.s, v.string.s,
options.maxStringLength, {
options.ansiColors .maxLength = options.maxStringLength,
.ansiColors = options.ansiColors,
// NB: Non-printing characters won't be escaped.
}
); );
} }

View file

@ -11,30 +11,51 @@
namespace nix { namespace nix {
std::ostream & std::ostream &
escapeString(std::ostream & str, const std::string_view string, size_t maxLength, bool ansiColors) escapeString(std::ostream & output, std::string_view string, EscapeStringOptions options)
{ {
size_t charsPrinted = 0; size_t charsPrinted = 0;
if (ansiColors) if (options.ansiColors) {
str << ANSI_MAGENTA; output << ANSI_MAGENTA;
str << "\""; }
for (auto i = string.begin(); i != string.end(); ++i) { output << "\"";
if (charsPrinted >= maxLength) { for (auto i = string.begin(); i != string.end(); ++i) {
str << "\" "; if (charsPrinted >= options.maxLength) {
printElided(str, string.length() - charsPrinted, "byte", "bytes", ansiColors); output << "\" ";
return str; printElided(
output, string.length() - charsPrinted, "byte", "bytes", options.ansiColors
);
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;
} }
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;
charsPrinted++; charsPrinted++;
} }
str << "\""; output << "\"";
if (ansiColors) if (options.ansiColors) {
str << ANSI_NORMAL; output << ANSI_NORMAL;
return str; }
return output;
}
std::string escapeString(std::string_view s, EscapeStringOptions options)
{
std::ostringstream output;
escapeString(output, s, options);
return output.str();
} }
}; // namespace nix }; // namespace nix

View file

@ -5,6 +5,41 @@
namespace nix { namespace nix {
/**
* Options for escaping strings in `escapeString`.
*
* With default optional parameters, the output string will round-trip through
* the Nix evaluator (i.e. you can copy/paste this function's output into the
* REPL and have it evaluate as the string that got passed in).
*
* With non-default optional parameters, the output string will be
* human-readable.
*/
struct EscapeStringOptions
{
/**
* If `maxLength` is decreased, some trailing portion of the string may be
* omitted with a message like `«123 bytes elided»`.
*/
size_t maxLength = std::numeric_limits<size_t>::max();
/**
* If `ansiColors` is set, the output will contain ANSI terminal escape
* sequences.
*/
bool ansiColors = false;
/**
* If `escapeNonPrinting` is set, non-printing ASCII characters (i.e. with
* byte values less than 0x20) will be printed in `\xhh` format, like
* `\x1d` (other than those that Nix supports, like `\n`, `\r`, `\t`).
* Note that this format is not yet supported by the Lix parser/evaluator!
*
* See: https://git.lix.systems/lix-project/lix/issues/149
*/
bool escapeNonPrinting = false;
};
/** /**
* Escape a string for output. * Escape a string for output.
* *
@ -14,21 +49,30 @@ namespace nix {
* *
* With non-default optional parameters, the output string will be * With non-default optional parameters, the output string will be
* human-readable. * human-readable.
*
* See `EscapeStringOptions` for more details on customizing the output.
*/ */
std::ostream &
escapeString(std::ostream & output, std::string_view s, EscapeStringOptions options = {});
std::ostream & escapeString( inline std::ostream & escapeString(std::ostream & output, const char * s)
std::ostream & output, {
const std::string_view string, return escapeString(output, std::string_view(s));
size_t maxLength = std::numeric_limits<size_t>::max(), }
bool ansiColors = false
); inline std::ostream & escapeString(std::ostream & output, const std::string & s)
{
return escapeString(output, std::string_view(s));
}
/** /**
* Escape a string for output, writing the escaped result to a new string. * Escape a string for output, writing the escaped result to a new string.
*/ */
inline std::ostream & escapeString(std::ostream & output, const char * string) std::string escapeString(std::string_view s, EscapeStringOptions options = {});
inline std::string escapeString(const char * s, EscapeStringOptions options = {})
{ {
return escapeString(output, std::string_view(string)); return escapeString(std::string_view(s), options);
} }
} // namespace nix } // namespace nix

View file

@ -0,0 +1,35 @@
#include "escape-string.hh"
#include "ansicolor.hh"
#include <gtest/gtest.h>
namespace nix {
TEST(EscapeString, simple) {
auto escaped = escapeString("puppy");
ASSERT_EQ(escaped, "\"puppy\"");
}
TEST(EscapeString, escaping) {
auto escaped = escapeString("\n\r\t \" \\ ${ooga booga}");
ASSERT_EQ(escaped, "\"\\n\\r\\t \\\" \\\\ \\${ooga booga}\"");
}
TEST(EscapeString, maxLength) {
auto escaped = escapeString("puppy", {.maxLength = 5});
ASSERT_EQ(escaped, "\"puppy\"");
escaped = escapeString("puppy doggy", {.maxLength = 5});
ASSERT_EQ(escaped, "\"puppy\" «6 bytes elided»");
}
TEST(EscapeString, ansiColors) {
auto escaped = escapeString("puppy doggy", {.maxLength = 5, .ansiColors = true});
ASSERT_EQ(escaped, ANSI_MAGENTA "\"puppy\" " ANSI_FAINT "«6 bytes elided»" ANSI_NORMAL);
}
TEST(EscapeString, escapeNonPrinting) {
auto escaped = escapeString("puppy\u0005doggy", {.escapeNonPrinting = true});
ASSERT_EQ(escaped, "\"puppy\\x05doggy\"");
}
} // namespace nix

View file

@ -39,6 +39,7 @@ libutil_tests_sources = files(
'libutil/closure.cc', 'libutil/closure.cc',
'libutil/compression.cc', 'libutil/compression.cc',
'libutil/config.cc', 'libutil/config.cc',
'libutil/escape-string.cc',
'libutil/git.cc', 'libutil/git.cc',
'libutil/hash.cc', 'libutil/hash.cc',
'libutil/hilite.cc', 'libutil/hilite.cc',