Tidied up string escape operations

This commit is contained in:
n1474335 2017-08-15 17:29:48 +00:00
parent 55806db00f
commit 4b22a409e7
3 changed files with 16 additions and 10 deletions

View File

@ -187,8 +187,8 @@ const Categories = [
"Parse UNIX file permissions",
"Swap endianness",
"Parse colour code",
"Escape String",
"Unescape String",
"Escape string",
"Unescape string",
]
},
{

View File

@ -3263,15 +3263,15 @@ const OperationConfig = {
}
]
},
"Escape String": {
description: "Escapes a string so that it can be embedded in another. For example, <code>Don't stop me now</code> becomes <code>Don\\'t stop me now</code>.",
"Escape string": {
description: "Escapes special characters in a string so that they do not cause conflicts. For example, <code>Don't stop me now</code> becomes <code>Don\\'t stop me now</code>.",
run: StrUtils.runEscape,
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, <code>Don\\'t stop me now</code> becomes <code>Don't stop me now</code>.",
"Unescape string": {
description: "Unescapes characters in a string that have been escaped. For example, <code>Don\\'t stop me now</code> becomes <code>Don't stop me now</code>.",
run: StrUtils.runUnescape,
inputType: "string",
outputType: "string",

View File

@ -448,6 +448,7 @@ const StrUtils = {
return outputs.join(sampleDelim);
},
/**
* @constant
* @default
@ -464,7 +465,7 @@ const StrUtils = {
],
/**
* Escapes a string for embedding in another string.
* Escape string operation.
*
* @author Vel0x [dalemy@microsoft.com]
*
@ -483,8 +484,9 @@ const StrUtils = {
return StrUtils._replaceByKeys(input, "unescaped", "escaped");
},
/**
* Unescapes a string that was part of another string
* Unescape string operation.
*
* @author Vel0x [dalemy@microsoft.com]
*
@ -503,9 +505,10 @@ const StrUtils = {
return StrUtils._replaceByKeys(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.
* ordering is determined by the patternKey and the replacementKey.
*
* @author Vel0x [dalemy@microsoft.com]
* @author Matt C [matt@artemisbot.uk]
@ -517,7 +520,10 @@ const StrUtils = {
*/
_replaceByKeys: function(input, patternKey, replacementKey) {
let output = input;
if (patternKey === "escaped") output = Utils.parseEscapedChars(input); // I've wrapped this to catch the \\x encoded characters
// Catch the \\x encoded characters
if (patternKey === "escaped") output = Utils.parseEscapedChars(input);
StrUtils.ESCAPE_REPLACEMENTS.forEach(replacement => {
output = output.split(replacement[patternKey]).join(replacement[replacementKey]);
});