2017-03-23 17:52:20 +00:00
|
|
|
|
import Utils from "../Utils.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* Unicode operations.
|
|
|
|
|
*
|
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
|
* @license Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
|
const Unicode = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constant
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
PREFIXES: ["\\u", "%u", "U+"],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unescape Unicode Characters operation.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} input
|
|
|
|
|
* @param {Object[]} args
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
|
runUnescape: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
|
let prefix = Unicode._prefixToRegex[args[0]],
|
2018-02-16 13:33:33 +00:00
|
|
|
|
regex = new RegExp(prefix+"([a-f\\d]{4})", "ig"),
|
2016-11-28 10:42:58 +00:00
|
|
|
|
output = "",
|
|
|
|
|
m,
|
|
|
|
|
i = 0;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
2016-12-14 16:39:17 +00:00
|
|
|
|
while ((m = regex.exec(input))) {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
// Add up to match
|
|
|
|
|
output += input.slice(i, m.index);
|
|
|
|
|
i = m.index;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
// Add match
|
|
|
|
|
output += Utils.chr(parseInt(m[1], 16));
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
i = regex.lastIndex;
|
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
// Add all after final match
|
|
|
|
|
output += input.slice(i, input.length);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
return output;
|
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
|
|
2018-03-26 22:25:36 +01:00
|
|
|
|
/**
|
|
|
|
|
* Escape Unicode Characters operation.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} input
|
|
|
|
|
* @param {Object[]} args
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
runEscape: function(input, args) {
|
|
|
|
|
const regexWhitelist = /[ -~]/i,
|
|
|
|
|
prefix = args[0],
|
|
|
|
|
encodeAll = args[1],
|
|
|
|
|
padding = args[2],
|
|
|
|
|
uppercaseHex = args[3];
|
|
|
|
|
|
|
|
|
|
let output = "",
|
|
|
|
|
character = "";
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
|
character = input[i];
|
|
|
|
|
if (!encodeAll && regexWhitelist.test(character)) {
|
|
|
|
|
// It’s a printable ASCII character so don’t escape it.
|
|
|
|
|
output += character;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cp = character.codePointAt(0).toString(16);
|
|
|
|
|
if (uppercaseHex) cp = cp.toUpperCase();
|
|
|
|
|
output += prefix + cp.padStart(padding, "0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* Lookup table to add prefixes to unicode delimiters so that they can be used in a regex.
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
* @constant
|
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
|
_prefixToRegex: {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
"\\u": "\\\\u",
|
|
|
|
|
"%u": "%u",
|
|
|
|
|
"U+": "U\\+"
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
|
|
export default Unicode;
|