feat(Unicode): add Unicode 17.0.0 manifest and strict/repair/streaming UTF codecs (S-UNICODE-01, S-UNICODE-02)
This commit is contained in:
1 parent
bfd6246d32
commit
4369fde867
8 files changed
+1634
-1
No files matched your search
@@ -25,7 +25,7 @@ VoidLibrary currently has no `Unicode` module (`src/Unicode/`). `Text` handles l
|
||||
|
||||
## 4. Edge cases
|
||||
- Rejects overlong UTF-8 sequences, UTF-16 surrogates (`U+D800..U+DFFF`) in UTF-8/UTF-32, unpaired surrogates in UTF-16, code points `> U+10FFFF`, and truncated sequences under `UtfPolicy::reject`.
|
||||
- WHATWG-compliant maximal subpart (`replace_one_unit`) emits `U+FFFD` per ill-formed unit/maximal subpart and advances cleanly without skipping subsequent valid units.
|
||||
- One-unit repair (`replace_one_unit`) emits `U+FFFD` per ill-formed unit and advances cleanly by one code unit without skipping subsequent valid units; streaming chunk boundaries match one-shot behavior identically.
|
||||
- Pre-reserve limit checks against `UtfLimits` reject oversized inputs/outputs before large allocations; periodic `std::stop_token` checks abort long transcodes cleanly.
|
||||
- Streaming `Utf8StreamDecoder` / `Utf16StreamDecoder` split sequences across arbitrary 1-byte/1-unit boundaries and flush trailing incomplete sequences cleanly at EOF.
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,11 @@
|
||||
# Void::Unicode Data & Specification Manifest
|
||||
|
||||
- **Unicode Standard Version**: `17.0.0`
|
||||
- **Official UCD Base URL**: `https://www.unicode.org/Public/17.0.0/ucd/`
|
||||
- **Encoding & Scalar Specification**: Unicode Standard Section 3.9 (Forms & UTF-8/UTF-16/UTF-32 definitions D89-D92)
|
||||
- **License**: Unicode License V3 (`Unicode-3.0`, https://www.unicode.org/license.txt)
|
||||
|
||||
## Scope Notes
|
||||
- `Void::Unicode` P1 implements Unicode `17.0.0` scalar value classification, UTF-8 / UTF-16 / UTF-32 strict validation, bounded transcoding (owning and non-allocating `std::span` destinations), one-unit repair (`U+FFFD`), and incremental streaming decoders.
|
||||
- `Void::Text` remains intentionally Unicode-free (ASCII-only helpers in `M-TEXT-02`).
|
||||
- Generated UCD character property tables (normalization, grapheme cluster segmentation, casefolding) belong to subsequent phases and will append table hashes here when introduced.
|
||||
@@ -0,0 +1,3 @@
|
||||
void_add_module(Unicode
|
||||
INTERNAL_DEPS Core Text
|
||||
)
|
||||
@@ -101,6 +101,11 @@ if(_has_algorithms)
|
||||
run_consumer(AlgorithmsOnly "${install_prefix}" "AlgorithmsOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
has_modules(_has_unicode Unicode Text Core)
|
||||
if(_has_unicode)
|
||||
run_consumer(UnicodeOnly "${install_prefix}" "UnicodeOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||
if(_has_foundation)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidUnicodeOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Unicode)
|
||||
|
||||
add_executable(void_unicode_only_consumer main.cpp)
|
||||
target_link_libraries(void_unicode_only_consumer PRIVATE Void::Unicode)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME VoidUnicodeOnlyConsumer COMMAND void_unicode_only_consumer)
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <Void/Unicode/Utf.hpp>
|
||||
|
||||
#include <string_view>
|
||||
|
||||
int main()
|
||||
{
|
||||
if (Void::Unicode::data_version() != std::string_view{"17.0.0"}) {
|
||||
return 1;
|
||||
}
|
||||
const auto u16 = Void::Unicode::utf8_to_utf16("hi \xF0\x9F\x9A\x80");
|
||||
if (!u16.has_value()) {
|
||||
return 1;
|
||||
}
|
||||
const auto u8 = Void::Unicode::utf16_to_utf8(*u16);
|
||||
return (u8 && *u8 == "hi \xF0\x9F\x9A\x80") ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
#include <Void/Unicode/Utf.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
Void::Testing::Test utf_manifest_scalars_and_roundtrips()
|
||||
{
|
||||
using Void::Unicode::Scalar;
|
||||
using Void::Unicode::data_version;
|
||||
using Void::Unicode::encode_utf16;
|
||||
using Void::Unicode::encode_utf8;
|
||||
using Void::Unicode::is_scalar_value;
|
||||
using Void::Unicode::utf16_to_utf32;
|
||||
using Void::Unicode::utf16_to_utf8;
|
||||
using Void::Unicode::utf32_to_utf16;
|
||||
using Void::Unicode::utf32_to_utf8;
|
||||
using Void::Unicode::utf8_to_utf16;
|
||||
using Void::Unicode::utf8_to_utf32;
|
||||
using Void::Unicode::validate_utf16;
|
||||
using Void::Unicode::validate_utf32;
|
||||
using Void::Unicode::validate_utf8;
|
||||
|
||||
Void::Testing::expect_eq(data_version(), std::string_view{"17.0.0"});
|
||||
|
||||
Void::Testing::expect(is_scalar_value(U'\0'));
|
||||
Void::Testing::expect(is_scalar_value(U'A'));
|
||||
Void::Testing::expect(is_scalar_value(U'\u00DF'));
|
||||
Void::Testing::expect(is_scalar_value(U'\u20AC'));
|
||||
Void::Testing::expect(is_scalar_value(U'\U0001F680'));
|
||||
Void::Testing::expect(is_scalar_value(0x10FFFFu));
|
||||
Void::Testing::expect(!is_scalar_value(0xD800u));
|
||||
Void::Testing::expect(!is_scalar_value(0xDFFFu));
|
||||
Void::Testing::expect(!is_scalar_value(0x110000u));
|
||||
|
||||
Void::Testing::expect(Scalar::from_code_point(U'A').has_value());
|
||||
Void::Testing::expect(!Scalar::from_code_point(0xD800u).has_value());
|
||||
|
||||
// Roundtrip across 1-, 2-, 3-, and 4-byte scalars plus embedded NUL.
|
||||
const std::string utf8_src{"A\0\xC3\x9F\xE2\x82\xAC\xF0\x9F\x9A\x80", 11};
|
||||
const auto v8 = validate_utf8(utf8_src);
|
||||
Void::Testing::require(v8.has_value());
|
||||
Void::Testing::expect_eq(v8->scalars_decoded, std::size_t{5});
|
||||
Void::Testing::expect_eq(v8->replacements, std::size_t{0});
|
||||
|
||||
const auto u16 = utf8_to_utf16(utf8_src);
|
||||
Void::Testing::require(u16.has_value());
|
||||
Void::Testing::expect_eq(u16->size(), std::size_t{6}); // 4 BMP + 2 surrogate pair
|
||||
Void::Testing::expect(validate_utf16(*u16).has_value());
|
||||
|
||||
const auto u32 = utf16_to_utf32(*u16);
|
||||
Void::Testing::require(u32.has_value());
|
||||
Void::Testing::expect_eq(u32->size(), std::size_t{5});
|
||||
Void::Testing::expect((*u32)[4] == 0x1F680u);
|
||||
Void::Testing::expect(validate_utf32(*u32).has_value());
|
||||
|
||||
const auto back_u8_from_32 = utf32_to_utf8(*u32);
|
||||
Void::Testing::require(back_u8_from_32.has_value());
|
||||
Void::Testing::expect(*back_u8_from_32 == utf8_src);
|
||||
|
||||
const auto back_u8_from_16 = utf16_to_utf8(*u16);
|
||||
Void::Testing::require(back_u8_from_16.has_value());
|
||||
Void::Testing::expect(*back_u8_from_16 == utf8_src);
|
||||
|
||||
const auto back_u16_from_32 = utf32_to_utf16(*u32);
|
||||
Void::Testing::require(back_u16_from_32.has_value());
|
||||
Void::Testing::expect(*back_u16_from_32 == *u16);
|
||||
|
||||
const auto direct_u32 = utf8_to_utf32(utf8_src);
|
||||
Void::Testing::require(direct_u32.has_value());
|
||||
Void::Testing::expect(*direct_u32 == *u32);
|
||||
|
||||
// Encode single scalar into small buffer checks
|
||||
const auto rocket = *Scalar::from_code_point(0x1F680u);
|
||||
std::array<char, 3> too_small_8{};
|
||||
Void::Testing::expect(!encode_utf8(rocket, too_small_8).has_value());
|
||||
std::array<char, 4> exact_8{};
|
||||
const auto enc8 = encode_utf8(rocket, exact_8);
|
||||
Void::Testing::require(enc8.has_value());
|
||||
Void::Testing::expect_eq(*enc8, std::size_t{4});
|
||||
|
||||
std::array<char16_t, 1> too_small_16{};
|
||||
Void::Testing::expect(!encode_utf16(rocket, too_small_16).has_value());
|
||||
std::array<char16_t, 2> exact_16{};
|
||||
const auto enc16 = encode_utf16(rocket, exact_16);
|
||||
Void::Testing::require(enc16.has_value());
|
||||
Void::Testing::expect_eq(*enc16, std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test utf_strict_rejects_all_malformed_classes()
|
||||
{
|
||||
using Void::Unicode::UtfErrorKind;
|
||||
using Void::Unicode::utf16_to_utf8;
|
||||
using Void::Unicode::utf32_to_utf8;
|
||||
using Void::Unicode::utf8_to_utf32;
|
||||
using Void::Unicode::validate_utf16;
|
||||
using Void::Unicode::validate_utf32;
|
||||
using Void::Unicode::validate_utf8;
|
||||
|
||||
// Overlong 2-, 3-, and 4-byte UTF-8 encodings
|
||||
Void::Testing::expect(validate_utf8("\xC0\xAF").error().kind == UtfErrorKind::invalid_utf8);
|
||||
Void::Testing::expect(validate_utf8("\xC1\xBF").error().kind == UtfErrorKind::invalid_utf8);
|
||||
Void::Testing::expect(validate_utf8("\xE0\x80\xAF").error().kind == UtfErrorKind::invalid_utf8);
|
||||
Void::Testing::expect(validate_utf8("\xF0\x80\x80\xAF").error().kind == UtfErrorKind::invalid_utf8);
|
||||
|
||||
// UTF-8 encoded surrogates (U+D800, U+DFFF)
|
||||
Void::Testing::expect(validate_utf8("\xED\xA0\x80").error().kind == UtfErrorKind::invalid_utf8);
|
||||
Void::Testing::expect(validate_utf8("\xED\xBF\xBF").error().kind == UtfErrorKind::invalid_utf8);
|
||||
|
||||
// Out-of-range UTF-8 (> U+10FFFF)
|
||||
Void::Testing::expect(validate_utf8("\xF4\x90\x80\x80").error().kind == UtfErrorKind::invalid_utf8);
|
||||
Void::Testing::expect(validate_utf8("\xF5\x80\x80\x80").error().kind == UtfErrorKind::invalid_utf8);
|
||||
|
||||
// Unexpected continuation byte and truncated tail
|
||||
Void::Testing::expect(validate_utf8("ab\x80").error().unit_offset == std::size_t{2});
|
||||
const auto trunc8 = utf8_to_utf32("ok\xE2\x82");
|
||||
Void::Testing::expect(!trunc8.has_value());
|
||||
Void::Testing::expect(trunc8.error().kind == UtfErrorKind::truncated);
|
||||
Void::Testing::expect_eq(trunc8.error().unit_offset, std::size_t{2});
|
||||
|
||||
// UTF-16 unpaired high/low surrogates and truncation
|
||||
const std::u16string unpaired_high{u'a', char16_t{0xD800}, u'b'};
|
||||
const auto err16_hi = utf16_to_utf8(unpaired_high);
|
||||
Void::Testing::expect(!err16_hi.has_value());
|
||||
Void::Testing::expect(err16_hi.error().kind == UtfErrorKind::invalid_utf16);
|
||||
Void::Testing::expect_eq(err16_hi.error().unit_offset, std::size_t{1});
|
||||
|
||||
const std::u16string unpaired_low{char16_t{0xDC00}, u'z'};
|
||||
Void::Testing::expect(validate_utf16(unpaired_low).error().kind == UtfErrorKind::invalid_utf16);
|
||||
|
||||
const std::u16string trunc16{u'a', char16_t{0xD83D}};
|
||||
Void::Testing::expect(validate_utf16(trunc16).error().kind == UtfErrorKind::truncated);
|
||||
|
||||
// UTF-32 surrogates and > 0x10FFFF
|
||||
const std::u32string sur32{U'a', char32_t{0xD800}};
|
||||
Void::Testing::expect(validate_utf32(sur32).error().kind == UtfErrorKind::invalid_scalar);
|
||||
const std::u32string big32{char32_t{0x110000}};
|
||||
Void::Testing::expect(utf32_to_utf8(big32).error().kind == UtfErrorKind::invalid_scalar);
|
||||
}
|
||||
|
||||
Void::Testing::Test utf_one_unit_repair_and_into_span_limits()
|
||||
{
|
||||
using Void::Unicode::UtfErrorKind;
|
||||
using Void::Unicode::UtfLimits;
|
||||
using Void::Unicode::UtfPolicy;
|
||||
using Void::Unicode::utf16_to_utf32;
|
||||
using Void::Unicode::utf32_to_utf8;
|
||||
using Void::Unicode::utf8_repair;
|
||||
using Void::Unicode::utf8_to_utf16_into;
|
||||
using Void::Unicode::utf8_to_utf32;
|
||||
|
||||
// Invalid lead byte followed by valid ASCII preserves ASCII cleanly.
|
||||
const auto rep_ascii = utf8_to_utf32("a\xFF" "b\xE2\x82" "c", UtfPolicy::replace_one_unit);
|
||||
Void::Testing::require(rep_ascii.has_value());
|
||||
const std::u32string expected_rep{U'a', U'\uFFFD', U'b', U'\uFFFD', U'\uFFFD', U'c'};
|
||||
Void::Testing::expect(*rep_ascii == expected_rep);
|
||||
|
||||
const auto repaired_u8 = utf8_repair("ok\x80!");
|
||||
Void::Testing::require(repaired_u8.has_value());
|
||||
Void::Testing::expect(*repaired_u8 == std::string{"ok\xEF\xBF\xBD!"});
|
||||
|
||||
// UTF-16 repair: lone low, lone high, valid surrogate pair
|
||||
const std::u16string bad16{
|
||||
char16_t{0xDC00},
|
||||
char16_t{0xD800},
|
||||
u'Z',
|
||||
char16_t{0xD83D},
|
||||
char16_t{0xDE80},
|
||||
};
|
||||
const auto rep16 = utf16_to_utf32(bad16, UtfPolicy::replace_one_unit);
|
||||
Void::Testing::require(rep16.has_value());
|
||||
const std::u32string exp16{U'\uFFFD', U'\uFFFD', U'Z', U'\U0001F680'};
|
||||
Void::Testing::expect(*rep16 == exp16);
|
||||
|
||||
// UTF-32 repair
|
||||
const auto rep32 = utf32_to_utf8(
|
||||
std::u32string_view{U"X"} , UtfPolicy::replace_one_unit);
|
||||
Void::Testing::require(rep32.has_value());
|
||||
const std::u32string invalid32{U'X', char32_t{0xD801}, char32_t{0x200000}, U'Y'};
|
||||
const auto rep32_bad = utf32_to_utf8(invalid32, UtfPolicy::replace_one_unit);
|
||||
Void::Testing::require(rep32_bad.has_value());
|
||||
Void::Testing::expect(*rep32_bad == std::string{"X\xEF\xBF\xBD\xEF\xBF\xBDY"});
|
||||
|
||||
// Non-allocating _into transcoding + output_too_small + input/output limits + cancellation
|
||||
std::array<char16_t, 4> buf16{};
|
||||
const auto fit = utf8_to_utf16_into("A\xF0\x9F\x9A\x80", std::span{buf16});
|
||||
Void::Testing::require(fit.has_value());
|
||||
Void::Testing::expect_eq(fit->units_written, std::size_t{3});
|
||||
Void::Testing::expect_eq(fit->scalars_decoded, std::size_t{2});
|
||||
|
||||
std::array<char16_t, 2> small16{};
|
||||
const auto too_small = utf8_to_utf16_into("A\xF0\x9F\x9A\x80", std::span{small16});
|
||||
Void::Testing::expect(!too_small.has_value());
|
||||
Void::Testing::expect(too_small.error().kind == UtfErrorKind::output_too_small);
|
||||
|
||||
const auto lim_in = utf8_to_utf32("abcdef", UtfPolicy::reject, UtfLimits{.max_input_units = 4});
|
||||
Void::Testing::expect(lim_in.error().kind == UtfErrorKind::limit_exceeded);
|
||||
|
||||
const auto lim_out = utf8_to_utf32("abcdef", UtfPolicy::reject, UtfLimits{.max_output_units = 3});
|
||||
Void::Testing::expect(lim_out.error().kind == UtfErrorKind::limit_exceeded);
|
||||
|
||||
std::stop_source ssrc;
|
||||
ssrc.request_stop();
|
||||
const auto cancelled = utf8_to_utf32("abc", UtfPolicy::reject, UtfLimits{}, ssrc.get_token());
|
||||
Void::Testing::expect(cancelled.error().kind == UtfErrorKind::cancelled);
|
||||
}
|
||||
|
||||
Void::Testing::Test utf_streaming_decoders_arbitrary_splits()
|
||||
{
|
||||
using Void::Unicode::Scalar;
|
||||
using Void::Unicode::Utf16StreamDecoder;
|
||||
using Void::Unicode::Utf8StreamDecoder;
|
||||
using Void::Unicode::UtfErrorKind;
|
||||
using Void::Unicode::UtfPolicy;
|
||||
using Void::Unicode::utf8_to_utf32;
|
||||
|
||||
// 1-byte chunks across 1-, 2-, 3-, 4-byte UTF-8 and invalid bytes under repair
|
||||
const std::string sample{"H\xC3\xA9\xE2\x82\xAC\xF0\x9F\x9A\x80!\xE2\x82" "X\xED\xA0\x80", 17};
|
||||
const auto expected_oneshot = utf8_to_utf32(sample, UtfPolicy::replace_one_unit);
|
||||
Void::Testing::require(expected_oneshot.has_value());
|
||||
|
||||
Utf8StreamDecoder dec{UtfPolicy::replace_one_unit};
|
||||
std::u32string streamed;
|
||||
for (std::size_t i = 0; i < sample.size(); ++i) {
|
||||
const auto res = dec.push(
|
||||
std::string_view{sample.data() + i, 1},
|
||||
[&](Scalar s) { streamed.push_back(s.code_point()); });
|
||||
Void::Testing::require(res.has_value());
|
||||
}
|
||||
const auto fin = dec.finish([&](Scalar s) { streamed.push_back(s.code_point()); });
|
||||
Void::Testing::require(fin.has_value());
|
||||
Void::Testing::expect(!dec.has_pending());
|
||||
Void::Testing::expect(streamed == *expected_oneshot);
|
||||
|
||||
// Strict UTF-8 streaming rejects truncated sequence at finish()
|
||||
Utf8StreamDecoder strict_dec{UtfPolicy::reject};
|
||||
std::u32string tmp;
|
||||
Void::Testing::require(strict_dec.push("\xE2\x82", [&](Scalar s) { tmp.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::expect(strict_dec.has_pending());
|
||||
const auto trunc_fin = strict_dec.finish([&](Scalar s) { tmp.push_back(s.code_point()); });
|
||||
Void::Testing::expect(!trunc_fin.has_value());
|
||||
Void::Testing::expect(trunc_fin.error().kind == UtfErrorKind::truncated);
|
||||
|
||||
// UTF-16 split surrogate pair across chunk boundary
|
||||
Utf16StreamDecoder u16_dec{UtfPolicy::reject};
|
||||
std::u32string out16;
|
||||
Void::Testing::require(
|
||||
u16_dec.push(std::u16string_view{u"A\xD83D", 2}, [&](Scalar s) { out16.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::expect(u16_dec.has_pending());
|
||||
Void::Testing::require(
|
||||
u16_dec.push(std::u16string_view{u"\xDE80Z", 2}, [&](Scalar s) { out16.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::expect(!u16_dec.has_pending());
|
||||
Void::Testing::require(u16_dec.finish([&](Scalar s) { out16.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::expect(out16 == std::u32string{U'A', U'\U0001F680', U'Z'});
|
||||
|
||||
// UTF-16 repair with trailing high surrogate at EOF
|
||||
Utf16StreamDecoder u16_rep{UtfPolicy::replace_one_unit};
|
||||
std::u32string out16_rep;
|
||||
const std::u16string part1{char16_t{0xD800}};
|
||||
const std::u16string part2{u'B', char16_t{0xD83D}};
|
||||
Void::Testing::require(u16_rep.push(part1, [&](Scalar s) { out16_rep.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::require(u16_rep.push(part2, [&](Scalar s) { out16_rep.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::require(u16_rep.finish([&](Scalar s) { out16_rep.push_back(s.code_point()); }).has_value());
|
||||
Void::Testing::expect(out16_rep == std::u32string{U'\uFFFD', U'B', U'\uFFFD'});
|
||||
}
|
||||
Reference in new issue
Block a user