Skip to content

Commit

Permalink
move use of utf8DecodeWasm to utf8.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed May 16, 2019
1 parent 6c55452 commit 6066940
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { prettyByte } from "./utils/prettyByte";
import { ExtensionCodec } from "./ExtensionCodec";
import { getInt64, getUint64 } from "./utils/int";
import { utf8Decode } from "./utils/utf8";
import { utf8DecodeWasm, WASM_AVAILABLE } from "./wasmFunctions";
import { createDataView, ensureUint8Array } from "./utils/typedArrays";

enum State {
Expand Down Expand Up @@ -379,12 +378,7 @@ export class Decoder {
throw MORE_DATA;
}

let object: string;
if (WASM_AVAILABLE) {
object = utf8DecodeWasm(this.bytes, this.pos + headOffset, byteLength);
} else {
object = utf8Decode(this.bytes, this.pos + headOffset, byteLength);
}
const object = utf8Decode(this.bytes, this.pos + headOffset, byteLength);
this.pos += headOffset + byteLength;
return object;
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/utf8.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { prettyByte } from "./prettyByte";
import { WASM_AVAILABLE, utf8DecodeWasm } from "../wasmFunctions";

const WASM_THRESHOLD = 0x100;

export function utf8Count(str: string): number {
const strLength = str.length;
Expand Down Expand Up @@ -86,6 +89,10 @@ export function utf8Encode(str: string, view: DataView, offset: number): void {
}

export function utf8Decode(bytes: Uint8Array, offset: number, byteLength: number): string {
if (WASM_AVAILABLE && byteLength > WASM_THRESHOLD) {
return utf8DecodeWasm(bytes, offset, byteLength);
}

const out: Array<number> = [];
const end = offset + byteLength;
while (offset < end) {
Expand Down

0 comments on commit 6066940

Please sign in to comment.