tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

LineSegmenter.mjs (11486B)


      1 // generated by diplomat-tool
      2 import { DataError } from "./DataError.mjs"
      3 import { DataProvider } from "./DataProvider.mjs"
      4 import { LineBreakIteratorUtf16 } from "./LineBreakIteratorUtf16.mjs"
      5 import { LineBreakOptions } from "./LineBreakOptions.mjs"
      6 import { Locale } from "./Locale.mjs"
      7 import wasm from "./diplomat-wasm.mjs";
      8 import * as diplomatRuntime from "./diplomat-runtime.mjs";
      9 
     10 
     11 /**
     12 * An ICU4X line-break segmenter, capable of finding breakpoints in strings.
     13 *
     14 * See the [Rust documentation for `LineSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html) for more information.
     15 */
     16 const LineSegmenter_box_destroy_registry = new FinalizationRegistry((ptr) => {
     17    wasm.icu4x_LineSegmenter_destroy_mv1(ptr);
     18 });
     19 
     20 export class LineSegmenter {
     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        this.#ptr = ptr;
     34        this.#selfEdge = selfEdge;
     35 
     36        // Are we being borrowed? If not, we can register.
     37        if (this.#selfEdge.length === 0) {
     38            LineSegmenter_box_destroy_registry.register(this, this.#ptr);
     39        }
     40 
     41        return this;
     42    }
     43    get ffiValue() {
     44        return this.#ptr;
     45    }
     46 
     47 
     48    /**
     49     * Construct a [`LineSegmenter`] with default options (no locale-based tailoring) using compiled data. It automatically loads the best
     50     * available payload data for Burmese, Khmer, Lao, and Thai.
     51     *
     52     * See the [Rust documentation for `new_auto`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_auto) for more information.
     53     */
     54    static createAuto() {
     55 
     56        const result = wasm.icu4x_LineSegmenter_create_auto_mv1();
     57 
     58        try {
     59            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
     60        }
     61 
     62        finally {
     63        }
     64    }
     65 
     66    /**
     67     * Construct a [`LineSegmenter`] with default options (no locale-based tailoring) and LSTM payload data for
     68     * Burmese, Khmer, Lao, and Thai, using compiled data.
     69     *
     70     * See the [Rust documentation for `new_lstm`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_lstm) for more information.
     71     */
     72    static createLstm() {
     73 
     74        const result = wasm.icu4x_LineSegmenter_create_lstm_mv1();
     75 
     76        try {
     77            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
     78        }
     79 
     80        finally {
     81        }
     82    }
     83 
     84    /**
     85     * Construct a [`LineSegmenter`] with default options (no locale-based tailoring) and dictionary payload data for
     86     * Burmese, Khmer, Lao, and Thai, using compiled data
     87     *
     88     * See the [Rust documentation for `new_dictionary`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_dictionary) for more information.
     89     */
     90    static createDictionary() {
     91 
     92        const result = wasm.icu4x_LineSegmenter_create_dictionary_mv1();
     93 
     94        try {
     95            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
     96        }
     97 
     98        finally {
     99        }
    100    }
    101 
    102    /**
    103     * Construct a [`LineSegmenter`] with custom options using compiled data. It automatically loads the best
    104     * available payload data for Burmese, Khmer, Lao, and Thai.
    105     *
    106     * See the [Rust documentation for `new_auto`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_auto) for more information.
    107     */
    108    static autoWithOptions(contentLocale, options) {
    109        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    110 
    111 
    112        const result = wasm.icu4x_LineSegmenter_create_auto_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
    113 
    114        try {
    115            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
    116        }
    117 
    118        finally {
    119            functionCleanupArena.free();
    120 
    121        }
    122    }
    123 
    124    /**
    125     * Construct a [`LineSegmenter`] with custom options. It automatically loads the best
    126     * available payload data for Burmese, Khmer, Lao, and Thai, using a particular data source.
    127     *
    128     * See the [Rust documentation for `new_auto`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_auto) for more information.
    129     */
    130    static autoWithOptionsAndProvider(provider, contentLocale, options) {
    131        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    132 
    133        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
    134 
    135 
    136        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, {}));
    137 
    138        try {
    139            if (!diplomatReceive.resultFlag) {
    140                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
    141                throw new globalThis.Error('DataError: ' + cause.value, { cause });
    142            }
    143            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
    144        }
    145 
    146        finally {
    147            functionCleanupArena.free();
    148 
    149            diplomatReceive.free();
    150        }
    151    }
    152 
    153    /**
    154     * Construct a [`LineSegmenter`] with custom options and LSTM payload data for
    155     * Burmese, Khmer, Lao, and Thai, using compiled data.
    156     *
    157     * See the [Rust documentation for `new_lstm`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_lstm) for more information.
    158     */
    159    static lstmWithOptions(contentLocale, options) {
    160        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    161 
    162 
    163        const result = wasm.icu4x_LineSegmenter_create_lstm_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
    164 
    165        try {
    166            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
    167        }
    168 
    169        finally {
    170            functionCleanupArena.free();
    171 
    172        }
    173    }
    174 
    175    /**
    176     * Construct a [`LineSegmenter`] with custom options and LSTM payload data for
    177     * Burmese, Khmer, Lao, and Thai, using a particular data source.
    178     *
    179     * See the [Rust documentation for `new_lstm`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_lstm) for more information.
    180     */
    181    static lstmWithOptionsAndProvider(provider, contentLocale, options) {
    182        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    183 
    184        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
    185 
    186 
    187        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, {}));
    188 
    189        try {
    190            if (!diplomatReceive.resultFlag) {
    191                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
    192                throw new globalThis.Error('DataError: ' + cause.value, { cause });
    193            }
    194            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
    195        }
    196 
    197        finally {
    198            functionCleanupArena.free();
    199 
    200            diplomatReceive.free();
    201        }
    202    }
    203 
    204    /**
    205     * Construct a [`LineSegmenter`] with custom options and dictionary payload data for
    206     * Burmese, Khmer, Lao, and Thai, using compiled data.
    207     *
    208     * See the [Rust documentation for `new_dictionary`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_dictionary) for more information.
    209     */
    210    static dictionaryWithOptions(contentLocale, options) {
    211        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    212 
    213 
    214        const result = wasm.icu4x_LineSegmenter_create_dictionary_with_options_v2_mv1(contentLocale.ffiValue ?? 0, ...LineBreakOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}));
    215 
    216        try {
    217            return new LineSegmenter(diplomatRuntime.internalConstructor, result, []);
    218        }
    219 
    220        finally {
    221            functionCleanupArena.free();
    222 
    223        }
    224    }
    225 
    226    /**
    227     * Construct a [`LineSegmenter`] with custom options and dictionary payload data for
    228     * Burmese, Khmer, Lao, and Thai, using a particular data source.
    229     *
    230     * See the [Rust documentation for `new_dictionary`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenter.html#method.new_dictionary) for more information.
    231     */
    232    static dictionaryWithOptionsAndProvider(provider, contentLocale, options) {
    233        let functionCleanupArena = new diplomatRuntime.CleanupArena();
    234 
    235        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
    236 
    237 
    238        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, {}));
    239 
    240        try {
    241            if (!diplomatReceive.resultFlag) {
    242                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
    243                throw new globalThis.Error('DataError: ' + cause.value, { cause });
    244            }
    245            return new LineSegmenter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
    246        }
    247 
    248        finally {
    249            functionCleanupArena.free();
    250 
    251            diplomatReceive.free();
    252        }
    253    }
    254 
    255    /**
    256     * Segments a string.
    257     *
    258     * Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
    259     * to the WHATWG Encoding Standard.
    260     *
    261     * See the [Rust documentation for `segment_utf16`](https://docs.rs/icu/latest/icu/segmenter/struct.LineSegmenterBorrowed.html#method.segment_utf16) for more information.
    262     */
    263    segment(input) {
    264        let functionGarbageCollectorGrip = new diplomatRuntime.GarbageCollectorGrip();
    265        const inputSlice = diplomatRuntime.DiplomatBuf.str16(wasm, input);
    266        // This lifetime edge depends on lifetimes 'a
    267        let aEdges = [this, inputSlice];
    268 
    269 
    270        const result = wasm.icu4x_LineSegmenter_segment_utf16_mv1(this.ffiValue, ...inputSlice.splat());
    271 
    272        try {
    273            return new LineBreakIteratorUtf16(diplomatRuntime.internalConstructor, result, [], aEdges);
    274        }
    275 
    276        finally {
    277            functionGarbageCollectorGrip.releaseToGarbageCollector();
    278 
    279        }
    280    }
    281 
    282    constructor(symbol, ptr, selfEdge) {
    283        return this.#internalConstructor(...arguments)
    284    }
    285 }