From 801c8a7568da673de3c826a3f1756ded1f08c3e1 Mon Sep 17 00:00:00 2001 From: Francesco Bigiarini Date: Fri, 24 Jul 2026 00:07:14 +0200 Subject: [PATCH] Add mjb_caseless_match (#40) Signed-off-by: Francesco Bigiarini --- API.md | 62 +++++++++- README.md | 3 + ROADMAP.md | 22 +--- TESTS.md | 40 +++--- src/CMakeLists.txt | 2 + src/api/index.ts | 30 +++++ src/api/mojibake.d.ts | 2 + src/api/tests/index.ts | 10 ++ src/case.c | 109 +++++++++++----- src/caseless.c | 199 +++++++++++++++++++++++++++++ src/cpp/mojibake.hpp | 18 +++ src/mojibake-internal.h | 3 + src/mojibake.h | 15 +++ src/site/index.html | 5 +- tests/CMakeLists.txt | 2 + tests/caseless.c | 119 ++++++++++++++++++ tests/example.c | 19 +++ tests/ext/cpp/normalization.cpp | 15 +++ tests/test.c | 1 + tests/test.h | 1 + utils/generate/functions.ts | 122 +++++++++++++++--- utils/generate/html-function.ts | 213 +++++++++++++------------------- utils/generate/types.ts | 109 ++++++++++++++++ 23 files changed, 907 insertions(+), 214 deletions(-) create mode 100644 src/caseless.c create mode 100644 tests/caseless.c diff --git a/API.md b/API.md index 44bb7810..1696c6d6 100644 --- a/API.md +++ b/API.md @@ -1191,6 +1191,64 @@ printf("UTF-16LE payload bytes (no terminator): %zu", output_size); See also: [`mjb_convert_encoding`](#mjb_convert_encoding), [`mjb_detect_encoding`](#mjb_detect_encoding), [`mjb_codepoint_encode`](#mjb_codepoint_encode). +## `mjb_caseless_match` + +Compare two strings using a Unicode caseless matching relation. + +```c +mjb_status mjb_caseless_match( + const char *s1, + size_t s1_byte_length, + mjb_encoding s1_encoding, + const char *s2, + size_t s2_byte_length, + mjb_encoding s2_encoding, + mjb_caseless_mode mode, + bool *matches +); +``` + +Compare two strings for case-insensitive equality using Unicode full default case folding. Canonical mode implements D145 and is recommended for ordinary text. Unnormalized mode implements D144 without normalization. Compatibility mode implements the iterated folding and compatibility normalization required by D146. Identifier mode implements D147 by applying NFD before `mjb_nfkc_casefold`; it removes default-ignorable codepoints but does not validate identifier syntax. + +- `s1` - The first string to compare +- `s1_byte_length` - The length of the first string in bytes, or `MJB_NUL_TERMINATED` +- `s1_encoding` - The encoding of the first string +- `s2` - The second string to compare +- `s2_byte_length` - The length of the second string in bytes, or `MJB_NUL_TERMINATED` +- `s2_encoding` - The encoding of the second string +- `mode` - The Unicode caseless matching relation +- `matches` - Whether the strings are a caseless match + +**Returns** + +- `MJB_STATUS_OK` - `matches` contains the comparison result +- `MJB_STATUS_INVALID_ARGUMENT` - `matches` is NULL, an input buffer is NULL with a non-zero size, or `mode` is invalid +- `MJB_STATUS_INVALID_ENCODING` - An input encoding is invalid or lacks byte-order information +- `MJB_STATUS_MALFORMED_INPUT` - An input contains an ill-formed code-unit sequence +- `MJB_STATUS_OVERFLOW` - An intermediate size would overflow +- `MJB_STATUS_NO_MEMORY` - Allocation failed +- `MJB_STATUS_UNSUPPORTED` - The identifier case-folding transform did not converge + +**Example** + +```c +const char *left = "Stra\xC3\x9F" "e"; +const char *right = "STRASSE"; +bool matches; + +if(mjb_caseless_match(left, strlen(left), MJB_ENC_UTF_8, + right, strlen(right), MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, &matches) != MJB_STATUS_OK) { + return 1; +} + +// Canonical caseless match: yes +printf("Canonical caseless match: %s", matches ? "yes" : "no"); +``` + +See also: [`mjb_map_case`](#mjb_map_case), [`mjb_nfkc_casefold`](#mjb_nfkc_casefold), [`mjb_normalize`](#mjb_normalize), [`mjb_collation_compare`](#mjb_collation_compare), [`mjb_is_identifier`](#mjb_is_identifier). + +Specifications: [The Unicode Standard, Version 18.0.0, Section 3.13.5: Default Caseless Matching](https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G33992), [UAX #31: Unicode Identifiers and Syntax, Unicode 18.0.0](https://www.unicode.org/reports/tr31/tr31-44.html). + ## `mjb_collation_compare` Compare two strings using UCA. @@ -3498,6 +3556,8 @@ without higher-level protocol tailoring. default locale is `MJB_LOCALE_EN`. Turkish and Azerbaijani apply dotted-I casing rules and Turkic case-folding mappings. Lithuanian applies dot-above casing rules; case folding remains the default non-Turkic mapping. +- `mjb_caseless_match` always uses full default (non-Turkic) case folding and is not affected by the + process-global locale. - `mjb_collation_compare` and `mjb_collation_key` use DUCET without locale collation tailoring. `mjb_collation_mode` only selects the UCA variable weighting strategy. - `mjb_display_width` uses its `mjb_width_context` argument to choose how East Asian Width @@ -3519,7 +3579,7 @@ policy. The table below maps the advertised Unicode algorithm and data claims to | ----- | -------------- | ----------------- | -------- | | Unicode Character Database data and derived properties | `mjb_codepoint_info`, `mjb_codepoint_property_binary`, `mjb_codepoint_property_int`, `mjb_codepoint_script_extensions`, script/block/category/numeric helpers | [UAX #44](https://www.unicode.org/reports/tr44/tr44-36.html), [UAX #24](https://www.unicode.org/reports/tr24/tr24-40.html), UCD 18.0.0 | Generated from UCD data files including `UnicodeData.txt`, `Blocks.txt`, `Scripts.txt`, `ScriptExtensions.txt`, `PropList.txt`, `DerivedCoreProperties.txt`, `PropertyAliases.txt`, and `PropertyValueAliases.txt`; every explicit Script_Extensions range is covered by `tests/properties.c`. | | Unicode Normalization Forms and quick check | `mjb_normalize`, `mjb_normalization_quick_check` | [UAX #15](https://www.unicode.org/reports/tr15/tr15-57.html) | `NormalizationTest.txt`, `DerivedNormalizationProps.txt`, `tests/normalization.c`, and `tests/quick-check.c`. | -| Default case conversion and caseless matching | `mjb_map_case`, `mjb_nfkc_casefold`, simple codepoint case helpers | [Unicode Core Section 3.13](https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G33992), [UAX #29](https://www.unicode.org/reports/tr29/tr29-48.html) for titlecase word boundaries, [UAX #31](https://www.unicode.org/reports/tr31/tr31-44.html) for identifier caseless matching | `SpecialCasing.txt`, `CaseFolding.txt`, `WordBreakTest.txt`, every explicit `NFKC_CF` mapping in `DerivedNormalizationProps.txt`, `tests/special-case.c`, `tests/case.c`, `tests/normalization.c`, and `tests/break-word.c`. | +| Default case conversion and caseless matching | `mjb_map_case`, `mjb_nfkc_casefold`, `mjb_caseless_match`, simple codepoint case helpers | [Unicode Core Section 3.13](https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G33992), [UAX #29](https://www.unicode.org/reports/tr29/tr29-48.html) for titlecase word boundaries, [UAX #31](https://www.unicode.org/reports/tr31/tr31-44.html) for identifier caseless matching | `SpecialCasing.txt`, `CaseFolding.txt`, `WordBreakTest.txt`, every explicit `NFKC_CF` mapping in `DerivedNormalizationProps.txt`, `tests/special-case.c`, `tests/case.c`, `tests/caseless.c`, `tests/normalization.c`, and `tests/break-word.c`. | | Grapheme, word, and sentence boundaries | `mjb_next_grapheme_break`, `mjb_next_word_break`, `mjb_next_sentence_break`, related truncation helpers | [UAX #29](https://www.unicode.org/reports/tr29/tr29-48.html) | `GraphemeBreakTest.txt`, `WordBreakTest.txt`, `SentenceBreakTest.txt`, `tests/segmentation.c`, `tests/break-word.c`, and `tests/break-sentence.c`. | | Line breaking | `mjb_next_line_break` | [UAX #14](https://www.unicode.org/reports/tr14/tr14-56.html) | `LineBreakTest.txt` and `tests/break-line.c`. | | Bidirectional Algorithm | `mjb_bidi_resolve`, `mjb_bidi_reorder_line`, `mjb_bidi_line_runs` | [UAX #9](https://www.unicode.org/reports/tr9/tr9-51.html) | `BidiCharacterTest.txt`, `BidiTest.txt`, `tests/bidi.c`, and `tests/bidi-class.c`. | diff --git a/README.md b/README.md index d312ee29..ca47dae7 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,9 @@ and header: `mojibake.c` and `mojibake.h`. Zero dependencies. **Sorting and comparison** +- **Caseless matching**: Unicode default, canonical, compatibility, and identifier + case-insensitive equality (`mjb_caseless_match`, + [Unicode 18.0.0 Section 3.13.5](https://www.unicode.org/versions/Unicode18.0.0/core-spec/chapter-3/#G34145)) - **Collation**: Unicode Collation Algorithm string comparison and sort keys, in shifted and non-ignorable modes (`mjb_collation_compare`, `mjb_collation_key`, `mjb_collation_key_into`, [UTS #10, Unicode 18.0.0](https://www.unicode.org/reports/tr10/tr10-54.html)) diff --git a/ROADMAP.md b/ROADMAP.md index 0c84dee9..d1399cbb 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -7,36 +7,26 @@ This is the Mojibake roadmap. For great justice. 1. **Resolved script sets** Add `mjb_string_script_set` by combining the `Script_Extensions` sets of every code point. See UTS #39 restriction levels. - 2. **Complete UTS #39 identifier checks** Generate compact tables from `IdentifierStatus.txt` and `IdentifierType.txt`, then add `mjb_identifier_check` and `mjb_string_restriction_level`. - -3. **Caseless comparison** - Add `mjb_collation_compare_caseless` with default, canonical, compatibility, and identifier modes. - -4. **Detailed validation and decoder iteration** +3. **Detailed validation and decoder iteration** Add `mjb_string_validate`, `mjb_decode_next`, and `mjb_decode_previous`. Use a shared diagnostic result with the first failing byte (or code-unit offset?) and a precise malformed-input kind. - -5. **Finish typed UCD access** +4. **Finish typed UCD access** Add typed getters for code-point, code-point-sequence, and string-valued properties. Follow with character age, bidi mirror, modern/alias/extended names, and reverse character-name lookup. Is this needed? - -6. **Explicit locale operations** +5. **Explicit locale operations** Implement the currently unsupported `mjb_locale_canonicalize` using a pinned IANA Language Subtag Registry snapshot. - -7. **UTS #46 IDNA** +6. **UTS #46 IDNA** Implement nontransitional `mjb_idna_to_ascii` and `mjb_idna_to_unicode`, including Punycode, STD3, hyphen, bidi, and joiner checks. Validate against the complete `IdnaTestV2.txt` suite. - -8. **Reusable configurable collators** +7. **Reusable configurable collators** Introduce immutable DUCET collator objects with strength, case, numeric, normalization, and variable-weighting options. - -9. **Streaming processing** +8. **Streaming processing** Add stateful `init`/`feed`/`finish` APIs for decoding and conversion, normalization, casing, and segmentation, using caller-kind of API. diff --git a/TESTS.md b/TESTS.md index 09c4f066..ea3ed393 100644 --- a/TESTS.md +++ b/TESTS.md @@ -1,6 +1,6 @@ # Test coverage -Mojibake run a total of **4,524,115** C assertions and **88** JavaScript assertions, including all +Mojibake run a total of **4,524,971** C assertions and **93** JavaScript assertions, including all the official tests included in the standard: 1. [auxiliary/GraphemeBreakTest.txt](https://www.unicode.org/Public/18.0.0/ucd/auxiliary/GraphemeBreakTest.txt) @@ -24,30 +24,31 @@ the official tests included in the standard: | `mjb_normalize_into` | 2824113 | | `mjb_bidi_resolve` | 582573 | | `mjb_collation_compare` | 424502 | -| `mjb_normalize` | 424423 | +| `mjb_normalize` | 424427 | | `mjb_bidi_reorder_line` | 91723 | | `mjb_nfkc_casefold_into` | 74749 | | `mjb_nfkc_casefold` | 32037 | | `mjb_map_case_into` | 24756 | -| `mjb_next_line_break` | 18800 | -| `mjb_result_free` | 10686 | +| `mjb_next_line_break` | 19346 | +| `mjb_result_free` | 10688 | | `mjb_classify_emoji_sequence` | 5286 | | `mjb_map_case` | 3492 | -| `mjb_next_word_break` | 1829 | +| `mjb_next_word_break` | 1947 | | `mjb_codepoint_emoji_properties` | 1429 | -| `mjb_next_grapheme_break` | 803 | +| `mjb_next_grapheme_break` | 880 | | `mjb_codepoint_script_extensions` | 639 | -| `mjb_next_sentence_break` | 459 | +| `mjb_next_sentence_break` | 515 | | `mjb_are_confusable` | 241 | | `mjb_filter` | 161 | | `mjb_convert_encoding` | 137 | | `mjb_locale_parse` | 121 | -| `mjb_count_codepoints` | 111 | +| `mjb_count_codepoints` | 119 | | `mjb_codepoint_encode` | 106 | | `mjb_codepoint_info` | 82 | | `mjb_filter_into` | 56 | | `mjb_confusable_skeleton_into` | 50 | | `mjb_display_width` | 49 | +| `mjb_caseless_match` | 45 | | `mjb_hangul_syllable_composition` | 45 | | `mjb_collation_key` | 41 | | `mjb_codepoint_is_valid` | 40 | @@ -60,26 +61,26 @@ the official tests included in the standard: | `mjb_confusable_skeleton` | 20 | | `mjb_convert_encoding_into` | 19 | | `mjb_hangul_syllable_name` | 19 | -| `mjb_is_utf8` | 18 | +| `mjb_is_utf8` | 19 | +| `mjb_set_locale` | 19 | | `mjb_codepoint_is_cjk_extension_ideograph` | 17 | -| `mjb_set_locale` | 17 | | `mjb_codepoint_block` | 16 | -| `mjb_is_utf16` | 15 | +| `mjb_is_utf16` | 16 | | `mjb_is_identifier` | 13 | | `mjb_bidi_line_runs` | 12 | -| `mjb_truncate_grapheme` | 12 | -| `mjb_detect_encoding` | 11 | -| `mjb_is_ascii` | 11 | -| `mjb_truncate_grapheme_width` | 11 | +| `mjb_detect_encoding` | 12 | +| `mjb_is_ascii` | 12 | +| `mjb_truncate_grapheme` | 11 | | `mjb_codepoint_script` | 10 | | `mjb_plane_name` | 9 | -| `mjb_truncate_word` | 9 | | `mjb_codepoint_is_cjk_ideograph` | 8 | | `mjb_reset` | 8 | -| `mjb_truncate_word_width` | 8 | +| `mjb_truncate_grapheme_width` | 8 | +| `mjb_truncate_word` | 8 | | `mjb_codepoint_is_id_start` | 7 | | `mjb_codepoint_property_binary` | 7 | | `mjb_for_each_character` | 7 | +| `mjb_truncate_word_width` | 7 | | `mjb_codepoint_is_combining` | 6 | | `mjb_codepoint_is_graphic` | 6 | | `mjb_codepoint_is_id_continue` | 6 | @@ -118,12 +119,13 @@ the official tests included in the standard: | `mjb_utf8_vsnprintf` | 2 | | `mjb_version` | 2 | | `mjb_version_number` | 2 | -| **Total** | **4524115** | +| **Total** | **4524971** | ## JavaScript | Test | Coverage | | ------------------------------------------- | ---------- | +| `Mojibake.caselessMatch` | 5 | | `Mojibake.areConfusable` | 3 | | `Mojibake.codepointPropertyBinary` | 3 | | `Mojibake.collationCompare` | 3 | @@ -200,4 +202,4 @@ the official tests included in the standard: | `Mojibake.unicodeVersion` | 1 | | `Mojibake.version` | 1 | | `Mojibake.versionNumber` | 1 | -| **Total** | **88** | +| **Total** | **93** | diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a7547d75..b5796ba0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES break-word.c buffer.c case.c + caseless.c codepoint.c collation.c cjk.c @@ -120,6 +121,7 @@ if(MJB_BUILD_WASM) "\"_mjb_codepoint_script_extensions\"" "\"_mjb_codepoint_encode\"" "\"_mjb_convert_encoding\"" + "\"_mjb_caseless_match\"" "\"_mjb_collation_compare\"" "\"_mjb_collation_key\"" "\"_mjb_map_case\"" diff --git a/src/api/index.ts b/src/api/index.ts index 8df8d7bf..f47adb48 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -258,6 +258,14 @@ export enum CollationMode { SHIFTED, }; +// mjb_caseless_mode +export enum CaselessMode { + CANONICAL, + UNNORMALIZED, + COMPATIBILITY, + IDENTIFIER, +}; + // mjb_identifier_profile export enum IdentifierProfile { DEFAULT, @@ -717,6 +725,28 @@ export class Mojibake { } } + // mjb_status mjb_caseless_match(const char *s1, size_t s1_byte_length, + // mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, + // mjb_caseless_mode mode, bool *matches) + caselessMatch(first: MojibakeInput, second: MojibakeInput, + mode = CaselessMode.CANONICAL, options: TextInputOptions = {}): boolean | null { + const firstInput = this.copyInput(first, options.encoding); + const secondInput = this.copyInput(second, options.additionalEncoding ?? options.encoding); + const matchesPtr = this.malloc(1); + + try { + const status = this.module._mjb_caseless_match(firstInput.ptr, firstInput.size, + firstInput.encoding, secondInput.ptr, secondInput.size, secondInput.encoding, mode, + matchesPtr); + + return status === Status.OK ? this.module.HEAPU8[matchesPtr] !== 0 : null; + } finally { + this.free(firstInput.ptr); + this.free(secondInput.ptr); + this.free(matchesPtr); + } + } + // mjb_status mjb_collation_compare(const char *s1, size_t s1_byte_length, // mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, // mjb_collation_mode mode, int *order) diff --git a/src/api/mojibake.d.ts b/src/api/mojibake.d.ts index 6828bd3e..82fe1408 100644 --- a/src/api/mojibake.d.ts +++ b/src/api/mojibake.d.ts @@ -59,6 +59,8 @@ export type MojibakeWasmModule = { _mjb_codepoint_encode: (codepoint: Codepoint, buffer: Pointer, byte_length: number, encoding: number) => number; // Convert from one encoding to another. _mjb_convert_encoding: (buffer: Pointer, byte_length: number, encoding: number, output_encoding: number, result: Pointer) => number; + // Compare two strings using a Unicode caseless matching relation. + _mjb_caseless_match: (s1: Pointer, s1_byte_length: number, s1_encoding: number, s2: Pointer, s2_byte_length: number, s2_encoding: number, mode: number, matches: Pointer) => number; // Compare two strings using UCA. _mjb_collation_compare: (s1: Pointer, s1_byte_length: number, s1_encoding: number, s2: Pointer, s2_byte_length: number, s2_encoding: number, mode: number, order: Pointer) => number; // Generate a UCA sort key for a string. diff --git a/src/api/tests/index.ts b/src/api/tests/index.ts index 77bc7071..5df758aa 100644 --- a/src/api/tests/index.ts +++ b/src/api/tests/index.ts @@ -7,6 +7,7 @@ import { Block, BreakType, + CaselessMode, CaseType, Category, Direction, @@ -75,6 +76,15 @@ ATT_ASSERT(mojibake.isASCII('Hello'), true, 'isASCII'); ATT_ASSERT(mojibake.codepointEncode(0x41)?.output, 'A', 'codepointEncode'); ATT_ASSERT(mojibake.convertEncoding('A', Encoding.UTF_16LE)?.output, 'A', 'convertEncoding'); ATT_ASSERT(mojibake.countCodepoints('H\u00E9ll\u00F6'), 5, 'countCodepoints'); +ATT_ASSERT(mojibake.caselessMatch('Straße', 'STRASSE'), true, 'caselessMatch'); +ATT_ASSERT(mojibake.caselessMatch('\u00C5', 'A\u030A', CaselessMode.UNNORMALIZED), false, + 'caselessMatch unnormalized'); +ATT_ASSERT(mojibake.caselessMatch('\u00C5', 'A\u030A', CaselessMode.CANONICAL), true, + 'caselessMatch canonical'); +ATT_ASSERT(mojibake.caselessMatch('ab\u00AD', 'ab', CaselessMode.IDENTIFIER), true, + 'caselessMatch identifier'); +ATT_ASSERT(mojibake.caselessMatch(new Uint8Array([0x80]), 'a'), null, + 'caselessMatch malformed input'); ATT_ASSERT(mojibake.collationCompare('hello', 'hello'), 0, 'collationCompare'); ATT_ASSERT((mojibake.collationCompare('a', 'b') ?? 0) < 0, true, 'collationCompare negative order'); diff --git a/src/case.c b/src/case.c index 2a7c413d..6169ab82 100644 --- a/src/case.c +++ b/src/case.c @@ -16,6 +16,7 @@ typedef struct mjb_map_case_write_context { mjb_encoding encoding; mjb_map_case_type type; mjb_encoding output_encoding; + bool turkic_case_folding; } mjb_map_case_write_context; // Casing context for the conditional mappings of SpecialCasing.txt. The flags describe the @@ -464,7 +465,7 @@ static mjb_status mjb_titlecase_process(const char *buffer, size_t byte_length, static mjb_status mjb_map_case_process(const char *buffer, size_t byte_length, mjb_encoding encoding, mjb_map_case_type type, mjb_encoding output_encoding, - mjb_output *output) { + bool turkic_case_folding, mjb_output *output) { if(type == MJB_CASE_TITLE) { return mjb_titlecase_process(buffer, byte_length, encoding, output_encoding, output); } @@ -472,8 +473,10 @@ static mjb_status mjb_map_case_process(const char *buffer, size_t byte_length, mjb_codepoint codepoint; uint8_t state = MJB_UTF_ACCEPT; bool in_error = false; - bool turkic = mjb_global.locale == MJB_LOCALE_TR || mjb_global.locale == MJB_LOCALE_AZ; - bool locale_sensitive = turkic || mjb_global.locale == MJB_LOCALE_LT; + bool folding = type == MJB_CASE_CASEFOLD || type == MJB_CASE_CASEFOLD_SIMPLE; + bool locale_sensitive = !folding && + (mjb_global.locale == MJB_LOCALE_TR || mjb_global.locale == MJB_LOCALE_AZ || + mjb_global.locale == MJB_LOCALE_LT); // The context is only needed for Final_Sigma (lowercase) and the language-sensitive rules. bool track_context = type == MJB_CASE_LOWER || locale_sensitive; mjb_map_case_context context = { false, false, false, locale_sensitive }; @@ -491,10 +494,10 @@ static mjb_status mjb_map_case_process(const char *buffer, size_t byte_length, continue; } - if(type == MJB_CASE_CASEFOLD || type == MJB_CASE_CASEFOLD_SIMPLE) { + if(folding) { // Turkic (T) foldings [CaseFolding.txt]: in tr/az, I folds to ı and İ to i, in // both full and simple folding. - if(turkic && (codepoint == 0x49 || codepoint == 0x130)) { + if(turkic_case_folding && (codepoint == 0x49 || codepoint == 0x130)) { mjb_status status = mjb_map_case_output_codepoint(codepoint == 0x49 ? 0x131 : 0x69, output, output_encoding); @@ -670,34 +673,12 @@ static mjb_status mjb_map_case_write(mjb_output *output, const void *context_poi const mjb_map_case_write_context *context = (const mjb_map_case_write_context *)context_pointer; return mjb_map_case_process(context->buffer, context->byte_length, context->encoding, - context->type, context->output_encoding, output); + context->type, context->output_encoding, context->turkic_case_folding, output); } -MJB_EXPORT mjb_status mjb_map_case(const char *buffer, size_t byte_length, mjb_encoding encoding, - mjb_map_case_type type, mjb_encoding output_encoding, mjb_result *result) { - if(result == NULL || (buffer == NULL && byte_length > 0)) { - return MJB_STATUS_INVALID_ARGUMENT; - } - - if(type != MJB_CASE_UPPER && type != MJB_CASE_LOWER && type != MJB_CASE_TITLE && - type != MJB_CASE_CASEFOLD && type != MJB_CASE_CASEFOLD_SIMPLE) { - return MJB_STATUS_INVALID_ARGUMENT; - } - - mjb_status status = mjb_resolve_input_byte_length(buffer, &byte_length, encoding); - - if(status != MJB_STATUS_OK) { - return status; - } - - if(byte_length == 0) { - result->output = (char *)buffer; - result->output_size = 0; - result->transformed = false; - - return MJB_STATUS_OK; - } - +static mjb_status mjb_map_case_result(const char *buffer, size_t byte_length, mjb_encoding encoding, + mjb_map_case_type type, mjb_encoding output_encoding, bool turkic_case_folding, + mjb_result *result) { char *allocated = (char *)mjb_alloc(byte_length); if(allocated == NULL) { @@ -706,8 +687,9 @@ MJB_EXPORT mjb_status mjb_map_case(const char *buffer, size_t byte_length, mjb_e mjb_output output; mjb_output_init_dynamic(&output, allocated, byte_length); - mjb_map_case_write_context context = { buffer, byte_length, encoding, type, output_encoding }; - status = mjb_map_case_write(&output, &context); + mjb_map_case_write_context context = { buffer, byte_length, encoding, type, output_encoding, + turkic_case_folding }; + mjb_status status = mjb_map_case_write(&output, &context); if(status != MJB_STATUS_OK) { mjb_free(output.buffer); @@ -737,6 +719,62 @@ MJB_EXPORT mjb_status mjb_map_case(const char *buffer, size_t byte_length, mjb_e return MJB_STATUS_OK; } +MJB_EXPORT mjb_status mjb_map_case(const char *buffer, size_t byte_length, mjb_encoding encoding, + mjb_map_case_type type, mjb_encoding output_encoding, mjb_result *result) { + if(result == NULL || (buffer == NULL && byte_length > 0)) { + return MJB_STATUS_INVALID_ARGUMENT; + } + + if(type != MJB_CASE_UPPER && type != MJB_CASE_LOWER && type != MJB_CASE_TITLE && + type != MJB_CASE_CASEFOLD && type != MJB_CASE_CASEFOLD_SIMPLE) { + return MJB_STATUS_INVALID_ARGUMENT; + } + + mjb_status status = mjb_resolve_input_byte_length(buffer, &byte_length, encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + + if(byte_length == 0) { + result->output = (char *)buffer; + result->output_size = 0; + result->transformed = false; + + return MJB_STATUS_OK; + } + + bool turkic_case_folding = (type == MJB_CASE_CASEFOLD || type == MJB_CASE_CASEFOLD_SIMPLE) && + (mjb_global.locale == MJB_LOCALE_TR || mjb_global.locale == MJB_LOCALE_AZ); + + return mjb_map_case_result(buffer, byte_length, encoding, type, output_encoding, + turkic_case_folding, result); +} + +mjb_status mjb_casefold_default(const char *buffer, size_t byte_length, mjb_encoding encoding, + mjb_encoding output_encoding, mjb_result *result) { + if(result == NULL || (buffer == NULL && byte_length > 0)) { + return MJB_STATUS_INVALID_ARGUMENT; + } + + mjb_status status = mjb_resolve_input_byte_length(buffer, &byte_length, encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + + if(byte_length == 0) { + result->output = (char *)buffer; + result->output_size = 0; + result->transformed = false; + + return MJB_STATUS_OK; + } + + return mjb_map_case_result(buffer, byte_length, encoding, MJB_CASE_CASEFOLD, output_encoding, + false, result); +} + MJB_EXPORT mjb_status mjb_map_case_into(const char *buffer, size_t byte_length, mjb_encoding encoding, mjb_map_case_type type, mjb_encoding output_encoding, void *output, size_t *output_size) { @@ -765,7 +803,10 @@ MJB_EXPORT mjb_status mjb_map_case_into(const char *buffer, size_t byte_length, return status; } - mjb_map_case_write_context context = { buffer, byte_length, encoding, type, output_encoding }; + bool turkic_case_folding = (type == MJB_CASE_CASEFOLD || type == MJB_CASE_CASEFOLD_SIMPLE) && + (mjb_global.locale == MJB_LOCALE_TR || mjb_global.locale == MJB_LOCALE_AZ); + mjb_map_case_write_context context = { buffer, byte_length, encoding, type, output_encoding, + turkic_case_folding }; return mjb_output_into(output, output_size, mjb_map_case_write, &context); } diff --git a/src/caseless.c b/src/caseless.c new file mode 100644 index 00000000..2a2d6b93 --- /dev/null +++ b/src/caseless.c @@ -0,0 +1,199 @@ +/** + * The Mojibake library + * + * This file is distributed under the MIT License. See LICENSE for details. + */ + +#include + +#include "mojibake-internal.h" +#include "utf.h" + +typedef struct mjb_caseless_value { + const char *buffer; + size_t byte_length; + mjb_encoding encoding; + bool owned; +} mjb_caseless_value; + +static bool mjb_caseless_mode_is_valid(mjb_caseless_mode mode) { + return mode == MJB_CASELESS_CANONICAL || mode == MJB_CASELESS_UNNORMALIZED || + mode == MJB_CASELESS_COMPATIBILITY || mode == MJB_CASELESS_IDENTIFIER; +} + +static void mjb_caseless_value_free(mjb_caseless_value *value) { + if(value->owned) { + mjb_free((void *)value->buffer); + } + + value->buffer = NULL; + value->byte_length = 0; + value->owned = false; +} + +static void mjb_caseless_value_replace(mjb_caseless_value *value, const mjb_result *next) { + const char *previous = value->buffer; + bool previous_owned = value->owned; + + if(previous_owned && next->output != previous) { + mjb_free((void *)previous); + } + + value->buffer = next->output; + value->byte_length = next->output_size; + value->encoding = MJB_ENC_UTF_8; + value->owned = next->transformed || (previous_owned && next->output == previous); +} + +static mjb_status mjb_caseless_normalize(mjb_caseless_value *value, mjb_normalization form) { + mjb_result next; + mjb_status status = mjb_normalize(value->buffer, value->byte_length, value->encoding, form, + MJB_ENC_UTF_8, &next); + + if(status == MJB_STATUS_OK) { + mjb_caseless_value_replace(value, &next); + } + + return status; +} + +static mjb_status mjb_caseless_casefold(mjb_caseless_value *value) { + mjb_result next; + mjb_status status = mjb_casefold_default(value->buffer, value->byte_length, value->encoding, + MJB_ENC_UTF_8, &next); + + if(status == MJB_STATUS_OK) { + mjb_caseless_value_replace(value, &next); + } + + return status; +} + +static mjb_status mjb_caseless_nfkc_casefold(mjb_caseless_value *value) { + mjb_result next; + mjb_status status = mjb_nfkc_casefold(value->buffer, value->byte_length, value->encoding, + MJB_ENC_UTF_8, &next); + + if(status == MJB_STATUS_OK) { + mjb_caseless_value_replace(value, &next); + } + + return status; +} + +static mjb_status mjb_caseless_transform(mjb_caseless_value *value, mjb_caseless_mode mode) { + mjb_status status; + + switch(mode) { + case MJB_CASELESS_UNNORMALIZED: + return mjb_caseless_casefold(value); + + case MJB_CASELESS_CANONICAL: + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFD); + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_casefold(value); + } + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFD); + } + + return status; + + case MJB_CASELESS_COMPATIBILITY: + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFD); + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_casefold(value); + } + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFKD); + } + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_casefold(value); + } + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFKD); + } + + return status; + + case MJB_CASELESS_IDENTIFIER: + status = mjb_caseless_normalize(value, MJB_NORMALIZATION_NFD); + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_nfkc_casefold(value); + } + + return status; + } + + return MJB_STATUS_INVALID_ARGUMENT; +} + +MJB_EXPORT mjb_status mjb_caseless_match(const char *s1, size_t s1_byte_length, + mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, + mjb_caseless_mode mode, bool *matches) { + if(matches == NULL) { + return MJB_STATUS_INVALID_ARGUMENT; + } + + *matches = false; + + if(!mjb_caseless_mode_is_valid(mode) || (s1 == NULL && s1_byte_length > 0) || + (s2 == NULL && s2_byte_length > 0)) { + return MJB_STATUS_INVALID_ARGUMENT; + } + + // First string. + mjb_status status = mjb_resolve_input_byte_length(s1, &s1_byte_length, s1_encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + + // Second string. + status = mjb_resolve_input_byte_length(s2, &s2_byte_length, s2_encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + + if(s1_byte_length > 0) { + status = mjb_validate_code_unit_sequence(s1, s1_byte_length, s1_encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + } + + if(s2_byte_length > 0) { + status = mjb_validate_code_unit_sequence(s2, s2_byte_length, s2_encoding); + + if(status != MJB_STATUS_OK) { + return status; + } + } + + mjb_caseless_value left = { s1, s1_byte_length, s1_encoding, false }; + mjb_caseless_value right = { s2, s2_byte_length, s2_encoding, false }; + status = mjb_caseless_transform(&left, mode); + + if(status == MJB_STATUS_OK) { + status = mjb_caseless_transform(&right, mode); + } + + if(status == MJB_STATUS_OK) { + *matches = left.byte_length == right.byte_length && + (left.byte_length == 0 || memcmp(left.buffer, right.buffer, left.byte_length) == 0); + } + + mjb_caseless_value_free(&right); + mjb_caseless_value_free(&left); + + return status; +} diff --git a/src/cpp/mojibake.hpp b/src/cpp/mojibake.hpp index d560b043..5eb28778 100644 --- a/src/cpp/mojibake.hpp +++ b/src/cpp/mojibake.hpp @@ -865,6 +865,24 @@ constexpr Filter &operator|=(Filter &left, Filter right) noexcept { return case_map(input, MJB_CASE_CASEFOLD_SIMPLE, input_encoding, output_encoding); } +enum class CaselessMode { + Canonical = MJB_CASELESS_CANONICAL, + Unnormalized = MJB_CASELESS_UNNORMALIZED, + Compatibility = MJB_CASELESS_COMPATIBILITY, + Identifier = MJB_CASELESS_IDENTIFIER +}; + +[[nodiscard]] inline bool caseless_match(std::string_view s1, std::string_view s2, + CaselessMode mode = CaselessMode::Canonical, mjb_encoding s1_encoding = MJB_ENC_UTF_8, + mjb_encoding s2_encoding = MJB_ENC_UTF_8) { + bool matches = false; + detail::check_status(mjb_caseless_match(s1.data(), s1.size(), s1_encoding, s2.data(), s2.size(), + s2_encoding, static_cast(mode), &matches), + "Caseless matching failed"); + + return matches; +} + [[nodiscard]] inline int compare(std::string_view s1, std::string_view s2, mjb_collation_mode mode = MJB_COLLATION_NON_IGNORABLE, mjb_encoding s1_encoding = MJB_ENC_UTF_8, mjb_encoding s2_encoding = MJB_ENC_UTF_8) { diff --git a/src/mojibake-internal.h b/src/mojibake-internal.h index 3615af8f..f76e99f0 100644 --- a/src/mojibake-internal.h +++ b/src/mojibake-internal.h @@ -94,6 +94,9 @@ mjb_status mjb_output_into(void *output, size_t *output_size, mjb_output_writer mjb_status mjb_output_copy_into(const void *buffer, size_t byte_length, void *output, size_t *output_size); +mjb_status mjb_casefold_default(const char *buffer, size_t byte_length, mjb_encoding encoding, + mjb_encoding output_encoding, mjb_result *result); + size_t mjb_grapheme_prefix_bytes(const char *buffer, size_t byte_length, mjb_encoding encoding, size_t max_bytes); diff --git a/src/mojibake.h b/src/mojibake.h index 66b58db6..61123a26 100644 --- a/src/mojibake.h +++ b/src/mojibake.h @@ -263,6 +263,18 @@ typedef enum mjb_map_case_type { MJB_CASE_CASEFOLD_SIMPLE // C + S statuses, length-preserving } mjb_map_case_type; +// Unicode caseless matching relation (Unicode 18.0.0, Core Specification D144-D147). +typedef enum mjb_caseless_mode { + // D145: NFD(toCasefold(NFD(X))) = NFD(toCasefold(NFD(Y))) + MJB_CASELESS_CANONICAL, + // D144: toCasefold(X) = toCasefold(Y) + MJB_CASELESS_UNNORMALIZED, + // D146: NFKD(toCasefold(NFKD(toCasefold(NFD(X))))) = NFKD(toCasefold(NFKD(toCasefold(NFD(Y))))) + MJB_CASELESS_COMPATIBILITY, + // D147: toNFKC_Casefold(NFD(X)) = toNFKC_Casefold(NFD(Y)) + MJB_CASELESS_IDENTIFIER +} mjb_caseless_mode; + typedef enum mjb_error { MJB_ERROR_NONE, MJB_ERROR_INVALID_ARGUMENT, @@ -590,6 +602,9 @@ MJB_EXPORT MJB_NODISCARD mjb_status mjb_convert_encoding(const char *buffer, siz // Convert from one encoding to another into a caller-provided buffer. MJB_EXPORT MJB_NODISCARD mjb_status mjb_convert_encoding_into(const char *buffer, size_t byte_length, mjb_encoding encoding, mjb_encoding output_encoding, void *output, size_t *output_size); +// Compare two strings using a Unicode caseless matching relation. +MJB_EXPORT MJB_NODISCARD mjb_status mjb_caseless_match(const char *s1, size_t s1_byte_length, mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, mjb_caseless_mode mode, bool *matches); + // Compare two strings using UCA. MJB_EXPORT MJB_NODISCARD mjb_status mjb_collation_compare(const char *s1, size_t s1_byte_length, mjb_encoding s1_encoding, const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, mjb_collation_mode mode, int *order); diff --git a/src/site/index.html b/src/site/index.html index 512daf35..5cb0226d 100644 --- a/src/site/index.html +++ b/src/site/index.html @@ -494,9 +494,8 @@ value = Array.from(form.querySelectorAll( `input[name="${func}-${arg.name}"]:checked` )).reduce((filters, input) => filters | parseInt(input.value, 10), 0); - } else if(arg.type === 'mjb_plane') { - value = parseInt(value, 10); } else if(arg.type === 'mjb_encoding') { + console.log(value); value = parseInt(value, 10); if(arg.name === 'encoding' || arg.name === 's1_encoding') { @@ -510,6 +509,8 @@ gotOptions = true; continue; + } else if(arg.is_enum) { + value = parseInt(value, 10); } else if(arg.type === 'mjb_codepoint') { value = readCodepoint(value); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 13ab483b..b531b485 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,6 +11,7 @@ set(TEST_SOURCES break-sentence.c break-word.c case.c + caseless.c cjk.c codepoint.c collation.c @@ -91,6 +92,7 @@ add_test(NAME "Break Line" COMMAND mojibake-test -f break_line) add_test(NAME "Break Sentence" COMMAND mojibake-test -f break_sentence) add_test(NAME "Break Word" COMMAND mojibake-test -f break_word) add_test(NAME "Case" COMMAND mojibake-test -f case) +add_test(NAME "Caseless" COMMAND mojibake-test -f caseless) add_test(NAME "CJK" COMMAND mojibake-test -f cjk) add_test(NAME "Codepoint" COMMAND mojibake-test -f codepoint) add_test(NAME "Collation" COMMAND mojibake-test -f collation) diff --git a/tests/caseless.c b/tests/caseless.c new file mode 100644 index 00000000..02f09d69 --- /dev/null +++ b/tests/caseless.c @@ -0,0 +1,119 @@ +/** + * The Mojibake library + * + * This file is distributed under the MIT License. See LICENSE for details. + */ + +#include "test.h" + +static void check_match(const char *s1, size_t s1_byte_length, mjb_encoding s1_encoding, + const char *s2, size_t s2_byte_length, mjb_encoding s2_encoding, mjb_caseless_mode mode, + bool expected, const char *name) { + bool matches = !expected; + + MJB_TEST_COVERAGE(mjb_caseless_match); + ATT_ASSERT_STATUS(mjb_caseless_match(s1, s1_byte_length, s1_encoding, s2, s2_byte_length, + s2_encoding, mode, &matches), + MJB_STATUS_OK, name) + ATT_ASSERT(matches, expected, name) +} + +int test_caseless(void *arg) { + bool matches = true; + + ATT_ASSERT_STATUS(mjb_caseless_match("a", 1, MJB_ENC_UTF_8, "A", 1, MJB_ENC_UTF_8, + MJB_CASELESS_CANONICAL, NULL), + MJB_STATUS_INVALID_ARGUMENT, "Caseless match rejects a NULL result") + ATT_ASSERT_STATUS(mjb_caseless_match(NULL, 1, MJB_ENC_UTF_8, "A", 1, MJB_ENC_UTF_8, + MJB_CASELESS_CANONICAL, &matches), + MJB_STATUS_INVALID_ARGUMENT, "Caseless match rejects a NULL first input") + ATT_ASSERT(matches, false, "Caseless match clears its result after invalid first input") + + matches = true; + ATT_ASSERT_STATUS(mjb_caseless_match("a", 1, MJB_ENC_UTF_8, NULL, 1, MJB_ENC_UTF_8, + MJB_CASELESS_CANONICAL, &matches), + MJB_STATUS_INVALID_ARGUMENT, "Caseless match rejects a NULL second input") + ATT_ASSERT(matches, false, "Caseless match clears its result after invalid second input") + + matches = true; + ATT_ASSERT_STATUS(mjb_caseless_match("a", 1, MJB_ENC_UTF_8, "A", 1, MJB_ENC_UTF_8, + (mjb_caseless_mode)99, &matches), + MJB_STATUS_INVALID_ARGUMENT, "Caseless match rejects an invalid mode") + ATT_ASSERT(matches, false, "Caseless match clears its result after an invalid mode") + + matches = true; + ATT_ASSERT_STATUS(mjb_caseless_match("\x80", 1, MJB_ENC_UTF_8, "a", 1, MJB_ENC_UTF_8, + MJB_CASELESS_UNNORMALIZED, &matches), + MJB_STATUS_MALFORMED_INPUT, "Caseless match rejects malformed first input") + ATT_ASSERT(matches, false, "Caseless match clears its result after malformed first input") + + matches = true; + ATT_ASSERT_STATUS(mjb_caseless_match("a", 1, MJB_ENC_UTF_8, "\x80", 1, MJB_ENC_UTF_8, + MJB_CASELESS_UNNORMALIZED, &matches), + MJB_STATUS_MALFORMED_INPUT, "Caseless match rejects malformed second input") + ATT_ASSERT(matches, false, "Caseless match clears its result after malformed second input") + + const char utf16_without_bom[] = { 'A', '\0' }; + ATT_ASSERT_STATUS(mjb_caseless_match(utf16_without_bom, sizeof(utf16_without_bom), + MJB_ENC_UTF_16, "a", 1, MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, &matches), + MJB_STATUS_INVALID_ENCODING, "Caseless match rejects generic UTF-16 without a BOM") + + check_match(NULL, 0, MJB_ENC_UTF_8, "", 0, MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, true, + "Empty strings are a canonical caseless match"); + check_match("a", MJB_NUL_TERMINATED, MJB_ENC_UTF_8, "A", MJB_NUL_TERMINATED, MJB_ENC_UTF_8, + MJB_CASELESS_CANONICAL, true, "Caseless match accepts NUL-terminated input"); + + check_match("Stra\xC3\x9F" + "e", + 7, MJB_ENC_UTF_8, "STRASSE", 7, MJB_ENC_UTF_8, MJB_CASELESS_UNNORMALIZED, true, + "Full case folding matches sharp s with SS"); + + check_match("\xC3\x85", 2, MJB_ENC_UTF_8, "A\xCC\x8A", 3, MJB_ENC_UTF_8, + MJB_CASELESS_UNNORMALIZED, false, + "Unnormalized caseless matching preserves canonical representation differences"); + check_match("\xC3\x85", 2, MJB_ENC_UTF_8, "A\xCC\x8A", 3, MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, + true, "Canonical caseless matching removes canonical representation differences"); + + check_match("A\xCC\x81\xCC\xA7", 5, MJB_ENC_UTF_8, "a\xCC\xA7\xCC\x81", 5, MJB_ENC_UTF_8, + MJB_CASELESS_UNNORMALIZED, false, + "Unnormalized caseless matching preserves combining-mark order"); + check_match("A\xCC\x81\xCC\xA7", 5, MJB_ENC_UTF_8, "a\xCC\xA7\xCC\x81", 5, MJB_ENC_UTF_8, + MJB_CASELESS_CANONICAL, true, "Canonical caseless matching reorders combining marks"); + + check_match("\xE3\x8E\x92", 3, MJB_ENC_UTF_8, "MHz", 3, MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, + false, "Canonical caseless matching preserves compatibility distinctions"); + check_match("\xE3\x8E\x92", 3, MJB_ENC_UTF_8, "MHz", 3, MJB_ENC_UTF_8, + MJB_CASELESS_COMPATIBILITY, true, + "Compatibility caseless matching folds letters introduced by decomposition"); + + check_match("ab\xC2\xAD", 4, MJB_ENC_UTF_8, "ab", 2, MJB_ENC_UTF_8, MJB_CASELESS_COMPATIBILITY, + false, "Compatibility caseless matching preserves default-ignorables"); + check_match("ab\xC2\xAD", 4, MJB_ENC_UTF_8, "ab", 2, MJB_ENC_UTF_8, MJB_CASELESS_IDENTIFIER, + true, "Identifier caseless matching removes default-ignorables"); + check_match("", 0, MJB_ENC_UTF_8, "\xC2\xAD", 2, MJB_ENC_UTF_8, MJB_CASELESS_IDENTIFIER, true, + "Identifier caseless matching can map non-empty input to empty"); + + const char embedded_nul_upper[] = { 'A', '\0', 'B' }; + const char embedded_nul_lower[] = { 'a', '\0', 'b' }; + check_match(embedded_nul_upper, sizeof(embedded_nul_upper), MJB_ENC_UTF_8, embedded_nul_lower, + sizeof(embedded_nul_lower), MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, true, + "Caseless match processes embedded NUL codepoints"); + + const char utf16le_strasse[] = { 'S', '\0', 'T', '\0', 'R', '\0', 'A', '\0', 'S', '\0', 'S', + '\0', 'E', '\0' }; + check_match("Stra\xC3\x9F" + "e", + 7, MJB_ENC_UTF_8, utf16le_strasse, sizeof(utf16le_strasse), MJB_ENC_UTF_16LE, + MJB_CASELESS_CANONICAL, true, "Caseless match compares different input encodings"); + + ATT_ASSERT_STATUS(mjb_set_locale(MJB_LOCALE_TR), MJB_STATUS_OK, + "Set Turkish locale for caseless matching") + check_match("I", 1, MJB_ENC_UTF_8, "i", 1, MJB_ENC_UTF_8, MJB_CASELESS_UNNORMALIZED, true, + "Caseless matching always uses default non-Turkic folding"); + check_match("I", 1, MJB_ENC_UTF_8, "\xC4\xB1", 2, MJB_ENC_UTF_8, MJB_CASELESS_UNNORMALIZED, + false, "Caseless matching does not use Turkic folding"); + ATT_ASSERT_STATUS(mjb_set_locale(MJB_LOCALE_EN), MJB_STATUS_OK, + "Restore default locale after caseless matching") + + return 0; +} diff --git a/tests/example.c b/tests/example.c index 2760a379..0b56a801 100644 --- a/tests/example.c +++ b/tests/example.c @@ -457,6 +457,25 @@ int test_example(void *arg) { ATT_ASSERT(test_buffer, "UTF-16LE payload bytes (no terminator): 8", "mjb_convert_encoding_into test failed") // Added by the script } +{ + // Example for mjb_caseless_match + MJB_TEST_COVERAGE(mjb_caseless_match); // Added by the script + const char *left = "Stra\xC3\x9F" "e"; + const char *right = "STRASSE"; + bool matches; + + if(mjb_caseless_match(left, strlen(left), MJB_ENC_UTF_8, + right, strlen(right), MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, &matches) != MJB_STATUS_OK) { + ATT_ASSERT(0, 1, "mjb_caseless_match test failed") // Added by the script + return 1; + } + + // Canonical caseless match: yes + // printf("Canonical caseless match: %s", matches ? "yes" : "no"); + snprintf(test_buffer, sizeof(test_buffer), "Canonical caseless match: %s", matches ? "yes" : "no"); // Added by the script + ATT_ASSERT(test_buffer, "Canonical caseless match: yes", "mjb_caseless_match test failed") // Added by the script +} + { // Example for mjb_collation_compare MJB_TEST_COVERAGE(mjb_collation_compare); // Added by the script diff --git a/tests/ext/cpp/normalization.cpp b/tests/ext/cpp/normalization.cpp index fdaadabb..729ff69b 100644 --- a/tests/ext/cpp/normalization.cpp +++ b/tests/ext/cpp/normalization.cpp @@ -34,6 +34,11 @@ int test_cpp_normalization(void *arg) { ATT_ASSERT(mjb::casefold("Stra\xC3\x9F" "e"), std::string("strasse"), "casefold") ATT_ASSERT(mjb::casefold_simple("\xE1\xBA\x9E"), std::string("\xC3\x9F"), "casefold_simple") + ATT_ASSERT(mjb::caseless_match("Stra\xC3\x9F" "e", "STRASSE"), true, "caseless_match") + ATT_ASSERT(mjb::caseless_match("\xC3\x85", "A\xCC\x8A", mjb::CaselessMode::Unnormalized), + false, "caseless_match unnormalized") + ATT_ASSERT(mjb::caseless_match("\xC3\x85", "A\xCC\x8A"), true, + "caseless_match canonical") ATT_ASSERT((int)mjb::detect_encoding("abc"), MJB_ENC_ASCII | MJB_ENC_UTF_8, "detect_encoding") @@ -95,6 +100,16 @@ int test_cpp_normalization(void *arg) { caught = false; + try { + (void)mjb::caseless_match(malformed_utf8, "a"); + } catch(const mjb::LibraryError &error) { + caught = error.status() == MJB_STATUS_MALFORMED_INPUT; + } + + ATT_ASSERT(caught, true, "caseless_match preserves malformed-input status") + + caught = false; + try { (void)mjb::is_confusable(malformed_utf8, "A"); } catch(const mjb::LibraryError &error) { diff --git a/tests/test.c b/tests/test.c index f33fb5b3..0a46a37c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -342,6 +342,7 @@ int main(int argc, char *const argv[]) { RUN_TEST(break_sentence) RUN_TEST(break_word) RUN_TEST(case) + RUN_TEST(caseless) RUN_TEST(cjk) RUN_TEST(codepoint) RUN_TEST(collation) diff --git a/tests/test.h b/tests/test.h index e2cec683..f24dbd62 100644 --- a/tests/test.h +++ b/tests/test.h @@ -27,6 +27,7 @@ int test_break_line(void *arg); int test_break_sentence(void *arg); int test_break_word(void *arg); int test_case(void *arg); +int test_caseless(void *arg); int test_cjk(void *arg); int test_codepoint(void *arg); int test_collation(void *arg); diff --git a/utils/generate/functions.ts b/utils/generate/functions.ts index 0747cc7f..5870e096 100644 --- a/utils/generate/functions.ts +++ b/utils/generate/functions.ts @@ -9,6 +9,7 @@ export type MojibakeArg = { type: string; description: string; wasm_generated: boolean; + is_enum?: boolean; // Memory-ownership note. ownership?: string; }; @@ -105,7 +106,8 @@ function encoding(description = 'The encoding of the string', name = 'encoding') name, type: 'mjb_encoding', description, - wasm_generated: false + wasm_generated: false, + is_enum: true } } @@ -213,7 +215,8 @@ printf("U+%04X lowercase: U+%04X", character.codepoint, character.lowercase);`, name: 'form', type: 'mjb_normalization', description: 'The normalization form to use', - wasm_generated: false + wasm_generated: false, + is_enum: true }, encoding('The output encoding of the string', 'output_encoding'), result() @@ -259,7 +262,8 @@ mjb_result_free(&result);`, name: 'form', type: 'mjb_normalization', description: 'The normalization form to use', - wasm_generated: false + wasm_generated: false, + is_enum: true }, encoding('The output encoding of the string', 'output_encoding'), { @@ -571,7 +575,8 @@ printf("NFKC casefold payload (no terminator): %.*s", (int)output_size, output); name: 'form', type: 'mjb_normalization', description: 'The normalization form to check', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'quick_check', @@ -1074,6 +1079,73 @@ if(output_size > sizeof(output) || mjb_convert_encoding_into(input, strlen(input printf("UTF-16LE payload bytes (no terminator): %zu", output_size);`, related: ['mjb_convert_encoding', 'mjb_detect_encoding', 'mjb_codepoint_encode'] }, + { + comment: 'Compare two strings using a Unicode caseless matching relation.', + ret: 'mjb_status', + name: 'mjb_caseless_match', + attributes: ['MJB_NODISCARD'], + args: [ + buffer('The first string to compare', 's1'), + byte_length('The length of the first string in bytes, or `MJB_NUL_TERMINATED`', + 's1_byte_length'), + encoding('The encoding of the first string', 's1_encoding'), + buffer('The second string to compare', 's2'), + byte_length('The length of the second string in bytes, or `MJB_NUL_TERMINATED`', + 's2_byte_length'), + encoding('The encoding of the second string', 's2_encoding'), + { + name: 'mode', + type: 'mjb_caseless_mode', + description: 'The Unicode caseless matching relation', + wasm_generated: false, + is_enum: true + }, + { + name: 'matches', + type: 'bool *', + description: 'Whether the strings are a caseless match', + wasm_generated: true + } + ], + wasm: true, + section: Section.SortingComparison, + details: 'Compare two strings for case-insensitive equality using Unicode full default case ' + + 'folding. Canonical mode implements D145 and is recommended for ordinary text. ' + + 'Unnormalized mode implements D144 without normalization. Compatibility mode implements ' + + 'the iterated folding and compatibility normalization required by D146. Identifier mode ' + + 'implements D147 by applying NFD before `mjb_nfkc_casefold`; it removes default-ignorable ' + + 'codepoints but does not validate identifier syntax.', + returns: [ + { value: 'MJB_STATUS_OK', description: '`matches` contains the comparison result' }, + { value: 'MJB_STATUS_INVALID_ARGUMENT', description: + '`matches` is NULL, an input buffer is NULL with a non-zero size, or `mode` is invalid' }, + { value: 'MJB_STATUS_INVALID_ENCODING', description: + 'An input encoding is invalid or lacks byte-order information' }, + { value: 'MJB_STATUS_MALFORMED_INPUT', description: + 'An input contains an ill-formed code-unit sequence' }, + { value: 'MJB_STATUS_OVERFLOW', description: 'An intermediate size would overflow' }, + { value: 'MJB_STATUS_NO_MEMORY', description: 'Allocation failed' }, + { value: 'MJB_STATUS_UNSUPPORTED', description: + 'The identifier case-folding transform did not converge' } + ], + example: `const char *left = "Stra\\xC3\\x9F" "e"; +const char *right = "STRASSE"; +bool matches; + +if(mjb_caseless_match(left, strlen(left), MJB_ENC_UTF_8, + right, strlen(right), MJB_ENC_UTF_8, MJB_CASELESS_CANONICAL, &matches) != MJB_STATUS_OK) { + return 1; +} + +// Canonical caseless match: yes +printf("Canonical caseless match: %s", matches ? "yes" : "no");`, + related: ['mjb_map_case', 'mjb_nfkc_casefold', 'mjb_normalize', + 'mjb_collation_compare', 'mjb_is_identifier'], + specs: [ + unicodeCore('Section 3.13.5', 'Default Caseless Matching', 'G33992'), + uax(31, 'Unicode Identifiers and Syntax') + ] + }, { comment: 'Compare two strings using UCA.', ret: 'mjb_status', @@ -1092,7 +1164,8 @@ printf("UTF-16LE payload bytes (no terminator): %zu", output_size);`, name: 'mode', type: 'mjb_collation_mode', description: 'The variable weighting strategy', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'order', @@ -1138,7 +1211,8 @@ printf("apple sorts before banana: %s", order < 0 ? "yes" : "no");`, name: 'mode', type: 'mjb_collation_mode', description: 'The variable weighting strategy', - wasm_generated: false + wasm_generated: false, + is_enum: true }, result('The pointer to store the binary sort key') ], @@ -1180,7 +1254,8 @@ mjb_result_free(&key);`, name: 'mode', type: 'mjb_collation_mode', description: 'The variable weighting strategy', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'output', @@ -1251,7 +1326,8 @@ printf("Sort key is non-empty: %s", output_size > 0 ? "yes" : "no");`, name: 'type', type: 'mjb_map_case_type', description: 'The type of case change', - wasm_generated: false + wasm_generated: false, + is_enum: true }, encoding('The output encoding of the string', 'output_encoding'), result() @@ -1479,7 +1555,8 @@ printf("U+20000 is a CJK extension ideograph: %s", mjb_codepoint_is_cjk_extensio name: 'category', type: 'mjb_category', description: 'The category to check', - wasm_generated: false + wasm_generated: false, + is_enum: true } ], wasm: true, @@ -1500,7 +1577,8 @@ printf("Uppercase letters are graphic: %s", graphic ? "yes" : "no");` name: 'category', type: 'mjb_category', description: 'The category to check', - wasm_generated: false + wasm_generated: false, + is_enum: true } ], wasm: true, @@ -1675,7 +1753,8 @@ printf("First two graphemes use %zu bytes", bytes);` name: 'context', type: 'mjb_width_context', description: 'The width context', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'max_columns', @@ -1730,7 +1809,8 @@ printf("First word segment uses %zu bytes", bytes);` name: 'context', type: 'mjb_width_context', description: 'The width context', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'max_columns', @@ -1761,7 +1841,8 @@ printf("Six columns include %zu bytes", bytes);` name: 'direction', type: 'mjb_direction', description: 'The base paragraph direction (LTR, RTL, or AUTO for P2/P3)', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'result', @@ -2033,7 +2114,8 @@ printf("Space is Pattern_White_Space: %s", whitespace ? "yes" : "no");`, name: 'profile', type: 'mjb_identifier_profile', description: 'The identifier profile (DEFAULT or NFKC)', - wasm_generated: false + wasm_generated: false, + is_enum: true } ], wasm: true, @@ -2367,7 +2449,8 @@ printf("U+1F600 is in the SMP: %s", plane == MJB_PLANE_SMP ? "yes" : "no");` name: 'plane', type: 'mjb_plane', description: 'The plane to check', - wasm_generated: false + wasm_generated: false, + is_enum: true } ], wasm: true, @@ -2385,7 +2468,8 @@ printf("Plane 16 is valid: %s", mjb_plane_is_valid(MJB_PLANE_PUA_B) ? "yes" : "n name: 'plane', type: 'mjb_plane', description: 'The plane to check', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'abbreviation', @@ -2598,7 +2682,8 @@ printf("U+754C is wide: %s", width == MJB_EAW_WIDE ? "yes" : "no");`, name: 'context', type: 'mjb_width_context', description: 'The width context for ambiguous-width characters', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'width', @@ -2652,7 +2737,8 @@ printf("Display columns: %zu", width);`, name: 'encoding', type: 'mjb_encoding', description: 'The encoding of the locale identifier', - wasm_generated: false + wasm_generated: false, + is_enum: true }, { name: 'locale', diff --git a/utils/generate/html-function.ts b/utils/generate/html-function.ts index 48f9685d..ba1ce0bc 100644 --- a/utils/generate/html-function.ts +++ b/utils/generate/html-function.ts @@ -4,12 +4,12 @@ * This file is distributed under the MIT License. See LICENSE for details. */ +import hljs from 'highlight.js/lib/core'; +import c from 'highlight.js/lib/languages/c'; import functions, { MojibakeArg, MojibakeFunction, MojibakeReturnCase, MojibakeSpecRef, Section } from './functions'; -import { categories } from './types'; -import hljs from 'highlight.js/lib/core'; -import c from 'highlight.js/lib/languages/c'; +import { caseModes, caseType, caseTypeValues, categories, collationModes, directions, encodings, encodingValues, filterFlags, filterFlagValues, identifierProfiles, normalizations, planes, planeValues, widthContexts } from './types'; const mojibakeTypes = new Set(); @@ -94,7 +94,7 @@ export class CFunction implements MojibakeFunction { } getLabelName(arg: number) { - return this.args[arg].name.replace('_', ' '); + return this.args[arg].name.replace(/_/g, ' '); } isInternal() { @@ -307,7 +307,6 @@ export class CFunction implements MojibakeFunction { private getCheckbox(arg: number): string { const disabled = this.args[arg].wasm_generated; const name = `${this.getName()}-${this.args[arg].name}`; - const description = this.getDescription(arg); let ret = `
` + ``; @@ -358,126 +357,92 @@ export class CFunction implements MojibakeFunction { for(const arg of this.args) { ret += `\n `; const type = arg.type; - if(type.startsWith('const char *')) { - ret += this.getInput(i); - } else if(type.startsWith('char *')) { - ret += this.getInput(i); - } else if(type.startsWith('size_t *')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_character *')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_emoji_properties *')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_emoji_sequence *')) { - ret += this.getInput(i); - } else if(type.startsWith('size_t')) { - ret += this.getInput(i); - } else if(type.startsWith('unsigned int')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_codepoint')) { - ret += this.getInput(i); - } else if(type.startsWith('bool')) { - ret += this.getCheckbox(i); - } else if(type.startsWith('mjb_category')) { - ret += this.getSelectInput(i, categories, null); - } else if(type.startsWith('mjb_encoding')) { - const options = [ - 'MJB_ENC_UTF_8', - 'MJB_ENC_UTF_16BE', - 'MJB_ENC_UTF_16LE', - 'MJB_ENC_UTF_32BE', - 'MJB_ENC_UTF_32LE', - ]; - // See mjb_encoding on mojibake.h - const values = [ - 0x2, - 0x8, - 0x10, - 0x40, - 0x80, - ]; - - ret += this.getSelectInput(i, options, values); - } else if(type.startsWith('mjb_filter_flags')) { - const options = [ - 'MJB_FILTER_NORMALIZE', - 'MJB_FILTER_SPACES', - 'MJB_FILTER_COLLAPSE_SPACES', - 'MJB_FILTER_CONTROLS', - 'MJB_FILTER_NUMERIC', - 'MJB_FILTER_LIMIT_COMBINING', - ]; - const values = [ - 0x1, - 0x2, - 0x4, - 0x8, - 0x10, - 0x20, - ]; - - ret += this.getBitfieldInput(i, options, values, 'Leave all unchecked for MJB_FILTER_NONE.'); - } else if(type.startsWith('mjb_map_case_type')) { - // See mjb_map_case_type on mojibake.h - const options = [ - 'MJB_CASE_UPPER', - 'MJB_CASE_LOWER', - 'MJB_CASE_TITLE', - 'MJB_CASE_CASEFOLD', - ]; - - // See mjb_encoding on mojibake.h - const values = [ - 1, - 2, - 3, - 4, - ]; - - ret += this.getSelectInput(i, options, values); - } else if(type.startsWith('mjb_result')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_property')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_locale_id')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_error')) { - ret += this.getInput(i); - } else if(type.startsWith('mjb_normalization')) { - // See mjb_normalization on mojibake.h - const options = [ - 'MJB_NORMALIZATION_NFC', - 'MJB_NORMALIZATION_NFD', - 'MJB_NORMALIZATION_NFKC', - 'MJB_NORMALIZATION_NFKD', - ]; - - ret += this.getSelectInput(i, options); - } else if(type.startsWith('mjb_plane')) { - // See mjb_plane on unicode.h - const options = [ - 'MJB_PLANE_BMP', - 'MJB_PLANE_SMP', - 'MJB_PLANE_SIP', - 'MJB_PLANE_TIP', - 'MJB_PLANE_SSP', - 'MJB_PLANE_PUA_A', - 'MJB_PLANE_PUA_B', - ]; - - // See mjb_plane on unicode.h - const values = [ - 0, - 1, - 2, - 3, - 4, - 5, - 16, - ]; - - ret += this.getSelectInput(i, options, values); + switch(type) { + case 'bool': + ret += this.getCheckbox(i); + break; + // case 'bool *': + case 'char *': + case 'const char *': + ret += this.getInput(i); + break; + // case 'const mjb_bidi_paragraph *': + // case 'const size_t *': + // case 'int *': + // case 'int32_t *': + // case 'mjb_alloc_fn': + // case 'mjb_bidi_paragraph *': + // case 'mjb_bidi_run *': + // case 'mjb_block_info *': + // case 'mjb_buffer_character *': + case 'mjb_caseless_mode': + ret += this.getSelectInput(i, caseModes, null); + break; + case 'mjb_category': + ret += this.getSelectInput(i, categories, null); + break; + // case 'mjb_character *': + case 'mjb_codepoint': + ret += this.getInput(i); + break; + // case 'mjb_codepoint *': + case 'mjb_collation_mode': + ret += this.getSelectInput(i, collationModes, null); + break; + case 'mjb_direction': + ret += this.getSelectInput(i, directions, null); + break; + // case 'mjb_east_asian_width *': + // case 'mjb_emoji_properties *': + // case 'mjb_emoji_sequence *': + case 'mjb_encoding': + ret += this.getSelectInput(i, encodings, encodingValues); + break; + // case 'mjb_error *': + case 'mjb_filter_flags': + ret += this.getBitfieldInput(i, filterFlags, filterFlagValues, + 'Leave all unchecked for MJB_FILTER_NONE.'); + break; + // case 'mjb_for_each_character_fn': + // case 'mjb_free_fn': + case 'mjb_identifier_profile': + ret += this.getSelectInput(i, identifierProfiles, null); + break; + case 'mjb_locale': + ret += this.getInput(i); + break; + // case 'mjb_locale_id *': + case 'mjb_map_case_type': + ret += this.getSelectInput(i, caseType, caseTypeValues); + break; + // case 'mjb_next_line_state *': + // case 'mjb_next_sentence_state *': + // case 'mjb_next_state *': + // case 'mjb_next_word_state *': + case 'mjb_normalization': + ret += this.getSelectInput(i, normalizations); + break; + // case 'mjb_numeric_value *': + case 'mjb_plane': + ret += this.getSelectInput(i, planes, planeValues); + break; + case 'mjb_property': // TODO: generate from mjb_property enum + ret += this.getInput(i); + break; + // case 'mjb_quick_check_result *': + // case 'mjb_realloc_fn': + // case 'mjb_result *': + // case 'mjb_script *': + case 'mjb_width_context': + ret += this.getSelectInput(i, widthContexts); + break; + case 'size_t': + ret += this.getInput(i); + break; + // case 'size_t *': + // case 'va_list': + // case 'void *': } ++i; diff --git a/utils/generate/types.ts b/utils/generate/types.ts index 69a89b1e..990e0cba 100644 --- a/utils/generate/types.ts +++ b/utils/generate/types.ts @@ -246,3 +246,112 @@ export interface BidiMirroringPair { cp: number; mirror: number; } + +// See mjb_caseless_mode on mojibake.h +export const caseModes = [ + 'MJB_CASELESS_CANONICAL', + 'MJB_CASELESS_UNNORMALIZED', + 'MJB_CASELESS_COMPATIBILITY', + 'MJB_CASELESS_IDENTIFIER', +]; + +export const collationModes = [ + 'MJB_COLLATION_DEFAULT', + 'MJB_COLLATION_NUMERIC', +]; + +export const directions = [ + 'MJB_DIRECTION_LTR', + 'MJB_DIRECTION_RTL', + 'MJB_DIRECTION_TTB', + 'MJB_DIRECTION_BTT', +]; + +export const encodings = [ + 'MJB_ENC_UTF_8', + 'MJB_ENC_UTF_16BE', + 'MJB_ENC_UTF_16LE', + 'MJB_ENC_UTF_32BE', + 'MJB_ENC_UTF_32LE', +]; + +export const encodingValues = [ + 0x2, + 0x8, + 0x10, + 0x40, + 0x80, +]; + +export const filterFlags = [ + 'MJB_FILTER_NORMALIZE', + 'MJB_FILTER_SPACES', + 'MJB_FILTER_COLLAPSE_SPACES', + 'MJB_FILTER_CONTROLS', + 'MJB_FILTER_NUMERIC', + 'MJB_FILTER_LIMIT_COMBINING', +]; + +export const filterFlagValues = [ + 0x1, + 0x2, + 0x4, + 0x8, + 0x10, + 0x20, +]; + +export const identifierProfiles = [ + 'MJB_IDENTIFIER_DEFAULT', + 'MJB_IDENTIFIER_NFKC' +]; + +export const caseType = [ + 'MJB_CASE_UPPER', + 'MJB_CASE_LOWER', + 'MJB_CASE_TITLE', + 'MJB_CASE_CASEFOLD', +]; + +// See mjb_encoding on mojibake.h +export const caseTypeValues = [ + 1, + 2, + 3, + 4, +]; + +export const normalizations = [ + 'MJB_NORMALIZATION_NFC', + 'MJB_NORMALIZATION_NFD', + 'MJB_NORMALIZATION_NFKC', + 'MJB_NORMALIZATION_NFKD', +]; + +// See mjb_plane on unicode.h +export const planes = [ + 'MJB_PLANE_BMP', + 'MJB_PLANE_SMP', + 'MJB_PLANE_SIP', + 'MJB_PLANE_TIP', + 'MJB_PLANE_SSP', + 'MJB_PLANE_PUA_A', + 'MJB_PLANE_PUA_B', +]; + +// See mjb_plane on unicode.h +export const planeValues = [ + 0, + 1, + 2, + 3, + 4, + 5, + 16, +]; + +export const widthContexts = [ + 'MJB_WIDTH_CONTEXT_WESTERN', + 'MJB_WIDTH_CONTEXT_EAST_ASIAN', + 'MJB_WIDTH_CONTEXT_AUTO', +];