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 /** 96 * Appends a boolean as string to the builder's internal buffer 97 * 98 * @param i value to be appended 99 * 100 * @returns builder with updated internal buffer 101 */ 102 public native append(i: boolean): StringBuilder; 103 104 /** 105 * Appends a byte as string to the builder's internal buffer 106 * 107 * @param i value to be appended 108 * 109 * @returns builder with updated internal buffer 110 */ 111 public native append(i: byte): StringBuilder; 112 113 /** 114 * Appends a short as string to the builder's internal buffer 115 * 116 * @param i value to be appended 117 * 118 * @returns builder with updated internal buffer 119 */ 120 public native append(i: short): StringBuilder; 121 122 /** 123 * Appends a char to the builder's internal buffer 124 * 125 * @param i value to be appended 126 * 127 * @returns builder with updated internal buffer 128 */ 129 public native append(i: char): StringBuilder; 130 131 /** 132 * Appends an int as string to the builder's internal buffer 133 * 134 * @param i value to be appended 135 * 136 * @returns builder with updated internal buffer 137 */ 138 public native append(i: int): StringBuilder; 139 140 /** 141 * Appends a long as string to the builder's internal buffer 142 * 143 * @param i value to be appended 144 * 145 * @returns builder with updated internal buffer 146 */ 147 public native append(i: long): StringBuilder; 148 149 /** 150 * Appends a float as string to the builder's internal buffer 151 * 152 * @param i value to be appended 153 * 154 * @returns builder with updated internal buffer 155 */ 156 public native append(i: float): StringBuilder; 157 158 /** 159 * Appends a double as string to the builder's internal buffer 160 * 161 * @param i value to be appended 162 * 163 * @returns builder with updated internal buffer 164 */ 165 public native append(i: double): StringBuilder; 166 167 /** 168 * Returns the string that was formed as a result of all append operations 169 * 170 * @see append 171 * 172 * @returns String - builder's current buffer as string 173 */ 174 public native override toString(): String; 175 176 /** 177 * Converts the primitive to a string 178 * 179 * @param i value to be converted 180 * 181 * @returns result of the conversion 182 */ 183 public static native toString(i: boolean): String; 184 185 /** 186 * Converts the primitive to a string 187 * 188 * @param i value to be converted 189 * 190 * @returns result of the conversion 191 */ 192 public static native toString(i: byte): String; 193 194 /** 195 * Converts the primitive to a string 196 * 197 * @param i value to be converted 198 * 199 * @returns result of the conversion 200 */ 201 public static native toString(i: short): String; 202 203 /** 204 * Converts the primitive to a string 205 * 206 * @param i value to be converted 207 * 208 * @returns result of the conversion 209 */ 210 public static native toString(i: char): String; 211 212 /** 213 * Converts the primitive to a string 214 * 215 * @param i value to be converted 216 * 217 * @returns result of the conversion 218 */ 219 public static native toString(i: int): String; 220 221 /** 222 * Converts the primitive to a string 223 * 224 * @param i value to be converted 225 * 226 * @returns result of the conversion 227 */ 228 public static native toString(i: long): String; 229 230 /** 231 * Converts the primitive to a string 232 * 233 * @param i value to be converted 234 * 235 * @returns result of the conversion 236 */ 237 public static toString(f: float): String { 238 return Float.toString(f); 239 } 240 241 /** 242 * Converts the primitive to a string 243 * 244 * @param i value to be converted 245 * 246 * @returns result of the conversion 247 */ 248 public static toString(d: double): String { 249 return Double.toString(d); 250 } 251 252} 253 254final class DoubleToStringCacheElement { 255 string: string; 256 // `lock` here and below is internal field used to provide atomicity 257 // and to align `number` by 8 bytes 258 lock: int; 259 number: double; 260} 261 262final class FloatToStringCacheElement { 263 string: string; 264 lock: int; 265 number: float; 266} 267 268final class LongToStringCacheElement { 269 string: string; 270 lock: int; 271 number: long; 272} 273