diff --git a/src/mol-io/common/utf8.ts b/src/mol-io/common/utf8.ts index 74f35811b389c4b4dd1e2266422cd6f028e52fdf..83cc3f0754f1e5a7b5d28303c697aa59903d52c0 100644 --- a/src/mol-io/common/utf8.ts +++ b/src/mol-io/common/utf8.ts @@ -1,5 +1,5 @@ /** - * 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++) {