Pārlūkot izejas kodu

use TextDecoder when available

Alexander Rose 5 gadi atpakaļ
vecāks
revīzija
9c2c48bd59
1 mainītis faili ar 12 papildinājumiem un 2 dzēšanām
  1. 12 2
      src/mol-io/common/utf8.ts

+ 12 - 2
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++) {