|
|
@ -127,17 +127,11 @@ namespace app {
|
|
|
|
// Returns the number of Unicode code points in the given UTF-16 string.
|
|
|
|
// Returns the number of Unicode code points in the given UTF-16 string.
|
|
|
|
function countUnicodeChars(str: string): number {
|
|
|
|
function countUnicodeChars(str: string): number {
|
|
|
|
let result: number = 0;
|
|
|
|
let result: number = 0;
|
|
|
|
for (let i = 0; i < str.length; i++, result++) {
|
|
|
|
for (const ch of str) {
|
|
|
|
const c: number = str.charCodeAt(i);
|
|
|
|
const cc = ch.codePointAt(0) as number;
|
|
|
|
if (c < 0xD800 || c >= 0xE000)
|
|
|
|
if (0xD800 <= cc && cc < 0xE000)
|
|
|
|
continue;
|
|
|
|
throw new RangeError("Invalid UTF-16 string");
|
|
|
|
else if (0xD800 <= c && c < 0xDC00 && i + 1 < str.length) { // High surrogate
|
|
|
|
result++;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
const d: number = str.charCodeAt(i);
|
|
|
|
|
|
|
|
if (0xDC00 <= d && d < 0xE000) // Low surrogate
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RangeError("Invalid UTF-16 string");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|