Class Utf8
- Namespace
- sofab
- Assembly
- SofaBuffers.dll
Turns a raw UTF-8 byte range into a C# string, rejecting a range that is not well-formed — the support layer for generated code, not part of the codec.
public static class Utf8
- Inheritance
-
Utf8
- Inherited Members
Remarks
IStream transcodes nothing: it hands a string payload to
String(int, int, int, byte[], int, int) as the wire bytes it read, and a field nobody
reads is skipped without being looked at (CORELIB_PLAN §6.4). Materializing the
value — and therefore validating it — is the consumer's step, which for a
generated message class means every string field of every schema. That
step has the same shape wherever it appears, so it lives here instead of being
emitted, rationale and all, into every generated source tree (generator#345).
Validate first, convert second — there is no other order.
UTF8 substitutes U+FFFD for malformed input, so a
check made on the resulting string can never fail: it holds a repaired value
that no longer remembers what arrived. MESSAGE_SPEC §8 forbids that repair in
every mode ("no silent replacement, ever"), and §7 wants the message rejected.
The strict codec below therefore does both at once: it throws on the first byte
it cannot decode rather than producing a string.
Strict means the Unicode scalar encoding and nothing else. Rejected:
overlong forms (including the C0 80 "modified UTF-8" NUL), the surrogate
code points U+D800..U+DFFF encoded as three bytes, anything above
U+10FFFF, a lead byte whose continuation bytes are missing or out of
range, and a bare continuation byte. C# string is a Unicode type and can
hold none of those, so this port is strict unconditionally — there is no
SOFAB_STRICT_UTF8 switch to turn off (§6.4).
Call it once, on the complete payload. Validity is a property of the
whole string field, never of a chunk: a multi-byte sequence split across
a feed is a well-formed prefix, not a defect. String(int, int, byte[], int, int, long) is
what arranges that, and calls this at the moment the last chunk lands.
Methods
Decode(byte[], int, int)
Materialize data[offset, offset + length) as a string,
rejecting a range that is not well-formed UTF-8.
public static string Decode(byte[] data, int offset, int length)
Parameters
databyte[]buffer holding the payload
offsetintfirst byte of the payload within
datalengthintpayload length in bytes
Returns
- string
the decoded string
Remarks
The rejection is a SofabException carrying
InvalidMessage — the §5.2 INVALID outcome.
Thrown from inside an IVisitor callback, which is where
generated code calls this from, it propagates out of
Feed(byte[], int, int, IVisitor) with the verdict
latched, exactly like the decoder's own rejections: the stream stays
Invalid regardless of what follows.
Only the named range is inspected. A chunk carrying the payload plus
whatever came after it decodes to the payload alone, so a caller passing the
declared total never has to trim its input first.
Exceptions
- SofabException
(InvalidMessage) when the range is not valid UTF-8.
IsValid(byte[], int, int)
Report whether data[offset, offset + length) is well-formed UTF-8,
without materializing anything.
public static bool IsValid(byte[] data, int offset, int length)
Parameters
Returns
- bool
trueif the range is valid UTF-8
Remarks
The verdict is the one Decode(byte[], int, int) applies, and the same one
WriteFixlen(int, byte[], int, int, FixlenType) applies on the way out: the Unicode scalar
encoding and nothing else — no overlongs, no encoded surrogates, nothing
above U+10FFFF, no truncated or stray sequence.
This is what a byte container needs. Where a schema's string
field is carried as raw bytes rather than as a C# string,
generated code has a value that can hold invalid UTF-8, and MESSAGE_SPEC §8
forbids emitting it (CORELIB_PLAN §6.4.1: encode-side validation is what
keeps a strict ecosystem's own encoders from producing bytes its decoders
reject). Checking it costs one vectorized pass and allocates nothing.