aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorRebecca Turner <rbt@sent.as>2024-03-28 16:26:42 -0700
committerRebecca Turner <rbt@sent.as>2024-03-29 16:26:29 -0700
commita5a25894c152848d1a57f97b2ef5542ddf6cdb9d (patch)
treeea623d3e50f133b6f1d651847eef67e9172a3218 /src/libexpr
parent5a54b0a20c80356de5098694353f506e73fb883f (diff)
Move `escapeString` to its own file
Change-Id: Ie5c954ec73c46c9d3c679ef99a83a29cc7a08352
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/nixexpr.cc3
-rw-r--r--src/libexpr/print-ambiguous.cc3
-rw-r--r--src/libexpr/print.cc66
-rw-r--r--src/libexpr/print.hh17
4 files changed, 16 insertions, 73 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index fd380935a..72a8764e6 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -4,6 +4,7 @@
#include "symbol-table.hh"
#include "util.hh"
#include "print.hh"
+#include "escape-string.hh"
#include <cstdlib>
@@ -36,7 +37,7 @@ void ExprFloat::show(const SymbolTable & symbols, std::ostream & str) const
void ExprString::show(const SymbolTable & symbols, std::ostream & str) const
{
- printLiteralString(str, s);
+ escapeString(str, s);
}
void ExprPath::show(const SymbolTable & symbols, std::ostream & str) const
diff --git a/src/libexpr/print-ambiguous.cc b/src/libexpr/print-ambiguous.cc
index 3f3c2c45a..ec30f5073 100644
--- a/src/libexpr/print-ambiguous.cc
+++ b/src/libexpr/print-ambiguous.cc
@@ -2,6 +2,7 @@
#include "print.hh"
#include "eval.hh"
#include "signals.hh"
+#include "escape-string.hh"
namespace nix {
@@ -27,7 +28,7 @@ void printAmbiguous(
printLiteralBool(str, v.boolean);
break;
case nString:
- printLiteralString(str, v.string.s);
+ escapeString(str, v.string.s);
break;
case nPath:
str << v.path().to_string(); // !!! escaping?
diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc
index 43e366d08..e5e6b9b21 100644
--- a/src/libexpr/print.cc
+++ b/src/libexpr/print.cc
@@ -2,6 +2,7 @@
#include <span>
#include <unordered_set>
+#include "escape-string.hh"
#include "print.hh"
#include "ansicolor.hh"
#include "store-api.hh"
@@ -11,57 +12,6 @@
namespace nix {
-void printElided(
- std::ostream & output,
- unsigned int value,
- const std::string_view single,
- const std::string_view plural,
- bool ansiColors)
-{
- if (ansiColors)
- output << ANSI_FAINT;
- output << "«";
- pluralize(output, value, single, plural);
- output << " elided»";
- if (ansiColors)
- output << ANSI_NORMAL;
-}
-
-
-std::ostream &
-printLiteralString(std::ostream & str, const std::string_view string, size_t maxLength, bool ansiColors)
-{
- size_t charsPrinted = 0;
- if (ansiColors)
- str << ANSI_MAGENTA;
- str << "\"";
- for (auto i = string.begin(); i != string.end(); ++i) {
- if (charsPrinted >= maxLength) {
- str << "\" ";
- printElided(str, string.length() - charsPrinted, "byte", "bytes", ansiColors);
- return str;
- }
-
- 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;
-}
-
-std::ostream &
-printLiteralString(std::ostream & str, const std::string_view string)
-{
- return printLiteralString(str, string, std::numeric_limits<size_t>::max(), false);
-}
-
std::ostream &
printLiteralBool(std::ostream & str, bool boolean)
{
@@ -93,7 +43,7 @@ printIdentifier(std::ostream & str, std::string_view s) {
else {
char c = s[0];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')) {
- printLiteralString(str, s);
+ escapeString(str, s);
return str;
}
for (auto c : s)
@@ -101,7 +51,7 @@ printIdentifier(std::ostream & str, std::string_view s) {
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '_' || c == '\'' || c == '-')) {
- printLiteralString(str, s);
+ escapeString(str, s);
return str;
}
str << s;
@@ -129,7 +79,7 @@ printAttributeName(std::ostream & str, std::string_view name) {
if (isVarName(name))
str << name;
else
- printLiteralString(str, name);
+ escapeString(str, name);
return str;
}
@@ -248,7 +198,13 @@ private:
void printString(Value & v)
{
- printLiteralString(output, v.string.s, options.maxStringLength, options.ansiColors);
+ // NB: Non-printing characters won't be escaped.
+ escapeString(
+ output,
+ v.string.s,
+ options.maxStringLength,
+ options.ansiColors
+ );
}
void printPath(Value & v)
diff --git a/src/libexpr/print.hh b/src/libexpr/print.hh
index 94cb11ca7..42826d94d 100644
--- a/src/libexpr/print.hh
+++ b/src/libexpr/print.hh
@@ -11,28 +11,13 @@
#include "fmt.hh"
#include "print-options.hh"
+#include "print-elided.hh"
namespace nix {
class EvalState;
struct Value;
-/**
- * Print a string as a Nix string literal.
- *
- * Quotes and fairly minimal escaping are added.
- *
- * @param o The output stream to print to
- * @param s The logical string
- */
-std::ostream & printLiteralString(std::ostream & o, std::string_view s);
-inline std::ostream & printLiteralString(std::ostream & o, const char * s) {
- return printLiteralString(o, std::string_view(s));
-}
-inline std::ostream & printLiteralString(std::ostream & o, const std::string & s) {
- return printLiteralString(o, std::string_view(s));
-}
-
/** Print `true` or `false`. */
std::ostream & printLiteralBool(std::ostream & o, bool b);