From 212f1cc9109aa0deb9e5d990bb2dba2b167618fb Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Thu, 15 Oct 2026 12:31:21 +0300 Subject: Keep installed headers from shadowing or polluting host names The pkg-config file put include/term on the host's include path, so ncurses' term.h could be picked up in place of ours. Hosts now write and the package adds only its include directory. The installed Unicode API had unprefixed names that collide with host symbols and macros. Rename them: term_ for functions, types, and static tables, TERM_ for macros and include guards. unicode_data.inc and tools/generate_unicode.py are renamed to match. The generator's inputs here are Unicode 15.1 and the checked-in table is 18.0.0, so the table data was not regenerated. Only the declarations were checked against the generator. utf8.h casts the malloc result so it compiles as C++. header_test.sh now includes with only include/ on the path. It also checks that a host which defines unicode_width, UTF8_ACCEPT, GRAPHEME_MAX_LEN, in_ranges, and similar names compiles with the Unicode headers as C11 and C++, in both declaration orders. Co-Authored-By: Claude Haiku 5.5 --- tests/gstore_test.c | 38 +++++++++++----------- tests/header_test.sh | 75 +++++++++++++++++++++++++++++++++++--------- tests/hist_test.c | 12 +++---- tests/model.c | 26 +++++++-------- tests/model.h | 2 +- tests/model_test.c | 12 +++---- tests/term_test.c | 18 +++++------ tests/unicode/unicode_test.c | 68 +++++++++++++++++++-------------------- 8 files changed, 148 insertions(+), 103 deletions(-) (limited to 'tests') diff --git a/tests/gstore_test.c b/tests/gstore_test.c index 637b136..ea582f3 100644 --- a/tests/gstore_test.c +++ b/tests/gstore_test.c @@ -32,7 +32,7 @@ static void test_direct_unit(void) /* Cluster returns a handle. */ uint32_t c1[2] = { 'e', 0x0301 }; uint32_t h1 = term_gstore_intern(g, c1, 2); - assert(grapheme_is_handle(h1)); + assert(term_grapheme_is_handle(h1)); assert(term_gstore_count(g) == 1); /* Identical cluster deduplicates to the same handle. */ @@ -43,17 +43,17 @@ static void test_direct_unit(void) /* Different cluster gets a distinct handle. */ uint32_t c2[2] = { 'a', 0x0300 }; uint32_t h2 = term_gstore_intern(g, c2, 2); - assert(grapheme_is_handle(h2)); + assert(term_grapheme_is_handle(h2)); assert(h2 != h1); assert(term_gstore_count(g) == 2); /* Expand both handles. */ - uint32_t out[GRAPHEME_MAX_LEN] = {0}; - int n = term_gstore_expand(g, h1, out, GRAPHEME_MAX_LEN); + uint32_t out[TERM_GRAPHEME_MAX_LEN] = {0}; + int n = term_gstore_expand(g, h1, out, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(out[0] == 'e' && out[1] == 0x0301); - n = term_gstore_expand(g, h2, out, GRAPHEME_MAX_LEN); + n = term_gstore_expand(g, h2, out, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(out[0] == 'a' && out[1] == 0x0300); @@ -63,20 +63,20 @@ static void test_direct_unit(void) assert(term_gstore_count(g) == 1); /* h1 is still valid. */ - n = term_gstore_expand(g, h1, out, GRAPHEME_MAX_LEN); + n = term_gstore_expand(g, h1, out, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(out[0] == 'e' && out[1] == 0x0301); /* h2 was reclaimed. */ - n = term_gstore_expand(g, h2, out, GRAPHEME_MAX_LEN); + n = term_gstore_expand(g, h2, out, TERM_GRAPHEME_MAX_LEN); assert(n == 1 && out[0] == 0xFFFD); /* New cluster can reuse the reclaimed slot. */ uint32_t c3[3] = { 'o', 0x0308, 0x0301 }; uint32_t h3 = term_gstore_intern(g, c3, 3); - assert(grapheme_is_handle(h3)); + assert(term_grapheme_is_handle(h3)); assert(term_gstore_count(g) == 2); - n = term_gstore_expand(g, h3, out, GRAPHEME_MAX_LEN); + n = term_gstore_expand(g, h3, out, TERM_GRAPHEME_MAX_LEN); assert(n == 3); assert(out[0] == 'o' && out[1] == 0x0308 && out[2] == 0x0301); @@ -105,8 +105,8 @@ static void test_two_terminals_isolation(void) const struct term_cell *row = term_snapshot_row(snap, 0); assert(row[0].text & TERM_TEXT_CLUSTER); - uint32_t cps[GRAPHEME_MAX_LEN] = {0}; - int n = term_snapshot_cluster(snap, row[0].text, cps, GRAPHEME_MAX_LEN); + uint32_t cps[TERM_GRAPHEME_MAX_LEN] = {0}; + int n = term_snapshot_cluster(snap, row[0].text, cps, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(cps[0] == 'e' && cps[1] == 0x0301); @@ -143,16 +143,16 @@ static void test_screen_clusters_survive_sweep(void) const struct term_cell *row = term_snapshot_row(snap, 0); assert(row[0].text & TERM_TEXT_CLUSTER); - uint32_t cps[GRAPHEME_MAX_LEN] = {0}; - int n = term_snapshot_cluster(snap, row[0].text, cps, GRAPHEME_MAX_LEN); + uint32_t cps[TERM_GRAPHEME_MAX_LEN] = {0}; + int n = term_snapshot_cluster(snap, row[0].text, cps, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(cps[0] == 'a' && cps[1] == 0x0300); - n = term_snapshot_cluster(snap, row[1].text, cps, GRAPHEME_MAX_LEN); + n = term_snapshot_cluster(snap, row[1].text, cps, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(cps[0] == 'b' && cps[1] == 0x0301); - n = term_snapshot_cluster(snap, row[2].text, cps, GRAPHEME_MAX_LEN); + n = term_snapshot_cluster(snap, row[2].text, cps, TERM_GRAPHEME_MAX_LEN); assert(n == 2); assert(cps[0] == 'c' && cps[1] == 0x0302); @@ -183,8 +183,8 @@ static void test_more_than_65536_clusters(void) size_t len = 0; buf[len++] = base; - len += (size_t)utf8_encode(cp1, buf + len); - len += (size_t)utf8_encode(cp2, buf + len); + len += (size_t)term_utf8_encode(cp1, buf + len); + len += (size_t)term_utf8_encode(cp2, buf + len); buf[len++] = '\r'; buf[len++] = '\n'; @@ -204,8 +204,8 @@ static void test_more_than_65536_clusters(void) const struct term_cell *row = term_snapshot_row(snap, y); assert(row[0].text & TERM_TEXT_CLUSTER); - uint32_t cps[GRAPHEME_MAX_LEN] = {0}; - int n = term_snapshot_cluster(snap, row[0].text, cps, GRAPHEME_MAX_LEN); + uint32_t cps[TERM_GRAPHEME_MAX_LEN] = {0}; + int n = term_snapshot_cluster(snap, row[0].text, cps, TERM_GRAPHEME_MAX_LEN); assert(n == 3); /* Live cluster count must be small (only bounded screen and history). */ diff --git a/tests/header_test.sh b/tests/header_test.sh index 4ba091b..9fd1041 100644 --- a/tests/header_test.sh +++ b/tests/header_test.sh @@ -1,25 +1,70 @@ #!/usr/bin/env bash -# A host includes term.h and nothing else. The header compiles alone as -# strict C11 and as C++, with only include/term on the include path. +# A host includes and nothing else. The package puts only the +# parent of term/ on the include path. The headers compile alone as strict +# C11 and as C++. set -euo pipefail cd "${1:-$(dirname "$0")/..}" cc=${CC:-cc} cxx=${CXX:-c++} status=0 -echo '#include "term.h"' | "$cc" -std=c11 -Wall -Wextra -Wpedantic -Werror \ - -Iinclude/term -x c -fsyntax-only - || { echo "term.h: not valid C11" >&2; status=1; } -if command -v "$cxx" >/dev/null; then - echo '#include "term.h"' | "$cxx" -std=c++17 -Wall -Wextra -Wpedantic -Werror \ - -Iinclude/term -x c++ -fsyntax-only - || { echo "term.h: not valid C++" >&2; status=1; } -fi + +# check LABEL: compile the code on stdin as C11 and as C++, with include/ +# on the path. +check() { + local label=$1 src + src=$(cat) + printf '%s\n' "$src" | "$cc" -std=c11 -Wall -Wextra -Wpedantic -Werror \ + -Iinclude -x c -fsyntax-only - || { echo "$label: not valid C11" >&2; status=1; } + if command -v "$cxx" >/dev/null; then + printf '%s\n' "$src" | "$cxx" -std=c++17 -Wall -Wextra -Wpedantic -Werror \ + -Iinclude -x c++ -fsyntax-only - || { echo "$label: not valid C++" >&2; status=1; } + fi +} + +check term.h <<'EOF' +#include +EOF # Apple headers define wide before a host includes the Unicode API. -unicode_header=$'typedef struct wide { int value; } wide;\n#include "unicode/width.h"' -echo "$unicode_header" | "$cc" -std=c11 -Wall -Wextra -Wpedantic -Werror \ - -Iinclude/term -x c -fsyntax-only - || { echo "unicode/width.h: conflicts with host types" >&2; status=1; } -if command -v "$cxx" >/dev/null; then - echo "$unicode_header" | "$cxx" -std=c++17 -Wall -Wextra -Wpedantic -Werror \ - -Iinclude/term -x c++ -fsyntax-only - || { echo "unicode/width.h: conflicts with C++ host types" >&2; status=1; } -fi +check "unicode/width.h: conflicts with host types" <<'EOF' +typedef struct wide { int value; } wide; +#include +EOF + +# A host that uses the names the Unicode API had before the term_ and +# TERM_ prefixes. The API must not clash with them in either order. +check "unicode API: host names declared first" <<'EOF' +int unicode_width(int); +static int in_ranges; +static int zero_width; +static int utf8d; +struct unicode_range { int lo; }; +#define UTF8_ACCEPT 7 +#define GRAPHEME_MAX_LEN 3 +#define IN_TABLE 1 +#define UNICODE_DATA_VERSION "host" +#define UTF_8_H +#include +#include +#include +int host_use(void) +{ + return in_ranges + zero_width + utf8d + unicode_width(1) + UTF8_ACCEPT + GRAPHEME_MAX_LEN; +} +EOF + +check "unicode API: host names declared after" <<'EOF' +#include +#include +#include +int unicode_width(int); +static int in_ranges; +#define UTF8_ACCEPT 7 +#define GRAPHEME_MAX_LEN 3 +int host_use(void) +{ + return in_ranges + unicode_width(1) + UTF8_ACCEPT + GRAPHEME_MAX_LEN; +} +EOF exit $status diff --git a/tests/hist_test.c b/tests/hist_test.c index d772c5a..691ae3c 100644 --- a/tests/hist_test.c +++ b/tests/hist_test.c @@ -129,9 +129,9 @@ static void test_clusters_roundtrip(void) struct cell out[2]; assert(term_hist_decode_segment(&h, 0, 0, 2, out, g) == 2); - assert(grapheme_is_handle(out[0].c)); - uint32_t got[GRAPHEME_MAX_LEN]; - assert(term_gstore_expand(g, out[0].c, got, GRAPHEME_MAX_LEN) == 3); + assert(term_grapheme_is_handle(out[0].c)); + uint32_t got[TERM_GRAPHEME_MAX_LEN]; + assert(term_gstore_expand(g, out[0].c, got, TERM_GRAPHEME_MAX_LEN) == 3); assert(got[0] == cps[0] && got[1] == cps[1] && got[2] == cps[2]); assert(out[1].c == 'X'); @@ -623,9 +623,9 @@ static void test_fuzz_rows(void) continue; } assert(a->fg == b->fg && a->flags == b->flags); - if (grapheme_is_handle(a->c)) { - uint32_t got[GRAPHEME_MAX_LEN]; - assert(term_gstore_expand(g, b->c, got, GRAPHEME_MAX_LEN) == 2 && got[1] == 0x0301); + if (term_grapheme_is_handle(a->c)) { + uint32_t got[TERM_GRAPHEME_MAX_LEN]; + assert(term_gstore_expand(g, b->c, got, TERM_GRAPHEME_MAX_LEN) == 2 && got[1] == 0x0301); } else { assert(a->c == b->c); } diff --git a/tests/model.c b/tests/model.c index 8dead6c..7934d4f 100644 --- a/tests/model.c +++ b/tests/model.c @@ -31,7 +31,7 @@ #define STR_KEEP 1024 struct cell { - uint32_t cp[GRAPHEME_MAX_LEN]; + uint32_t cp[TERM_GRAPHEME_MAX_LEN]; uint8_t n, width; uint16_t attrs; term_color fg, bg; @@ -98,7 +98,7 @@ struct model { bool tabs[MAX_TABS]; struct keyboard keyboard[2]; /* The last printed glyph, for REP and cluster joins. */ - uint32_t last[GRAPHEME_MAX_LEN]; + uint32_t last[TERM_GRAPHEME_MAX_LEN]; int last_n; int prompt; /* screen row of the last OSC 133;A, or -1 */ unsigned flags; @@ -158,7 +158,7 @@ static bool pristine(const struct model *m) static bool is_ri(uint32_t cp) { - return cp - 0x1F1E6u <= 0x19u && grapheme_regional_indicator(cp); + return cp - 0x1F1E6u <= 0x19u && term_grapheme_regional_indicator(cp); } /* ---- replies ---- */ @@ -417,13 +417,13 @@ static void fill_gap(struct row *r, int x) static uint16_t presentation(const uint32_t *cps, int n) { - bool pictographic = unicode_extended_pictographic(cps[0]); + bool pictographic = term_unicode_extended_pictographic(cps[0]); bool emoji = false; for (int i = 1; i < n; i++) { if (cps[i] == 0xFE0F) return TERM_ATTR_EMOJI; if (cps[i] == 0xFE0E) return TERM_ATTR_TEXT; - if (cps[i] == 0x20E3 || grapheme_regional_indicator(cps[i]) || - (pictographic && (cps[i] == 0x200D || grapheme_emoji_modifier(cps[i]) || + if (cps[i] == 0x20E3 || term_grapheme_regional_indicator(cps[i]) || + (pictographic && (cps[i] == 0x200D || term_grapheme_emoji_modifier(cps[i]) || (cps[i] >= 0xE0020 && cps[i] <= 0xE007F)))) emoji = true; } @@ -443,7 +443,7 @@ static void put_glyph(struct model *m, const uint32_t *cps, int n, uint16_t attr { if (n > 1) attrs = (uint16_t)((attrs & ~(TERM_ATTR_EMOJI | TERM_ATTR_TEXT)) | presentation(cps, n)); - int width = unicode_width(cps[0]) == 2 ? 2 : 1; + int width = term_unicode_width(cps[0]) == 2 ? 2 : 1; if (attrs & TERM_ATTR_EMOJI) width = 2; if (attrs & TERM_ATTR_TEXT) width = 1; if (m->pending) wrap_to_next_row(m); @@ -495,14 +495,14 @@ static void put_glyph(struct model *m, const uint32_t *cps, int n, uint16_t attr static bool joins(uint32_t cp, int width) { if (cp < 0x300) return false; - return width == 0 || grapheme_emoji_modifier(cp) || - (width == 1 && unicode_spacing_mark(cp)); + return width == 0 || term_grapheme_emoji_modifier(cp) || + (width == 1 && term_unicode_spacing_mark(cp)); } static bool cluster_joins(const uint32_t *cps, int n, uint32_t cp, int width) { if (joins(cp, width)) return true; - if (cps[n - 1] == 0x200D) return unicode_extended_pictographic(cp); + if (cps[n - 1] == 0x200D) return term_unicode_extended_pictographic(cp); return n == 1 && is_ri(cp) && is_ri(cps[0]); } @@ -527,10 +527,10 @@ static bool extend_cluster(struct model *m, uint32_t cp, int width) return joins(cp, width); if (!cluster_joins(prev->cp, prev->n, cp, width)) return false; - if (prev->n >= GRAPHEME_MAX_LEN) + if (prev->n >= TERM_GRAPHEME_MAX_LEN) return true; struct cell old = *prev; - uint32_t cps[GRAPHEME_MAX_LEN]; + uint32_t cps[TERM_GRAPHEME_MAX_LEN]; memcpy(cps, old.cp, (size_t)old.n * sizeof(*cps)); cps[old.n] = cp; int n = old.n + 1; @@ -590,7 +590,7 @@ static void print_cp(struct model *m, uint32_t cp) emit(m, cp); return; } - int width = unicode_width(cp); + int width = term_unicode_width(cp); if (!extend_cluster(m, cp, width)) emit(m, cp); } diff --git a/tests/model.h b/tests/model.h index 9a4316f..a6b5572 100644 --- a/tests/model.h +++ b/tests/model.h @@ -19,7 +19,7 @@ #include struct model_cell { - uint32_t cp[GRAPHEME_MAX_LEN]; + uint32_t cp[TERM_GRAPHEME_MAX_LEN]; uint8_t n; /* code points; 0 for the right half of a wide cell */ uint8_t width; /* 1, 2 for a wide cell, 0 for its right half */ uint16_t attrs; /* TERM_ATTR_* */ diff --git a/tests/model_test.c b/tests/model_test.c index 395097e..6af30b5 100644 --- a/tests/model_test.c +++ b/tests/model_test.c @@ -122,8 +122,8 @@ static void dump_all(struct model *m, struct term *t, struct term_snapshot *snap term_snapshot_update(snap, t); const struct term_cell *row = term_snapshot_row(snap, r < total ? 0 : r - total); for (int x = 0; x < size.x; x++) { - uint32_t cp[GRAPHEME_MAX_LEN] = {0}; - if (row[x].text) term_snapshot_cluster(snap, row[x].text, cp, GRAPHEME_MAX_LEN); + uint32_t cp[TERM_GRAPHEME_MAX_LEN] = {0}; + if (row[x].text) term_snapshot_cluster(snap, row[x].text, cp, TERM_GRAPHEME_MAX_LEN); fputc(glyph(cp[0]), log); } } else { @@ -201,9 +201,9 @@ static const char *compare(struct model *m, struct term *t, struct term_snapshot const struct model_cell *want_row = model_row(m, retained); for (int x = 0; x < size.x; x++) { const struct model_cell *e = &want_row[x]; - uint32_t cps[GRAPHEME_MAX_LEN] = {0}; + uint32_t cps[TERM_GRAPHEME_MAX_LEN] = {0}; int n = row[x].text ? term_snapshot_cluster(snap, row[x].text, cps, - GRAPHEME_MAX_LEN) : 0; + TERM_GRAPHEME_MAX_LEN) : 0; if (n == e->n && row[x].width == e->width && row[x].attrs == e->attrs && row[x].fg == e->fg && row[x].bg == e->bg && !memcmp(cps, e->cp, (size_t)n * sizeof(*cps))) @@ -211,9 +211,9 @@ static const char *compare(struct model *m, struct term *t, struct term_snapshot if (log) { fprintf(log, "retained row %d (offset %d):\n term: ", retained, offset); for (int k = 0; k < size.x; k++) { - uint32_t cp[GRAPHEME_MAX_LEN] = {0}; + uint32_t cp[TERM_GRAPHEME_MAX_LEN] = {0}; if (row[k].text) - term_snapshot_cluster(snap, row[k].text, cp, GRAPHEME_MAX_LEN); + term_snapshot_cluster(snap, row[k].text, cp, TERM_GRAPHEME_MAX_LEN); fputc(glyph(cp[0]), log); } fprintf(log, "\n model: "); diff --git a/tests/term_test.c b/tests/term_test.c index 66dbd96..9dbe902 100644 --- a/tests/term_test.c +++ b/tests/term_test.c @@ -1861,15 +1861,15 @@ int main(void) test_snapshot_follows_new_terminal(); test_bulk_rows_survive_edits(); - assert(!strcmp(UNICODE_DATA_VERSION, "18.0.0")); - assert(!strcmp(term_unicode_version(), UNICODE_DATA_VERSION)); - assert(unicode_width('A') == 1); - assert(unicode_width(0x0301) == 0); - assert(unicode_width(0x754C) == 2); - assert(unicode_width(0x1FAE0) == 2); - assert(unicode_emoji_presentation(0x1FAE0)); - assert(unicode_width(0x1FAEB) == 2); - assert(unicode_emoji_presentation(0x1FAEB)); + assert(!strcmp(TERM_UNICODE_DATA_VERSION, "18.0.0")); + assert(!strcmp(term_unicode_version(), TERM_UNICODE_DATA_VERSION)); + assert(term_unicode_width('A') == 1); + assert(term_unicode_width(0x0301) == 0); + assert(term_unicode_width(0x754C) == 2); + assert(term_unicode_width(0x1FAE0) == 2); + assert(term_unicode_emoji_presentation(0x1FAE0)); + assert(term_unicode_width(0x1FAEB) == 2); + assert(term_unicode_emoji_presentation(0x1FAEB)); { /* A Jamo Extended-B vowel draws inside its leading consonant. */ struct term *t = term_new(&(struct term_options){ .size = { .x = 10, .y = 2 } }); diff --git a/tests/unicode/unicode_test.c b/tests/unicode/unicode_test.c index 7fc6e3e..e3cf5a7 100644 --- a/tests/unicode/unicode_test.c +++ b/tests/unicode/unicode_test.c @@ -16,17 +16,17 @@ static void test_utf8_decode_one(void) for (size_t i = 0; i < sizeof(valid) / sizeof(*valid); i++) { uint32_t cp = 0xdeadbeef; for (int n = 0; n < valid[i].len; n++) { - int r = utf8_decode_one(valid[i].bytes, n, &cp); + int r = term_utf8_decode_one(valid[i].bytes, n, &cp); assert(r == 0); assert(cp == 0xdeadbeef); } - int r = utf8_decode_one(valid[i].bytes, valid[i].len, &cp); + int r = term_utf8_decode_one(valid[i].bytes, valid[i].len, &cp); assert(r == valid[i].len); assert(cp == valid[i].cp); char followed[8]; memcpy(followed, valid[i].bytes, valid[i].len); memcpy(followed + valid[i].len, "next", 4); - r = utf8_decode_one(followed, valid[i].len + 4, &cp); + r = term_utf8_decode_one(followed, valid[i].len + 4, &cp); assert(r == valid[i].len); assert(cp == valid[i].cp); } @@ -38,7 +38,7 @@ static void test_utf8_decode_one(void) }; for (size_t i = 0; i < sizeof(invalid) / sizeof(*invalid); i++) { uint32_t cp = 0xdeadbeef; - int r = utf8_decode_one(invalid[i], strlen(invalid[i]), &cp); + int r = term_utf8_decode_one(invalid[i], strlen(invalid[i]), &cp); assert(r == 0); assert(cp == 0xdeadbeef); } @@ -48,24 +48,24 @@ static void test_scalar_roundtrips(void) { for (uint32_t cp = 0; cp <= 0x10ffff; cp++) { if (cp >= 0xd800 && cp <= 0xdfff) continue; - char bytes[UTF8_MAX_BYTES]; - int len = utf8_encode(cp, bytes); - uint32_t scalar = 0, streamed = 0, state = UTF8_ACCEPT; - int r = utf8_decode_one(bytes, (size_t)len, &scalar); + char bytes[TERM_UTF8_MAX_BYTES]; + int len = term_utf8_encode(cp, bytes); + uint32_t scalar = 0, streamed = 0, state = TERM_UTF8_ACCEPT; + int r = term_utf8_decode_one(bytes, (size_t)len, &scalar); assert(r == len); assert(scalar == cp); for (int i = 0; i < len; i++) { - utf8_decode(&state, &streamed, (unsigned char)bytes[i]); - if (i + 1 < len) assert(state != UTF8_ACCEPT && state != UTF8_REJECT); + term_utf8_decode(&state, &streamed, (unsigned char)bytes[i]); + if (i + 1 < len) assert(state != TERM_UTF8_ACCEPT && state != TERM_UTF8_REJECT); } - assert(state == UTF8_ACCEPT && streamed == cp); + assert(state == TERM_UTF8_ACCEPT && streamed == cp); } static const uint32_t invalid[] = {0xd800, 0xdfff, 0x110000, UINT32_MAX}; for (size_t i = 0; i < sizeof(invalid) / sizeof(*invalid); i++) { - char bytes[UTF8_MAX_BYTES]; - int len = utf8_encode(invalid[i], bytes); + char bytes[TERM_UTF8_MAX_BYTES]; + int len = term_utf8_encode(invalid[i], bytes); uint32_t cp = 0; - int r = utf8_decode_one(bytes, len, &cp); + int r = term_utf8_decode_one(bytes, len, &cp); assert(len == 3 && r == len && cp == 0xfffd); } } @@ -74,26 +74,26 @@ static void test_utf8_sanitize(void) { char dst[64]; - utf8_sanitize(dst, sizeof(dst), "hello \xe2\x82\xac\xf0\x9f\x98\x80"); + term_utf8_sanitize(dst, sizeof(dst), "hello \xe2\x82\xac\xf0\x9f\x98\x80"); assert(!strcmp(dst, "hello \xe2\x82\xac\xf0\x9f\x98\x80")); - utf8_sanitize(dst, sizeof(dst), "a\xff\x80" "b\xed\xa0\x80" "c\xe2\x82"); + term_utf8_sanitize(dst, sizeof(dst), "a\xff\x80" "b\xed\xa0\x80" "c\xe2\x82"); assert(!strcmp(dst, "abc")); - utf8_sanitize(dst, sizeof(dst), ""); + term_utf8_sanitize(dst, sizeof(dst), ""); assert(!strcmp(dst, "")); - utf8_sanitize(dst, sizeof(dst), "\xff\xfe"); + term_utf8_sanitize(dst, sizeof(dst), "\xff\xfe"); assert(!strcmp(dst, "")); - utf8_sanitize(dst, 6, "ab\xe2\x82\xac" "cd"); + term_utf8_sanitize(dst, 6, "ab\xe2\x82\xac" "cd"); assert(!strcmp(dst, "ab\xe2\x82\xac")); - utf8_sanitize(dst, 5, "ab\xe2\x82\xac" "cd"); + term_utf8_sanitize(dst, 5, "ab\xe2\x82\xac" "cd"); assert(!strcmp(dst, "abcd")); memset(dst, 'x', sizeof(dst)); - utf8_sanitize(dst, 4, "abcdef"); + term_utf8_sanitize(dst, 4, "abcdef"); assert(!strcmp(dst, "abc")); memset(dst, 'x', sizeof(dst)); - utf8_sanitize(dst, 0, "abc"); + term_utf8_sanitize(dst, 0, "abc"); assert(dst[0] == 'x'); } @@ -101,30 +101,30 @@ static void test_utf8_sanitize(void) * stays unused. */ static void test_ground_invalid_subparts(void) { - uint32_t state = UTF8_ACCEPT, codep = 0, cp = 0xdead; - int n = utf8_decode_ground(&state, &codep, "\xe2\x80\x1b\xac", 4, &cp); + uint32_t state = TERM_UTF8_ACCEPT, codep = 0, cp = 0xdead; + int n = term_utf8_decode_ground(&state, &codep, "\xe2\x80\x1b\xac", 4, &cp); assert(n == 2); assert(cp == 0xFFFD); - assert(state == UTF8_ACCEPT); - n = utf8_decode_ground(&state, &codep, "\xac", 1, &cp); + assert(state == TERM_UTF8_ACCEPT); + n = term_utf8_decode_ground(&state, &codep, "\xac", 1, &cp); assert(n == 1); assert(cp == 0xFFFD); - assert(state == UTF8_ACCEPT); - n = utf8_decode_ground(&state, &codep, "\xe2\x82z", 3, &cp); + assert(state == TERM_UTF8_ACCEPT); + n = term_utf8_decode_ground(&state, &codep, "\xe2\x82z", 3, &cp); assert(n == 2); assert(cp == 0xFFFD); - assert(state == UTF8_ACCEPT); - n = utf8_decode_ground(&state, &codep, "\xe2\x82", 2, &cp); + assert(state == TERM_UTF8_ACCEPT); + n = term_utf8_decode_ground(&state, &codep, "\xe2\x82", 2, &cp); assert(n == 2); assert(cp == 0); - assert(state != UTF8_ACCEPT); + assert(state != TERM_UTF8_ACCEPT); - state = UTF8_ACCEPT; + state = TERM_UTF8_ACCEPT; codep = 0; - n = utf8_decode_ground(&state, &codep, "\xe2\x82\xac", 3, &cp); + n = term_utf8_decode_ground(&state, &codep, "\xe2\x82\xac", 3, &cp); assert(n == 3); assert(cp == 0x20ac); - assert(state == UTF8_ACCEPT); + assert(state == TERM_UTF8_ACCEPT); } int main(void) -- cgit v1.2.3