summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-10 19:24:24 -0400
committerGitHub <noreply@github.com>2020-03-10 19:24:24 -0400
commitf7bbcb6df403ca565b6dcf3ab468967f830b527d (patch)
treea466132d1e0d69a8e91a0cd52414402778d3eaa6
parent36c55f0b17e7c2697543edc38e444d01da4f4a5c (diff)
parent72219ba3530f9da02d3ec1d704baac6f88863682 (diff)
Merge pull request #408 from toasted-nutbread/use-code-point
Replace charCodeAt and fromCharCode with codePointAt and fromCodePoint
-rw-r--r--ext/bg/js/handlebars.js4
-rw-r--r--ext/bg/js/japanese.js49
-rw-r--r--ext/bg/js/translator.js11
-rw-r--r--ext/mixed/js/display-generator.js2
4 files changed, 34 insertions, 32 deletions
diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js
index b1443447..3ee4e7fa 100644
--- a/ext/bg/js/handlebars.js
+++ b/ext/bg/js/handlebars.js
@@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-/*global jpIsCharCodeKanji, jpDistributeFurigana, Handlebars*/
+/*global jpIsCodePointKanji, jpDistributeFurigana, Handlebars*/
function handlebarsEscape(text) {
return Handlebars.Utils.escapeExpression(text);
@@ -62,7 +62,7 @@ function handlebarsFuriganaPlain(options) {
function handlebarsKanjiLinks(options) {
let result = '';
for (const c of options.fn(this)) {
- if (jpIsCharCodeKanji(c.charCodeAt(0))) {
+ if (jpIsCodePointKanji(c.codePointAt(0))) {
result += `<a href="#" class="kanji-link">${c}</a>`;
} else {
result += c;
diff --git a/ext/bg/js/japanese.js b/ext/bg/js/japanese.js
index abb32da4..fc69dbba 100644
--- a/ext/bg/js/japanese.js
+++ b/ext/bg/js/japanese.js
@@ -115,9 +115,9 @@ const JP_JAPANESE_RANGES = [
// Helper functions
-function _jpIsCharCodeInRanges(charCode, ranges) {
+function _jpIsCodePointInRanges(codePoint, ranges) {
for (const [min, max] of ranges) {
- if (charCode >= min && charCode <= max) {
+ if (codePoint >= min && codePoint <= max) {
return true;
}
}
@@ -127,16 +127,16 @@ function _jpIsCharCodeInRanges(charCode, ranges) {
// Character code testing functions
-function jpIsCharCodeKanji(charCode) {
- return _jpIsCharCodeInRanges(charCode, JP_CJK_RANGES);
+function jpIsCodePointKanji(codePoint) {
+ return _jpIsCodePointInRanges(codePoint, JP_CJK_RANGES);
}
-function jpIsCharCodeKana(charCode) {
- return _jpIsCharCodeInRanges(charCode, JP_KANA_RANGES);
+function jpIsCodePointKana(codePoint) {
+ return _jpIsCodePointInRanges(codePoint, JP_KANA_RANGES);
}
-function jpIsCharCodeJapanese(charCode) {
- return _jpIsCharCodeInRanges(charCode, JP_JAPANESE_RANGES);
+function jpIsCodePointJapanese(codePoint) {
+ return _jpIsCodePointInRanges(codePoint, JP_JAPANESE_RANGES);
}
@@ -144,8 +144,8 @@ function jpIsCharCodeJapanese(charCode) {
function jpIsStringEntirelyKana(str) {
if (str.length === 0) { return false; }
- for (let i = 0, ii = str.length; i < ii; ++i) {
- if (!jpIsCharCodeKana(str.charCodeAt(i))) {
+ for (const c of str) {
+ if (!jpIsCodePointKana(c.codePointAt(0))) {
return false;
}
}
@@ -154,8 +154,8 @@ function jpIsStringEntirelyKana(str) {
function jpIsStringPartiallyJapanese(str) {
if (str.length === 0) { return false; }
- for (let i = 0, ii = str.length; i < ii; ++i) {
- if (jpIsCharCodeJapanese(str.charCodeAt(i))) {
+ for (const c of str) {
+ if (jpIsCodePointJapanese(c.codePointAt(0))) {
return true;
}
}
@@ -264,8 +264,8 @@ function jpDistributeFurigana(expression, reading) {
const groups = [];
let modePrev = null;
for (const c of expression) {
- const charCode = c.charCodeAt(0);
- const modeCurr = jpIsCharCodeKanji(charCode) || charCode === JP_ITERATION_MARK_CHAR_CODE ? 'kanji' : 'kana';
+ const codePoint = c.codePointAt(0);
+ const modeCurr = jpIsCodePointKanji(codePoint) || codePoint === JP_ITERATION_MARK_CHAR_CODE ? 'kanji' : 'kana';
if (modeCurr === modePrev) {
groups[groups.length - 1].text += c;
} else {
@@ -311,10 +311,11 @@ function jpDistributeFuriganaInflected(expression, reading, source) {
function jpConvertHalfWidthKanaToFullWidth(text, sourceMapping) {
let result = '';
- const ii = text.length;
const hasSourceMapping = Array.isArray(sourceMapping);
- for (let i = 0; i < ii; ++i) {
+ // This function is safe to use charCodeAt instead of codePointAt, since all
+ // the relevant characters are represented with a single UTF-16 character code.
+ for (let i = 0, ii = text.length; i < ii; ++i) {
const c = text[i];
const mapping = JP_HALFWIDTH_KATAKANA_MAPPING.get(c);
if (typeof mapping !== 'string') {
@@ -355,13 +356,13 @@ function jpConvertHalfWidthKanaToFullWidth(text, sourceMapping) {
function jpConvertNumericTofullWidth(text) {
let result = '';
- for (let i = 0, ii = text.length; i < ii; ++i) {
- let c = text.charCodeAt(i);
+ for (const char of text) {
+ let c = char.codePointAt(0);
if (c >= 0x30 && c <= 0x39) { // ['0', '9']
c += 0xff10 - 0x30; // 0xff10 = '0' full width
- result += String.fromCharCode(c);
+ result += String.fromCodePoint(c);
} else {
- result += text[i];
+ result += char;
}
}
return result;
@@ -377,9 +378,9 @@ function jpConvertAlphabeticToKana(text, sourceMapping) {
sourceMapping.fill(1);
}
- for (let i = 0; i < ii; ++i) {
+ for (const char of text) {
// Note: 0x61 is the character code for 'a'
- let c = text.charCodeAt(i);
+ let c = char.codePointAt(0);
if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z']
c += (0x61 - 0x41);
} else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z']
@@ -395,10 +396,10 @@ function jpConvertAlphabeticToKana(text, sourceMapping) {
result += jpToHiragana(part, sourceMapping, result.length);
part = '';
}
- result += text[i];
+ result += char;
continue;
}
- part += String.fromCharCode(c);
+ part += String.fromCodePoint(c);
}
if (part.length > 0) {
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 781eb968..c01a7124 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -20,7 +20,7 @@
dictTermsMergeBySequence, dictTagBuildSource, dictTermsMergeByGloss, dictTermsSort, dictTagsSort
dictEnabledSet, dictTermsGroup, dictTermsCompressTags, dictTermsUndupe, dictTagSanitize
jpDistributeFurigana, jpConvertHalfWidthKanaToFullWidth, jpConvertNumericTofullWidth
-jpConvertAlphabeticToKana, jpHiraganaToKatakana, jpKatakanaToHiragana, jpIsCharCodeJapanese
+jpConvertAlphabeticToKana, jpHiraganaToKatakana, jpKatakanaToHiragana, jpIsCodePointJapanese
Database, Deinflector*/
class Translator {
@@ -621,13 +621,14 @@ class Translator {
static getSearchableText(text, options) {
if (!options.scanning.alphanumeric) {
- const ii = text.length;
- for (let i = 0; i < ii; ++i) {
- if (!jpIsCharCodeJapanese(text.charCodeAt(i))) {
- text = text.substring(0, i);
+ let newText = '';
+ for (const c of text) {
+ if (!jpIsCodePointJapanese(c.codePointAt(0))) {
break;
}
+ newText += c;
}
+ text = newText;
}
return text;
diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js
index d7e77cc0..470e2a15 100644
--- a/ext/mixed/js/display-generator.js
+++ b/ext/mixed/js/display-generator.js
@@ -298,7 +298,7 @@ class DisplayGenerator {
}
static _isCharacterKanji(c) {
- const code = c.charCodeAt(0);
+ const code = c.codePointAt(0);
return (
code >= 0x4e00 && code < 0x9fb0 ||
code >= 0x3400 && code < 0x4dc0