Skip to content

Commit

Permalink
Update text.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jun 2, 2024
1 parent fe2ec50 commit 98913f1
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 87 deletions.
5 changes: 2 additions & 3 deletions source/coreml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import * as base from './base.js';
import * as text from './text.js';

const coreml = {};

Expand Down Expand Up @@ -53,8 +52,8 @@ coreml.ModelFactory = class {
}
if (identifier === 'model.mil') {
try {
const reader = text.Reader.open(context.stream, 2048);
const signature = reader.read();
const reader = context.peek('text');
const signature = reader.peek('\n', 2048);
if (signature !== undefined) {
if (signature.trim().startsWith('program')) {
context.type = 'coreml.mil';
Expand Down
43 changes: 23 additions & 20 deletions source/darknet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import * as text from './text.js';

const darknet = {};

darknet.ModelFactory = class {
Expand All @@ -16,19 +14,24 @@ darknet.ModelFactory = class {
}
return;
}
try {
const reader = text.Reader.open(context.stream, 65536);
for (let line = reader.read(); line !== undefined; line = reader.read()) {
const content = line.trim();
if (content.length > 0 && !content.startsWith('#')) {
if (content.startsWith('[') && content.endsWith(']')) {
context.type = 'darknet.model';
}
return;
const reader = context.peek('text');
if (reader) {
try {
for (let line = reader.read('\n', 65536); line !== undefined; line = reader.read('\n', 65536)) {
const content = line.trim();
if (content.length > 0 && !content.startsWith('#')) {
if (content.startsWith('[') && content.endsWith(']')) {
reader.seek(0);
context.type = 'darknet.model';
context.target = reader;
}
return;
}
}
} catch {
// continue regardless of error
}
} catch {
// continue regardless of error
reader.seek(0);
}
}

Expand All @@ -43,18 +46,18 @@ darknet.ModelFactory = class {
const weights = context.target;
const name = `${basename}.cfg`;
const content = await context.fetch(name);
const reader = new darknet.Reader(content.stream, content.identifier);
const reader = new darknet.Reader(content.peek('text'), content.identifier);
return new darknet.Model(metadata, reader, weights);
}
case 'darknet.model': {
try {
const name = `${basename}.weights`;
const content = await context.fetch(name);
const weights = darknet.Weights.open(content);
const reader = new darknet.Reader(context.stream, context.identifier);
const reader = new darknet.Reader(context.target, context.identifier);
return new darknet.Model(metadata, reader, weights);
} catch {
const reader = new darknet.Reader(context.stream, context.identifier);
const reader = new darknet.Reader(context.target, context.identifier);
return new darknet.Model(metadata, reader, null);
}
}
Expand Down Expand Up @@ -874,19 +877,19 @@ darknet.TensorShape = class {

darknet.Reader = class {

constructor(stream, identifier) {
this.stream = stream;
constructor(reader, identifier) {
this.reader = reader;
this.identifier = identifier;
}

read() {
// read_cfg
const sections = [];
let section = null;
const reader = text.Reader.open(this.stream);
const reader = this.reader;
let lineNumber = 0;
const setup = /^setup.*\.cfg$/.test(this.identifier);
for (let content = reader.read(); content !== undefined; content = reader.read()) {
for (let content = reader.read('\n'); content !== undefined; content = reader.read('\n')) {
lineNumber++;
const line = content.replace(/\s/g, '');
if (line.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion source/dlc.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ dlc.Container = class {
delete this._metadata;
const reader = text.Reader.open(stream);
for (;;) {
const line = reader.read();
const line = reader.read('\n');
if (line === undefined) {
break;
}
Expand Down
51 changes: 28 additions & 23 deletions source/ncnn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import * as base from './base.js';
import * as text from './text.js';

const ncnn = {};

Expand All @@ -22,21 +21,28 @@ ncnn.ModelFactory = class {
}
}
} else if (identifier.endsWith('.param') || identifier.endsWith('.cfg.ncnn')) {
try {
const reader = text.Reader.open(context.stream, 2048);
const signature = reader.read();
if (signature !== undefined) {
if (signature.trim() === '7767517') {
context.type = 'ncnn.model';
return;
}
const header = signature.trim().split(' ');
if (header.length === 2 && header.every((value) => value >>> 0 === parseFloat(value))) {
context.type = 'ncnn.model';
const reader = context.peek('text');
if (reader) {
try {
const signature = reader.read('\n', 2048);
if (signature !== undefined) {
if (signature.trim() === '7767517') {
reader.seek(0);
context.type = 'ncnn.model';
context.target = reader;
return;
}
const header = signature.trim().split(' ');
if (header.length === 2 && header.every((value) => value >>> 0 === parseFloat(value))) {
reader.seek(0);
context.type = 'ncnn.model';
context.target = reader;
}
}
} catch {
// continue regardless of error
}
} catch {
// continue regardless of error
reader.seek(0);
}
} else if (identifier.endsWith('.bin') || identifier.endsWith('.weights.ncnn')) {
const stream = context.stream;
Expand Down Expand Up @@ -80,8 +86,8 @@ ncnn.ModelFactory = class {
const reader = new ncnn.BinaryParamReader(param);
return new ncnn.Model(metadata, reader, bin);
};
const openText = (param, bin) => {
const reader = new ncnn.TextParamReader(param);
const openText = (reader, bin) => {
reader = new ncnn.TextParamReader(reader);
return new ncnn.Model(metadata, reader, bin);
};
const identifier = context.identifier.toLowerCase();
Expand All @@ -96,9 +102,9 @@ ncnn.ModelFactory = class {
try {
const content = await context.fetch(bin);
const buffer = content.stream.peek();
return openText(context.stream.peek(), buffer);
return openText(context.target, buffer);
} catch {
return openText(context.stream.peek(), null);
return openText(context.target, null);
}
}
case 'ncnn.model.bin': {
Expand All @@ -120,8 +126,8 @@ ncnn.ModelFactory = class {
}
try {
const content = await context.fetch(file);
const buffer = content.stream.peek();
return openText(buffer, context.stream.peek());
const reader = content.peek('text');
return openText(reader, context.stream.peek());
} catch {
const content = await context.fetch(`${file}.bin`);
const buffer = content.stream.peek();
Expand Down Expand Up @@ -634,11 +640,10 @@ ncnn.Utility = class {

ncnn.TextParamReader = class {

constructor(buffer) {
const reader = text.Reader.open(buffer);
constructor(reader) {
const lines = [];
for (;;) {
const line = reader.read();
const line = reader.read('\n');
if (line === undefined) {
break;
}
Expand Down
7 changes: 2 additions & 5 deletions source/nnabla.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import * as text from './text.js';

const nnabla = {};

nnabla.ModelFactory = class {
Expand Down Expand Up @@ -29,9 +27,8 @@ nnabla.ModelFactory = class {
let version = '';
if (contexts.has('nnp_version.txt')) {
const context = contexts.get('nnp_version.txt');
const stream = context.stream;
const reader = text.Reader.open(stream);
version = reader.read();
const reader = context.read('text');
version = reader.read('\n');
version = version.split('\r').shift();
}
if (contexts.has('parameter.protobuf')) {
Expand Down
2 changes: 1 addition & 1 deletion source/nnef.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ nnef.TextReader = class {
static open(stream) {
const reader = text.Reader.open(stream);
for (let i = 0; i < 32; i++) {
const line = reader.read();
const line = reader.read('\n');
const match = /version\s*(\d+\.\d+);/.exec(line);
if (match) {
return new nnef.TextReader(stream, match[1]);
Expand Down
2 changes: 1 addition & 1 deletion source/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,7 @@ onnx.TextReader = class {
const reader = text.Reader.open(buffer);
const lines = [];
for (let i = 0; i < 32; i++) {
const line = reader.read();
const line = reader.read('\n');
if (line === undefined) {
break;
}
Expand Down
51 changes: 35 additions & 16 deletions source/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ text.Decoder.Utf16LE = class {
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 (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));
}
Expand Down Expand Up @@ -225,8 +225,8 @@ text.Decoder.Utf16BE = class {
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 (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));
}
Expand Down Expand Up @@ -288,37 +288,56 @@ text.Decoder.Utf32BE = class {

text.Reader = class {

constructor(data, length) {
this._decoder = text.Decoder.open(data);
this._position = 0;
this._length = length || Number.MAX_SAFE_INTEGER;
constructor(data
) {
this.decoder = text.Decoder.open(data);
this.position = 0;
}

static open(data, length) {
return new text.Reader(data, length);
}

read() {
if (this._position >= this._length) {
seek(position) {
this.position = position;
this.decoder.position = 0;
if (position > 0) {
this.read(undefined, position);
delete this.length;
}
}

peek(terminal, length) {
const position = this.position;
const offset = this.decoder.position;
const content = this.read(terminal, length);
this.decoder.position = offset;
this.position = position;
return content;
}

read(terminal, length) {
length = length || this.length || Number.MAX_SAFE_INTEGER;
if (length && this.position >= length) {
return undefined;
}
let line = '';
let buffer = null;
for (;;) {
const c = this._decoder.decode();
const c = this.decoder.decode();
if (c === undefined) {
this._length = this._position;
this.length = this.position;
break;
}
this._position++;
if (this._position > this._length) {
this.position++;
if (length && this.position > length) {
break;
}
if (c === '\n') {
if (c === terminal) {
break;
}
line += c;
if (line.length >= 32) {
if (line.length >= 64) {
buffer = buffer || [];
buffer.push(line);
line = '';
Expand Down
Loading

0 comments on commit 98913f1

Please sign in to comment.