diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js
index ce46d221..7599aaca 100755
--- a/src/core/config/Categories.js
+++ b/src/core/config/Categories.js
@@ -188,6 +188,8 @@ const Categories = [
"Parse UNIX file permissions",
"Swap endianness",
"Parse colour code",
+ "Escape String",
+ "Unescape String",
]
},
{
diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js
index b0c005b3..645b7629 100755
--- a/src/core/config/OperationConfig.js
+++ b/src/core/config/OperationConfig.js
@@ -3252,6 +3252,20 @@ const OperationConfig = {
}
]
},
+ "Escape String": {
+ description: "Escapes a string so that it can be embedded in another. For example, Don't stop me now
becomes Don\\'t stop me now
.",
+ run: StrUtils.run_escape,
+ inputType: "string",
+ outputType: "string",
+ args: []
+ },
+ "Unescape String": {
+ description: "Unescapes a string that was embedded inside another so that it can be used in it's own right. For example, Don\\'t stop me now
becomes Don't stop me now
.",
+ run: StrUtils.run_unescape,
+ inputType: "string",
+ outputType: "string",
+ args: []
+ },
"To Morse Code": {
description: "Translates alphanumeric characters into International Morse Code.
Ignores non-Morse characters.
e.g. SOS
becomes ... --- ...
",
run: MorseCode.runTo,
diff --git a/src/core/operations/StrUtils.js b/src/core/operations/StrUtils.js
index 698e7eef..9250620f 100755
--- a/src/core/operations/StrUtils.js
+++ b/src/core/operations/StrUtils.js
@@ -460,6 +460,70 @@ const StrUtils = {
return Utils.parseEscapedChars(input);
},
+ /**
+ * @constant
+ * @default
+ */
+ ESCAPE_REPLACEMENTS: [
+ {"escaped": "\\\\", "unescaped": "\\"}, // Must be first
+ {"escaped": "\\'", "unescaped": "'"},
+ {"escaped": "\\\"", "unescaped": "\""},
+ {"escaped": "\\n", "unescaped": "\n"},
+ {"escaped": "\\r", "unescaped": "\r"},
+ ],
+
+ /**
+ * Escapes a string for embedding in another string.
+ *
+ * Example: "Don't do that" -> "Don\'t do that"
+ *
+ * @author Vel0x [dalemy@microsoft.com]
+ *
+ * @param {string} input
+ * @param {Object[]} args
+ * @returns {string}
+ */
+ runEscape: function(input, args) {
+ return StrUtils._replace_by_keys(input, "unescaped", "escaped");
+ },
+
+ /**
+ * Unescapes a string that was part of another string
+ *
+ * Example: "Don\'t do that" -> "Don't do that"
+ *
+ * @author Vel0x [dalemy@microsoft.com]
+ *
+ * @param {string} input
+ * @param {Object[]} args
+ * @returns {string}
+ */
+ runUnescape: function(input, args) {
+ return StrUtils._replace_by_keys(input, "escaped", "unescaped");
+ },
+
+ /**
+ * Replaces all matching tokens in ESCAPE_REPLACEMENTS with the correction. The
+ * ordering is determined by the pattern_key and the replacement_key.
+ *
+ * @author Vel0x [dalemy@microsoft.com]
+ * @author Matt C [matt@artemisbot.uk]
+ *
+ * @param {string} input
+ * @param {string} pattern_key
+ * @param {string} replacement_key
+ * @returns {string}
+ */
+ _replaceByKeys: function(input, patternKey, replacementKey) {
+ const replacementsLength = StrUtils.ESCAPE_REPLACEMENTS.length;
+ let output = input;
+ for (let i = 0; i < replacementsLength; i++) {
+ const replacement = StrUtils.ESCAPE_REPLACEMENTS[i];
+ output = output.split(replacement[patternKey]).join(replacement[replacementKey]);
+ }
+ return output;
+ },
+
/**
* Head lines operation.