2
0
Lutz Roeder 4 жил өмнө
parent
commit
3b7d99dc7b

+ 2 - 2
source/app.js

@@ -150,8 +150,8 @@ class Application {
         require("crypto").randomFillSync(buffer);
         buffer[6] = buffer[6] & 0x0f | 0x40;
         buffer[8] = buffer[8] & 0x3f | 0x80;
-        const text = Array.from(buffer).map((value) => value < 0x10 ? '0' + value.toString(16) : value.toString(16)).join('');
-        return text.slice(0, 8) + '-' + text.slice(8, 12) + '-' + text.slice(12, 16) + '-' + text.slice(16, 20) + '-' + text.slice(20, 32);
+        const code = Array.from(buffer).map((value) => value < 0x10 ? '0' + value.toString(16) : value.toString(16)).join('');
+        return code.slice(0, 8) + '-' + code.slice(8, 12) + '-' + code.slice(12, 16) + '-' + code.slice(16, 20) + '-' + code.slice(20, 32);
     }
 
     _openFileDialog() {

+ 3 - 3
source/barracuda.js

@@ -543,14 +543,14 @@ barracuda.BinaryReader = class {
     }
 
     string() {
-        let text = '';
+        let content = '';
         const size = this.int32();
         let position = this._position;
         this.skip(size);
         for (let i = 0; i < size; i++) {
-            text += String.fromCharCode(this._buffer[position++]);
+            content += String.fromCharCode(this._buffer[position++]);
         }
-        return text;
+        return content;
     }
 
     strings() {

+ 0 - 222
source/base.js

@@ -549,226 +549,6 @@ DataView.prototype.getBits = DataView.prototype.getBits || function(offset, bits
     return value;
 };
 
-base.TextDecoder = class {
-
-    static open(data) {
-        if (typeof data === 'string') {
-            return new base.TextDecoder.String(data);
-        }
-        const buffer = data instanceof Uint8Array ? data : data.peek();
-        const length = buffer.length;
-        if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
-            return new base.TextDecoder.Utf8(buffer, 3);
-        }
-        if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
-            return new base.TextDecoder.Utf16LE(buffer, 2);
-        }
-        if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
-            return new base.TextDecoder.Utf16BE(buffer, 2);
-        }
-        if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) {
-            throw new Error("Unsupported UTF-32 big-endian encoding.");
-        }
-        if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) {
-            throw new Error("Unsupported UTF-32 little-endian encoding.");
-        }
-        if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) {
-            throw new Error("Unsupported UTF-7 encoding.");
-        }
-        if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) {
-            throw new Error("Unsupported UTF-7 encoding.");
-        }
-        if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) {
-            throw new Error("Unsupported GB-18030 encoding.");
-        }
-        if (length > 4 && (length % 2) == 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) {
-            const lo = new Uint32Array(256);
-            const hi = new Uint32Array(256);
-            for (let i = 0; i < length; i += 2) {
-                lo[buffer[i]]++;
-                hi[buffer[i + 1]]++;
-            }
-            if (lo[0x00] === 0 && (hi[0x00] / (length >> 1)) > 0.5) {
-                return new base.TextDecoder.Utf16LE(buffer, 0);
-            }
-            if (hi[0x00] === 0 && (lo[0x00] / (length >> 1)) > 0.5) {
-                return new base.TextDecoder.Utf16BE(buffer, 0);
-            }
-        }
-        return new base.TextDecoder.Utf8(buffer, 0);
-    }
-};
-
-base.TextDecoder.String = class {
-
-    constructor(buffer) {
-        this.buffer = buffer;
-        this.position = 0;
-        this.length = buffer.length;
-    }
-
-    decode() {
-        if (this.position < this.length) {
-            return this.buffer[this.position++];
-        }
-        return undefined;
-    }
-};
-
-base.TextDecoder.Utf8 = class {
-
-    constructor(buffer, position) {
-        this.position = position || 0;
-        this.buffer = buffer;
-    }
-
-    decode() {
-        const c = this.buffer[this.position];
-        if (c === undefined) {
-            return c;
-        }
-        this.position++;
-        if (c < 0x80) {
-            return String.fromCodePoint(c);
-        }
-        if (c >= 0xC2 && c <= 0xDF) {
-            if (this.buffer[this.position] !== undefined) {
-                const c2 = this.buffer[this.position];
-                this.position++;
-                return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F));
-            }
-        }
-        if (c >= 0xE0 && c <= 0xEF) {
-            if (this.buffer[this.position + 1] !== undefined) {
-                const c2 = this.buffer[this.position];
-                if ((c !== 0xE0 || c2 >= 0xA0) && (c !== 0xED || c2 <= 0x9f)) {
-                    const c3 = this.buffer[this.position + 1];
-                    if (c3 >= 0x80 && c3 < 0xFB) {
-                        this.position += 2;
-                        return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
-                    }
-                }
-            }
-        }
-        if (c >= 0xF0 && c <= 0xF4) {
-            if (this.buffer[this.position + 2] !== undefined) {
-                const c2 = this.buffer[this.position];
-                if ((c !== 0xF0 || c2 >= 0x90) && (c !== 0xF4 || c2 <= 0x8f)) {
-                    const c3 = this.buffer[this.position + 1];
-                    if (c3 >= 0x80 && c3 < 0xFB) {
-                        const c4 = this.buffer[this.position + 2];
-                        this.position += 3;
-                        return String.fromCodePoint(((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
-                    }
-                }
-            }
-        }
-        return String.fromCharCode(0xfffd);
-    }
-};
-
-base.TextDecoder.Utf16LE = class {
-
-    constructor(buffer, position) {
-        this.buffer = buffer;
-        this.position = position || 0;
-        this.length = buffer.length;
-    }
-
-    decode() {
-        if (this.position + 1 < this.length) {
-            const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8);
-            if (c < 0xD800 || c >= 0xDFFF) {
-                return String.fromCharCode(c);
-            }
-            if (c >= 0xD800 && c < 0xDBFF) {
-                if (this._position + 1 < this._length) {
-                    const c2 = this._buffer[this._position++] | (this._buffer[this._position++] << 8);
-                    if (c >= 0xDC00 || c < 0xDFFF) {
-                        return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
-                    }
-                }
-            }
-            return String.fromCharCode(0xfffd);
-        }
-        return undefined;
-    }
-};
-
-base.TextDecoder.Utf16BE = class {
-
-    constructor(buffer, position) {
-        this.buffer = buffer;
-        this.position = position || 0;
-        this.length = buffer.length;
-    }
-
-    decode() {
-        if (this.position + 1 < this.length) {
-            const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++];
-            if (c < 0xD800 || c >= 0xDFFF) {
-                return String.fromCharCode(c);
-            }
-            if (c >= 0xD800 && c < 0xDBFF) {
-                if (this._position + 1 < this._length) {
-                    const c2 = (this._buffer[this._position++] << 8) | this._buffer[this._position++];
-                    if (c >= 0xDC00 || c < 0xDFFF) {
-                        return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
-                    }
-                }
-            }
-            return String.fromCharCode(0xfffd);
-        }
-        return undefined;
-    }
-};
-
-base.TextReader = class {
-
-    constructor(data, length) {
-        this._decoder = base.TextDecoder.open(data);
-        this._position = 0;
-        this._length = length || Number.MAX_SAFE_INTEGER;
-    }
-
-    static open(data, length) {
-        return new base.TextReader(data, length);
-    }
-
-    read() {
-        if (this._position >= this._length) {
-            return undefined;
-        }
-        let line = '';
-        let buffer = null;
-        for (;;) {
-            const c = this._decoder.decode();
-            if (c === undefined) {
-                this._length = this._position;
-                break;
-            }
-            this._position++;
-            if (this._position > this._length) {
-                break;
-            }
-            if (c === '\n') {
-                break;
-            }
-            line += c;
-            if (line.length >= 32) {
-                buffer = buffer || [];
-                buffer.push(line);
-                line = '';
-            }
-        }
-        if (buffer) {
-            buffer.push(line);
-            return buffer.join('');
-        }
-        return line;
-    }
-};
-
 if (typeof window !== 'undefined' && typeof window.Long != 'undefined') {
     window.long = { Long: window.Long };
     window.Int64 = base.Int64;
@@ -778,6 +558,4 @@ if (typeof window !== 'undefined' && typeof window.Long != 'undefined') {
 if (typeof module !== 'undefined' && typeof module.exports === 'object') {
     module.exports.Int64 = base.Int64;
     module.exports.Uint64 = base.Uint64;
-    module.exports.TextDecoder = base.TextDecoder;
-    module.exports.TextReader = base.TextReader;
 }

+ 3 - 3
source/cntk.js

@@ -1312,13 +1312,13 @@ cntk_v1.Reader = class {
     }
 
     string() {
-        let text = '';
+        let content = '';
         let c = this.uint16();
         while (c != 0) {
-            text += String.fromCharCode(c);
+            content += String.fromCharCode(c);
             c = this.uint16();
         }
-        return text;
+        return content;
     }
 
     strings(count) {

+ 11 - 11
source/darknet.js

@@ -1,7 +1,7 @@
 /* jshint esversion: 6 */
 
 var darknet = darknet || {};
-var base = base || require('./base');
+var text = text || require('./text');
 
 darknet.ModelFactory = class {
 
@@ -16,17 +16,17 @@ darknet.ModelFactory = class {
                 break;
             default:
                 try {
-                    const reader = base.TextReader.open(context.stream.peek(), 65536);
+                    const reader = text.Reader.open(context.stream.peek(), 65536);
                     for (;;) {
                         const line = reader.read();
                         if (line === undefined) {
                             break;
                         }
-                        const text = line.trim();
-                        if (text.length === 0 || text.startsWith('#')) {
+                        const content = line.trim();
+                        if (content.length === 0 || content.startsWith('#')) {
                             continue;
                         }
-                        if (text.startsWith('[') && text.endsWith(']')) {
+                        if (content.startsWith('[') && content.endsWith(']')) {
                             return 'darknet.model';
                         }
                         return undefined;
@@ -94,15 +94,15 @@ darknet.Graph = class {
         // read_cfg
         const sections = [];
         let section = null;
-        const reader = base.TextReader.open(cfg);
+        const reader = text.Reader.open(cfg);
         let lineNumber = 0;
         for (;;) {
             lineNumber++;
-            const text = reader.read();
-            if (text === undefined) {
+            const content = reader.read();
+            if (content === undefined) {
                 break;
             }
-            const line = text.replace(/\s/g, '');
+            const line = content.replace(/\s/g, '');
             if (line.length > 0) {
                 switch (line[0]) {
                     case '#':
@@ -120,11 +120,11 @@ darknet.Graph = class {
                     }
                     default: {
                         if (!section || line[0] < 0x20 || line[0] > 0x7E) {
-                            throw new darknet.Error("Invalid cfg '" + text.replace(/[^\x20-\x7E]+/g, '?').trim() + "' at line " + lineNumber.toString() + ".");
+                            throw new darknet.Error("Invalid cfg '" + content.replace(/[^\x20-\x7E]+/g, '?').trim() + "' at line " + lineNumber.toString() + ".");
                         }
                         const index = line.indexOf('=');
                         if (index < 0) {
-                            throw new darknet.Error("Invalid cfg '" + text.replace(/[^\x20-\x7E]+/g, '?').trim() + "' at line " + lineNumber.toString() + ".");
+                            throw new darknet.Error("Invalid cfg '" + content.replace(/[^\x20-\x7E]+/g, '?').trim() + "' at line " + lineNumber.toString() + ".");
                         }
                         const key = line.substring(0, index);
                         const value = line.substring(index + 1);

+ 3 - 3
source/gzip.js

@@ -35,15 +35,15 @@ gzip.Entry = class {
             throw new gzip.Error('Invalid gzip signature.');
         }
         const string = () => {
-            let text = '';
+            let content = '';
             while (stream.position < stream.length) {
                 const value = stream.byte();
                 if (value === 0x00) {
                     break;
                 }
-                text += String.fromCharCode(value);
+                content += String.fromCharCode(value);
             }
-            return text;
+            return content;
         };
         const reader = new gzip.BinaryReader(stream.read(8));
         const compressionMethod = reader.byte();

+ 4 - 4
source/hdf5.js

@@ -377,20 +377,20 @@ hdf5.Reader = class {
     }
 
     static decode(data, encoding) {
-        let text = '';
+        let content = '';
         if (encoding == 'utf-8') {
             if (!hdf5.Reader._utf8Decoder) {
                 hdf5.Reader._utf8Decoder = new TextDecoder('utf-8');
             }
-            text = hdf5.Reader._utf8Decoder.decode(data);
+            content = hdf5.Reader._utf8Decoder.decode(data);
         }
         else {
             if (!hdf5.Reader._asciiDecoder) {
                 hdf5.Reader._asciiDecoder = new TextDecoder('ascii');
             }
-            text = hdf5.Reader._asciiDecoder.decode(data);
+            content = hdf5.Reader._asciiDecoder.decode(data);
         }
-        return text.replace(/\0/g, '');
+        return content.replace(/\0/g, '');
     }
 
     offset() {

+ 1 - 0
source/index.html

@@ -15,6 +15,7 @@
 <link rel="fluid-icon" type="image/png" href="icon.png">
 <script type="text/javascript" src="dagre.js"></script>
 <script type="text/javascript" src="base.js"></script>
+<script type="text/javascript" src="text.js"></script>
 <script type="text/javascript" src="json.js"></script>
 <script type="text/javascript" src="python.js"></script>
 <script type="text/javascript" src="protobuf.js"></script>

+ 3 - 3
source/json.js

@@ -1,12 +1,12 @@
 /* jshint esversion: 6 */
 
 var json = json || {};
-var base = base || require('./base');
+var text = text || require('./text');
 
 json.TextReader = class {
 
     static open(data) {
-        const decoder = base.TextDecoder.open(data);
+        const decoder = text.Decoder.open(data);
         let state = 'start';
         for (let i = 0; i < 0x100; i++) {
             const c = decoder.decode();
@@ -64,7 +64,7 @@ json.TextReader = class {
     }
 
     read() {
-        const decoder = base.TextDecoder.open(this._data);
+        const decoder = text.Decoder.open(this._data);
         const stack = [];
         this._decoder = decoder;
         this._position = 0;

+ 4 - 4
source/mxnet.js

@@ -64,8 +64,8 @@ mxnet.ModelFactory = class {
                         const decoder = new TextDecoder('utf-8');
                         if (stream) {
                             const buffer = stream.peek();
-                            const text = decoder.decode(buffer);
-                            const json = JSON.parse(text);
+                            const content = decoder.decode(buffer);
+                            const json = JSON.parse(content);
                             if (json.Model) {
                                 const modelFormat = json.Model['Model-Format'];
                                 if (modelFormat && modelFormat != 'MXNet-Symbolic') {
@@ -139,8 +139,8 @@ mxnet.ModelFactory = class {
                             if (json.Model && json.Model.Signature) {
                                 return context.request(json.Model.Signature).then((stream) => {
                                     const buffer = stream.peek();
-                                    const text = decoder.decode(buffer);
-                                    manifest.signature = JSON.parse(text);
+                                    const content = decoder.decode(buffer);
+                                    manifest.signature = JSON.parse(content);
                                     return manifest;
                                 }).catch (() => {
                                     return manifest;

+ 8 - 8
source/ncnn.js

@@ -1,7 +1,7 @@
 /* jshint esversion: 6 */
 
 var ncnn = ncnn || {};
-var base = base || require('./base');
+var text = text || require('./text');
 
 // https://github.com/Tencent/ncnn/wiki/param-and-model-file-structure
 // https://github.com/Tencent/ncnn/wiki/operation-param-weight-table
@@ -12,7 +12,7 @@ ncnn.ModelFactory = class {
     match(context) {
         const identifier = context.identifier.toLowerCase();
         if (identifier.endsWith('.param') || identifier.endsWith('.cfg.ncnn')) {
-            const reader = base.TextReader.open(context.stream.peek(), 2048);
+            const reader = text.Reader.open(context.stream, 2048);
             const signature = reader.read();
             if (signature !== undefined) {
                 if (signature.trim() === '7767517') {
@@ -88,18 +88,18 @@ ncnn.ModelFactory = class {
                     });
                 }
                 case 'ncnn.weights': {
-                    let text = null;
+                    let content = null;
                     if (identifier.endsWith('bin')) {
-                        text = context.identifier.substring(0, context.identifier.length - 4) + '.param';
+                        content = context.identifier.substring(0, context.identifier.length - 4) + '.param';
                     }
                     else if (identifier.endsWith('.weights.ncnn')) {
-                        text = context.identifier.substring(0, context.identifier.length - 13) + '.cfg.ncnn';
+                        content = context.identifier.substring(0, context.identifier.length - 13) + '.cfg.ncnn';
                     }
-                    return context.request(text, null).then((stream) => {
+                    return context.request(content, null).then((stream) => {
                         const buffer = stream.peek();
                         return openText(buffer, context.stream.peek());
                     }).catch(() => {
-                        return context.request(text + '.bin', null).then((stream) => {
+                        return context.request(content + '.bin', null).then((stream) => {
                             const buffer = stream.peek();
                             return openBinary(buffer, context.stream.peek());
                         });
@@ -797,7 +797,7 @@ ncnn.Utility = class {
 ncnn.TextParamReader = class {
 
     constructor(buffer) {
-        const reader = base.TextReader.open(buffer);
+        const reader = text.Reader.open(buffer);
         const lines = [];
         for (;;) {
             const line = reader.read();

+ 2 - 2
source/openvino.js

@@ -1,7 +1,7 @@
 /* jshint esversion: 6 */
 
 var openvino = openvino || {};
-var base = base || require('./base');
+var text = text || require('./text');
 
 openvino.ModelFactory = class {
 
@@ -10,7 +10,7 @@ openvino.ModelFactory = class {
         const extension = identifier.split('.').pop().toLowerCase();
         if (extension === 'xml') {
             try {
-                const reader = base.TextReader.open(context.stream.peek(), 2048);
+                const reader = text.Reader.open(context.stream.peek(), 2048);
                 for (;;) {
                     const line = reader.read();
                     if (line === undefined) {

+ 10 - 9
source/protobuf.js

@@ -3,6 +3,7 @@
 
 var protobuf = protobuf || {};
 var base = base || require('./base');
+var text = text || require('./text');
 
 protobuf.get = (name) => {
     protobuf._map = protobuf._map || new Map();
@@ -605,7 +606,7 @@ protobuf.TextReader = class {
 
     static open(data) {
         const buffer = data instanceof Uint8Array ? data : data.peek();
-        const decoder = base.TextDecoder.open(buffer);
+        const decoder = text.Decoder.open(buffer);
         let first = true;
         for (let i = 0; i < 0x100; i++) {
             const c = decoder.decode();
@@ -637,7 +638,7 @@ protobuf.TextReader = class {
     }
 
     constructor(buffer) {
-        this._decoder = base.TextDecoder.open(buffer);
+        this._decoder = text.Decoder.open(buffer);
         this.reset();
     }
 
@@ -988,12 +989,12 @@ protobuf.TextReader = class {
         const end = this._position;
         const position = this._decoder.position;
         this._decoder.position = start;
-        let text = '';
+        let content = '';
         while (this._decoder.position < end) {
-            text += this._decoder.decode();
+            content += this._decoder.decode();
         }
         this._decoder.position = position;
-        return text;
+        return content;
     }
 
     skip() {
@@ -1137,7 +1138,7 @@ protobuf.TextReader = class {
             case '"':
             case "'": {
                 const quote = c;
-                let text = c;
+                let content = c;
                 for (;;) {
                     c = this._decoder.decode();
                     if (c === undefined || c === '\n') {
@@ -1207,17 +1208,17 @@ protobuf.TextReader = class {
                                 break;
                             }
                         }
-                        text += c;
+                        content += c;
                         continue;
                     }
                     else {
-                        text += c;
+                        content += c;
                         if (c === quote) {
                             break;
                         }
                     }
                 }
-                this._token = text;
+                this._token = content;
                 return;
             }
             case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':

+ 2 - 2
source/pytorch.js

@@ -2271,8 +2271,8 @@ pytorch.Container.Zip = class {
                 const stream = entries.get(name);
                 const buffer = stream.peek();
                 const decoder = new TextDecoder('utf-8');
-                const text = decoder.decode(buffer);
-                model = JSON.parse(text);
+                const content = decoder.decode(buffer);
+                model = JSON.parse(content);
                 if (!model.mainModule) {
                     return null;
                 }

+ 2 - 2
source/sklearn.js

@@ -501,8 +501,8 @@ sklearn.Tensor = class {
                     case 'string': {
                         const buffer = context.data.subarray(context.index, context.index + context.itemsize);
                         const index = buffer.indexOf(0);
-                        const text = context.decoder.decode(index >= 0 ? buffer.subarray(0, index) : buffer);
-                        results.push(text);
+                        const content = context.decoder.decode(index >= 0 ? buffer.subarray(0, index) : buffer);
+                        results.push(content);
                         context.index += context.itemsize;
                         context.count++;
                         break;

+ 3 - 3
source/tar.js

@@ -147,15 +147,15 @@ tar.BinaryReader = class {
     string(length) {
         const buffer = this.read(length);
         let position = 0;
-        let text = '';
+        let content = '';
         for (let i = 0; i < length; i++) {
             const c = buffer[position++];
             if (c === 0) {
                 break;
             }
-            text += String.fromCharCode(c);
+            content += String.fromCharCode(c);
         }
-        return text;
+        return content;
     }
 };
 

+ 3 - 3
source/tengine.js

@@ -945,18 +945,18 @@ tengine.BinaryReader = class {
 
     string() {
         const position = this.uint32();
-        let text = '';
+        let content = '';
         if (position) {
             const next = this._position;
             this.seek(position);
             const size = this.uint32();
             this.seek(this.uint32());
             for(let i = 0; i < size - 1; i++) {
-                text += String.fromCharCode(this._buffer[this._position++]);
+                content += String.fromCharCode(this._buffer[this._position++]);
             }
             this.seek(next);
         }
-        return text;
+        return content;
     }
 };
 

+ 228 - 0
source/text.js

@@ -0,0 +1,228 @@
+/* jshint esversion: 6 */
+
+var text = text || {};
+
+text.Decoder = class {
+
+    static open(data) {
+        if (typeof data === 'string') {
+            return new text.Decoder.String(data);
+        }
+        const buffer = data instanceof Uint8Array ? data : data.peek();
+        const length = buffer.length;
+        if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
+            return new text.Decoder.Utf8(buffer, 3);
+        }
+        if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
+            return new text.Decoder.Utf16LE(buffer, 2);
+        }
+        if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
+            return new text.Decoder.Utf16BE(buffer, 2);
+        }
+        if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) {
+            throw new Error("Unsupported UTF-32 big-endian encoding.");
+        }
+        if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) {
+            throw new Error("Unsupported UTF-32 little-endian encoding.");
+        }
+        if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) {
+            throw new Error("Unsupported UTF-7 encoding.");
+        }
+        if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) {
+            throw new Error("Unsupported UTF-7 encoding.");
+        }
+        if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) {
+            throw new Error("Unsupported GB-18030 encoding.");
+        }
+        if (length > 4 && (length % 2) == 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) {
+            const lo = new Uint32Array(256);
+            const hi = new Uint32Array(256);
+            for (let i = 0; i < length; i += 2) {
+                lo[buffer[i]]++;
+                hi[buffer[i + 1]]++;
+            }
+            if (lo[0x00] === 0 && (hi[0x00] / (length >> 1)) > 0.5) {
+                return new text.Decoder.Utf16LE(buffer, 0);
+            }
+            if (hi[0x00] === 0 && (lo[0x00] / (length >> 1)) > 0.5) {
+                return new text.Decoder.Utf16BE(buffer, 0);
+            }
+        }
+        return new text.Decoder.Utf8(buffer, 0);
+    }
+};
+
+text.Decoder.String = class {
+
+    constructor(buffer) {
+        this.buffer = buffer;
+        this.position = 0;
+        this.length = buffer.length;
+    }
+
+    decode() {
+        if (this.position < this.length) {
+            return this.buffer[this.position++];
+        }
+        return undefined;
+    }
+};
+
+text.Decoder.Utf8 = class {
+
+    constructor(buffer, position) {
+        this.position = position || 0;
+        this.buffer = buffer;
+    }
+
+    decode() {
+        const c = this.buffer[this.position];
+        if (c === undefined) {
+            return c;
+        }
+        this.position++;
+        if (c < 0x80) {
+            return String.fromCodePoint(c);
+        }
+        if (c >= 0xC2 && c <= 0xDF) {
+            if (this.buffer[this.position] !== undefined) {
+                const c2 = this.buffer[this.position];
+                this.position++;
+                return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F));
+            }
+        }
+        if (c >= 0xE0 && c <= 0xEF) {
+            if (this.buffer[this.position + 1] !== undefined) {
+                const c2 = this.buffer[this.position];
+                if ((c !== 0xE0 || c2 >= 0xA0) && (c !== 0xED || c2 <= 0x9f)) {
+                    const c3 = this.buffer[this.position + 1];
+                    if (c3 >= 0x80 && c3 < 0xFB) {
+                        this.position += 2;
+                        return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
+                    }
+                }
+            }
+        }
+        if (c >= 0xF0 && c <= 0xF4) {
+            if (this.buffer[this.position + 2] !== undefined) {
+                const c2 = this.buffer[this.position];
+                if ((c !== 0xF0 || c2 >= 0x90) && (c !== 0xF4 || c2 <= 0x8f)) {
+                    const c3 = this.buffer[this.position + 1];
+                    if (c3 >= 0x80 && c3 < 0xFB) {
+                        const c4 = this.buffer[this.position + 2];
+                        this.position += 3;
+                        return String.fromCodePoint(((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
+                    }
+                }
+            }
+        }
+        return String.fromCharCode(0xfffd);
+    }
+};
+
+text.Decoder.Utf16LE = class {
+
+    constructor(buffer, position) {
+        this.buffer = buffer;
+        this.position = position || 0;
+        this.length = buffer.length;
+    }
+
+    decode() {
+        if (this.position + 1 < this.length) {
+            const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8);
+            if (c < 0xD800 || c >= 0xDFFF) {
+                return String.fromCharCode(c);
+            }
+            if (c >= 0xD800 && c < 0xDBFF) {
+                if (this._position + 1 < this._length) {
+                    const c2 = this._buffer[this._position++] | (this._buffer[this._position++] << 8);
+                    if (c >= 0xDC00 || c < 0xDFFF) {
+                        return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
+                    }
+                }
+            }
+            return String.fromCharCode(0xfffd);
+        }
+        return undefined;
+    }
+};
+
+text.Decoder.Utf16BE = class {
+
+    constructor(buffer, position) {
+        this.buffer = buffer;
+        this.position = position || 0;
+        this.length = buffer.length;
+    }
+
+    decode() {
+        if (this.position + 1 < this.length) {
+            const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++];
+            if (c < 0xD800 || c >= 0xDFFF) {
+                return String.fromCharCode(c);
+            }
+            if (c >= 0xD800 && c < 0xDBFF) {
+                if (this._position + 1 < this._length) {
+                    const c2 = (this._buffer[this._position++] << 8) | this._buffer[this._position++];
+                    if (c >= 0xDC00 || c < 0xDFFF) {
+                        return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
+                    }
+                }
+            }
+            return String.fromCharCode(0xfffd);
+        }
+        return undefined;
+    }
+};
+
+text.Reader = class {
+
+    constructor(data, length) {
+        this._decoder = text.Decoder.open(data);
+        this._position = 0;
+        this._length = length || Number.MAX_SAFE_INTEGER;
+    }
+
+    static open(data, length) {
+        return new text.Reader(data, length);
+    }
+
+    read() {
+        if (this._position >= this._length) {
+            return undefined;
+        }
+        let line = '';
+        let buffer = null;
+        for (;;) {
+            const c = this._decoder.decode();
+            if (c === undefined) {
+                this._length = this._position;
+                break;
+            }
+            this._position++;
+            if (this._position > this._length) {
+                break;
+            }
+            if (c === '\n') {
+                break;
+            }
+            line += c;
+            if (line.length >= 32) {
+                buffer = buffer || [];
+                buffer.push(line);
+                line = '';
+            }
+        }
+        if (buffer) {
+            buffer.push(line);
+            return buffer.join('');
+        }
+        return line;
+    }
+};
+
+if (typeof module !== 'undefined' && typeof module.exports === 'object') {
+    module.exports.Decoder = text.Decoder;
+    module.exports.Reader = text.Reader;
+}

+ 9 - 9
source/tnn.js

@@ -1,7 +1,7 @@
 /* jshint esversion: 6 */
 
 var tnn = tnn || {};
-var base = base || require('./base');
+var text = text || require('./text');
 
 tnn.ModelFactory = class {
 
@@ -9,10 +9,10 @@ tnn.ModelFactory = class {
         const identifier = context.identifier.toLowerCase();
         if (identifier.endsWith('.tnnproto')) {
             try {
-                const reader = base.TextReader.open(context.stream.peek(), 2048);
-                const text = reader.read();
-                if (text !== undefined) {
-                    const line = text.trim();
+                const reader = text.Reader.open(context.stream.peek(), 2048);
+                const content = reader.read();
+                if (content !== undefined) {
+                    const line = content.trim();
                     if (line.startsWith('"') && line.endsWith('"')) {
                         const header = line.replace(/(^")|("$)/g, '').split(',').shift().trim().split(' ');
                         if (header.length === 3 || (header.length >= 4 && (header[3] === '4206624770' || header[3] == '4206624772'))) {
@@ -628,7 +628,7 @@ tnn.Metadata = class {
 tnn.TextProtoReader = class {
 
     constructor(buffer) {
-        const reader = base.TextReader.open(buffer);
+        const reader = text.Reader.open(buffer);
         let lines = [];
         for (;;) {
             const line = reader.read();
@@ -905,9 +905,9 @@ tnn.BinaryReader = class {
     }
 
     expect(name) {
-        const text = this.string();
-        if (name !== text) {
-            throw new tnn.Error("Invalid string '" + text + "' instead of '" + name + "'.");
+        const content = this.string();
+        if (name !== content) {
+            throw new tnn.Error("Invalid string '" + content + "' instead of '" + name + "'.");
         }
     }
 };

+ 6 - 6
source/torch.js

@@ -1216,8 +1216,8 @@ torch.TextReader = class {
     int64s(size) {
         const array = [];
         if (size > 0) {
-            const text = this._textDecoder.decode(this.line(Number.MAX_SAFE_INTEGER));
-            for (const token of text.split(' ')) {
+            const content = this._textDecoder.decode(this.line(Number.MAX_SAFE_INTEGER));
+            for (const token of content.split(' ')) {
                 const number = Number.parseInt(token, 10);
                 if (Number.isNaN(token - number)) {
                     throw new torch.Error("Couldn't parse int64 '" + token + "'.");
@@ -1259,11 +1259,11 @@ torch.TextReader = class {
             return '';
         }
         const data = this.line(size);
-        const text = this._textDecoder.decode(data);
-        if (size != text.length) {
-            throw torch.Error('Invalid text length.');
+        const content = this._textDecoder.decode(data);
+        if (size != content.length) {
+            throw torch.Error('Invalid string length.');
         }
-        return text;
+        return content;
     }
 
     storage(size, itemSize, dataType) {

+ 21 - 21
source/view-sidebar.js

@@ -525,16 +525,16 @@ class NodeAttributeView {
                 break;
             }
             default: {
-                let text = sidebar.NodeSidebar.formatAttributeValue(value, type);
-                if (text && text.length > 1000) {
-                    text = text.substring(0, 1000) + '\u2026';
+                let content = sidebar.NodeSidebar.formatAttributeValue(value, type);
+                if (content && content.length > 1000) {
+                    content = content.substring(0, 1000) + '\u2026';
                 }
-                if (text && typeof text === 'string') {
-                    text = text.split('<').join('&lt;').split('>').join('&gt;');
+                if (content && typeof text === 'string') {
+                    content = content.split('<').join('&lt;').split('>').join('&gt;');
                 }
                 const line = this._host.document.createElement('div');
                 line.className = 'sidebar-view-item-value-line';
-                line.innerHTML = (text ? text : '&nbsp;');
+                line.innerHTML = content ? content : '&nbsp;';
                 this._element.appendChild(line);
             }
         }
@@ -1539,16 +1539,16 @@ markdown.Generator = class {
             if (match) {
                 source = source.substring(match[0].length);
                 const language = match[2] ? match[2].trim() : match[2];
-                let text = match[3] || '';
+                let content = match[3] || '';
                 const matchIndent = match[0].match(/^(\s+)(?:```)/);
                 if (matchIndent !== null) {
                     const indent = matchIndent[1];
-                    text = text.split('\n').map(node => {
+                    content = content.split('\n').map(node => {
                         const match = node.match(/^\s+/);
                         return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node;
                     }).join('\n');
                 }
-                tokens.push({ type: 'code', language: language, text: text });
+                tokens.push({ type: 'code', language: language, text: content });
                 continue;
             }
             match = this._headingRegExp.exec(source);
@@ -1873,11 +1873,11 @@ markdown.Generator = class {
             match = this._codespanRegExp.exec(source);
             if (match) {
                 source = source.substring(match[0].length);
-                let text = match[2].replace(/\n/g, ' ');
-                if (/[^ ]/.test(text) && text.startsWith(' ') && text.endsWith(' ')) {
-                    text = text.substring(1, text.length - 1);
+                let content = match[2].replace(/\n/g, ' ');
+                if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) {
+                    content = content.substring(1, content.length - 1);
                 }
-                tokens.push({ type: 'codespan', text: this._encode(text) });
+                tokens.push({ type: 'codespan', text: this._encode(content) });
                 continue;
             }
             match = this._brRegExp.exec(source);
@@ -2177,18 +2177,18 @@ markdown.Generator = class {
         return slug;
     }
 
-    _encode(text) {
-        if (this._escapeTestRegExp.test(text)) {
-            return text.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
+    _encode(content) {
+        if (this._escapeTestRegExp.test(content)) {
+            return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]);
         }
-        return text;
+        return content;
     }
 
-    _escape(text) {
-        if (this._escapeTestNoEncodeRegExp.test(text)) {
-            return text.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
+    _escape(content) {
+        if (this._escapeTestNoEncodeRegExp.test(content)) {
+            return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]);
         }
-        return text;
+        return content;
     }
 };
 

+ 18 - 18
source/view.js

@@ -865,9 +865,9 @@ view.View = class {
                 this._sidebar.open(content, 'Model Properties');
             }
             catch (error) {
-                const text = " in '" + this._model.identifier + "'.";
-                if (error && !error.message.endsWith(text) && (error.context === undefined || error.context === true)) {
-                    error.message = error.message.replace(/\.$/, '') + text;
+                const content = " in '" + this._model.identifier + "'.";
+                if (error && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) {
+                    error.message = error.message.replace(/\.$/, '') + content;
                 }
                 this.error(error, 'Error showing model properties.', null);
             }
@@ -922,9 +922,9 @@ view.View = class {
                 this._sidebar.open(nodeSidebar.render(), 'Node Properties');
             }
             catch (error) {
-                const text = " in '" + this._model.identifier + "'.";
-                if (error && !error.message.endsWith(text) && (error.context === undefined || error.context === true)) {
-                    error.message = error.message.replace(/\.$/, '') + text;
+                const content = " in '" + this._model.identifier + "'.";
+                if (error && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) {
+                    error.message = error.message.replace(/\.$/, '') + content;
                 }
                 this.error(error, 'Error showing node properties.', null);
             }
@@ -1213,7 +1213,7 @@ view.Argument = class {
         if (this._from && this._to) {
             for (let i = 0; i < this._to.length; i++) {
                 const to = this._to[i];
-                let text = '';
+                let content = '';
                 const type = this._argument.type;
 
                 if (type &&
@@ -1221,16 +1221,16 @@ view.Argument = class {
                     type.shape.dimensions &&
                     type.shape.dimensions.length > 0 &&
                     type.shape.dimensions.every((dim) => !dim || Number.isInteger(dim) || dim instanceof base.Int64 || (typeof dim === 'string' && (dim.length < 2 || dim === 'None')))) {
-                    text = type.shape.dimensions.map((dim) => dim || '?').join('\u00D7');
+                    content = type.shape.dimensions.map((dim) => dim || '?').join('\u00D7');
                 }
                 if (this.context.view.showNames) {
-                    text = this._argument.name.split('\n').shift(); // custom argument id
+                    content = this._argument.name.split('\n').shift(); // custom argument id
                 }
                 const edge = this.context.createEdge(this._from, to);
                 edge.v = this._from.name;
                 edge.w = to.name;
-                if (text) {
-                    edge.label = text;
+                if (content) {
+                    edge.label = content;
                 }
                 edge.id = 'edge-' + this._argument.name;
                 if (this._controlDependencies && this._controlDependencies.has(i)) {
@@ -1772,12 +1772,12 @@ view.ModelFactoryService = class {
                     }
                 }
                 const format = (tags) => {
-                    const text = Object.entries(tags).map((pair) => {
+                    const content = Object.entries(tags).map((pair) => {
                         const key = pair[0];
                         const value = pair[1];
                         return key.toString() + ':' + (Object(value) === value ? '{' + format(value) + '}' : value.toString());
                     });
-                    return text.join(',');
+                    return content.join(',');
                 };
                 const content = format(tags);
                 throw new view.Error("Unsupported Protocol Buffers content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip());
@@ -1838,9 +1838,9 @@ view.ModelFactoryService = class {
                 const id = modules.shift();
                 return this._host.require(id).then((module) => {
                     const updateErrorContext = (error, context) => {
-                        const text = " in '" + context.identifier + "'.";
-                        if (error && !error.message.endsWith(text) && (error.context === undefined || error.context === true)) {
-                            error.message = error.message.replace(/\.$/, '') + text;
+                        const content = " in '" + context.identifier + "'.";
+                        if (error && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) {
+                            error.message = error.message.replace(/\.$/, '') + content;
                         }
                     };
                     if (!module.ModelFactory) {
@@ -2086,9 +2086,9 @@ view.ModelFactoryService = class {
             ];
             /* eslint-enable no-control-regex */
             const buffer = stream.peek(Math.min(4096, stream.length));
-            const text = String.fromCharCode.apply(null, buffer);
+            const content = String.fromCharCode.apply(null, buffer);
             for (const entry of entries) {
-                if (text.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
+                if (content.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
                     return Promise.reject(new view.Error('Invalid file content. File contains ' + entry.name + '.', true));
                 }
             }

+ 2 - 2
test/models.js

@@ -109,8 +109,8 @@ class TestHost {
             return Promise.reject(new Error("The file '" + file + "' does not exist."));
         }
         if (encoding) {
-            const text = fs.readFileSync(pathname, encoding);
-            return Promise.resolve(text);
+            const content = fs.readFileSync(pathname, encoding);
+            return Promise.resolve(content);
         }
         const buffer = fs.readFileSync(pathname, null);
         const stream = new TestBinaryStream(buffer);

+ 19 - 19
tools/flatc.js

@@ -610,44 +610,44 @@ flatc.Parser.Tokenizer = class {
         if (this._position >= this._text.length) {
             return { type: 'eof', value: '' };
         }
-        const text = this._text.slice(this._position);
+        const content = this._text.slice(this._position);
 
-        const boolean_constant = text.match(/^(true|false)/);
+        const boolean_constant = content.match(/^(true|false)/);
         if (boolean_constant) {
-            const text = boolean_constant[0];
-            return { type: 'boolean', token: text, value: text === 'true' };
+            const content = boolean_constant[0];
+            return { type: 'boolean', token: content, value: content === 'true' };
         }
 
-        const identifier = text.match(/^[a-zA-Z_][a-zA-Z0-9_.]*/);
+        const identifier = content.match(/^[a-zA-Z_][a-zA-Z0-9_.]*/);
         if (identifier) {
             return { type: 'id', token: identifier[0] };
         }
 
-        const string_constant = text.match(/^".*?"/) || text.match(/^'.*?'/);
+        const string_constant = content.match(/^".*?"/) || content.match(/^'.*?'/);
         if (string_constant) {
-            const text = string_constant[0];
-            return { type: 'string', token: text, value: text.substring(1, text.length - 1) };
+            const content = string_constant[0];
+            return { type: 'string', token: content, value: content.substring(1, content.length - 1) };
         }
 
-        const dec_float_constant = text.match(/^[-+]?(([.][0-9]+)|([0-9]+[.][0-9]*)|([0-9]+))([eE][-+]?[0-9]+)?/);
+        const dec_float_constant = content.match(/^[-+]?(([.][0-9]+)|([0-9]+[.][0-9]*)|([0-9]+))([eE][-+]?[0-9]+)?/);
         if (dec_float_constant) {
-            const text = dec_float_constant[0];
-            if (text.indexOf('.') !== -1 || text.indexOf('e') !== -1) {
-                return { type: 'float', token: text, value: parseFloat(text) };
+            const content = dec_float_constant[0];
+            if (content.indexOf('.') !== -1 || content.indexOf('e') !== -1) {
+                return { type: 'float', token: content, value: parseFloat(content) };
             }
         }
 
-        const hex_float_constant = text.match(/^[-+]?0[xX](([.][0-9a-fA-F]+)|([0-9a-fA-F]+[.][0-9a-fA-F]*)|([0-9a-fA-F]+))([pP][-+]?[0-9]+)/);
+        const hex_float_constant = content.match(/^[-+]?0[xX](([.][0-9a-fA-F]+)|([0-9a-fA-F]+[.][0-9a-fA-F]*)|([0-9a-fA-F]+))([pP][-+]?[0-9]+)/);
         if (hex_float_constant) {
             throw new flatc.Error('Unsupported hexadecimal constant.');
         }
 
-        const dec_integer_constant = text.match(/^[-+]?[0-9]+/);
+        const dec_integer_constant = content.match(/^[-+]?[0-9]+/);
         if (dec_integer_constant) {
-            const text = dec_integer_constant[0];
-            return { type: 'integer', token: text, value: parseInt(text, 10) };
+            const content = dec_integer_constant[0];
+            return { type: 'integer', token: content, value: parseInt(content, 10) };
         }
-        const hex_integer_constant = text.match(/^[-+]?0[xX][0-9a-fA-F]+/);
+        const hex_integer_constant = content.match(/^[-+]?0[xX][0-9a-fA-F]+/);
         if (hex_integer_constant) {
             throw new flatc.Error('Unsupported hexadecimal constant.');
         }
@@ -809,8 +809,8 @@ flatc.Root = class extends flatc.Object {
     _parseFile(paths, file) {
         if (!this._files.has(file)) {
             this._files.add(file);
-            const text = fs.readFileSync(file, 'utf-8');
-            const parser = new flatc.Parser(text, file, this);
+            const content = fs.readFileSync(file, 'utf-8');
+            const parser = new flatc.Parser(content, file, this);
             const includes = parser.include();
             for (const include of includes) {
                 const includeFile = this._resolve(paths, file, include);