Files
Library/docs/roadmap/modules/Text.md

142 lines
9.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Text, byte encodings and parsing utilities
Status: planning only; tasks below are **PLANNED**. Source baseline:
`f1f9ba375b22abf1adcc7c550ec21ac20895c0a4`, inspected 2026-11-24.
## Existing source and tests
[Text's declaration](../../../src/Text/module.cmake) has no dependencies.
[Str](../../../include/Void/Text/Str.hpp) supplies trim, case conversion, split/join,
replacement, padding and numeric conversion in `Void::Str`.
[Format](../../../include/Void/Text/Format.hpp) adds placeholder rendering, wrapping and
tables; [Tokenizer](../../../include/Void/Text/Tokenizer.hpp) owns ordered matching
rules and allocating tokens; [Semver](../../../include/Void/Text/Semver.hpp) supplies
versions and ranges. [Diff](../../../include/Void/Text/Diff.hpp) implements Myers-style
edit reconstruction with stored trace vectors and line adapters. This exposes a meaningful
memory/work bound to add, including checked conversion from size_t to internal indices.
Byte encoders live in `Void::Encoding::Base64`, `Hex` and `Url`:
[Base64](../../../include/Void/Text/Base64.hpp),
[Hex](../../../include/Void/Text/Hex.hpp), [Url](../../../include/Void/Text/Url.hpp).
URL decoding explicitly distinguishes plus-as-space; URL-safe Base64 decoding rejects
padding and invalid trailing bits. [Html](../../../include/Void/Text/Html.hpp) has
entity unescaping, not a complete HTML parser or sanitizer. Neither existing case helpers
nor byte length are a Unicode normalization or grapheme contract.
Current suites include [Str](../../../tests/standalone/Text/Str.cpp),
[Encoding](../../../tests/standalone/Text/Encoding.cpp),
[Tokenizer](../../../tests/standalone/Text/Tokenizer.cpp),
[Format](../../../tests/standalone/Text/Format.cpp),
[Semver](../../../tests/standalone/Text/Semver.cpp), and
[Diff](../../../tests/standalone/Text/Diff.cpp). Graph parsing is partial at Diff lines
30/96; both signatures and surrounding algorithm were read directly.
## Boundary and native implementation
Text remains byte/ASCII infrastructure under [C-01/C-05/C-13](../ARCHITECTURE.md).
NEW Unicode owns UTF decoding, normalization, casing and segmentation. Select one-way
dependency **Unicode→Text**: Text never imports Unicode. An opt-in NEW
`include/Void/Unicode/TextAdapters.hpp` lives with Unicode. Existing Text byte operations
remain useful without versioned Unicode tables. General numeric policy stays in Math;
legacy Text numeric wrappers may use a small explicitly declared Math adapter header,
not force Math on every Str include. Native byte scanners and standard string/span/
from_chars are sufficient; no regex engine, ICU or formatting dependency is required.
Generic diff mechanics move to Algorithms; Text keeps line/patch formatting above it.
## NEW bounded APIs and dataflow
Illustrative NEW additions in NEW `include/Void/Text/Bounded.hpp`:
```cpp
namespace Void::Text {
struct TextLimits {
std::size_t max_input = 16 * 1024 * 1024;
std::size_t max_output = 16 * 1024 * 1024;
std::size_t max_parts = 65536;
};
struct TextError { enum Code { invalid_encoding, invalid_rule, limit } code;
std::size_t offset; };
std::expected<std::vector<std::string_view>, TextError>
split_views(std::string_view input, std::string_view delimiter, TextLimits);
std::expected<std::string, TextError>
replace_bounded(std::string_view input, std::string_view needle,
std::string_view replacement, TextLimits);
}
```
The code is a proposed shape, not an existing compiling example. `split_views` borrows
input; delimiter is literal bytes; empty delimiter is invalid, empty fields are retained
unless an explicit policy drops them. Returned views expire with input. `replace_bounded`
counts matches then checks multiply/add before reserving; it never grows beyond the
configured cap. Empty needle is invalid. Trim/case helpers gain explicit `_ascii` names
and deterministic classification of bytes 0–127; bytes ≥128 are preserved, avoiding
locale-dependent behavior. Unicode casing is requested from Unicode explicitly.
Encoders gain `encode_into(span<char>)`/`decode_into(span<byte>)`, reporting required
capacity and error offset. Validate the whole token and required output size before
publishing output for all-or-nothing overloads. Separate streaming codecs expose consumed/
produced counts and partial-output semantics. Base64 defaults are strict alphabet,
padding and unused-bit validation; URL-safe unpadded is a named profile. Permissive MIME
whitespace decoding is explicit. Hex rejects odd length by default. URL percent decoding
does not apply UTF normalization, path traversal resolution or repeated decoding.
Protocol owners decide when decoding is permitted; Text does not reinterpret signed bytes.
Tokenizer's NEW view mode returns kind, byte range, line and byte-column offsets without
copying lexemes. Defaults: 128 rules, 65,536 tokens, 1 MiB/token and the common 16 MiB input
cap. First matching rule wins, preserving current ordered-rule intent. A callback result
of zero means no match; a result beyond remaining bytes is `invalid_rule`. Every successful
token advances, preventing non-progress loops. Rule callables may allocate or throw;
exceptions propagate unless the caller selects an explicit Functional adapter. An
incremental tokenizer owns its incomplete token buffer and limits it; views passed to
callbacks are valid only for that callback. Long scans check optional stop tokens every
4 KiB and return an explicit stopped result with consumed offset.
Format gains sink/output-span variants and bounded table cell/column counts (256 columns,
65,536 rows, 16 MiB output). Missing placeholders remain literal by compatibility policy;
strict rendering reports missing keys. Width is bytes in Text, never terminal display
width. Semver separates strict SemVer parsing from the existing convenient `v` prefix
profile, and caps versions at 4 KiB, identifiers at 256 and range clauses at 128. Range
evaluation documents prerelease admission and build metadata comparison.
Diff defaults cap combined input at 65,536 elements, edit distance at 4,096 and scratch at
16 MiB. Exceeding a work limit returns `limit`, not a silently approximate patch. Use
Algorithms' checked Myers engine and leave later linear-space reconstruction behind the
measured large-diff capability gate. A patch includes source identity/expected deletes;
applying it validates input instead of merely emitting inserts/equalities as proof of a
matching source. Human patch rendering escapes control bytes and caps output.
## Implementation tasks
| Task | Phase; dependencies | Scope and acceptance | Recommended role |
| --- | --- | --- | --- |
| M-TEXT-01 | P0; current source | Freeze legacy byte behavior, encoding profiles, Semver ordering and diff fixtures; document allocating/borrowed return lifetimes. | sol_implementer |
| M-TEXT-02 | P1; M-TEXT-01 | Add ASCII-explicit helpers, view splitting and checked bounded growth; high bytes and overflow sizes are deterministic without Unicode linkage. | sol_implementer |
| M-TEXT-03 | P2; M-TEXT-02 | Add strict into-span and streaming encoding APIs; every split boundary, malformed tail and insufficient-output case preserves its stated publication contract. | sol_implementer |
| M-TEXT-04 | P2; M-TEXT-02 | Add tokenizer view/incremental modes and rule budgets; invalid rule lengths and chunk-split tokens cannot overread or loop forever. | sol_implementer |
| M-TEXT-05 | P2; M-TEXT-02 | Complete bounded formatting and strict/compatibility Semver profiles; prerelease/range and output-limit tests pass. | sol_implementer |
| M-TEXT-06 | P2; Algorithms bounded-diff gate; M-TEXT-01 | Delegate generic diff, add checked patch apply and rendering; replay reconstructs target and mismatched source is rejected. | sol_implementer |
| M-TEXT-07 | P3; M-TEXT-03–06; Unicode conformance gate | Publish optional Unicode adapter examples and installed consumers; Text-only binary links without Unicode tables and legacy adapters retain fixtures. | sol_implementer |
## Validation, resources and migration
CURRENT checks are the linked suites and parent baseline, with no new test execution.
PROPOSED tests include arbitrary byte fuzzing, every codec chunk split, invalid padding,
overlong percent escape tails, embedded NUL, CRLF location tracking, adversarial tokenizer
callbacks, integer length overflow, large repeated diff inputs and strict Semver vectors.
Round-trip tests alone are insufficient: independently specified invalid inputs must fail.
No security claim treats HTML unescaping as sanitization or URL decoding as authorization.
Measure ASCII and mixed UTF-8 byte workloads, allocations per split/token, streaming
throughput, diff worst-case scratch and build cost under [PERFORMANCE](../PERFORMANCE.md).
Report p50/p95/p99, peak RSS/CPU and limits hit; do not compare owning and borrowed output
without labeling their lifetime/work difference. No module starts threads; streaming
state is owned by its caller and finalization reports incomplete input.
[Migration](../MIGRATION.md) retains old namespaces and byte semantics through named
compatibility functions. Unicode adapters never silently normalize keys or signed
documents. Existing URL-safe Base64 strictness stays protected. Rollback selects the old
package; applications that persisted normalized data need their own format/version
transition, not a Text-wide rewrite. See [requirements](../REQUIREMENTS.md),
[DEPENDENCIES](../DEPENDENCIES.md), [VALIDATION](../VALIDATION.md),
[DELIVERY](../DELIVERY.md) and [Unicode](../subsystems/Unicode.md).