GraphemeClusterSegmenter.mjs (4553B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { GraphemeClusterBreakIteratorUtf16 } from "./GraphemeClusterBreakIteratorUtf16.mjs" 5 import wasm from "./diplomat-wasm.mjs"; 6 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 7 8 9 /** 10 * An ICU4X grapheme-cluster-break segmenter, capable of finding grapheme cluster breakpoints 11 * in strings. 12 * 13 * See the [Rust documentation for `GraphemeClusterSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html) for more information. 14 */ 15 const GraphemeClusterSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => { 16 wasm.icu4x_GraphemeClusterSegmenter_destroy_mv1(ptr); 17 }); 18 19 export class GraphemeClusterSegmenter { 20 // Internal ptr reference: 21 #ptr = null; 22 23 // Lifetimes are only to keep dependencies alive. 24 // Since JS won't garbage collect until there are no incoming edges. 25 #selfEdge = []; 26 27 #internalConstructor(symbol, ptr, selfEdge) { 28 if (symbol !== diplomatRuntime.internalConstructor) { 29 console.error("GraphemeClusterSegmenter is an Opaque type. You cannot call its constructor."); 30 return; 31 } 32 this.#ptr = ptr; 33 this.#selfEdge = selfEdge; 34 35 // Are we being borrowed? If not, we can register. 36 if (this.#selfEdge.length === 0) { 37 GraphemeClusterSegmenter_box_destroy_registry.register(this, this.#ptr); 38 } 39 40 return this; 41 } 42 get ffiValue() { 43 return this.#ptr; 44 } 45 46 47 /** 48 * Construct an [`GraphemeClusterSegmenter`] using compiled data. 49 * 50 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.new) for more information. 51 */ 52 #defaultConstructor() { 53 54 const result = wasm.icu4x_GraphemeClusterSegmenter_create_mv1(); 55 56 try { 57 return new GraphemeClusterSegmenter(diplomatRuntime.internalConstructor, result, []); 58 } 59 60 finally { 61 } 62 } 63 64 /** 65 * Construct an [`GraphemeClusterSegmenter`]. 66 * 67 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.new) for more information. 68 */ 69 static createWithProvider(provider) { 70 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 71 72 73 const result = wasm.icu4x_GraphemeClusterSegmenter_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 74 75 try { 76 if (!diplomatReceive.resultFlag) { 77 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 78 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 79 } 80 return new GraphemeClusterSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 81 } 82 83 finally { 84 diplomatReceive.free(); 85 } 86 } 87 88 /** 89 * Segments a string. 90 * 91 * Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according 92 * to the WHATWG Encoding Standard. 93 * 94 * See the [Rust documentation for `segment_utf16`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenterBorrowed.html#method.segment_utf16) for more information. 95 */ 96 segment(input) { 97 let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip(); 98 const inputSlice = diplomatRuntime.DiplomatBuf.str16(wasm, input); 99 // This lifetime edge depends on lifetimes 'a 100 let aEdges = [this, inputSlice]; 101 102 103 const result = wasm.icu4x_GraphemeClusterSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat()); 104 105 try { 106 return new GraphemeClusterBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges); 107 } 108 109 finally { 110 functionGarbageCollectorGrip.releaseToGarbageCollector(); 111 112 } 113 } 114 115 constructor() { 116 if (arguments[0] === diplomatRuntime.exposeConstructor) { 117 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 118 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 119 return this.#internalConstructor(...arguments); 120 } else { 121 return this.#defaultConstructor(...arguments); 122 } 123 } 124 }