2016-12-23 22:31:38 +03:00
|
|
|
exports[`test UnicodeUtils.js 1`] = `
|
|
|
|
"/**
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} codeUnit A Unicode code-unit, in range [0, 0x10FFFF]
|
|
|
|
* @return {boolean} Whether code-unit is in a surrogate (hi/low) range
|
|
|
|
*/
|
|
|
|
function inSurrogateRange(codeUnit) {
|
|
|
|
return 0xD800 <= codeUnit && codeUnit <= 0xDFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the length of the original Unicode character at given position in the
|
|
|
|
* String by looking into the UTF-16 code unit; that is equal to 1 for any
|
|
|
|
* non-surrogate characters in BMP ([U+0000..U+D7FF] and [U+E000, U+FFFF]); and
|
|
|
|
* returns 2 for the hi/low surrogates ([U+D800..U+DFFF]), which are in fact
|
|
|
|
* representing non-BMP characters ([U+10000..U+10FFFF]).
|
|
|
|
*
|
|
|
|
* Examples:
|
2016-12-27 21:29:31 +03:00
|
|
|
* - '\u0020' => 1
|
|
|
|
* - '\u3020' => 1
|
|
|
|
* - '\uD835' => 2
|
|
|
|
* - '\uD835\uDDEF' => 2
|
|
|
|
* - '\uDDEF' => 2
|
2016-12-23 22:31:38 +03:00
|
|
|
*
|
|
|
|
* @param {string} str Non-empty string
|
|
|
|
* @param {number} pos Position in the string to look for one code unit
|
|
|
|
* @return {number} Number 1 or 2
|
|
|
|
*/
|
|
|
|
function utf16Length(str, pos) {
|
|
|
|
return 1 + inSurrogateRange(str.charCodeAt(pos));
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @param {number} codeUnit A Unicode code-unit, in range [0, 0x10FFFF]
|
|
|
|
* @return {boolean} Whether code-unit is in a surrogate (hi/low) range
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Return the length of the original Unicode character at given position in the
|
|
|
|
* String by looking into the UTF-16 code unit; that is equal to 1 for any
|
|
|
|
* non-surrogate characters in BMP ([U+0000..U+D7FF] and [U+E000, U+FFFF]); and
|
|
|
|
* returns 2 for the hi/low surrogates ([U+D800..U+DFFF]), which are in fact
|
|
|
|
* representing non-BMP characters ([U+10000..U+10FFFF]).
|
|
|
|
*
|
|
|
|
* Examples:
|
2016-12-27 21:29:31 +03:00
|
|
|
* - '\u0020' => 1
|
|
|
|
* - '\u3020' => 1
|
|
|
|
* - '\uD835' => 2
|
|
|
|
* - '\uD835\uDDEF' => 2
|
|
|
|
* - '\uDDEF' => 2
|
2016-12-23 22:31:38 +03:00
|
|
|
*
|
|
|
|
* @param {string} str Non-empty string
|
|
|
|
* @param {number} pos Position in the string to look for one code unit
|
|
|
|
* @return {number} Number 1 or 2
|
|
|
|
*/
|
2016-12-27 21:29:31 +03:00
|
|
|
function inSurrogateRange(codeUnit) {
|
|
|
|
return [object Number] <= codeUnit && codeUnit <= [object Number];
|
|
|
|
}
|
2016-12-23 22:31:38 +03:00
|
|
|
function utf16Length(str, pos) {
|
2016-12-27 21:29:31 +03:00
|
|
|
return [object Number] + inSurrogateRange(str.charCodeAt(pos));
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
"
|
|
|
|
`;
|