aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrebecca “wiggles” turner <rbt@sent.as>2024-04-04 17:41:13 +0000
committerGerrit Code Review <gerrit@lix>2024-04-04 17:41:13 +0000
commitb44514819bdb972b6e2beb6cd27efecb092ba514 (patch)
tree48c129f95f2a25540b8cbbcba355f0c6172bb924 /src
parentc263554ec66fd20f45d40f8f0708217d6bf2711f (diff)
parentea10fe7ab0df085b51189adabdb079fc4442c6be (diff)
Merge "Add `EscapeStringOptions` and `escapeString` tests" into main
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/print.cc8
-rw-r--r--src/libutil/escape-string.cc57
-rw-r--r--src/libutil/escape-string.hh60
3 files changed, 96 insertions, 29 deletions
diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc
index e5e6b9b21..8d7e2ab34 100644
--- a/src/libexpr/print.cc
+++ b/src/libexpr/print.cc
@@ -198,12 +198,14 @@ private:
void printString(Value & v)
{
- // NB: Non-printing characters won't be escaped.
escapeString(
output,
v.string.s,
- options.maxStringLength,
- options.ansiColors
+ {
+ .maxLength = options.maxStringLength,
+ .outputAnsiColors = options.ansiColors,
+ // NB: Non-printing characters won't be escaped.
+ }
);
}
diff --git a/src/libutil/escape-string.cc b/src/libutil/escape-string.cc
index 8160403cd..c3cea96d2 100644
--- a/src/libutil/escape-string.cc
+++ b/src/libutil/escape-string.cc
@@ -11,30 +11,51 @@
namespace nix {
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;
- if (ansiColors)
- str << ANSI_MAGENTA;
- str << "\"";
+ if (options.outputAnsiColors) {
+ output << ANSI_MAGENTA;
+ }
+ output << "\"";
for (auto i = string.begin(); i != string.end(); ++i) {
- if (charsPrinted >= maxLength) {
- str << "\" ";
- printElided(str, string.length() - charsPrinted, "byte", "bytes", ansiColors);
- return str;
+ 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;
}
- 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++;
}
- str << "\"";
- if (ansiColors)
- str << ANSI_NORMAL;
- return str;
+ 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();
}
}; // namespace nix
diff --git a/src/libutil/escape-string.hh b/src/libutil/escape-string.hh
index 28c6c8d64..7f0a9e701 100644
--- a/src/libutil/escape-string.hh
+++ b/src/libutil/escape-string.hh
@@ -6,6 +6,41 @@
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 `outputAnsiColors` is set, the string will be colored the color of literals, using
+ * ANSI escape codes.
+ */
+ bool outputAnsiColors = 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.
*
* With default optional parameters, the output string will round-trip through
@@ -14,21 +49,30 @@ namespace nix {
*
* With non-default optional parameters, the output string will be
* human-readable.
+ *
+ * See `EscapeStringOptions` for more details on customizing the output.
*/
+std::ostream &
+escapeString(std::ostream & output, std::string_view s, EscapeStringOptions options = {});
+
+inline std::ostream & escapeString(std::ostream & output, const char * s)
+{
+ return escapeString(output, std::string_view(s));
+}
-std::ostream & escapeString(
- std::ostream & output,
- const std::string_view string,
- 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.
*/
-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