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", "Parse UNIX file permissions",
"Swap endianness", "Swap endianness",
"Parse colour code", "Parse colour code",
"Escape String", "Escape string",
"Unescape String", "Unescape string",
] ]
}, },
{ {

View File

@ -3263,15 +3263,15 @@ const OperationConfig = {
} }
] ]
}, },
"Escape String": { "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>.", 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, run: StrUtils.runEscape,
inputType: "string", inputType: "string",
outputType: "string", outputType: "string",
args: [] args: []
}, },
"Unescape String": { "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>.", 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, run: StrUtils.runUnescape,
inputType: "string", inputType: "string",
outputType: "string", outputType: "string",

View File

@ -448,6 +448,7 @@ const StrUtils = {
return outputs.join(sampleDelim); return outputs.join(sampleDelim);
}, },
/** /**
* @constant * @constant
* @default * @default
@ -464,7 +465,7 @@ const StrUtils = {
], ],
/** /**
* Escapes a string for embedding in another string. * Escape string operation.
* *
* @author Vel0x [dalemy@microsoft.com] * @author Vel0x [dalemy@microsoft.com]
* *
@ -483,8 +484,9 @@ const StrUtils = {
return StrUtils._replaceByKeys(input, "unescaped", "escaped"); return StrUtils._replaceByKeys(input, "unescaped", "escaped");
}, },
/** /**
* Unescapes a string that was part of another string * Unescape string operation.
* *
* @author Vel0x [dalemy@microsoft.com] * @author Vel0x [dalemy@microsoft.com]
* *
@ -503,9 +505,10 @@ const StrUtils = {
return StrUtils._replaceByKeys(input, "escaped", "unescaped"); return StrUtils._replaceByKeys(input, "escaped", "unescaped");
}, },
/** /**
* Replaces all matching tokens in ESCAPE_REPLACEMENTS with the correction. The * 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 Vel0x [dalemy@microsoft.com]
* @author Matt C [matt@artemisbot.uk] * @author Matt C [matt@artemisbot.uk]
@ -517,7 +520,10 @@ const StrUtils = {
*/ */
_replaceByKeys: function(input, patternKey, replacementKey) { _replaceByKeys: function(input, patternKey, replacementKey) {
let output = input; 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 => { StrUtils.ESCAPE_REPLACEMENTS.forEach(replacement => {
output = output.split(replacement[patternKey]).join(replacement[replacementKey]); output = output.split(replacement[patternKey]).join(replacement[replacementKey]);
}); });