aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorEugene Sandulenko2019-12-09 21:59:49 +0100
committerEugene Sandulenko2019-12-10 00:32:54 +0100
commit6b6dc218133f406c89ca6446dcaa5ba68f464c2a (patch)
treed372a18a2a86c0cad25ff650d7fe3c1f03cb1dac /common
parent3d86a5c09d4caa6ba3aaaa77cdc9400d5a7ff847 (diff)
downloadscummvm-rg350-6b6dc218133f406c89ca6446dcaa5ba68f464c2a.tar.gz
scummvm-rg350-6b6dc218133f406c89ca6446dcaa5ba68f464c2a.tar.bz2
scummvm-rg350-6b6dc218133f406c89ca6446dcaa5ba68f464c2a.zip
COMMON: Added helper function to produce printable strings
Diffstat (limited to 'common')
-rw-r--r--common/str.cpp37
-rw-r--r--common/str.h9
2 files changed, 46 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 383a61a687..95b13be2aa 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -1094,6 +1094,43 @@ size_t strnlen(const char *src, size_t maxSize) {
return counter;
}
+String toPrintable(const String &in, bool keepNewLines) {
+ Common::String res;
+
+ const char *tr = "\x01\x02\x03\x04\x05\x06" "a"
+ //"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
+ "b" "t" "n" "v" "f" "r\x0e\x0f"
+ "\x10\x11\x12\x13\x14\x15\x16\x17"
+ "\x18\x19\x1a" "e\x1c\x1d\x1e\x1f";
+
+ for (const char *p = in.c_str(); *p; p++) {
+ if (*p == '\n') {
+ if (keepNewLines)
+ res += *p;
+ else
+ res += "\\n";
+
+ continue;
+ }
+
+ if (*p < 0x20 || *p == '\'' || *p == '\"' || *p == '\\') {
+ res += '\\';
+
+ if (*p < 0x20) {
+ if (tr[*p + 1] < 0x20)
+ res += Common::String::format("x%02x", *p);
+ else
+ res += tr[*p + 1];
+ } else {
+ res += *p; // We will escape it
+ }
+ } else
+ res += *p;
+ }
+
+ return res;
+}
+
} // End of namespace Common
// Portable implementation of stricmp / strcasecmp / strcmpi.
diff --git a/common/str.h b/common/str.h
index 2c6c142a73..811214bac6 100644
--- a/common/str.h
+++ b/common/str.h
@@ -491,6 +491,15 @@ size_t strnlen(const char *src, size_t maxSize);
*/
#define tag2str(x) Common::tag2string(x).c_str()
+/**
+ * Converts string with all non-printable characters properly escaped
+ * with use of C++ escape sequences
+ *
+ * @param src The source string.
+ * @param keepNewLines Whether keep newlines or convert them to '\n', default: true.
+ * @return The converted string.
+ */
+String toPrintable(const String &src, bool keepNewLines = true);
} // End of namespace Common