1"use strict"; 2/* 3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16Object.defineProperty(exports, "__esModule", { value: true }); 17exports.CustomTextDecoder = exports.CustomTextEncoder = void 0; 18class CustomTextEncoder { 19 constructor(encoder = ((typeof TextEncoder != "undefined") ? new TextEncoder() : undefined)) { 20 this.encoder = encoder; 21 } 22 static stringLength(input) { 23 let length = 0; 24 for (let i = 0; i < input.length; i++) { 25 length++; 26 let cp = input.codePointAt(i); 27 if (cp >= 0x10000) { 28 i++; 29 } 30 } 31 return length; 32 } 33 encodedLength(input) { 34 let length = 0; 35 for (let i = 0; i < input.length; i++) { 36 let cp = input.codePointAt(i); 37 if (cp < 0x80) { 38 length += 1; 39 } 40 else if (cp < 0x800) { 41 length += 2; 42 } 43 else if (cp < 0x10000) { 44 length += 3; 45 } 46 else { 47 length += 4; 48 i++; 49 } 50 } 51 return length; 52 } 53 addLength(array, offset, len) { 54 array[offset] = (len & 0xff); 55 array[offset + 1] = ((len >> 8) & 0xff); 56 array[offset + 2] = ((len >> 16) & 0xff); 57 array[offset + 3] = ((len >> 24) & 0xff); 58 } 59 static getHeaderLength(array, offset = 0) { 60 return (array[offset] | (array[offset + 1] << 8) | (array[offset + 2] << 16) | (array[offset + 3] << 24)); 61 } 62 // Produces array of bytes with encoded string headed by 4 bytes (little endian) size information: 63 // [s0][s1][s2][s3] [c_0] ... [c_size-1] 64 encode(input, addLength = true) { 65 let headerLen = addLength ? CustomTextEncoder.HeaderLen : 0; 66 let result; 67 if (!input) { 68 result = new Uint8Array(headerLen); 69 } 70 else if (this.encoder !== undefined) { 71 result = this.encoder.encode('s'.repeat(headerLen) + input); 72 } 73 else { 74 let length = this.encodedLength(input); 75 result = new Uint8Array(length + headerLen); 76 this.encodeInto(input, result, headerLen); 77 } 78 if (addLength) { 79 this.addLength(result, 0, result.length - headerLen); 80 } 81 return result; 82 } 83 // Produces encoded array of strings with size information. 84 encodeArray(strings) { 85 let totalBytes = CustomTextEncoder.HeaderLen; 86 let lengths = new Int32Array(strings.length); 87 for (let i = 0; i < lengths.length; i++) { 88 let len = this.encodedLength(strings[i]); 89 lengths[i] = len; 90 totalBytes += len + CustomTextEncoder.HeaderLen; 91 } 92 let array = new Uint8Array(totalBytes); 93 let position = 0; 94 this.addLength(array, position, lengths.length); 95 position += CustomTextEncoder.HeaderLen; 96 for (let i = 0; i < lengths.length; i++) { 97 this.addLength(array, position, lengths[i]); 98 position += CustomTextEncoder.HeaderLen; 99 this.encodeInto(strings[i], array, position); 100 position += lengths[i]; 101 } 102 return array; 103 } 104 encodeInto(input, result, position) { 105 if (this.encoder !== undefined) { 106 this.encoder.encodeInto(input, result.subarray(position, result.length)); 107 return result; 108 } 109 let index = position; 110 for (let stringPosition = 0; stringPosition < input.length; stringPosition++) { 111 let cp = input.codePointAt(stringPosition); 112 if (cp < 0x80) { 113 result[index++] = (cp | 0); 114 } 115 else if (cp < 0x800) { 116 result[index++] = ((cp >> 6) | 0xc0); 117 result[index++] = ((cp & 0x3f) | 0x80); 118 } 119 else if (cp < 0x10000) { 120 result[index++] = ((cp >> 12) | 0xe0); 121 result[index++] = (((cp >> 6) & 0x3f) | 0x80); 122 result[index++] = ((cp & 0x3f) | 0x80); 123 } 124 else { 125 result[index++] = ((cp >> 18) | 0xf0); 126 result[index++] = (((cp >> 12) & 0x3f) | 0x80); 127 result[index++] = (((cp >> 6) & 0x3f) | 0x80); 128 result[index++] = ((cp & 0x3f) | 0x80); 129 stringPosition++; 130 } 131 } 132 result[index] = 0; 133 return result; 134 } 135} 136exports.CustomTextEncoder = CustomTextEncoder; 137CustomTextEncoder.HeaderLen = Int32Array.BYTES_PER_ELEMENT; 138class CustomTextDecoder { 139 constructor(decoder = ((typeof TextDecoder != "undefined") ? new TextDecoder() : undefined)) { 140 this.decoder = decoder; 141 } 142 decode(input) { 143 if (this.decoder !== undefined) { 144 return this.decoder.decode(input); 145 } 146 const cpSize = Math.min(CustomTextDecoder.cpArrayMaxSize, input.length); 147 let codePoints = new Int32Array(cpSize); 148 let cpIndex = 0; 149 let index = 0; 150 let result = ""; 151 while (index < input.length) { 152 let elem = input[index]; 153 let lead = elem & 0xff; 154 let count = 0; 155 let value = 0; 156 if (lead < 0x80) { 157 count = 1; 158 value = elem; 159 } 160 else if ((lead >> 5) == 0x6) { 161 value = ((elem << 6) & 0x7ff) + (input[index + 1] & 0x3f); 162 count = 2; 163 } 164 else if ((lead >> 4) == 0xe) { 165 value = ((elem << 12) & 0xffff) + ((input[index + 1] << 6) & 0xfff) + 166 (input[index + 2] & 0x3f); 167 count = 3; 168 } 169 else if ((lead >> 3) == 0x1e) { 170 value = ((elem << 18) & 0x1fffff) + ((input[index + 1] << 12) & 0x3ffff) + 171 ((input[index + 2] << 6) & 0xfff) + (input[index + 3] & 0x3f); 172 count = 4; 173 } 174 codePoints[cpIndex++] = value; 175 if (cpIndex == cpSize) { 176 cpIndex = 0; 177 result += String.fromCodePoint(...codePoints); 178 } 179 index += count; 180 } 181 if (cpIndex > 0) { 182 result += String.fromCodePoint(...codePoints.slice(0, cpIndex)); 183 } 184 return result; 185 } 186} 187exports.CustomTextDecoder = CustomTextDecoder; 188CustomTextDecoder.cpArrayMaxSize = 128; 189//# sourceMappingURL=strings.js.map