CaseMapper.mjs (13357B)
1 // generated by diplomat-tool 2 import { CodePointSetBuilder } from "./CodePointSetBuilder.mjs" 3 import { DataError } from "./DataError.mjs" 4 import { DataProvider } from "./DataProvider.mjs" 5 import { Locale } from "./Locale.mjs" 6 import { TitlecaseOptions } from "./TitlecaseOptions.mjs" 7 import wasm from "./diplomat-wasm.mjs"; 8 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 9 10 11 /** 12 * See the [Rust documentation for `CaseMapper`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapper.html) for more information. 13 */ 14 const CaseMapper_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_CaseMapper_destroy_mv1(ptr); 16 }); 17 18 export class CaseMapper { 19 // Internal ptr reference: 20 #ptr = null; 21 22 // Lifetimes are only to keep dependencies alive. 23 // Since JS won't garbage collect until there are no incoming edges. 24 #selfEdge = []; 25 26 #internalConstructor(symbol, ptr, selfEdge) { 27 if (symbol !== diplomatRuntime.internalConstructor) { 28 console.error("CaseMapper is an Opaque type. You cannot call its constructor."); 29 return; 30 } 31 this.#ptr = ptr; 32 this.#selfEdge = selfEdge; 33 34 // Are we being borrowed? If not, we can register. 35 if (this.#selfEdge.length === 0) { 36 CaseMapper_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 46 /** 47 * Construct a new CaseMapper instance using compiled data. 48 * 49 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapper.html#method.new) for more information. 50 */ 51 #defaultConstructor() { 52 53 const result = wasm.icu4x_CaseMapper_create_mv1(); 54 55 try { 56 return new CaseMapper(diplomatRuntime.internalConstructor, result, []); 57 } 58 59 finally { 60 } 61 } 62 63 /** 64 * Construct a new CaseMapper instance using a particular data source. 65 * 66 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapper.html#method.new) for more information. 67 */ 68 static createWithProvider(provider) { 69 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 70 71 72 const result = wasm.icu4x_CaseMapper_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 73 74 try { 75 if (!diplomatReceive.resultFlag) { 76 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 77 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 78 } 79 return new CaseMapper(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 80 } 81 82 finally { 83 diplomatReceive.free(); 84 } 85 } 86 87 /** 88 * Returns the full lowercase mapping of the given string 89 * 90 * See the [Rust documentation for `lowercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.lowercase) for more information. 91 */ 92 lowercase(s, locale) { 93 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 94 95 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 96 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 97 98 wasm.icu4x_CaseMapper_lowercase_mv1(this.ffiValue, ...sSlice.splat(), locale.ffiValue, write.buffer); 99 100 try { 101 return write.readString8(); 102 } 103 104 finally { 105 functionCleanupArena.free(); 106 107 write.free(); 108 } 109 } 110 111 /** 112 * Returns the full uppercase mapping of the given string 113 * 114 * See the [Rust documentation for `uppercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.uppercase) for more information. 115 */ 116 uppercase(s, locale) { 117 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 118 119 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 120 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 121 122 wasm.icu4x_CaseMapper_uppercase_mv1(this.ffiValue, ...sSlice.splat(), locale.ffiValue, write.buffer); 123 124 try { 125 return write.readString8(); 126 } 127 128 finally { 129 functionCleanupArena.free(); 130 131 write.free(); 132 } 133 } 134 135 /** 136 * Returns the full lowercase mapping of the given string, using compiled data (avoids having to allocate a CaseMapper object) 137 * 138 * See the [Rust documentation for `lowercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.lowercase) for more information. 139 */ 140 static lowercaseWithCompiledData(s, locale) { 141 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 142 143 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 144 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 145 146 wasm.icu4x_CaseMapper_lowercase_with_compiled_data_mv1(...sSlice.splat(), locale.ffiValue, write.buffer); 147 148 try { 149 return write.readString8(); 150 } 151 152 finally { 153 functionCleanupArena.free(); 154 155 write.free(); 156 } 157 } 158 159 /** 160 * Returns the full uppercase mapping of the given string, using compiled data (avoids having to allocate a CaseMapper object) 161 * 162 * See the [Rust documentation for `uppercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.uppercase) for more information. 163 */ 164 static uppercaseWithCompiledData(s, locale) { 165 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 166 167 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 168 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 169 170 wasm.icu4x_CaseMapper_uppercase_with_compiled_data_mv1(...sSlice.splat(), locale.ffiValue, write.buffer); 171 172 try { 173 return write.readString8(); 174 } 175 176 finally { 177 functionCleanupArena.free(); 178 179 write.free(); 180 } 181 } 182 183 /** 184 * Returns the full titlecase mapping of the given string, performing head adjustment without 185 * loading additional data. 186 * (if head adjustment is enabled in the options) 187 * 188 * The `v1` refers to the version of the options struct, which may change as we add more options 189 * 190 * See the [Rust documentation for `titlecase_segment_with_only_case_data`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.titlecase_segment_with_only_case_data) for more information. 191 */ 192 titlecaseSegmentWithOnlyCaseData(s, locale, options) { 193 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 194 195 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 196 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 197 198 wasm.icu4x_CaseMapper_titlecase_segment_with_only_case_data_v1_mv1(this.ffiValue, ...sSlice.splat(), locale.ffiValue, ...TitlecaseOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}), write.buffer); 199 200 try { 201 return write.readString8(); 202 } 203 204 finally { 205 functionCleanupArena.free(); 206 207 write.free(); 208 } 209 } 210 211 /** 212 * Case-folds the characters in the given string 213 * 214 * See the [Rust documentation for `fold`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.fold) for more information. 215 */ 216 fold(s) { 217 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 218 219 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 220 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 221 222 wasm.icu4x_CaseMapper_fold_mv1(this.ffiValue, ...sSlice.splat(), write.buffer); 223 224 try { 225 return write.readString8(); 226 } 227 228 finally { 229 functionCleanupArena.free(); 230 231 write.free(); 232 } 233 } 234 235 /** 236 * Case-folds the characters in the given string 237 * using Turkic (T) mappings for dotted/dotless I. 238 * 239 * See the [Rust documentation for `fold_turkic`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.fold_turkic) for more information. 240 */ 241 foldTurkic(s) { 242 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 243 244 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 245 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 246 247 wasm.icu4x_CaseMapper_fold_turkic_mv1(this.ffiValue, ...sSlice.splat(), write.buffer); 248 249 try { 250 return write.readString8(); 251 } 252 253 finally { 254 functionCleanupArena.free(); 255 256 write.free(); 257 } 258 } 259 260 /** 261 * Adds all simple case mappings and the full case folding for `c` to `builder`. 262 * Also adds special case closure mappings. 263 * 264 * In other words, this adds all characters that this casemaps to, as 265 * well as all characters that may casemap to this one. 266 * 267 * Note that since CodePointSetBuilder does not contain strings, this will 268 * ignore string mappings. 269 * 270 * Identical to the similarly named method on `CaseMapCloser`, use that if you 271 * plan on using string case closure mappings too. 272 * 273 * See the [Rust documentation for `add_case_closure_to`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.add_case_closure_to) for more information. 274 */ 275 addCaseClosureTo(c, builder) { 276 wasm.icu4x_CaseMapper_add_case_closure_to_mv1(this.ffiValue, c, builder.ffiValue); 277 278 try {} 279 280 finally { 281 } 282 } 283 284 /** 285 * Returns the simple lowercase mapping of the given character. 286 * 287 * This function only implements simple and common mappings. 288 * Full mappings, which can map one char to a string, are not included. 289 * For full mappings, use `CaseMapperBorrowed::lowercase`. 290 * 291 * See the [Rust documentation for `simple_lowercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.simple_lowercase) for more information. 292 */ 293 simpleLowercase(ch) { 294 295 const result = wasm.icu4x_CaseMapper_simple_lowercase_mv1(this.ffiValue, ch); 296 297 try { 298 return result; 299 } 300 301 finally { 302 } 303 } 304 305 /** 306 * Returns the simple uppercase mapping of the given character. 307 * 308 * This function only implements simple and common mappings. 309 * Full mappings, which can map one char to a string, are not included. 310 * For full mappings, use `CaseMapperBorrowed::uppercase`. 311 * 312 * See the [Rust documentation for `simple_uppercase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.simple_uppercase) for more information. 313 */ 314 simpleUppercase(ch) { 315 316 const result = wasm.icu4x_CaseMapper_simple_uppercase_mv1(this.ffiValue, ch); 317 318 try { 319 return result; 320 } 321 322 finally { 323 } 324 } 325 326 /** 327 * Returns the simple titlecase mapping of the given character. 328 * 329 * This function only implements simple and common mappings. 330 * Full mappings, which can map one char to a string, are not included. 331 * For full mappings, use `CaseMapperBorrowed::titlecase_segment`. 332 * 333 * See the [Rust documentation for `simple_titlecase`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.simple_titlecase) for more information. 334 */ 335 simpleTitlecase(ch) { 336 337 const result = wasm.icu4x_CaseMapper_simple_titlecase_mv1(this.ffiValue, ch); 338 339 try { 340 return result; 341 } 342 343 finally { 344 } 345 } 346 347 /** 348 * Returns the simple casefolding of the given character. 349 * 350 * This function only implements simple folding. 351 * For full folding, use `CaseMapperBorrowed::fold`. 352 * 353 * See the [Rust documentation for `simple_fold`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.simple_fold) for more information. 354 */ 355 simpleFold(ch) { 356 357 const result = wasm.icu4x_CaseMapper_simple_fold_mv1(this.ffiValue, ch); 358 359 try { 360 return result; 361 } 362 363 finally { 364 } 365 } 366 367 /** 368 * Returns the simple casefolding of the given character in the Turkic locale 369 * 370 * This function only implements simple folding. 371 * For full folding, use `CaseMapperBorrowed::fold_turkic`. 372 * 373 * See the [Rust documentation for `simple_fold_turkic`](https://docs.rs/icu/latest/icu/casemap/struct.CaseMapperBorrowed.html#method.simple_fold_turkic) for more information. 374 */ 375 simpleFoldTurkic(ch) { 376 377 const result = wasm.icu4x_CaseMapper_simple_fold_turkic_mv1(this.ffiValue, ch); 378 379 try { 380 return result; 381 } 382 383 finally { 384 } 385 } 386 387 constructor() { 388 if (arguments[0] === diplomatRuntime.exposeConstructor) { 389 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 390 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 391 return this.#internalConstructor(...arguments); 392 } else { 393 return this.#defaultConstructor(...arguments); 394 } 395 } 396 }