Convert src/core/to_unicode_map.js
to use standard classes
This commit is contained in:
parent
33ea6b1131
commit
22539b52fa
@ -15,99 +15,89 @@
|
|||||||
|
|
||||||
import { unreachable } from "../shared/util.js";
|
import { unreachable } from "../shared/util.js";
|
||||||
|
|
||||||
const ToUnicodeMap = (function ToUnicodeMapClosure() {
|
class ToUnicodeMap {
|
||||||
// eslint-disable-next-line no-shadow
|
constructor(cmap = []) {
|
||||||
function ToUnicodeMap(cmap = []) {
|
|
||||||
// The elements of this._map can be integers or strings, depending on how
|
// The elements of this._map can be integers or strings, depending on how
|
||||||
// `cmap` was created.
|
// `cmap` was created.
|
||||||
this._map = cmap;
|
this._map = cmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
ToUnicodeMap.prototype = {
|
get length() {
|
||||||
get length() {
|
return this._map.length;
|
||||||
return this._map.length;
|
}
|
||||||
},
|
|
||||||
|
|
||||||
forEach(callback) {
|
forEach(callback) {
|
||||||
for (const charCode in this._map) {
|
for (const charCode in this._map) {
|
||||||
callback(charCode, this._map[charCode].charCodeAt(0));
|
callback(charCode, this._map[charCode].charCodeAt(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
has(i) {
|
||||||
|
return this._map[i] !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(i) {
|
||||||
|
return this._map[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
charCodeOf(value) {
|
||||||
|
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which
|
||||||
|
// are both very sparse and very large (see issue8372.pdf).
|
||||||
|
const map = this._map;
|
||||||
|
if (map.length <= 0x10000) {
|
||||||
|
return map.indexOf(value);
|
||||||
|
}
|
||||||
|
for (const charCode in map) {
|
||||||
|
if (map[charCode] === value) {
|
||||||
|
return charCode | 0;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
has(i) {
|
amend(map) {
|
||||||
return this._map[i] !== undefined;
|
for (const charCode in map) {
|
||||||
},
|
this._map[charCode] = map[charCode];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get(i) {
|
class IdentityToUnicodeMap {
|
||||||
return this._map[i];
|
constructor(firstChar, lastChar) {
|
||||||
},
|
|
||||||
|
|
||||||
charCodeOf(value) {
|
|
||||||
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which
|
|
||||||
// are both very sparse and very large (see issue8372.pdf).
|
|
||||||
const map = this._map;
|
|
||||||
if (map.length <= 0x10000) {
|
|
||||||
return map.indexOf(value);
|
|
||||||
}
|
|
||||||
for (const charCode in map) {
|
|
||||||
if (map[charCode] === value) {
|
|
||||||
return charCode | 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
},
|
|
||||||
|
|
||||||
amend(map) {
|
|
||||||
for (const charCode in map) {
|
|
||||||
this._map[charCode] = map[charCode];
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return ToUnicodeMap;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
|
|
||||||
// eslint-disable-next-line no-shadow
|
|
||||||
function IdentityToUnicodeMap(firstChar, lastChar) {
|
|
||||||
this.firstChar = firstChar;
|
this.firstChar = firstChar;
|
||||||
this.lastChar = lastChar;
|
this.lastChar = lastChar;
|
||||||
}
|
}
|
||||||
|
|
||||||
IdentityToUnicodeMap.prototype = {
|
get length() {
|
||||||
get length() {
|
return this.lastChar + 1 - this.firstChar;
|
||||||
return this.lastChar + 1 - this.firstChar;
|
}
|
||||||
},
|
|
||||||
|
|
||||||
forEach(callback) {
|
forEach(callback) {
|
||||||
for (let i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
|
for (let i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
|
||||||
callback(i, i);
|
callback(i, i);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
has(i) {
|
has(i) {
|
||||||
return this.firstChar <= i && i <= this.lastChar;
|
return this.firstChar <= i && i <= this.lastChar;
|
||||||
},
|
}
|
||||||
|
|
||||||
get(i) {
|
get(i) {
|
||||||
if (this.firstChar <= i && i <= this.lastChar) {
|
if (this.firstChar <= i && i <= this.lastChar) {
|
||||||
return String.fromCharCode(i);
|
return String.fromCharCode(i);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
},
|
}
|
||||||
|
|
||||||
charCodeOf(v) {
|
charCodeOf(v) {
|
||||||
return Number.isInteger(v) && v >= this.firstChar && v <= this.lastChar
|
return Number.isInteger(v) && v >= this.firstChar && v <= this.lastChar
|
||||||
? v
|
? v
|
||||||
: -1;
|
: -1;
|
||||||
},
|
}
|
||||||
|
|
||||||
amend(map) {
|
amend(map) {
|
||||||
unreachable("Should not call amend()");
|
unreachable("Should not call amend()");
|
||||||
},
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
return IdentityToUnicodeMap;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export { IdentityToUnicodeMap, ToUnicodeMap };
|
export { IdentityToUnicodeMap, ToUnicodeMap };
|
||||||
|
Loading…
Reference in New Issue
Block a user