• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { LineBreakIteratorUtf16 } from "./LineBreakIteratorUtf16.mjs"
5import { LineBreakOptions } from "./LineBreakOptions.mjs"
6import { Locale } from "./Locale.mjs"
7import wasm from "./diplomat-wasm.mjs";
8import * as diplomatRuntime from "./diplomat-runtime.mjs";
9
10
11/** An ICU4X line-break segmenter, capable of finding breakpoints in strings.
12*
13*See the [Rust documentation for `LineSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html) for more information.
14*/
15const LineSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => {
16    wasm.icu4x_LineSegmenter_destroy_mv1(ptr);
17});
18
19export class LineSegmenter {
20
21    // Internal ptr reference:
22    #ptr = null;
23
24    // Lifetimes are only to keep dependencies alive.
25    // Since JS won't garbage collect until there are no incoming edges.
26    #selfEdge = [];
27
28    #internalConstructor(symbol, ptr, selfEdge) {
29        if (symbol !== diplomatRuntime.internalConstructor) {
30            console.error("LineSegmenter is an Opaque type. You cannot call its constructor.");
31            return;
32        }
33
34        this.#ptr = ptr;
35        this.#selfEdge = selfEdge;
36
37        // Are we being borrowed? If not, we can register.
38        if (this.#selfEdge.length === 0) {
39            LineSegmenter_box_destroy_registry.register(this, this.#ptr);
40        }
41
42        return this;
43    }
44    get ffiValue() {
45        return this.#ptr;
46    }
47
48    static createAuto() {
49        const result = wasm.icu4x_LineSegmenter_create_auto_mv1();
50
51        try {
52            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
53        }
54
55        finally {}
56    }
57
58    static createLstm() {
59        const result = wasm.icu4x_LineSegmenter_create_lstm_mv1();
60
61        try {
62            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
63        }
64
65        finally {}
66    }
67
68    static createDictionary() {
69        const result = wasm.icu4x_LineSegmenter_create_dictionary_mv1();
70
71        try {
72            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
73        }
74
75        finally {}
76    }
77
78    static autoWithOptions(contentLocale, options) {
79        let functionCleanupArena = new diplomatRuntime.CleanupArena();
80
81        const result = wasm.icu4x_LineSegmenter_create_auto_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
82
83        try {
84            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
85        }
86
87        finally {
88            functionCleanupArena.free();
89        }
90    }
91
92    static autoWithOptionsAndProvider(provider, contentLocale, options) {
93        let functionCleanupArena = new diplomatRuntime.CleanupArena();
94
95        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
96
97        const result = wasm.icu4x_LineSegmenter_create_auto_with_options_v2_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
98
99        try {
100            if (!diplomatReceive.resultFlag) {
101                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
102                throw new globalThis.Error('DataError: ' + cause.value, { cause });
103            }
104            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
105        }
106
107        finally {
108            functionCleanupArena.free();
109
110            diplomatReceive.free();
111        }
112    }
113
114    static lstmWithOptions(contentLocale, options) {
115        let functionCleanupArena = new diplomatRuntime.CleanupArena();
116
117        const result = wasm.icu4x_LineSegmenter_create_lstm_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
118
119        try {
120            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
121        }
122
123        finally {
124            functionCleanupArena.free();
125        }
126    }
127
128    static lstmWithOptionsAndProvider(provider, contentLocale, options) {
129        let functionCleanupArena = new diplomatRuntime.CleanupArena();
130
131        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
132
133        const result = wasm.icu4x_LineSegmenter_create_lstm_with_options_v2_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
134
135        try {
136            if (!diplomatReceive.resultFlag) {
137                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
138                throw new globalThis.Error('DataError: ' + cause.value, { cause });
139            }
140            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
141        }
142
143        finally {
144            functionCleanupArena.free();
145
146            diplomatReceive.free();
147        }
148    }
149
150    static dictionaryWithOptions(contentLocale, options) {
151        let functionCleanupArena = new diplomatRuntime.CleanupArena();
152
153        const result = wasm.icu4x_LineSegmenter_create_dictionary_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
154
155        try {
156            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
157        }
158
159        finally {
160            functionCleanupArena.free();
161        }
162    }
163
164    static dictionaryWithOptionsAndProvider(provider, contentLocale, options) {
165        let functionCleanupArena = new diplomatRuntime.CleanupArena();
166
167        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
168
169        const result = wasm.icu4x_LineSegmenter_create_dictionary_with_options_v2_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
170
171        try {
172            if (!diplomatReceive.resultFlag) {
173                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
174                throw new globalThis.Error('DataError: ' + cause.value, { cause });
175            }
176            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
177        }
178
179        finally {
180            functionCleanupArena.free();
181
182            diplomatReceive.free();
183        }
184    }
185
186    segment(input) {
187        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
188        const inputSlice = functionGarbageCollectorGrip.alloc(diplomatRuntime.DiplomatBuf.str16(wasm, input));
189
190        // This lifetime edge depends on lifetimes 'a
191        let aEdges = [this, inputSlice];
192
193        const result = wasm.icu4x_LineSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat());
194
195        try {
196            return new LineBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges);
197        }
198
199        finally {
200            functionGarbageCollectorGrip.releaseToGarbageCollector();
201        }
202    }
203
204    constructor(symbol, ptr, selfEdge) {
205        return this.#internalConstructor(...arguments)
206    }
207}