From 9c2c48bd5982649ab178ec3a912e9c3387abc2f3 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alex.rose@rcsb.org> Date: Tue, 28 Jan 2020 11:44:09 -0800 Subject: [PATCH] use TextDecoder when available --- src/mol-io/common/utf8.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/mol-io/common/utf8.ts b/src/mol-io/common/utf8.ts index 74f35811b..83cc3f075 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++) { -- GitLab