mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
* Replace the streaming DFA decoder with the whole-scalar decoder Move mjb_decode_next_raw from encoding.c into utf.h as a static inline helper, add mjb_decode_begin to resolve the byte order and BOM once per input, and migrate every mjb_next_codepoint caller to it. Remove the DFA (mjb_next_codepoint, mjb_decode_step, mjb_decode_result, utf8.h, utf32.h) and the boundary-offset guesses (mjb_cluster_start, mjb_codepoint_encoded_bytes, mjb_break_position, ...). utf16.h stays for the Windows CLI. Break iterators now record the exact start offset of each codepoint in state.position and decode each maximal subpart of malformed input to its own U+FFFD. The unused state and in_error members of the break state structs are removed; position moves to byte 4 on wasm32, so the TypeScript API and the layout asserts are updated. Valid-text decode loop (2 MB mixed script, best of 15, -O3): UTF-8 6.25 -> 2.59 ms, UTF-16LE 3.23 -> 1.62 ms, UTF-32LE 3.14 -> 2.24 ms. This also fixes mjb_bidi_resolve byte offsets of multi-byte UTF-8 characters, which pointed at their last byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> * Initialize had_error when a break iteration starts The step returns early, before its own initialization, when the text has no buffer or no length. The count helpers then read had_error uninitialized (clang-analyzer-core.uninitialized.Branch). Set it in the wrappers together with position. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> --------- Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
777 lines
35 KiB
C
777 lines
35 KiB
C
/**
|
|
* The Mojibake library
|
|
*
|
|
* This file is distributed under the MIT License. See LICENSE for details.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../src/utf.h"
|
|
#include "test.h"
|
|
|
|
static bool next_codepoint(const mjb_character *character, mjb_character_position type,
|
|
void *context) {
|
|
(void)context;
|
|
(void)type;
|
|
printf(" \x1B[31mU+%04X\x1B[0m", (unsigned int)character->codepoint);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool has_only_latin1(const char *source, size_t source_size) {
|
|
mjb_codepoint current_codepoint = 0;
|
|
size_t length = strnlen(source, source_size);
|
|
|
|
for(size_t i = 0;;) {
|
|
// Find next codepoint.
|
|
mjb_status status = mjb_decode_next_raw(source, length, MJB_ENC_UTF_8, &i,
|
|
¤t_codepoint, NULL);
|
|
|
|
if(status == MJB_STATUS_END_OF_INPUT) {
|
|
break;
|
|
}
|
|
|
|
if(status != MJB_STATUS_OK || current_codepoint >= 0x100) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static int check_normalization(char *source, size_t source_size, char *normalized,
|
|
size_t normalized_size, mjb_normalization form, unsigned int current_line, const char *step) {
|
|
mjb_result result = { NULL, 0, 0, false };
|
|
char test_name[128];
|
|
|
|
MJB_TEST_COVERAGE(mjb_normalize);
|
|
mjb_text text = mjb_text_utf8(source, source_size);
|
|
mjb_status status = mjb_normalize(&text, MJB_MALFORMED_STOP, form, MJB_ENC_UTF_8, &result,
|
|
NULL);
|
|
|
|
if(status != MJB_STATUS_OK) {
|
|
snprintf(test_name, 128, "#%u %s", current_line, step);
|
|
ATT_ASSERT(true, false, test_name);
|
|
|
|
if(result.output != NULL && result.output != source) {
|
|
(void)mjb_result_free(&result);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
snprintf(test_name, 128, "#%u %s", current_line, step);
|
|
|
|
if(is_exit_on_error()) {
|
|
if(strcmp(result.output, normalized) != 0) {
|
|
mjb_status print_status;
|
|
|
|
printf("\n%s: source:", test_name);
|
|
text = mjb_text_utf8(source, source_size);
|
|
print_status = mjb_for_each_codepoint(&text, MJB_MALFORMED_STOP, next_codepoint, NULL,
|
|
NULL);
|
|
(void)print_status; // we can ignore this.
|
|
printf("\nExpected: ");
|
|
text = mjb_text_utf8(normalized, normalized_size);
|
|
print_status = mjb_for_each_codepoint(&text, MJB_MALFORMED_STOP, next_codepoint, NULL,
|
|
NULL);
|
|
(void)print_status; // we can ignore this.
|
|
printf("\nGot: ");
|
|
text = mjb_text_utf8(result.output, result.output_size);
|
|
print_status = mjb_for_each_codepoint(&text, MJB_MALFORMED_STOP, next_codepoint, NULL,
|
|
NULL);
|
|
(void)print_status; // we can ignore this.
|
|
puts("");
|
|
}
|
|
}
|
|
|
|
// clang-format off
|
|
int test_ret = ATT_ASSERT(result.output, normalized, test_name);
|
|
|
|
// (strange reformatting of the ATT_ASSERT macro)
|
|
if(result.output != NULL && result.output != source) {
|
|
(void)mjb_result_free(&result);
|
|
}
|
|
// clang-format on
|
|
|
|
if(!test_ret) {
|
|
return test_ret;
|
|
}
|
|
|
|
bool is_ascii = mjb_is_ascii(source, source_size);
|
|
|
|
// An ASCII string should not be normalized.
|
|
if(is_ascii) {
|
|
snprintf(test_name, 128, "#%u %s is ASCII and not normalized", current_line, step);
|
|
test_ret = ATT_ASSERT(result.owned, false, test_name);
|
|
}
|
|
|
|
if(form == MJB_NORMALIZATION_NFC && has_only_latin1(source, source_size)) {
|
|
snprintf(test_name, 128, "#%u %s \"%s\" is Latin-1 and not normalized", current_line, step,
|
|
source);
|
|
test_ret = ATT_ASSERT(result.owned, false, test_name);
|
|
}
|
|
|
|
MJB_TEST_COVERAGE(mjb_normalize_into);
|
|
mjb_buffer output = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, form, &output, NULL),
|
|
MJB_STATUS_OK, test_name);
|
|
ATT_ASSERT(output.size, normalized_size, test_name);
|
|
|
|
char into_output[513];
|
|
ATT_ASSERT((int)(output.size < sizeof(into_output)), true, test_name);
|
|
|
|
if(output.size >= sizeof(into_output)) {
|
|
return 0;
|
|
}
|
|
|
|
memset(into_output, '#', sizeof(into_output));
|
|
output = mjb_buffer_make(into_output, output.size, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, form, &output, NULL),
|
|
MJB_STATUS_OK, test_name);
|
|
ATT_ASSERT(output.size, normalized_size, test_name);
|
|
ATT_ASSERT((int)memcmp(into_output, normalized, normalized_size), 0, test_name);
|
|
ATT_ASSERT(into_output[output.size], '#', test_name);
|
|
|
|
return test_ret;
|
|
}
|
|
|
|
static void check_nfkc_casefold_into(const char *source, size_t source_size,
|
|
mjb_encoding output_encoding, const mjb_result *allocated, const char *name) {
|
|
mjb_text text = mjb_text_utf8(source, source_size);
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, output_encoding);
|
|
|
|
MJB_TEST_COVERAGE(mjb_nfkc_casefold_into);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL), MJB_STATUS_OK,
|
|
name);
|
|
ATT_ASSERT(into.size, allocated->output_size, name);
|
|
|
|
char output[513];
|
|
ATT_ASSERT((int)(into.size < sizeof(output)), true, name);
|
|
|
|
if(into.size >= sizeof(output)) {
|
|
return;
|
|
}
|
|
|
|
memset(output, '#', sizeof(output));
|
|
into = mjb_buffer_make(output, into.size, output_encoding);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL), MJB_STATUS_OK,
|
|
name);
|
|
ATT_ASSERT(into.size, allocated->output_size, name);
|
|
ATT_ASSERT((int)memcmp(output, allocated->output, into.size), 0, name);
|
|
ATT_ASSERT(output[into.size], '#', name);
|
|
}
|
|
|
|
static void check_nfkc_casefold(const char *source, size_t source_size, const char *expected,
|
|
size_t expected_size, const char *name) {
|
|
mjb_result result = { NULL, 0, 0, false };
|
|
|
|
MJB_TEST_COVERAGE(mjb_nfkc_casefold);
|
|
mjb_text text = mjb_text_utf8(source, source_size);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &result, NULL),
|
|
MJB_STATUS_OK, name);
|
|
ATT_ASSERT(result.output_size, expected_size, name);
|
|
ATT_ASSERT((int)memcmp(result.output, expected, expected_size), 0, name);
|
|
ATT_ASSERT(result.owned, true, name);
|
|
check_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, &result, name);
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK, name);
|
|
}
|
|
|
|
static void test_nfkc_casefold(void) {
|
|
mjb_result result = { NULL, 0, 0, false };
|
|
|
|
mjb_text text = mjb_text_utf8(NULL, 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &result, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold rejects NULL buffer");
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(NULL, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &result, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold rejects NULL text");
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, NULL, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold rejects NULL result");
|
|
text = mjb_text_utf8("", 0);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &result, NULL),
|
|
MJB_STATUS_OK, "NFKC casefold accepts empty input");
|
|
ATT_ASSERT(result.owned, false, "NFKC casefold empty input is borrowed");
|
|
|
|
text = mjb_text_utf8(NULL, 1);
|
|
mjb_buffer into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold into rejects NULL buffer");
|
|
ATT_ASSERT(into.size, (size_t)0, "NFKC casefold into clears size after invalid input");
|
|
into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(NULL, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold into rejects NULL text");
|
|
ATT_ASSERT(into.size, (size_t)0, "NFKC casefold into clears size after NULL text");
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, NULL, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "NFKC casefold into rejects NULL output");
|
|
text = mjb_text_utf8("", 0);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_OK, "NFKC casefold into measures empty input");
|
|
ATT_ASSERT(into.size, (size_t)0, "NFKC casefold into empty size");
|
|
|
|
const char *into_input = "Stra\xC3\x9F"
|
|
"e\xC2\xAD";
|
|
text = mjb_text_utf8(into_input, 9);
|
|
into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_OK, "NFKC casefold into queries required size");
|
|
ATT_ASSERT(into.size, (size_t)7, "NFKC casefold into required size");
|
|
|
|
char into_output[8] = { '#', '#', '#', '#', '#', '#', '#', '#' };
|
|
into = mjb_buffer_make(into_output, 6, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_OUTPUT_TOO_SMALL, "NFKC casefold into reports a small output buffer");
|
|
ATT_ASSERT(into.size, (size_t)7, "NFKC casefold into preserves required size");
|
|
ATT_ASSERT((int)memcmp(into_output + 6, "##", 2), 0,
|
|
"NFKC casefold into is not written past its capacity");
|
|
|
|
into = mjb_buffer_make(into_output, 7, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold_into(&text, MJB_MALFORMED_STOP, &into, NULL),
|
|
MJB_STATUS_OK, "NFKC casefold into writes into exact capacity");
|
|
ATT_ASSERT(into.size, (size_t)7, "NFKC casefold into written size");
|
|
ATT_ASSERT((int)memcmp(into_output, "strasse", 7), 0, "NFKC casefold into output");
|
|
ATT_ASSERT(into_output[7], '#', "NFKC casefold into does not write a terminator");
|
|
|
|
check_nfkc_casefold("Stra\xC3\x9F"
|
|
"e\xC2\xAD",
|
|
9, "strasse", 7, "NFKC casefold folds sharp s and removes soft hyphen");
|
|
check_nfkc_casefold("\xEF\xAC\x83", 3, "ffi", 3,
|
|
"NFKC casefold expands compatibility ligature");
|
|
check_nfkc_casefold("A\xCC\x8A", 3, "\xC3\xA5", 2, "NFKC casefold composes mappings to NFC");
|
|
check_nfkc_casefold("\xE2\x84\xAA", 3, "k", 1,
|
|
"NFKC casefold applies compatibility case mapping");
|
|
check_nfkc_casefold("\xF0\x9F\x98\x80", 4, "\xF0\x9F\x98\x80", 4,
|
|
"NFKC casefold safely preserves a supplementary codepoint");
|
|
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_16LE, &result, NULL),
|
|
MJB_STATUS_OK, "NFKC casefold supports UTF-16 output");
|
|
ATT_ASSERT(result.output_size, (size_t)2, "NFKC casefold UTF-16 output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "a\0", 2), 0, "NFKC casefold UTF-16 output bytes");
|
|
check_nfkc_casefold_into("A", 1, MJB_ENC_UTF_16LE, &result,
|
|
"NFKC casefold into supports UTF-16 output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK, "NFKC casefold frees UTF-16 output");
|
|
}
|
|
|
|
// Verify Unicode R5 against every explicit NFKC_CF mapping. The normative string transform maps
|
|
// each character and then applies NFC, so expected property values are normalized before
|
|
// comparison.
|
|
static void test_nfkc_casefold_file(void) {
|
|
FILE *file = fopen("./unicode-data/UCD/DerivedNormalizationProps.txt", "r");
|
|
|
|
if(file == NULL) {
|
|
ATT_ASSERT("Not opened", "Opened file", "Valid NFKC casefold data file");
|
|
return;
|
|
}
|
|
|
|
char line[2048];
|
|
unsigned int current_line = 1;
|
|
|
|
while(fgets(line, sizeof(line), file) != NULL) {
|
|
char *first_semicolon = strchr(line, ';');
|
|
|
|
if(first_semicolon == NULL) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
char *property = first_semicolon + 1;
|
|
|
|
while(*property == ' ') {
|
|
++property;
|
|
}
|
|
|
|
if(strncmp(property, "NFKC_CF", 7) != 0) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
char *second_semicolon = strchr(property, ';');
|
|
|
|
if(second_semicolon == NULL) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
*first_semicolon = '\0';
|
|
char *comment = strchr(second_semicolon + 1, '#');
|
|
if(comment != NULL) {
|
|
*comment = '\0';
|
|
}
|
|
|
|
char *range_cursor = NULL;
|
|
unsigned long parsed_start = strtoul(line, &range_cursor, 16);
|
|
|
|
if(range_cursor == line || parsed_start > MJB_CODEPOINT_MAX) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
unsigned long parsed_end = parsed_start;
|
|
|
|
if(range_cursor[0] == '.' && range_cursor[1] == '.') {
|
|
char *range_start = range_cursor + 2;
|
|
parsed_end = strtoul(range_start, &range_cursor, 16);
|
|
|
|
if(range_cursor == range_start || parsed_end > MJB_CODEPOINT_MAX ||
|
|
parsed_end < parsed_start) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
mjb_codepoint start = (mjb_codepoint)parsed_start;
|
|
mjb_codepoint end = (mjb_codepoint)parsed_end;
|
|
|
|
char expected_mapping[128];
|
|
size_t mapping_size = get_string_from_codepoints(second_semicolon + 1,
|
|
sizeof(expected_mapping), expected_mapping);
|
|
mjb_result expected;
|
|
mjb_text text = mjb_text_utf8(expected_mapping, mapping_size);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP,
|
|
MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, &expected, NULL),
|
|
MJB_STATUS_OK, "Normalize expected NFKC casefold mapping");
|
|
|
|
for(mjb_codepoint codepoint = start; codepoint <= end; ++codepoint) {
|
|
char source[8];
|
|
unsigned int source_size = mjb_codepoint_encode(codepoint, source, sizeof(source),
|
|
MJB_ENC_UTF_8);
|
|
mjb_result actual;
|
|
char test_name[128];
|
|
snprintf(test_name, sizeof(test_name), "NFKC_CF #%u U+%04X", current_line, codepoint);
|
|
|
|
text = mjb_text_utf8(source, source_size);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8,
|
|
&actual, NULL),
|
|
MJB_STATUS_OK, test_name);
|
|
ATT_ASSERT(actual.output_size, expected.output_size, test_name);
|
|
ATT_ASSERT((int)memcmp(actual.output, expected.output, expected.output_size), 0,
|
|
test_name);
|
|
check_nfkc_casefold_into(source, source_size, MJB_ENC_UTF_8, &actual, test_name);
|
|
ATT_ASSERT_STATUS(mjb_result_free(&actual), MJB_STATUS_OK, test_name);
|
|
}
|
|
|
|
if(expected.owned) {
|
|
(void)mjb_result_free(&expected);
|
|
}
|
|
++current_line;
|
|
}
|
|
|
|
fclose(file);
|
|
}
|
|
|
|
static void test_normalization_malformed_policies(void) {
|
|
const char malformed[] = "A\x80"
|
|
"e\xCC\x81";
|
|
mjb_diagnostic diagnostic;
|
|
mjb_result result = { NULL, 0, 0, false };
|
|
|
|
mjb_text text = mjb_text_utf8(malformed, sizeof(malformed) - 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text,
|
|
MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, &result,
|
|
&diagnostic),
|
|
MJB_STATUS_MALFORMED_INPUT, "Normalize stop policy rejects malformed input");
|
|
ATT_ASSERT((unsigned int)diagnostic.error,
|
|
(unsigned int)MJB_TEXT_ERROR_UNEXPECTED_CONTINUATION,
|
|
"Normalize reports malformed input kind");
|
|
ATT_ASSERT(diagnostic.byte_offset, (size_t)1, "Normalize reports malformed byte offset");
|
|
ATT_ASSERT(diagnostic.byte_length, (size_t)1, "Normalize reports malformed byte length");
|
|
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text,
|
|
MJB_MALFORMED_REPLACE, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, &result,
|
|
&diagnostic),
|
|
MJB_STATUS_OK, "Normalize replace policy accepts malformed input");
|
|
ATT_ASSERT(result.output_size, (size_t)6, "Normalize replacement output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "A\xEF\xBF\xBD\xC3\xA9", result.output_size), 0,
|
|
"Normalize replacement output");
|
|
ATT_ASSERT(diagnostic.byte_offset, (size_t)1,
|
|
"Normalize replacement retains malformed diagnostic");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK,
|
|
"Free normalization replacement result");
|
|
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text,
|
|
MJB_MALFORMED_SKIP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8, &result,
|
|
&diagnostic),
|
|
MJB_STATUS_OK, "Normalize skip policy accepts malformed input");
|
|
ATT_ASSERT(result.output_size, (size_t)3, "Normalize skip output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "A\xC3\xA9", result.output_size), 0,
|
|
"Normalize skip output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK, "Free normalization skip result");
|
|
|
|
// Out-of-range UTF-32 units and UTF-16 units after an unpaired high surrogate.
|
|
const char malformed_utf32[] = { '\x00', '\x00', '\x11', '\x00', 'A', '\0', '\0', '\0' };
|
|
mjb_text text_utf32 = mjb_text_utf32le(malformed_utf32, sizeof(malformed_utf32));
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text_utf32, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_32LE, &result, &diagnostic),
|
|
MJB_STATUS_MALFORMED_INPUT, "Normalize stop policy rejects out-of-range UTF-32");
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text_utf32, MJB_MALFORMED_REPLACE, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_32LE, &result, &diagnostic),
|
|
MJB_STATUS_OK, "Normalize replace policy repairs out-of-range UTF-32");
|
|
ATT_ASSERT(result.output_size, (size_t)8, "Normalize UTF-32 replacement output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "\xFD\xFF\0\0A\0\0\0", result.output_size), 0,
|
|
"Normalize UTF-32 replacement output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK,
|
|
"Free normalization UTF-32 replacement result");
|
|
|
|
const char malformed_utf16[] = { '\x00', '\xD8', 'A', '\0' };
|
|
mjb_text text_utf16 = mjb_text_utf16le(malformed_utf16, sizeof(malformed_utf16));
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text_utf16, MJB_MALFORMED_REPLACE, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_UTF_8, &result, &diagnostic),
|
|
MJB_STATUS_OK, "Normalize replace policy repairs unpaired UTF-16 surrogate");
|
|
ATT_ASSERT(result.output_size, (size_t)4, "Normalize UTF-16 replacement output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "\xEF\xBF\xBD"
|
|
"A",
|
|
result.output_size),
|
|
0, "Normalize UTF-16 replacement keeps the unit after the surrogate");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK,
|
|
"Free normalization UTF-16 replacement result");
|
|
|
|
mjb_buffer measure = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_REPLACE, MJB_NORMALIZATION_NFC,
|
|
&measure, &diagnostic),
|
|
MJB_STATUS_OK, "Normalize into measures replacement output");
|
|
ATT_ASSERT(measure.size, (size_t)6, "Normalize into replacement size");
|
|
|
|
const char malformed_casefold[] = "A\x80"
|
|
"B";
|
|
text = mjb_text_utf8(malformed_casefold, sizeof(malformed_casefold) - 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_REPLACE, MJB_ENC_UTF_8, &result,
|
|
&diagnostic),
|
|
MJB_STATUS_OK, "NFKC casefold replace policy accepts malformed input");
|
|
ATT_ASSERT(result.output_size, (size_t)5, "NFKC casefold replacement output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "a\xEF\xBF\xBD"
|
|
"b",
|
|
result.output_size),
|
|
0, "NFKC casefold replacement output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK,
|
|
"Free NFKC casefold replacement result");
|
|
|
|
text = mjb_text_utf8(malformed_casefold, sizeof(malformed_casefold) - 1);
|
|
ATT_ASSERT_STATUS(mjb_nfkc_casefold(&text, MJB_MALFORMED_SKIP, MJB_ENC_UTF_8, &result,
|
|
&diagnostic),
|
|
MJB_STATUS_OK, "NFKC casefold skip policy accepts malformed input");
|
|
ATT_ASSERT(result.output_size, (size_t)2, "NFKC casefold skip output size");
|
|
ATT_ASSERT((int)memcmp(result.output, "ab", result.output_size), 0,
|
|
"NFKC casefold skip output");
|
|
ATT_ASSERT_STATUS(mjb_result_free(&result), MJB_STATUS_OK,
|
|
"Free NFKC casefold skip result");
|
|
}
|
|
|
|
/**
|
|
* Run unicode-data/UCD/NormalizationTest.txt tests
|
|
*/
|
|
ATT_TEST(normalization) {
|
|
char line[1024];
|
|
unsigned int current_line = 1;
|
|
// unsigned int index = 0;
|
|
mjb_result guard_result;
|
|
|
|
test_nfkc_casefold();
|
|
test_nfkc_casefold_file();
|
|
test_normalization_malformed_policies();
|
|
|
|
mjb_text text = mjb_text_utf8(NULL, 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8,
|
|
&guard_result, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize rejects NULL buffer");
|
|
ATT_ASSERT_STATUS(mjb_normalize(NULL, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8,
|
|
&guard_result, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize rejects NULL text");
|
|
text = mjb_text_utf8("", 0);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_8,
|
|
NULL, NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize rejects NULL result");
|
|
|
|
char noncharacter_source[] = "\xF1\x8F\xBF\xBE>\xCC\xB8";
|
|
char noncharacter_normalized[] = "\xF1\x8F\xBF\xBE\xE2\x89\xAF";
|
|
check_normalization(noncharacter_source, 7, noncharacter_normalized, 7, MJB_NORMALIZATION_NFC,
|
|
0, "NFC preserves noncharacters while composing");
|
|
char hangul_source[] = "\xE1\x84\x8F\xE1\x85\xB5\xE1\x86\xBF.>\xCC\xB8";
|
|
char hangul_normalized[] = "\xED\x82\xBC.\xE2\x89\xAF";
|
|
check_normalization(hangul_source, 13, hangul_normalized, 7, MJB_NORMALIZATION_NFC, 0,
|
|
"NFC composes after Hangul compaction");
|
|
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, MJB_ENC_UTF_16LE,
|
|
&guard_result, NULL),
|
|
MJB_STATUS_OK, "Normalize converts output encoding for already-normalized input");
|
|
ATT_ASSERT(guard_result.owned, true,
|
|
"Normalize converted already-normalized input owned");
|
|
ATT_ASSERT(guard_result.output_size, (size_t)2,
|
|
"Normalize converted already-normalized input size");
|
|
ATT_ASSERT((int)memcmp(guard_result.output, "A\0", 2), 0,
|
|
"Normalize converted already-normalized input bytes");
|
|
if(guard_result.owned) {
|
|
(void)mjb_result_free(&guard_result);
|
|
}
|
|
|
|
text = mjb_text_utf8("\xC3\xA9", 2);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_ASCII, &guard_result, NULL),
|
|
MJB_STATUS_UNSUPPORTED, "Normalize rejects unrepresentable ASCII output for normalized input");
|
|
text = mjb_text_utf8("e\xCC\x81", 3);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC,
|
|
MJB_ENC_ASCII, &guard_result, NULL),
|
|
MJB_STATUS_UNSUPPORTED, "Normalize rejects unrepresentable ASCII output while composing");
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC,
|
|
(mjb_encoding)(MJB_ENC_UTF_8 | MJB_ENC_UTF_16LE), &guard_result, NULL),
|
|
MJB_STATUS_INVALID_ENCODING, "Normalize rejects combined output encoding flags");
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
text = mjb_text_utf8(NULL, 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize into rejects NULL buffer");
|
|
ATT_ASSERT(into.size, (size_t)0, "Normalize into clears size after invalid input");
|
|
into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(NULL, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize into rejects NULL text");
|
|
ATT_ASSERT(into.size, (size_t)0, "Normalize into clears size after NULL text");
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, NULL,
|
|
NULL),
|
|
MJB_STATUS_INVALID_ARGUMENT, "Normalize into rejects NULL output");
|
|
|
|
into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, (mjb_normalization)99, &into,
|
|
NULL),
|
|
MJB_STATUS_INVALID_FORM, "Normalize into rejects invalid form");
|
|
ATT_ASSERT(into.size, (size_t)0, "Normalize into clears size after invalid form");
|
|
|
|
into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_16);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_INVALID_ENCODING, "Normalize into rejects output without byte order");
|
|
ATT_ASSERT(into.size, (size_t)0, "Normalize into clears size after invalid output encoding");
|
|
|
|
into = mjb_buffer_make(NULL, 9, MJB_ENC_UTF_8);
|
|
text = mjb_text_utf8("", 0);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into measures empty input");
|
|
ATT_ASSERT(into.size, (size_t)0, "Normalize into empty size");
|
|
|
|
const char *into_input = "Cafe\xCC\x81";
|
|
into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
text = mjb_text_utf8(into_input, 6);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into queries required size");
|
|
ATT_ASSERT(into.size, (size_t)5, "Normalize into required size");
|
|
|
|
char into_output[6] = { '#', '#', '#', '#', '#', '#' };
|
|
into = mjb_buffer_make(into_output, 4, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_OUTPUT_TOO_SMALL, "Normalize into reports a small output buffer");
|
|
ATT_ASSERT(into.size, (size_t)5, "Normalize into preserves required size");
|
|
ATT_ASSERT((int)memcmp(into_output + 4, "##", 2), 0,
|
|
"Normalize into is not written past its capacity");
|
|
|
|
into = mjb_buffer_make(into_output, 5, MJB_ENC_UTF_8);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into writes into exact capacity");
|
|
ATT_ASSERT(into.size, (size_t)5, "Normalize into written size");
|
|
ATT_ASSERT((int)memcmp(into_output, "Caf\xC3\xA9", 5), 0, "Normalize into output");
|
|
ATT_ASSERT(into_output[5], '#', "Normalize into does not write a terminator");
|
|
|
|
char nfd_output[4] = { '#', '#', '#', '#' };
|
|
into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
text = mjb_text_utf8("\xC3\xA9", 2);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFD, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into measures decomposed output");
|
|
ATT_ASSERT(into.size, (size_t)3, "Normalize into decomposed required size");
|
|
into.data = nfd_output;
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFD, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into writes decomposed output");
|
|
ATT_ASSERT((int)memcmp(nfd_output, "e\xCC\x81", 3), 0,
|
|
"Normalize into decomposed output bytes");
|
|
ATT_ASSERT(nfd_output[3], '#', "Normalize into decomposed output has no terminator");
|
|
|
|
char utf16_output[3] = { '#', '#', '#' };
|
|
into = mjb_buffer_make(utf16_output, sizeof(utf16_output) - 1, MJB_ENC_UTF_16LE);
|
|
text = mjb_text_utf8("A", 1);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFC, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into converts already-normalized output encoding");
|
|
ATT_ASSERT(into.size, (size_t)2, "Normalize into converted output size");
|
|
ATT_ASSERT((int)memcmp(utf16_output, "A\0", 2), 0,
|
|
"Normalize into converted output bytes");
|
|
ATT_ASSERT(utf16_output[2], '#', "Normalize into converted output has no terminator");
|
|
|
|
// Chain the written output into another call: NFD of U+00E9 is U+0065 U+0301 in UTF-16LE.
|
|
char chained_output[8];
|
|
into = mjb_buffer_make(chained_output, sizeof(chained_output), MJB_ENC_UTF_16LE);
|
|
text = mjb_text_utf8("\xC3\xA9", 2);
|
|
ATT_ASSERT_STATUS(mjb_normalize_into(&text, MJB_MALFORMED_STOP, MJB_NORMALIZATION_NFD, &into,
|
|
NULL),
|
|
MJB_STATUS_OK, "Normalize into writes output to chain");
|
|
ATT_ASSERT(into.size, (size_t)4, "Normalize into chained output size");
|
|
|
|
mjb_text chained = mjb_buffer_text(&into);
|
|
size_t chained_count = 0;
|
|
ATT_ASSERT_STATUS(mjb_codepoint_count(&chained, MJB_MALFORMED_STOP, &chained_count, NULL),
|
|
MJB_STATUS_OK, "Count the chained normalize output");
|
|
ATT_ASSERT(chained_count, (size_t)2, "Chained normalize output has two codepoints");
|
|
|
|
// 256 characters is enough for any test.
|
|
const char source[256] = { 0 };
|
|
const char nfc[256] = { 0 };
|
|
const char nfd[256] = { 0 };
|
|
const char nfkc[256] = { 0 };
|
|
const char nfkd[256] = { 0 };
|
|
|
|
size_t source_size = 0;
|
|
size_t nfc_size = 0;
|
|
size_t nfd_size = 0;
|
|
size_t nfkc_size = 0;
|
|
size_t nfkd_size = 0;
|
|
|
|
FILE *file = fopen("./unicode-data/UCD/NormalizationTest.txt", "r");
|
|
|
|
if(file == NULL) {
|
|
ATT_ASSERT("Not opened", "Opened file", "Valid normalization test file");
|
|
|
|
return;
|
|
}
|
|
|
|
// Parse the file
|
|
while(fgets(line, 1024, file)) {
|
|
if(line[0] == '#' || line[0] == '@' || strnlen(line, 1024) <= 1) {
|
|
++current_line;
|
|
|
|
continue;
|
|
}
|
|
|
|
// Reset everything
|
|
source_size = 0;
|
|
nfc_size = 0;
|
|
nfd_size = 0;
|
|
nfkc_size = 0;
|
|
nfkd_size = 0;
|
|
|
|
char *token, *string, *tofree;
|
|
tofree = string = strdup(line);
|
|
unsigned int field = 0;
|
|
|
|
while((token = mjb_test_strsep(&string, ";")) != NULL) {
|
|
switch(field) {
|
|
case 0: // Source
|
|
source_size = get_string_from_codepoints(token, 256, (char *)source);
|
|
break;
|
|
|
|
case 1: // NFC
|
|
nfc_size = get_string_from_codepoints(token, 256, (char *)nfc);
|
|
break;
|
|
|
|
case 2: // NFD
|
|
nfd_size = get_string_from_codepoints(token, 256, (char *)nfd);
|
|
break;
|
|
|
|
case 3: // NFKC
|
|
nfkc_size = get_string_from_codepoints(token, 256, (char *)nfkc);
|
|
break;
|
|
|
|
case 4: // NFKD
|
|
nfkd_size = get_string_from_codepoints(token, 256, (char *)nfkd);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
// Skip trailing comments
|
|
if(++field == 5) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
free(tofree);
|
|
|
|
/**
|
|
* NFD
|
|
* nfd == toNFD(source) == toNFD(nfc) == toNFD(nfd)
|
|
* nfkd == toNFD(nfkc) == toNFD(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfd, nfd_size,
|
|
MJB_NORMALIZATION_NFD, current_line, "nfd == toNFD(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfd, nfd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfd, nfd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFD,
|
|
current_line, "nfd == toNFD(nfkd)");
|
|
|
|
/**
|
|
* NFKD
|
|
* nfkd == toNFKD(source) == toNFKD(nfc) == toNFKD(nfd) == toNFKD(nfkc) == toNFKD(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFKD,
|
|
current_line, "nfkd == toNFKD(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfkd, nfkd_size, MJB_NORMALIZATION_NFKD,
|
|
current_line, "nfkd == toNFKD(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkd, nfkd_size,
|
|
MJB_NORMALIZATION_NFKD, current_line, "nfkd == toNFKD(nfkd)");
|
|
|
|
/**
|
|
* NFC
|
|
* nfc == toNFC(source) == toNFC(nfc) == toNFC(nfd)
|
|
* nfkc == toNFC(nfkc) == toNFC(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfc, nfc_size,
|
|
MJB_NORMALIZATION_NFC, current_line, "nfc == toNFC(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfc, nfc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfc, nfc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFC,
|
|
current_line, "nfc == toNFC(nfkd)");
|
|
|
|
/**
|
|
* NFKC
|
|
* nfkc == toNFKC(source) == toNFKC(nfc) == toNFKC(nfd) == toNFKC(nfkc) == toNFKC(nfkd)
|
|
*/
|
|
check_normalization((char *)source, source_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(source)");
|
|
check_normalization((char *)nfc, nfc_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFKC,
|
|
current_line, "nfkc == toNFKC(nfc)");
|
|
check_normalization((char *)nfd, nfd_size, (char *)nfkc, nfkc_size, MJB_NORMALIZATION_NFKC,
|
|
current_line, "nfkc == toNFKC(nfd)");
|
|
check_normalization((char *)nfkc, nfkc_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(nfkc)");
|
|
check_normalization((char *)nfkd, nfkd_size, (char *)nfkc, nfkc_size,
|
|
MJB_NORMALIZATION_NFKC, current_line, "nfkc == toNFKC(nfkd)");
|
|
|
|
memset((void *)source, 0, 256);
|
|
memset((void *)nfc, 0, 256);
|
|
memset((void *)nfd, 0, 256);
|
|
memset((void *)nfkc, 0, 256);
|
|
memset((void *)nfkd, 0, 256);
|
|
|
|
++current_line;
|
|
}
|
|
|
|
fclose(file);
|
|
}
|