Skip to content
Snippets Groups Projects
Commit 9c2c48bd authored by Alexander Rose's avatar Alexander Rose
Browse files

use TextDecoder when available

parent df6d49b2
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
* Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* Adapted from https://github.com/rcsb/mmtf-javascript
* @author Alexander Rose <alexander.rose@weirdbyte.de>
......@@ -53,7 +53,7 @@ function throwError(err: string) {
throw new Error(err);
}
export function utf8Read(data: Uint8Array, offset: number, length: number) {
function _utf8Read(data: Uint8Array, offset: number, length: number) {
let chars = __chars;
let str: string[] | undefined = void 0, chunk: string[] = [], chunkSize = 512, chunkOffset = 0;
......@@ -98,6 +98,16 @@ export function utf8Read(data: Uint8Array, offset: number, length: number) {
return str.join('');
}
const utf8Decoder = (typeof TextDecoder !== 'undefined') ? new TextDecoder() : undefined
export function utf8Read(data: Uint8Array, offset: number, length: number) {
if (utf8Decoder) {
const input = (offset || length !== data.length) ? data.subarray(offset, offset + length) : data
return utf8Decoder.decode(input)
} else {
return _utf8Read(data, offset, length)
}
}
export function utf8ByteCount(str: string) {
let count = 0;
for (let i = 0, l = str.length; i < l; i++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment