aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/escape-string.hh
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/libutil/escape-string.hh
parent5a54b0a20c80356de5098694353f506e73fb883f (diff)
Move `escapeString` to its own file
Change-Id: Ie5c954ec73c46c9d3c679ef99a83a29cc7a08352
Diffstat (limited to 'src/libutil/escape-string.hh')
-rw-r--r--src/libutil/escape-string.hh34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libutil/escape-string.hh b/src/libutil/escape-string.hh
new file mode 100644
index 000000000..28c6c8d64
--- /dev/null
+++ b/src/libutil/escape-string.hh
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <limits>
+#include <ostream>
+
+namespace nix {
+
+/**
+ * Escape a string for output.
+ *
+ * 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.
+ */
+
+std::ostream & escapeString(
+ std::ostream & output,
+ const std::string_view string,
+ size_t maxLength = std::numeric_limits<size_t>::max(),
+ bool ansiColors = false
+);
+
+/**
+ * Escape a string for output, writing the escaped result to a new string.
+ */
+inline std::ostream & escapeString(std::ostream & output, const char * string)
+{
+ return escapeString(output, std::string_view(string));
+}
+
+} // namespace nix