Socket
Socket
Sign inDemoInstall

unicode-trie

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unicode-trie - npm Package Compare versions

Comparing version 0.3.1 to 1.0.0

LICENSE

800

builder.js

@@ -1,173 +0,302 @@

// Generated by CoffeeScript 1.7.1
var UnicodeTrie, UnicodeTrieBuilder, pako;
const UnicodeTrie = require('./');
const pako = require('pako');
UnicodeTrie = require('./');
// Shift size for getting the index-1 table offset.
const SHIFT_1 = 6 + 5;
pako = require('pako');
// Shift size for getting the index-2 table offset.
const SHIFT_2 = 5;
UnicodeTrieBuilder = (function() {
var BAD_UTF8_DATA_OFFSET, CP_PER_INDEX_1_ENTRY, DATA_0800_OFFSET, DATA_BLOCK_LENGTH, DATA_GRANULARITY, DATA_MASK, DATA_NULL_OFFSET, DATA_START_OFFSET, INDEX_1_LENGTH, INDEX_1_OFFSET, INDEX_2_BLOCK_LENGTH, INDEX_2_BMP_LENGTH, INDEX_2_MASK, INDEX_2_NULL_OFFSET, INDEX_2_OFFSET, INDEX_2_START_OFFSET, INDEX_GAP_LENGTH, INDEX_GAP_OFFSET, INDEX_SHIFT, INITIAL_DATA_LENGTH, LSCP_INDEX_2_LENGTH, LSCP_INDEX_2_OFFSET, MAX_DATA_LENGTH, MAX_INDEX_1_LENGTH, MAX_INDEX_2_LENGTH, MAX_INDEX_LENGTH, MEDIUM_DATA_LENGTH, NEW_DATA_START_OFFSET, OMITTED_BMP_INDEX_1_LENGTH, SHIFT_1, SHIFT_1_2, SHIFT_2, UTF8_2B_INDEX_2_LENGTH, UTF8_2B_INDEX_2_OFFSET, equal_int;
// Difference between the two shift sizes,
// for getting an index-1 offset from an index-2 offset. 6=11-5
const SHIFT_1_2 = SHIFT_1 - SHIFT_2;
SHIFT_1 = 6 + 5;
// Number of index-1 entries for the BMP. 32=0x20
// This part of the index-1 table is omitted from the serialized form.
const OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
SHIFT_2 = 5;
// Number of code points per index-1 table entry. 2048=0x800
const CP_PER_INDEX_1_ENTRY = 1 << SHIFT_1;
SHIFT_1_2 = SHIFT_1 - SHIFT_2;
// Number of entries in an index-2 block. 64=0x40
const INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
// Mask for getting the lower bits for the in-index-2-block offset. */
const INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
CP_PER_INDEX_1_ENTRY = 1 << SHIFT_1;
// Number of entries in a data block. 32=0x20
const DATA_BLOCK_LENGTH = 1 << SHIFT_2;
INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
// Mask for getting the lower bits for the in-data-block offset.
const DATA_MASK = DATA_BLOCK_LENGTH - 1;
INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
// Shift size for shifting left the index array values.
// Increases possible data size with 16-bit index values at the cost
// of compactability.
// This requires data blocks to be aligned by DATA_GRANULARITY.
const INDEX_SHIFT = 2;
DATA_BLOCK_LENGTH = 1 << SHIFT_2;
// The alignment size of a data block. Also the granularity for compaction.
const DATA_GRANULARITY = 1 << INDEX_SHIFT;
DATA_MASK = DATA_BLOCK_LENGTH - 1;
// The BMP part of the index-2 table is fixed and linear and starts at offset 0.
// Length=2048=0x800=0x10000>>SHIFT_2.
const INDEX_2_OFFSET = 0;
INDEX_SHIFT = 2;
// The part of the index-2 table for U+D800..U+DBFF stores values for
// lead surrogate code _units_ not code _points_.
// Values for lead surrogate code _points_ are indexed with this portion of the table.
// Length=32=0x20=0x400>>SHIFT_2. (There are 1024=0x400 lead surrogates.)
const LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
const LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
DATA_GRANULARITY = 1 << INDEX_SHIFT;
// Count the lengths of both BMP pieces. 2080=0x820
const INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
INDEX_2_OFFSET = 0;
// The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
// Length 32=0x20 for lead bytes C0..DF, regardless of SHIFT_2.
const UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
const UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; // U+0800 is the first code point after 2-byte UTF-8
LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
// The index-1 table, only used for supplementary code points, at offset 2112=0x840.
// Variable length, for code points up to highStart, where the last single-value range starts.
// Maximum length 512=0x200=0x100000>>SHIFT_1.
// (For 0x100000 supplementary code points U+10000..U+10ffff.)
//
// The part of the index-2 table for supplementary code points starts
// after this index-1 table.
//
// Both the index-1 table and the following part of the index-2 table
// are omitted completely if there is only BMP data.
const INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
const MAX_INDEX_1_LENGTH = 0x100000 >> SHIFT_1;
LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
// The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
// Used with linear access for single bytes 0..0xbf for simple error handling.
// Length 64=0x40, not DATA_BLOCK_LENGTH.
const BAD_UTF8_DATA_OFFSET = 0x80;
INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
// The start of non-linear-ASCII data blocks, at offset 192=0xc0.
// !!!!
const DATA_START_OFFSET = 0xc0;
UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
// The null data block.
// Length 64=0x40 even if DATA_BLOCK_LENGTH is smaller,
// to work with 6-bit trail bytes from 2-byte UTF-8.
const DATA_NULL_OFFSET = DATA_START_OFFSET;
UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6;
// The start of allocated data blocks.
const NEW_DATA_START_OFFSET = DATA_NULL_OFFSET + 0x40;
INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
// The start of data blocks for U+0800 and above.
// Below, compaction uses a block length of 64 for 2-byte UTF-8.
// From here on, compaction uses DATA_BLOCK_LENGTH.
// Data values for 0x780 code points beyond ASCII.
const DATA_0800_OFFSET = NEW_DATA_START_OFFSET + 0x780;
MAX_INDEX_1_LENGTH = 0x100000 >> SHIFT_1;
// Start with allocation of 16k data entries. */
const INITIAL_DATA_LENGTH = 1 << 14;
BAD_UTF8_DATA_OFFSET = 0x80;
// Grow about 8x each time.
const MEDIUM_DATA_LENGTH = 1 << 17;
DATA_START_OFFSET = 0xc0;
// Maximum length of the runtime data array.
// Limited by 16-bit index values that are left-shifted by INDEX_SHIFT,
// and by uint16_t UTrie2Header.shiftedDataLength.
const MAX_DATA_LENGTH_RUNTIME = 0xffff << INDEX_SHIFT;
DATA_NULL_OFFSET = DATA_START_OFFSET;
const INDEX_1_LENGTH = 0x110000 >> SHIFT_1;
NEW_DATA_START_OFFSET = DATA_NULL_OFFSET + 0x40;
// Maximum length of the build-time data array.
// One entry per 0x110000 code points, plus the illegal-UTF-8 block and the null block,
// plus values for the 0x400 surrogate code units.
const MAX_DATA_LENGTH_BUILDTIME = 0x110000 + 0x40 + 0x40 + 0x400;
DATA_0800_OFFSET = NEW_DATA_START_OFFSET + 0x780;
// At build time, leave a gap in the index-2 table,
// at least as long as the maximum lengths of the 2-byte UTF-8 index-2 table
// and the supplementary index-1 table.
// Round up to INDEX_2_BLOCK_LENGTH for proper compacting.
const INDEX_GAP_OFFSET = INDEX_2_BMP_LENGTH;
const INDEX_GAP_LENGTH = ((UTF8_2B_INDEX_2_LENGTH + MAX_INDEX_1_LENGTH) + INDEX_2_MASK) & ~INDEX_2_MASK;
INITIAL_DATA_LENGTH = 1 << 14;
// Maximum length of the build-time index-2 array.
// Maximum number of Unicode code points (0x110000) shifted right by SHIFT_2,
// plus the part of the index-2 table for lead surrogate code points,
// plus the build-time index gap,
// plus the null index-2 block.)
const MAX_INDEX_2_LENGTH = (0x110000 >> SHIFT_2) + LSCP_INDEX_2_LENGTH + INDEX_GAP_LENGTH + INDEX_2_BLOCK_LENGTH;
MEDIUM_DATA_LENGTH = 1 << 17;
// The null index-2 block, following the gap in the index-2 table.
const INDEX_2_NULL_OFFSET = INDEX_GAP_OFFSET + INDEX_GAP_LENGTH;
MAX_DATA_LENGTH = 0xffff << INDEX_SHIFT;
// The start of allocated index-2 blocks.
const INDEX_2_START_OFFSET = INDEX_2_NULL_OFFSET + INDEX_2_BLOCK_LENGTH;
INDEX_1_LENGTH = 0x110000 >> SHIFT_1;
// Maximum length of the runtime index array.
// Limited by its own 16-bit index values, and by uint16_t UTrie2Header.indexLength.
// (The actual maximum length is lower,
// (0x110000>>SHIFT_2)+UTF8_2B_INDEX_2_LENGTH+MAX_INDEX_1_LENGTH.)
const MAX_INDEX_LENGTH = 0xffff;
MAX_DATA_LENGTH = 0x110000 + 0x40 + 0x40 + 0x400;
const equal_int = (a, s, t, length) => {
for (let i = 0; i < length; i++) {
if (a[s + i] !== a[t + i]) {
return false;
}
}
INDEX_GAP_OFFSET = INDEX_2_BMP_LENGTH;
return true;
};
INDEX_GAP_LENGTH = ((UTF8_2B_INDEX_2_LENGTH + MAX_INDEX_1_LENGTH) + INDEX_2_MASK) & ~INDEX_2_MASK;
MAX_INDEX_2_LENGTH = (0x110000 >> SHIFT_2) + LSCP_INDEX_2_LENGTH + INDEX_GAP_LENGTH + INDEX_2_BLOCK_LENGTH;
INDEX_2_NULL_OFFSET = INDEX_GAP_OFFSET + INDEX_GAP_LENGTH;
INDEX_2_START_OFFSET = INDEX_2_NULL_OFFSET + INDEX_2_BLOCK_LENGTH;
MAX_INDEX_LENGTH = 0xffff;
function UnicodeTrieBuilder(initialValue, errorValue) {
var i, j, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _ref, _s, _t;
this.initialValue = initialValue != null ? initialValue : 0;
this.errorValue = errorValue != null ? errorValue : 0;
class UnicodeTrieBuilder {
constructor(initialValue, errorValue) {
let i, j;
if (initialValue == null) {
initialValue = 0;
}
this.initialValue = initialValue;
if (errorValue == null) {
errorValue = 0;
}
this.errorValue = errorValue;
this.index1 = new Int32Array(INDEX_1_LENGTH);
this.index2 = new Int32Array(MAX_INDEX_2_LENGTH);
this.highStart = 0x110000;
this.data = new Uint32Array(INITIAL_DATA_LENGTH);
this.dataCapacity = INITIAL_DATA_LENGTH;
this.firstFreeBlock = 0;
this.isCompacted = false;
this.map = new Int32Array(MAX_DATA_LENGTH >> SHIFT_2);
for (i = _i = 0; _i < 0x80; i = _i += 1) {
// Multi-purpose per-data-block table.
//
// Before compacting:
//
// Per-data-block reference counters/free-block list.
// 0: unused
// >0: reference counter (number of index-2 entries pointing here)
// <0: next free data block in free-block list
//
// While compacting:
//
// Map of adjusted indexes, used in compactData() and compactIndex2().
// Maps from original indexes to new ones.
this.map = new Int32Array(MAX_DATA_LENGTH_BUILDTIME >> SHIFT_2);
for (i = 0; i < 0x80; i++) {
this.data[i] = this.initialValue;
}
for (i = _j = i; _j < 0xc0; i = _j += 1) {
for (i = i; i < 0xc0; i++) {
this.data[i] = this.errorValue;
}
for (i = _k = DATA_NULL_OFFSET; _k < NEW_DATA_START_OFFSET; i = _k += 1) {
for (i = DATA_NULL_OFFSET; i < NEW_DATA_START_OFFSET; i++) {
this.data[i] = this.initialValue;
}
this.dataNullOffset = DATA_NULL_OFFSET;
this.dataLength = NEW_DATA_START_OFFSET;
// set the index-2 indexes for the 2=0x80>>SHIFT_2 ASCII data blocks
i = 0;
for (j = _l = 0; _l < 128; j = _l += DATA_BLOCK_LENGTH) {
for (j = 0; j < 0x80; j += DATA_BLOCK_LENGTH) {
this.index2[i] = j;
this.map[i++] = 1;
}
for (j = _m = j; DATA_BLOCK_LENGTH > 0 ? _m < 0xc0 : _m > 0xc0; j = _m += DATA_BLOCK_LENGTH) {
// reference counts for the bad-UTF-8-data block
for (j = j; j < 0xc0; j += DATA_BLOCK_LENGTH) {
this.map[i++] = 0;
}
this.map[i++] = (0x110000 >> SHIFT_2) - (0x80 >> SHIFT_2) + 1 + LSCP_INDEX_2_LENGTH;
// Reference counts for the null data block: all blocks except for the ASCII blocks.
// Plus 1 so that we don't drop this block during compaction.
// Plus as many as needed for lead surrogate code points.
// i==newTrie->dataNullOffset
this.map[i++] = ((0x110000 >> SHIFT_2) - (0x80 >> SHIFT_2)) + 1 + LSCP_INDEX_2_LENGTH;
j += DATA_BLOCK_LENGTH;
for (j = _n = j; DATA_BLOCK_LENGTH > 0 ? _n < NEW_DATA_START_OFFSET : _n > NEW_DATA_START_OFFSET; j = _n += DATA_BLOCK_LENGTH) {
for (j = j; j < NEW_DATA_START_OFFSET; j += DATA_BLOCK_LENGTH) {
this.map[i++] = 0;
}
for (i = _o = _ref = 0x80 >> SHIFT_2; _o < INDEX_2_BMP_LENGTH; i = _o += 1) {
// set the remaining indexes in the BMP index-2 block
// to the null data block
for (i = 0x80 >> SHIFT_2; i < INDEX_2_BMP_LENGTH; i++) {
this.index2[i] = DATA_NULL_OFFSET;
}
for (i = _p = 0; _p < INDEX_GAP_LENGTH; i = _p += 1) {
// Fill the index gap with impossible values so that compaction
// does not overlap other index-2 blocks with the gap.
for (i = 0; i < INDEX_GAP_LENGTH; i++) {
this.index2[INDEX_GAP_OFFSET + i] = -1;
}
for (i = _q = 0; _q < INDEX_2_BLOCK_LENGTH; i = _q += 1) {
// set the indexes in the null index-2 block
for (i = 0; i < INDEX_2_BLOCK_LENGTH; i++) {
this.index2[INDEX_2_NULL_OFFSET + i] = DATA_NULL_OFFSET;
}
this.index2NullOffset = INDEX_2_NULL_OFFSET;
this.index2Length = INDEX_2_START_OFFSET;
// set the index-1 indexes for the linear index-2 block
j = 0;
for (i = _r = 0; _r < OMITTED_BMP_INDEX_1_LENGTH; i = _r += 1) {
for (i = 0; i < OMITTED_BMP_INDEX_1_LENGTH; i++) {
this.index1[i] = j;
j += INDEX_2_BLOCK_LENGTH;
}
for (i = _s = i; _s < INDEX_1_LENGTH; i = _s += 1) {
// set the remaining index-1 indexes to the null index-2 block
for (i = i; i < INDEX_1_LENGTH; i++) {
this.index1[i] = INDEX_2_NULL_OFFSET;
}
for (i = _t = 0x80; _t < 2048; i = _t += DATA_BLOCK_LENGTH) {
// Preallocate and reset data for U+0080..U+07ff,
// for 2-byte UTF-8 which will be compacted in 64-blocks
// even if DATA_BLOCK_LENGTH is smaller.
for (i = 0x80; i < 0x800; i += DATA_BLOCK_LENGTH) {
this.set(i, this.initialValue);
}
return;
}
UnicodeTrieBuilder.prototype.set = function(codePoint, value) {
var block;
if (codePoint < 0 || codePoint > 0x10ffff) {
set(codePoint, value) {
if ((codePoint < 0) || (codePoint > 0x10ffff)) {
throw new Error('Invalid code point');
}
if (this.isCompacted) {
throw new Error('Already compacted');
}
block = this._getDataBlock(codePoint, true);
const block = this._getDataBlock(codePoint, true);
this.data[block + (codePoint & DATA_MASK)] = value;
return this;
};
}
UnicodeTrieBuilder.prototype.setRange = function(start, end, value, overwrite) {
var block, i2, limit, nextStart, repeatBlock, rest, setRepeatBlock;
setRange(start, end, value, overwrite) {
let block, repeatBlock;
if (overwrite == null) {
overwrite = true;
}
if (start > 0x10ffff || end > 0x10ffff || start > end) {
if ((start > 0x10ffff) || (end > 0x10ffff) || (start > end)) {
throw new Error('Invalid code point');
}
if (this.isCompacted) {
throw new Error('Already compacted');
}
if (!overwrite && value === this.initialValue) {
return this;
if (!overwrite && (value === this.initialValue)) {
return this; // nothing to do
}
limit = end + 1;
let limit = end + 1;
if ((start & DATA_MASK) !== 0) {
// set partial block at [start..following block boundary
block = this._getDataBlock(start, true);
nextStart = (start + DATA_BLOCK_LENGTH) & ~DATA_MASK;
const nextStart = (start + DATA_BLOCK_LENGTH) & ~DATA_MASK;
if (nextStart <= limit) {

@@ -181,4 +310,10 @@ this._fillBlock(block, start & DATA_MASK, DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);

}
rest = limit & DATA_MASK;
// number of positions in the last, partial block
const rest = limit & DATA_MASK;
// round down limit to a block boundary
limit &= ~DATA_MASK;
// iterate over all-value blocks
if (value === this.initialValue) {

@@ -189,20 +324,46 @@ repeatBlock = this.dataNullOffset;

}
while (start < limit) {
setRepeatBlock = false;
if (value === this.initialValue && this._isInNullBlock(start, true)) {
start += DATA_BLOCK_LENGTH;
let setRepeatBlock = false;
if ((value === this.initialValue) && this._isInNullBlock(start, true)) {
start += DATA_BLOCK_LENGTH; // nothing to do
continue;
}
i2 = this._getIndex2Block(start, true);
// get index value
let i2 = this._getIndex2Block(start, true);
i2 += (start >> SHIFT_2) & INDEX_2_MASK;
block = this.index2[i2];
if (this._isWritableBlock(block)) {
if (overwrite && block >= DATA_0800_OFFSET) {
// already allocated
if (overwrite && (block >= DATA_0800_OFFSET)) {
// We overwrite all values, and it's not a
// protected (ASCII-linear or 2-byte UTF-8) block:
// replace with the repeatBlock.
setRepeatBlock = true;
} else {
// protected block: just write the values into this block
this._fillBlock(block, 0, DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
}
} else if (this.data[block] !== value && (overwrite || block === this.dataNullOffset)) {
} else if ((this.data[block] !== value) && (overwrite || (block === this.dataNullOffset))) {
// Set the repeatBlock instead of the null block or previous repeat block:
//
// If !isWritableBlock() then all entries in the block have the same value
// because it's the null block or a range block (the repeatBlock from a previous
// call to utrie2_setRange32()).
// No other blocks are used multiple times before compacting.
//
// The null block is the only non-writable block with the initialValue because
// of the repeatBlock initialization above. (If value==initialValue, then
// the repeatBlock will be the null data block.)
//
// We set our repeatBlock if the desired value differs from the block's value,
// and if we overwrite any data or if the data is all initial values
// (which is the same as the block being the null block, see above).
setRepeatBlock = true;
}
if (setRepeatBlock) {

@@ -212,2 +373,3 @@ if (repeatBlock >= 0) {

} else {
// create and set and fill the repeatBlock
repeatBlock = this._getDataBlock(start, true);

@@ -217,23 +379,29 @@ this._writeBlock(repeatBlock, value);

}
start += DATA_BLOCK_LENGTH;
}
if (rest > 0) {
// set partial block at [last block boundary..limit
block = this._getDataBlock(start, true);
this._fillBlock(block, 0, rest, value, this.initialValue, overwrite);
}
return this;
};
}
UnicodeTrieBuilder.prototype.get = function(c, fromLSCP) {
var block, i2;
get(c, fromLSCP) {
let i2;
if (fromLSCP == null) {
fromLSCP = true;
}
if (c < 0 || c > 0x10ffff) {
if ((c < 0) || (c > 0x10ffff)) {
return this.errorValue;
}
if (c >= this.highStart && (!(c >= 0xd800 && c < 0xdc00) || fromLSCP)) {
if ((c >= this.highStart) && (!((c >= 0xd800) && (c < 0xdc00)) || fromLSCP)) {
return this.data[this.dataLength - DATA_GRANULARITY];
}
if ((c >= 0xd800 && c < 0xdc00) && fromLSCP) {
if (((c >= 0xd800) && (c < 0xdc00)) && fromLSCP) {
i2 = (LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2)) + (c >> SHIFT_2);

@@ -243,36 +411,42 @@ } else {

}
block = this.index2[i2];
const block = this.index2[i2];
return this.data[block + (c & DATA_MASK)];
};
}
UnicodeTrieBuilder.prototype._isInNullBlock = function(c, forLSCP) {
var block, i2;
if ((c & 0xfffffc00) === 0xd800 && forLSCP) {
i2 = LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2) + (c >> SHIFT_2);
_isInNullBlock(c, forLSCP) {
let i2;
if (((c & 0xfffffc00) === 0xd800) && forLSCP) {
i2 = (LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2)) + (c >> SHIFT_2);
} else {
i2 = this.index1[c >> SHIFT_1] + ((c >> SHIFT_2) & INDEX_2_MASK);
}
block = this.index2[i2];
const block = this.index2[i2];
return block === this.dataNullOffset;
};
}
UnicodeTrieBuilder.prototype._allocIndex2Block = function() {
var newBlock, newTop;
newBlock = this.index2Length;
newTop = newBlock + INDEX_2_BLOCK_LENGTH;
_allocIndex2Block() {
const newBlock = this.index2Length;
const newTop = newBlock + INDEX_2_BLOCK_LENGTH;
if (newTop > this.index2.length) {
// Should never occur.
// Either MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
// or the code writes more values than should be possible.
throw new Error("Internal error in Trie2 creation.");
}
this.index2Length = newTop;
this.index2.set(this.index2.subarray(this.index2NullOffset, this.index2NullOffset + INDEX_2_BLOCK_LENGTH), newBlock);
return newBlock;
};
}
UnicodeTrieBuilder.prototype._getIndex2Block = function(c, forLSCP) {
var i1, i2;
if (c >= 0xd800 && c < 0xdc00 && forLSCP) {
_getIndex2Block(c, forLSCP) {
if ((c >= 0xd800) && (c < 0xdc00) && forLSCP) {
return LSCP_INDEX_2_OFFSET;
}
i1 = c >> SHIFT_1;
i2 = this.index1[i1];
const i1 = c >> SHIFT_1;
let i2 = this.index1[i1];
if (i2 === this.index2NullOffset) {

@@ -282,26 +456,35 @@ i2 = this._allocIndex2Block();

}
return i2;
};
}
UnicodeTrieBuilder.prototype._isWritableBlock = function(block) {
return block !== this.dataNullOffset && this.map[block >> SHIFT_2] === 1;
};
_isWritableBlock(block) {
return (block !== this.dataNullOffset) && (this.map[block >> SHIFT_2] === 1);
}
UnicodeTrieBuilder.prototype._allocDataBlock = function(copyBlock) {
var capacity, newBlock, newData, newTop;
_allocDataBlock(copyBlock) {
let newBlock;
if (this.firstFreeBlock !== 0) {
// get the first free block
newBlock = this.firstFreeBlock;
this.firstFreeBlock = -this.map[newBlock >> SHIFT_2];
} else {
// get a new block from the high end
newBlock = this.dataLength;
newTop = newBlock + DATA_BLOCK_LENGTH;
const newTop = newBlock + DATA_BLOCK_LENGTH;
if (newTop > this.dataCapacity) {
// out of memory in the data array
let capacity;
if (this.dataCapacity < MEDIUM_DATA_LENGTH) {
capacity = MEDIUM_DATA_LENGTH;
} else if (this.dataCapacity < MAX_DATA_LENGTH) {
capacity = MAX_DATA_LENGTH;
} else if (this.dataCapacity < MAX_DATA_LENGTH_BUILDTIME) {
capacity = MAX_DATA_LENGTH_BUILDTIME;
} else {
// Should never occur.
// Either MAX_DATA_LENGTH_BUILDTIME is incorrect,
// or the code writes more values than should be possible.
throw new Error("Internal error in Trie2 creation.");
}
newData = new Uint32Array(capacity);
const newData = new Uint32Array(capacity);
newData.set(this.data.subarray(0, this.dataLength));

@@ -311,45 +494,50 @@ this.data = newData;

}
this.dataLength = newTop;
}
this.data.set(this.data.subarray(copyBlock, copyBlock + DATA_BLOCK_LENGTH), newBlock);
this.map[newBlock >> SHIFT_2] = 0;
return newBlock;
};
}
UnicodeTrieBuilder.prototype._releaseDataBlock = function(block) {
_releaseDataBlock(block) {
// put this block at the front of the free-block chain
this.map[block >> SHIFT_2] = -this.firstFreeBlock;
return this.firstFreeBlock = block;
};
this.firstFreeBlock = block;
}
UnicodeTrieBuilder.prototype._setIndex2Entry = function(i2, block) {
var oldBlock;
++this.map[block >> SHIFT_2];
oldBlock = this.index2[i2];
_setIndex2Entry(i2, block) {
++this.map[block >> SHIFT_2]; // increment first, in case block == oldBlock!
const oldBlock = this.index2[i2];
if (--this.map[oldBlock >> SHIFT_2] === 0) {
this._releaseDataBlock(oldBlock);
}
return this.index2[i2] = block;
};
UnicodeTrieBuilder.prototype._getDataBlock = function(c, forLSCP) {
var i2, newBlock, oldBlock;
i2 = this._getIndex2Block(c, forLSCP);
this.index2[i2] = block;
}
_getDataBlock(c, forLSCP) {
let i2 = this._getIndex2Block(c, forLSCP);
i2 += (c >> SHIFT_2) & INDEX_2_MASK;
oldBlock = this.index2[i2];
const oldBlock = this.index2[i2];
if (this._isWritableBlock(oldBlock)) {
return oldBlock;
}
newBlock = this._allocDataBlock(oldBlock);
// allocate a new data block
const newBlock = this._allocDataBlock(oldBlock);
this._setIndex2Entry(i2, newBlock);
return newBlock;
};
}
UnicodeTrieBuilder.prototype._fillBlock = function(block, start, limit, value, initialValue, overwrite) {
var i, _i, _j, _ref, _ref1, _ref2, _ref3;
_fillBlock(block, start, limit, value, initialValue, overwrite) {
let i;
if (overwrite) {
for (i = _i = _ref = block + start, _ref1 = block + limit; _i < _ref1; i = _i += 1) {
for (i = block + start; i < block + limit; i++) {
this.data[i] = value;
}
} else {
for (i = _j = _ref2 = block + start, _ref3 = block + limit; _j < _ref3; i = _j += 1) {
for (i = block + start; i < block + limit; i++) {
if (this.data[i] === initialValue) {

@@ -360,18 +548,19 @@ this.data[i] = value;

}
};
}
UnicodeTrieBuilder.prototype._writeBlock = function(block, value) {
var limit;
limit = block + DATA_BLOCK_LENGTH;
_writeBlock(block, value) {
const limit = block + DATA_BLOCK_LENGTH;
while (block < limit) {
this.data[block++] = value;
}
};
}
UnicodeTrieBuilder.prototype._findHighStart = function(highValue) {
var block, c, data32, i1, i2, i2Block, index2NullOffset, initialValue, j, nullBlock, prev, prevBlock, prevI2Block, value;
data32 = this.data;
initialValue = this.initialValue;
index2NullOffset = this.index2NullOffset;
nullBlock = this.dataNullOffset;
_findHighStart(highValue) {
let prevBlock, prevI2Block;
const data32 = this.data;
const { initialValue } = this;
const { index2NullOffset } = this;
const nullBlock = this.dataNullOffset;
// set variables for previous range
if (highValue === initialValue) {

@@ -384,13 +573,19 @@ prevI2Block = index2NullOffset;

}
prev = 0x110000;
i1 = INDEX_1_LENGTH;
c = prev;
const prev = 0x110000;
// enumerate index-2 blocks
let i1 = INDEX_1_LENGTH;
let c = prev;
while (c > 0) {
i2Block = this.index1[--i1];
const i2Block = this.index1[--i1];
if (i2Block === prevI2Block) {
// the index-2 block is the same as the previous one, and filled with highValue
c -= CP_PER_INDEX_1_ENTRY;
continue;
}
prevI2Block = i2Block;
if (i2Block === index2NullOffset) {
// this is the null index-2 block
if (highValue !== initialValue) {

@@ -401,11 +596,15 @@ return c;

} else {
i2 = INDEX_2_BLOCK_LENGTH;
// enumerate data blocks for one index-2 block
let i2 = INDEX_2_BLOCK_LENGTH;
while (i2 > 0) {
block = this.index2[i2Block + --i2];
const block = this.index2[i2Block + --i2];
if (block === prevBlock) {
// the block is the same as the previous one, and filled with highValue
c -= DATA_BLOCK_LENGTH;
continue;
}
prevBlock = block;
if (block === nullBlock) {
// this is the null data block
if (highValue !== initialValue) {

@@ -416,5 +615,5 @@ return c;

} else {
j = DATA_BLOCK_LENGTH;
let j = DATA_BLOCK_LENGTH;
while (j > 0) {
value = data32[block + --j];
const value = data32[block + --j];
if (value !== highValue) {

@@ -429,19 +628,11 @@ return c;

}
// deliver last range
return 0;
};
}
equal_int = function(a, s, t, length) {
var i, _i;
for (i = _i = 0; _i < length; i = _i += 1) {
if (a[s + i] !== a[t + i]) {
return false;
}
}
return true;
};
UnicodeTrieBuilder.prototype._findSameDataBlock = function(dataLength, otherBlock, blockLength) {
var block;
_findSameDataBlock(dataLength, otherBlock, blockLength) {
// ensure that we do not even partially get past dataLength
dataLength -= blockLength;
block = 0;
let block = 0;
while (block <= dataLength) {

@@ -453,9 +644,10 @@ if (equal_int(this.data, block, otherBlock, blockLength)) {

}
return -1;
};
}
UnicodeTrieBuilder.prototype._findSameIndex2Block = function(index2Length, otherBlock) {
var block, _i;
_findSameIndex2Block(index2Length, otherBlock) {
// ensure that we do not even partially get past index2Length
index2Length -= INDEX_2_BLOCK_LENGTH;
for (block = _i = 0; _i <= index2Length; block = _i += 1) {
for (let block = 0; block <= index2Length; block++) {
if (equal_int(this.index2, block, otherBlock, INDEX_2_BLOCK_LENGTH)) {

@@ -465,10 +657,12 @@ return block;

}
return -1;
};
}
UnicodeTrieBuilder.prototype._compactData = function() {
var blockCount, blockLength, i, mapIndex, movedStart, newStart, overlap, start, _i, _j, _k, _l, _ref;
newStart = DATA_START_OFFSET;
start = 0;
i = 0;
_compactData() {
// do not compact linear-ASCII data
let newStart = DATA_START_OFFSET;
let start = 0;
let i = 0;
while (start < newStart) {

@@ -478,6 +672,13 @@ this.map[i++] = start;

}
blockLength = 64;
blockCount = blockLength >> SHIFT_2;
// Start with a block length of 64 for 2-byte UTF-8,
// then switch to DATA_BLOCK_LENGTH.
let blockLength = 64;
let blockCount = blockLength >> SHIFT_2;
start = newStart;
while (start < this.dataLength) {
// start: index of first entry of current block
// newStart: index where the current block is to be moved
// (right after current end of already-compacted data)
var mapIndex, movedStart;
if (start === DATA_0800_OFFSET) {

@@ -487,41 +688,66 @@ blockLength = DATA_BLOCK_LENGTH;

}
// skip blocks that are not used
if (this.map[start >> SHIFT_2] <= 0) {
// advance start to the next block
start += blockLength;
// leave newStart with the previous block!
continue;
}
// search for an identical block
if ((movedStart = this._findSameDataBlock(newStart, start, blockLength)) >= 0) {
// found an identical block, set the other block's index value for the current block
mapIndex = start >> SHIFT_2;
for (i = _i = blockCount; _i > 0; i = _i += -1) {
for (i = blockCount; i > 0; i--) {
this.map[mapIndex++] = movedStart;
movedStart += DATA_BLOCK_LENGTH;
}
// advance start to the next block
start += blockLength;
// leave newStart with the previous block!
continue;
}
overlap = blockLength - DATA_GRANULARITY;
while (overlap > 0 && !equal_int(this.data, newStart - overlap, start, overlap)) {
// see if the beginning of this block can be overlapped with the end of the previous block
// look for maximum overlap (modulo granularity) with the previous, adjacent block
let overlap = blockLength - DATA_GRANULARITY;
while ((overlap > 0) && !equal_int(this.data, (newStart - overlap), start, overlap)) {
overlap -= DATA_GRANULARITY;
}
if (overlap > 0 || newStart < start) {
if ((overlap > 0) || (newStart < start)) {
// some overlap, or just move the whole block
movedStart = newStart - overlap;
mapIndex = start >> SHIFT_2;
for (i = _j = blockCount; _j > 0; i = _j += -1) {
for (i = blockCount; i > 0; i--) {
this.map[mapIndex++] = movedStart;
movedStart += DATA_BLOCK_LENGTH;
}
// move the non-overlapping indexes to their new positions
start += overlap;
for (i = _k = _ref = blockLength - overlap; _k > 0; i = _k += -1) {
for (i = blockLength - overlap; i > 0; i--) {
this.data[newStart++] = this.data[start++];
}
} else {
} else { // no overlap && newStart==start
mapIndex = start >> SHIFT_2;
for (i = _l = blockCount; _l > 0; i = _l += -1) {
for (i = blockCount; i > 0; i--) {
this.map[mapIndex++] = start;
start += DATA_BLOCK_LENGTH;
}
newStart = start;
}
}
// now adjust the index-2 table
i = 0;
while (i < this.index2Length) {
// Gap indexes are invalid (-1). Skip over the gap.
if (i === INDEX_GAP_OFFSET) {

@@ -533,3 +759,6 @@ i += INDEX_GAP_LENGTH;

}
this.dataNullOffset = this.map[this.dataNullOffset >> SHIFT_2];
// ensure dataLength alignment
while ((newStart & (DATA_GRANULARITY - 1)) !== 0) {

@@ -539,9 +768,10 @@ this.data[newStart++] = this.initialValue;

this.dataLength = newStart;
};
}
UnicodeTrieBuilder.prototype._compactIndex2 = function() {
var i, movedStart, newStart, overlap, start, _i, _j, _ref;
newStart = INDEX_2_BMP_LENGTH;
start = 0;
i = 0;
_compactIndex2() {
// do not compact linear-BMP index-2 blocks
let newStart = INDEX_2_BMP_LENGTH;
let start = 0;
let i = 0;
while (start < newStart) {

@@ -551,21 +781,42 @@ this.map[i++] = start;

}
// Reduce the index table gap to what will be needed at runtime.
newStart += UTF8_2B_INDEX_2_LENGTH + ((this.highStart - 0x10000) >> SHIFT_1);
start = INDEX_2_NULL_OFFSET;
while (start < this.index2Length) {
// start: index of first entry of current block
// newStart: index where the current block is to be moved
// (right after current end of already-compacted data)
// search for an identical block
var movedStart;
if ((movedStart = this._findSameIndex2Block(newStart, start)) >= 0) {
// found an identical block, set the other block's index value for the current block
this.map[start >> SHIFT_1_2] = movedStart;
// advance start to the next block
start += INDEX_2_BLOCK_LENGTH;
// leave newStart with the previous block!
continue;
}
overlap = INDEX_2_BLOCK_LENGTH - 1;
while (overlap > 0 && !equal_int(this.index2, newStart - overlap, start, overlap)) {
// see if the beginning of this block can be overlapped with the end of the previous block
// look for maximum overlap with the previous, adjacent block
let overlap = INDEX_2_BLOCK_LENGTH - 1;
while ((overlap > 0) && !equal_int(this.index2, (newStart - overlap), start, overlap)) {
--overlap;
}
if (overlap > 0 || newStart < start) {
if ((overlap > 0) || (newStart < start)) {
// some overlap, or just move the whole block
this.map[start >> SHIFT_1_2] = newStart - overlap;
// move the non-overlapping indexes to their new positions
start += overlap;
for (i = _i = _ref = INDEX_2_BLOCK_LENGTH - overlap; _i > 0; i = _i += -1) {
for (i = INDEX_2_BLOCK_LENGTH - overlap; i > 0; i--) {
this.index2[newStart++] = this.index2[start++];
}
} else {
} else { // no overlap && newStart==start
this.map[start >> SHIFT_1_2] = start;

@@ -576,16 +827,27 @@ start += INDEX_2_BLOCK_LENGTH;

}
for (i = _j = 0; _j < INDEX_1_LENGTH; i = _j += 1) {
// now adjust the index-1 table
for (i = 0; i < INDEX_1_LENGTH; i++) {
this.index1[i] = this.map[this.index1[i] >> SHIFT_1_2];
}
this.index2NullOffset = this.map[this.index2NullOffset >> SHIFT_1_2];
// Ensure data table alignment:
// Needs to be granularity-aligned for 16-bit trie
// (so that dataMove will be down-shiftable),
// and 2-aligned for uint32_t data.
// Arbitrary value: 0x3fffc not possible for real data.
while ((newStart & ((DATA_GRANULARITY - 1) | 1)) !== 0) {
this.index2[newStart++] = 0x0000ffff << INDEX_SHIFT;
}
return this.index2Length = newStart;
};
UnicodeTrieBuilder.prototype._compact = function() {
var highStart, highValue, suppHighStart;
highValue = this.get(0x10ffff);
highStart = this._findHighStart(highValue);
this.index2Length = newStart;
}
_compact() {
// find highStart and round it up
let highValue = this.get(0x10ffff);
let highStart = this._findHighStart(highValue);
highStart = (highStart + (CP_PER_INDEX_1_ENTRY - 1)) & ~(CP_PER_INDEX_1_ENTRY - 1);

@@ -595,7 +857,12 @@ if (highStart === 0x110000) {

}
// Set trie->highStart only after utrie2_get32(trie, highStart).
// Otherwise utrie2_get32(trie, highStart) would try to read the highValue.
this.highStart = highStart;
if (this.highStart < 0x110000) {
suppHighStart = this.highStart <= 0x10000 ? 0x10000 : this.highStart;
// Blank out [highStart..10ffff] to release associated data blocks.
const suppHighStart = this.highStart <= 0x10000 ? 0x10000 : this.highStart;
this.setRange(suppHighStart, 0x10ffff, this.initialValue, true);
}
this._compactData();

@@ -605,2 +872,6 @@ if (this.highStart > 0x10000) {

}
// Store the highValue in the data array and round up the dataLength.
// Must be done after compactData() because that assumes that dataLength
// is a multiple of DATA_BLOCK_LENGTH.
this.data[this.dataLength++] = highValue;

@@ -610,10 +881,12 @@ while ((this.dataLength & (DATA_GRANULARITY - 1)) !== 0) {

}
return this.isCompacted = true;
};
UnicodeTrieBuilder.prototype.freeze = function() {
var allIndexesLength, data, dataMove, dest, destIdx, i, index1Length, index2Offset, indexLength, _i, _j, _k, _l, _m, _n, _ref, _ref1, _ref2, _ref3;
this.isCompacted = true;
}
freeze() {
let allIndexesLength, i;
if (!this.isCompacted) {
this._compact();
}
if (this.highStart <= 0x10000) {

@@ -624,60 +897,89 @@ allIndexesLength = INDEX_1_OFFSET;

}
dataMove = allIndexesLength;
if (allIndexesLength > MAX_INDEX_LENGTH || (dataMove + this.dataNullOffset) > 0xffff || (dataMove + DATA_0800_OFFSET) > 0xffff || (dataMove + this.dataLength) > MAX_DATA_LENGTH) {
const dataMove = allIndexesLength;
// are indexLength and dataLength within limits?
if ((allIndexesLength > MAX_INDEX_LENGTH) || // for unshifted indexLength
((dataMove + this.dataNullOffset) > 0xffff) || // for unshifted dataNullOffset
((dataMove + DATA_0800_OFFSET) > 0xffff) || // for unshifted 2-byte UTF-8 index-2 values
((dataMove + this.dataLength) > MAX_DATA_LENGTH_RUNTIME)) { // for shiftedDataLength
throw new Error("Trie data is too large.");
}
indexLength = allIndexesLength + this.dataLength;
data = new Int32Array(indexLength);
destIdx = 0;
for (i = _i = 0; _i < INDEX_2_BMP_LENGTH; i = _i += 1) {
data[destIdx++] = (this.index2[i] + dataMove) >> INDEX_SHIFT;
// calculate the sizes of, and allocate, the index and data arrays
const indexLength = allIndexesLength + this.dataLength;
const data = new Int32Array(indexLength);
// write the index-2 array values shifted right by INDEX_SHIFT, after adding dataMove
let destIdx = 0;
for (i = 0; i < INDEX_2_BMP_LENGTH; i++) {
data[destIdx++] = ((this.index2[i] + dataMove) >> INDEX_SHIFT);
}
for (i = _j = 0, _ref = 0xc2 - 0xc0; _j < _ref; i = _j += 1) {
data[destIdx++] = dataMove + BAD_UTF8_DATA_OFFSET;
// write UTF-8 2-byte index-2 values, not right-shifted
for (i = 0; i < 0xc2 - 0xc0; i++) { // C0..C1
data[destIdx++] = (dataMove + BAD_UTF8_DATA_OFFSET);
}
for (i = _k = i, _ref1 = 0xe0 - 0xc0; _k < _ref1; i = _k += 1) {
data[destIdx++] = dataMove + this.index2[i << (6 - SHIFT_2)];
for (i = i; i < 0xe0 - 0xc0; i++) { // C2..DF
data[destIdx++] = (dataMove + this.index2[i << (6 - SHIFT_2)]);
}
if (this.highStart > 0x10000) {
index1Length = (this.highStart - 0x10000) >> SHIFT_1;
index2Offset = INDEX_2_BMP_LENGTH + UTF8_2B_INDEX_2_LENGTH + index1Length;
for (i = _l = 0; _l < index1Length; i = _l += 1) {
data[destIdx++] = INDEX_2_OFFSET + this.index1[i + OMITTED_BMP_INDEX_1_LENGTH];
const index1Length = (this.highStart - 0x10000) >> SHIFT_1;
const index2Offset = INDEX_2_BMP_LENGTH + UTF8_2B_INDEX_2_LENGTH + index1Length;
// write 16-bit index-1 values for supplementary code points
for (i = 0; i < index1Length; i++) {
data[destIdx++] = (INDEX_2_OFFSET + this.index1[i + OMITTED_BMP_INDEX_1_LENGTH]);
}
for (i = _m = 0, _ref2 = this.index2Length - index2Offset; _m < _ref2; i = _m += 1) {
data[destIdx++] = (dataMove + this.index2[index2Offset + i]) >> INDEX_SHIFT;
// write the index-2 array values for supplementary code points,
// shifted right by INDEX_SHIFT, after adding dataMove
for (i = 0; i < this.index2Length - index2Offset; i++) {
data[destIdx++] = ((dataMove + this.index2[index2Offset + i]) >> INDEX_SHIFT);
}
}
for (i = _n = 0, _ref3 = this.dataLength; _n < _ref3; i = _n += 1) {
// write 16-bit data values
for (i = 0; i < this.dataLength; i++) {
data[destIdx++] = this.data[i];
}
dest = new UnicodeTrie({
data: data,
const dest = new UnicodeTrie({
data,
highStart: this.highStart,
errorValue: this.errorValue
});
return dest;
};
}
UnicodeTrieBuilder.prototype.toBuffer = function() {
var b, buf, compressed, data, i, trie, _i, _len;
trie = this.freeze();
data = new Uint8Array(trie.data.buffer);
compressed = pako.deflateRaw(data);
// Generates a Buffer containing the serialized and compressed trie.
// Trie data is compressed twice using the deflate algorithm to minimize file size.
// Format:
// uint32_t highStart;
// uint32_t errorValue;
// uint32_t uncompressedDataLength;
// uint8_t trieData[dataLength];
toBuffer() {
const trie = this.freeze();
const data = new Uint8Array(trie.data.buffer);
let compressed = pako.deflateRaw(data);
compressed = pako.deflateRaw(compressed);
buf = new Buffer(compressed.length + 12);
const buf = new Buffer(compressed.length + 12);
buf.writeUInt32BE(trie.highStart, 0);
buf.writeUInt32BE(trie.errorValue, 4);
buf.writeUInt32BE(data.length, 8);
for (i = _i = 0, _len = compressed.length; _i < _len; i = ++_i) {
b = compressed[i];
for (let i = 0; i < compressed.length; i++) {
const b = compressed[i];
buf[i + 12] = b;
}
return buf;
};
}
}
return UnicodeTrieBuilder;
})();
module.exports = UnicodeTrieBuilder;
module.exports = UnicodeTrieBuilder;

@@ -1,45 +0,72 @@

// Generated by CoffeeScript 1.7.1
var UnicodeTrie, inflate;
const inflate = require('tiny-inflate');
inflate = require('tiny-inflate');
// Shift size for getting the index-1 table offset.
const SHIFT_1 = 6 + 5;
UnicodeTrie = (function() {
var DATA_BLOCK_LENGTH, DATA_GRANULARITY, DATA_MASK, INDEX_1_OFFSET, INDEX_2_BLOCK_LENGTH, INDEX_2_BMP_LENGTH, INDEX_2_MASK, INDEX_SHIFT, LSCP_INDEX_2_LENGTH, LSCP_INDEX_2_OFFSET, OMITTED_BMP_INDEX_1_LENGTH, SHIFT_1, SHIFT_1_2, SHIFT_2, UTF8_2B_INDEX_2_LENGTH, UTF8_2B_INDEX_2_OFFSET;
// Shift size for getting the index-2 table offset.
const SHIFT_2 = 5;
SHIFT_1 = 6 + 5;
// Difference between the two shift sizes,
// for getting an index-1 offset from an index-2 offset. 6=11-5
const SHIFT_1_2 = SHIFT_1 - SHIFT_2;
SHIFT_2 = 5;
// Number of index-1 entries for the BMP. 32=0x20
// This part of the index-1 table is omitted from the serialized form.
const OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
SHIFT_1_2 = SHIFT_1 - SHIFT_2;
// Number of entries in an index-2 block. 64=0x40
const INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
// Mask for getting the lower bits for the in-index-2-block offset. */
const INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
// Shift size for shifting left the index array values.
// Increases possible data size with 16-bit index values at the cost
// of compactability.
// This requires data blocks to be aligned by DATA_GRANULARITY.
const INDEX_SHIFT = 2;
INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
// Number of entries in a data block. 32=0x20
const DATA_BLOCK_LENGTH = 1 << SHIFT_2;
INDEX_SHIFT = 2;
// Mask for getting the lower bits for the in-data-block offset.
const DATA_MASK = DATA_BLOCK_LENGTH - 1;
DATA_BLOCK_LENGTH = 1 << SHIFT_2;
// The part of the index-2 table for U+D800..U+DBFF stores values for
// lead surrogate code _units_ not code _points_.
// Values for lead surrogate code _points_ are indexed with this portion of the table.
// Length=32=0x20=0x400>>SHIFT_2. (There are 1024=0x400 lead surrogates.)
const LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
const LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
DATA_MASK = DATA_BLOCK_LENGTH - 1;
// Count the lengths of both BMP pieces. 2080=0x820
const INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
// The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
// Length 32=0x20 for lead bytes C0..DF, regardless of SHIFT_2.
const UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
const UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; // U+0800 is the first code point after 2-byte UTF-8
LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
// The index-1 table, only used for supplementary code points, at offset 2112=0x840.
// Variable length, for code points up to highStart, where the last single-value range starts.
// Maximum length 512=0x200=0x100000>>SHIFT_1.
// (For 0x100000 supplementary code points U+10000..U+10ffff.)
//
// The part of the index-2 table for supplementary code points starts
// after this index-1 table.
//
// Both the index-1 table and the following part of the index-2 table
// are omitted completely if there is only BMP data.
const INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
// The alignment size of a data block. Also the granularity for compaction.
const DATA_GRANULARITY = 1 << INDEX_SHIFT;
UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
class UnicodeTrie {
constructor(data) {
const isBuffer = (typeof data.readUInt32BE === 'function') && (typeof data.slice === 'function');
UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6;
INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
DATA_GRANULARITY = 1 << INDEX_SHIFT;
function UnicodeTrie(data) {
var isBuffer, uncompressedLength, view;
isBuffer = typeof data.readUInt32BE === 'function' && typeof data.slice === 'function';
if (isBuffer || data instanceof Uint8Array) {
// read binary format
let uncompressedLength;
if (isBuffer) {

@@ -51,3 +78,3 @@ this.highStart = data.readUInt32BE(0);

} else {
view = new DataView(data.buffer);
const view = new DataView(data.buffer);
this.highStart = view.getUint32(0);

@@ -58,24 +85,39 @@ this.errorValue = view.getUint32(4);

}
// double inflate the actual trie data
data = inflate(data, new Uint8Array(uncompressedLength));
data = inflate(data, new Uint8Array(uncompressedLength));
this.data = new Uint32Array(data.buffer);
} else {
this.data = data.data, this.highStart = data.highStart, this.errorValue = data.errorValue;
// pre-parsed data
({ data: this.data, highStart: this.highStart, errorValue: this.errorValue } = data);
}
}
UnicodeTrie.prototype.get = function(codePoint) {
var index;
if (codePoint < 0 || codePoint > 0x10ffff) {
get(codePoint) {
let index;
if ((codePoint < 0) || (codePoint > 0x10ffff)) {
return this.errorValue;
}
if (codePoint < 0xd800 || (codePoint > 0xdbff && codePoint <= 0xffff)) {
if ((codePoint < 0xd800) || ((codePoint > 0xdbff) && (codePoint <= 0xffff))) {
// Ordinary BMP code point, excluding leading surrogates.
// BMP uses a single level lookup. BMP index starts at offset 0 in the index.
// data is stored in the index array itself.
index = (this.data[codePoint >> SHIFT_2] << INDEX_SHIFT) + (codePoint & DATA_MASK);
return this.data[index];
}
if (codePoint <= 0xffff) {
// Lead Surrogate Code Point. A Separate index section is stored for
// lead surrogate code units and code points.
// The main index has the code unit data.
// For this function, we need the code point data.
index = (this.data[LSCP_INDEX_2_OFFSET + ((codePoint - 0xd800) >> SHIFT_2)] << INDEX_SHIFT) + (codePoint & DATA_MASK);
return this.data[index];
}
if (codePoint < this.highStart) {
// Supplemental code point, use two-level lookup.
index = this.data[(INDEX_1_OFFSET - OMITTED_BMP_INDEX_1_LENGTH) + (codePoint >> SHIFT_1)];

@@ -86,9 +128,7 @@ index = this.data[index + ((codePoint >> SHIFT_2) & INDEX_2_MASK)];

}
return this.data[this.data.length - DATA_GRANULARITY];
};
}
}
return UnicodeTrie;
})();
module.exports = UnicodeTrie;
module.exports = UnicodeTrie;
{
"name": "unicode-trie",
"version": "0.3.1",
"version": "1.0.0",
"description": "Unicode Trie data structure for fast character metadata lookup, ported from ICU",
"devDependencies": {
"mocha": "^1.20.1",
"coffee-script": "^1.7.1",
"coffee-coverage": "^0.4.2"
"mocha": "^6.1.4",
"nyc": "^14.1.1"
},
"scripts": {
"prepublish": "make build",
"postpublish": "make clean",
"test": "mocha"
"test": "mocha",
"coverage": "nyc mocha"
},

@@ -15,0 +13,0 @@ "repository": {

@@ -32,24 +32,24 @@ # unicode-trie

```coffeescript
UnicodeTrieBuilder = require 'unicode-trie/builder'
```js
const UnicodeTrieBuilder = require('unicode-trie/builder');
# create a trie
t = new UnicodeTrieBuilder
// create a trie
let t = new UnicodeTrieBuilder();
# optional parameters for default value, and error value
# if not provided, both are set to 0
t = new UnicodeTrieBuilder 10, 999
// optional parameters for default value, and error value
// if not provided, both are set to 0
t = new UnicodeTrieBuilder(10, 999);
# set individual values and ranges
t.set 0x4567, 99
t.setRange 0x40, 0xe7, 0x1234
// set individual values and ranges
t.set(0x4567, 99);
t.setRange(0x40, 0xe7, 0x1234);
# you can lookup a value if you like
t.get 0x4567 # => 99
// you can lookup a value if you like
t.get(0x4567); // => 99
# get a compiled trie (returns a UnicodeTrie object)
trie = t.freeze()
// get a compiled trie (returns a UnicodeTrie object)
const trie = t.freeze();
# write compressed trie to a binary file
fs.writeFile 'data.trie', t.toBuffer()
// write compressed trie to a binary file
fs.writeFile('data.trie', t.toBuffer());
```

@@ -63,12 +63,12 @@

```coffeescript
UnicodeTrie = require 'unicode-trie'
fs = require 'fs'
```js
const UnicodeTrie = require('unicode-trie');
const fs = require('fs');
# load serialized trie from binary file
data = fs.readFileSync 'data.trie'
trie = new UnicodeTrie data
// load serialized trie from binary file
const data = fs.readFileSync('data.trie');
const trie = new UnicodeTrie(data);
# lookup a value
trie.get 0x4567 # => 99
// lookup a value
trie.get(0x4567); // => 99
```

@@ -75,0 +75,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc