1/* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package std.core; 17 18type NullableObject = Object | null 19 20/** 21 * Performance-oriented class for string construction 22 */ 23export final class StringBuilder { 24 private static readonly INITIAL_BUF_SIZE: int = 16; 25 26 private buf: NullableObject[] = new NullableObject[StringBuilder.INITIAL_BUF_SIZE]; // array with pointers to strings or char[] 27 private index: int = 0; // index of the current free element in the buf 28 private length: int = 0; // length of the resulting string 29 private compress: boolean = true; // compress or not the resulting string. 30 31 /** 32 * Construct a new builder instance with the initial buffer of 16 chars 33 */ 34 constructor() {} 35 36 /** 37 * Construct a new builder instance with the provided array of chars 38 * 39 * @param fromChars array that will be used to initialize the builder 40 */ 41 constructor(fromChars: char[]) { 42 let s: String = new String(fromChars); 43 this.buf[this.index++] = s; 44 this.length = s.getLength(); 45 if (!s.isCompressed()) { 46 this.compress = false; 47 } 48 } 49 50 /** 51 * Constructs a new builder instance with provided string 52 * 53 * @param s string that will be used to initialize the builder 54 */ 55 public constructor (s: String) { 56 this.buf[this.index++] = s; 57 this.length = s.getLength(); 58 if (!s.isCompressed()) { 59 this.compress = false; 60 } 61 } 62 63 /** 64 * Concatenates two strings and return a result as a new string 65 * 66 * @param lhs left string (prefix) 67 * 68 * @param rhs right string (suffix) 69 * 70 * @returns result of concatenation 71 */ 72 public static native concatStrings(lhs: String, rhs: String): String; 73 74 /** 75 * Appends an object representation as string to the builder's internal buffer 76 * 77 * @param o object that will be converted to a string 78 * 79 * @returns builder with updated internal buffer 80 */ 81 public append(o: Object): StringBuilder { 82 this.append(o.toString()); 83 return this; 84 } 85 86 /** 87 * Appends a string to the builder's internal buffer 88 * 89 * @param s string to be appended 90 * 91 * @returns builder with updated internal buffer 92 */ 93 public native append(s: String): StringBuilder; 94 95 private native append(s0: String, s1: String): StringBuilder; 96 private native append(s0: String, s1: String, s2: String): StringBuilder; 97 private native append(s0: String, s1: String, s2: String, s3: String): StringBuilder; 98 99 /** 100 * Appends a boolean as string to the builder's internal buffer 101 * 102 * @param i value to be appended 103 * 104 * @returns builder with updated internal buffer 105 */ 106 public native append(i: boolean): StringBuilder; 107 108 /** 109 * Appends a byte as string to the builder's internal buffer 110 * 111 * @param i value to be appended 112 * 113 * @returns builder with updated internal buffer 114 */ 115 public native append(i: byte): StringBuilder; 116 117 /** 118 * Appends a short as string to the builder's internal buffer 119 * 120 * @param i value to be appended 121 * 122 * @returns builder with updated internal buffer 123 */ 124 public native append(i: short): StringBuilder; 125 126 /** 127 * Appends a char to the builder's internal buffer 128 * 129 * @param i value to be appended 130 * 131 * @returns builder with updated internal buffer 132 */ 133 public native append(i: char): StringBuilder; 134 135 /** 136 * Appends an int as string to the builder's internal buffer 137 * 138 * @param i value to be appended 139 * 140 * @returns builder with updated internal buffer 141 */ 142 public native append(i: int): StringBuilder; 143 144 /** 145 * Appends a long as string to the builder's internal buffer 146 * 147 * @param i value to be appended 148 * 149 * @returns builder with updated internal buffer 150 */ 151 public native append(i: long): StringBuilder; 152 153 /** 154 * Appends a float as string to the builder's internal buffer 155 * 156 * @param i value to be appended 157 * 158 * @returns builder with updated internal buffer 159 */ 160 public native append(i: float): StringBuilder; 161 162 /** 163 * Appends a double as string to the builder's internal buffer 164 * 165 * @param i value to be appended 166 * 167 * @returns builder with updated internal buffer 168 */ 169 public native append(i: double): StringBuilder; 170 171 /** 172 * Returns the string that was formed as a result of all append operations 173 * 174 * @see append 175 * 176 * @returns String - builder's current buffer as string 177 */ 178 public native override toString(): String; 179 180 /** 181 * Converts the primitive to a string 182 * 183 * @param i value to be converted 184 * 185 * @returns result of the conversion 186 */ 187 public static native toString(i: boolean): String; 188 189 /** 190 * Converts the primitive to a string 191 * 192 * @param i value to be converted 193 * 194 * @returns result of the conversion 195 */ 196 public static native toString(i: byte): String; 197 198 /** 199 * Converts the primitive to a string 200 * 201 * @param i value to be converted 202 * 203 * @returns result of the conversion 204 */ 205 public static native toString(i: short): String; 206 207 /** 208 * Converts the primitive to a string 209 * 210 * @param i value to be converted 211 * 212 * @returns result of the conversion 213 */ 214 public static native toString(i: char): String; 215 216 /** 217 * Converts the primitive to a string 218 * 219 * @param i value to be converted 220 * 221 * @returns result of the conversion 222 */ 223 public static native toString(i: int): String; 224 225 /** 226 * Converts the primitive to a string 227 * 228 * @param i value to be converted 229 * 230 * @returns result of the conversion 231 */ 232 public static native toString(i: long): String; 233 234 /** 235 * Converts the primitive to a string 236 * 237 * @param i value to be converted 238 * 239 * @returns result of the conversion 240 */ 241 public static toString(f: float): String { 242 return Float.toString(f); 243 } 244 245 /** 246 * Converts the primitive to a string 247 * 248 * @param i value to be converted 249 * 250 * @returns result of the conversion 251 */ 252 public static toString(d: double): String { 253 return Double.toString(d); 254 } 255 256} 257 258final class DoubleToStringCacheElement { 259 string: string = ""; 260 // `lock` here and below is internal field used to provide atomicity 261 // and to align `number` by 8 bytes 262 lock: int; 263 number: double; 264} 265 266final class FloatToStringCacheElement { 267 string: string = ""; 268 lock: int; 269 number: float; 270} 271 272final class LongToStringCacheElement { 273 string: string = ""; 274 lock: int; 275 number: long; 276} 277