Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string escape/unescape operations #62

Merged
merged 1 commit into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/js/config/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ var Categories = [
"Parse UNIX file permissions",
"Swap endianness",
"Parse colour code",
"Escape String",
"Unescape String",
]
},
{
Expand Down
16 changes: 15 additions & 1 deletion src/js/config/OperationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2952,5 +2952,19 @@ var OperationConfig = {
value: Cipher.SUBS_CIPHERTEXT
}
]
}
},
"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>.",
run: String_.run_escape,
input_type: "string",
output_type: "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>.",
run: String_.run_unescape,
input_type: "string",
output_type: "string",
args: []
},
};
67 changes: 67 additions & 0 deletions src/js/operations/String.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* String operations.
* Namespace is appended with an underscore to prevent overwriting the global String object.
*
* @author Vel0x
* @namespace
*/
var String_ = {

/**
* @constant
* @default
*/
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"
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run_escape: function(input, args) {
return String_._replace_by_keys(input, "unescaped", "escaped")
},

/**
* Unescapes a string that was part of another string
*
* Example: "Don\'t do that" -> "Don't do that"
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run_unescape: function(input, args) {
return String_._replace_by_keys(input, "escaped", "unescaped")
},

/**
* Replaces all matching tokens in REPLACEMENTS with the correction. The
* ordering is determined by the pattern_key and the replacement_key.
*
* @param {string} input
* @param {string} pattern_key
* @param {string} replacement_key
* @returns {string}
*/
_replace_by_keys: function(input, pattern_key, replacement_key) {
var output = input;
var replacementsLength = String_.REPLACEMENTS.length;
for (var i = 0; i < replacementsLength; i++) {
var replacement = String_.REPLACEMENTS[i];
output = output.split(replacement[pattern_key]).join(replacement[replacement_key]);
}
return output
},

};