yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import defaultValue from "./defaultValue.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import RuntimeError from "./RuntimeError.js";
 
/**
 * Reads a string from a Uint8Array.
 *
 * @function
 *
 * @param {Uint8Array} uint8Array The Uint8Array to read from.
 * @param {Number} [byteOffset=0] The byte offset to start reading from.
 * @param {Number} [byteLength] The byte length to read. If byteLength is omitted the remainder of the buffer is read.
 * @returns {String} The string.
 *
 * @private
 */
function getStringFromTypedArray(uint8Array, byteOffset, byteLength) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(uint8Array)) {
    throw new DeveloperError("uint8Array is required.");
  }
  if (byteOffset < 0) {
    throw new DeveloperError("byteOffset cannot be negative.");
  }
  if (byteLength < 0) {
    throw new DeveloperError("byteLength cannot be negative.");
  }
  if (byteOffset + byteLength > uint8Array.byteLength) {
    throw new DeveloperError("sub-region exceeds array bounds.");
  }
  //>>includeEnd('debug');
 
  byteOffset = defaultValue(byteOffset, 0);
  byteLength = defaultValue(byteLength, uint8Array.byteLength - byteOffset);
 
  uint8Array = uint8Array.subarray(byteOffset, byteOffset + byteLength);
 
  return getStringFromTypedArray.decode(uint8Array);
}
 
// Exposed functions for testing
getStringFromTypedArray.decodeWithTextDecoder = function (view) {
  var decoder = new TextDecoder("utf-8");
  return decoder.decode(view);
};
 
getStringFromTypedArray.decodeWithFromCharCode = function (view) {
  var result = "";
  var codePoints = utf8Handler(view);
  var length = codePoints.length;
  for (var i = 0; i < length; ++i) {
    var cp = codePoints[i];
    if (cp <= 0xffff) {
      result += String.fromCharCode(cp);
    } else {
      cp -= 0x10000;
      result += String.fromCharCode((cp >> 10) + 0xd800, (cp & 0x3ff) + 0xdc00);
    }
  }
  return result;
};
 
function inRange(a, min, max) {
  return min <= a && a <= max;
}
 
// This code is inspired by public domain code found here: https://github.com/inexorabletash/text-encoding
function utf8Handler(utfBytes) {
  var codePoint = 0;
  var bytesSeen = 0;
  var bytesNeeded = 0;
  var lowerBoundary = 0x80;
  var upperBoundary = 0xbf;
 
  var codePoints = [];
  var length = utfBytes.length;
  for (var i = 0; i < length; ++i) {
    var currentByte = utfBytes[i];
 
    // If bytesNeeded = 0, then we are starting a new character
    if (bytesNeeded === 0) {
      // 1 Byte Ascii character
      if (inRange(currentByte, 0x00, 0x7f)) {
        // Return a code point whose value is byte.
        codePoints.push(currentByte);
        continue;
      }
 
      // 2 Byte character
      if (inRange(currentByte, 0xc2, 0xdf)) {
        bytesNeeded = 1;
        codePoint = currentByte & 0x1f;
        continue;
      }
 
      // 3 Byte character
      if (inRange(currentByte, 0xe0, 0xef)) {
        // If byte is 0xE0, set utf-8 lower boundary to 0xA0.
        if (currentByte === 0xe0) {
          lowerBoundary = 0xa0;
        }
        // If byte is 0xED, set utf-8 upper boundary to 0x9F.
        if (currentByte === 0xed) {
          upperBoundary = 0x9f;
        }
 
        bytesNeeded = 2;
        codePoint = currentByte & 0xf;
        continue;
      }
 
      // 4 Byte character
      if (inRange(currentByte, 0xf0, 0xf4)) {
        // If byte is 0xF0, set utf-8 lower boundary to 0x90.
        if (currentByte === 0xf0) {
          lowerBoundary = 0x90;
        }
        // If byte is 0xF4, set utf-8 upper boundary to 0x8F.
        if (currentByte === 0xf4) {
          upperBoundary = 0x8f;
        }
 
        bytesNeeded = 3;
        codePoint = currentByte & 0x7;
        continue;
      }
 
      throw new RuntimeError("String decoding failed.");
    }
 
    // Out of range, so ignore the first part(s) of the character and continue with this byte on its own
    if (!inRange(currentByte, lowerBoundary, upperBoundary)) {
      codePoint = bytesNeeded = bytesSeen = 0;
      lowerBoundary = 0x80;
      upperBoundary = 0xbf;
      --i;
      continue;
    }
 
    // Set appropriate boundaries, since we've now checked byte 2 of a potential longer character
    lowerBoundary = 0x80;
    upperBoundary = 0xbf;
 
    // Add byte to code point
    codePoint = (codePoint << 6) | (currentByte & 0x3f);
 
    // We have the correct number of bytes, so push and reset for next character
    ++bytesSeen;
    if (bytesSeen === bytesNeeded) {
      codePoints.push(codePoint);
      codePoint = bytesNeeded = bytesSeen = 0;
    }
  }
 
  return codePoints;
}
 
if (typeof TextDecoder !== "undefined") {
  getStringFromTypedArray.decode =
    getStringFromTypedArray.decodeWithTextDecoder;
} else {
  getStringFromTypedArray.decode =
    getStringFromTypedArray.decodeWithFromCharCode;
}
export default getStringFromTypedArray;