ComposingNormalizer.mjs (7037B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import wasm from "./diplomat-wasm.mjs"; 5 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 6 7 8 /** 9 * See the [Rust documentation for `ComposingNormalizer`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizer.html) for more information. 10 */ 11 const ComposingNormalizer_box_destroy_registry = new FinalizationRegistry((ptr) => { 12 wasm.icu4x_ComposingNormalizer_destroy_mv1(ptr); 13 }); 14 15 export class ComposingNormalizer { 16 // Internal ptr reference: 17 #ptr = null; 18 19 // Lifetimes are only to keep dependencies alive. 20 // Since JS won't garbage collect until there are no incoming edges. 21 #selfEdge = []; 22 23 #internalConstructor(symbol, ptr, selfEdge) { 24 if (symbol !== diplomatRuntime.internalConstructor) { 25 console.error("ComposingNormalizer is an Opaque type. You cannot call its constructor."); 26 return; 27 } 28 this.#ptr = ptr; 29 this.#selfEdge = selfEdge; 30 31 // Are we being borrowed? If not, we can register. 32 if (this.#selfEdge.length === 0) { 33 ComposingNormalizer_box_destroy_registry.register(this, this.#ptr); 34 } 35 36 return this; 37 } 38 get ffiValue() { 39 return this.#ptr; 40 } 41 42 43 /** 44 * Construct a new ComposingNormalizer instance for NFC using compiled data. 45 * 46 * See the [Rust documentation for `new_nfc`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizer.html#method.new_nfc) for more information. 47 */ 48 static createNfc() { 49 50 const result = wasm.icu4x_ComposingNormalizer_create_nfc_mv1(); 51 52 try { 53 return new ComposingNormalizer(diplomatRuntime.internalConstructor, result, []); 54 } 55 56 finally { 57 } 58 } 59 60 /** 61 * Construct a new ComposingNormalizer instance for NFC using a particular data source. 62 * 63 * See the [Rust documentation for `new_nfc`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizer.html#method.new_nfc) for more information. 64 */ 65 static createNfcWithProvider(provider) { 66 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 67 68 69 const result = wasm.icu4x_ComposingNormalizer_create_nfc_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 70 71 try { 72 if (!diplomatReceive.resultFlag) { 73 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 74 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 75 } 76 return new ComposingNormalizer(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 77 } 78 79 finally { 80 diplomatReceive.free(); 81 } 82 } 83 84 /** 85 * Construct a new ComposingNormalizer instance for NFKC using compiled data. 86 * 87 * See the [Rust documentation for `new_nfkc`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizer.html#method.new_nfkc) for more information. 88 */ 89 static createNfkc() { 90 91 const result = wasm.icu4x_ComposingNormalizer_create_nfkc_mv1(); 92 93 try { 94 return new ComposingNormalizer(diplomatRuntime.internalConstructor, result, []); 95 } 96 97 finally { 98 } 99 } 100 101 /** 102 * Construct a new ComposingNormalizer instance for NFKC using a particular data source. 103 * 104 * See the [Rust documentation for `new_nfkc`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizer.html#method.new_nfkc) for more information. 105 */ 106 static createNfkcWithProvider(provider) { 107 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 108 109 110 const result = wasm.icu4x_ComposingNormalizer_create_nfkc_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 111 112 try { 113 if (!diplomatReceive.resultFlag) { 114 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 115 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 116 } 117 return new ComposingNormalizer(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 118 } 119 120 finally { 121 diplomatReceive.free(); 122 } 123 } 124 125 /** 126 * Normalize a string 127 * 128 * Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according 129 * to the WHATWG Encoding Standard. 130 * 131 * See the [Rust documentation for `normalize_utf8`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizerBorrowed.html#method.normalize_utf8) for more information. 132 */ 133 normalize(s) { 134 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 135 136 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 137 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 138 139 wasm.icu4x_ComposingNormalizer_normalize_mv1(this.ffiValue, ...sSlice.splat(), write.buffer); 140 141 try { 142 return write.readString8(); 143 } 144 145 finally { 146 functionCleanupArena.free(); 147 148 write.free(); 149 } 150 } 151 152 /** 153 * Check if a string is normalized 154 * 155 * Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according 156 * to the WHATWG Encoding Standard. 157 * 158 * See the [Rust documentation for `is_normalized_utf16`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizerBorrowed.html#method.is_normalized_utf16) for more information. 159 */ 160 isNormalized(s) { 161 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 162 163 const sSlice = diplomatRuntime.DiplomatBuf.str16(wasm, s); 164 165 const result = wasm.icu4x_ComposingNormalizer_is_normalized_utf16_mv1(this.ffiValue, ...sSlice.splat()); 166 167 try { 168 return result; 169 } 170 171 finally { 172 functionCleanupArena.free(); 173 174 } 175 } 176 177 /** 178 * Return the index a slice of potentially-invalid UTF-16 is normalized up to 179 * 180 * See the [Rust documentation for `split_normalized_utf16`](https://docs.rs/icu/latest/icu/normalizer/struct.ComposingNormalizerBorrowed.html#method.split_normalized_utf16) for more information. 181 */ 182 isNormalizedUpTo(s) { 183 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 184 185 const sSlice = diplomatRuntime.DiplomatBuf.str16(wasm, s); 186 187 const result = wasm.icu4x_ComposingNormalizer_is_normalized_utf16_up_to_mv1(this.ffiValue, ...sSlice.splat()); 188 189 try { 190 return result; 191 } 192 193 finally { 194 functionCleanupArena.free(); 195 196 } 197 } 198 199 constructor(symbol, ptr, selfEdge) { 200 return this.#internalConstructor(...arguments) 201 } 202 }