Strings, Bytes, and encoding#

The String and Bytes slot types and the contract on how they relate. DS strings are Unicode; bytes are byte sequences; conversion between them is explicit.

Paired with:

  • [LLEX] for string-literal lexical syntax.

  • [LEXP] for string operations (interpolation, ordering, regex and the rest).

  • [LFAC] for slot-type declarations.

  • [LADP] for adapter-side obligations on String / Bytes wire data.

What this is not#

  • Not a string-operation library spec. Operations live in [LEXP]; this doc only fixes the underlying type semantics.

  • Not a Unicode normalisation spec. Comparison is by codepoint sequence; normalisation forms (NFC, NFD, NFKC, NFKD) are not applied implicitly.

  • Not a locale spec. String operations are locale-agnostic; no locale-aware comparison, casing, or sorting.


The String type [LSTR.1]#

[LSTR.1.1] A String value is a sequence of Unicode codepoints. Every codepoint in [U+0001, U+10FFFF] is admissible except the surrogate range [U+D800, U+DFFF], which is forbidden per RFC 3629. U+0000 is not admissible: no String holds it. A literal that would spell it is a compile error ([LGRA.2.9.4]), and a value that would carry it is refused where it enters ([LSTR.1.5]).

ratified — checks: idris×3 — see also: LGRA.2.9.4, LSTR.1.5

[LSTR.1.5] A U+0000 is refused where it would enter, never cut. A runtime must refuse a value that would put U+0000 into a String, or into a string or object key of a Json value, at each place such a value enters:

  • From the host, as a string value: a call that supplies such a string — a String slot, a Json string, a Json object key — returns DS_ERR_INVAL and changes nothing. A binding whose host strings can hold U+0000 refuses it before the call, rather than passing a value the call would cut short.

  • From JSON text the runtime parses, whoever supplies it: a \u0000 escape, or a raw U+0000, makes the text malformed, and the parse refuses it as it refuses any malformed text. JSON text a host hands to the parser takes this arm, not the one above.

  • From a recording read on replay: a string holding U+0000 is a malformed atom body, and the reader refuses the stream.

  • From an artefact: a string field holding U+0000 is a body that has not decoded, and the loader refuses the artefact ([RDSC.9.12]).

A value is never truncated at a U+0000, and a U+0000 is never silently dropped.

see also: RDSC.9.12

[LSTR.1.2] The internal encoding is UTF-8. Implementations may use any in-memory representation, but the wire-level form between adapters, runtime, and persistent storage is UTF-8.

[LSTR.1.3] String values are immutable. Operations that produce modified text (case change, substitution, replacement) return new String values; the original is unchanged.

[LSTR.1.4] Empty strings are admissible. The empty string is distinct from a missing slot; a slot whose value is "" is present, with length zero.


Length and indexing [LSTR.2]#

[LSTR.2.1] len(s) returns the number of Unicode codepoints in s, as Int. Not the byte count, not the grapheme-cluster count, not the UTF-16 code-unit count. So café with a precomposed é (U+00E9) has length 4, and a waving-hand emoji followed by a skin-tone modifier (U+1F44B U+1F3FD) has length 2, though it renders as one grapheme.

ratified — checks: idris×4

[LSTR.2.2] String indexing is codepoint-indexed. s[i] returns the codepoint at zero-based index i as a single-codepoint String.

An out-of-range index raises a ComputationError and abandons the activation. The operation is well typed — Int carries no length bound — so there is no type error to raise; there is simply no answer, which is the ComputationError test and the same one that places division by zero there ([LERR.1.4.10]). len(s) ([LSTR.2.1]) is the guard.

A negative index is a compile error when the index is a literal (s[-1]), and a ComputationError when it is computed — the split [LTIM.8.3.2] already uses for format identifiers. A literal negative index is a bug the compiler can see.

Deferred: not binding in this edition.

see also: LERR.1.4.10, LSTR.2.1, LTIM.8.3.2

[LSTR.2.4] Byte-level work on text is the adapter's, done before the fact is asserted: the language does not convert a String to Bytes ([LSTR.4.6]).


see also: LSTR.4.6

Capacity [LSTR.11]#

[LSTR.11.1] A String slot may declare capacity N ([LGRA.5.1]), where N is a positive integer. Every value the slot holds then has at most N codepoints, counted as len counts them ([LSTR.2.1]). A slot that declares no capacity is unbounded.

capacity is admitted on String slots only. The compiler must refuse it on a slot of any other type (DS_VALIDATE_CAPACITY_BAD_TYPE), and must refuse an N below 1 (DS_VALIDATE_CAPACITY_NOT_POSITIVE).

ratified — see also: LGRA.5.1, LSTR.2.1

[LSTR.11.2] The compiler must refuse a string literal written to a slot whose capacity the literal exceeds (DS_VALIDATE_CAPACITY_LITERAL_EXCEEDED).

ratified

[LSTR.11.3] A value longer than its slot's capacity that reaches the slot at run time, in an asserted fact or an emitted one, is a bounds refusal ([LERR.1.5]). The engine must not store it: the assert or the emit does not happen, and the BoundsError names CAP_VALUE_CAPACITY ([RTCT.7.1]), with the capacity as its limit and the value's length as the observed count. A refused emit stops its firing there: the actions after it do not run ([LERR.1.5]). In a FactExternal, at any depth, it refuses the whole record, and that status is the whole refusal: there is no BoundsError ([NCBK.10.4]).

The engine must not truncate the value instead. A shortened string is a different value, and every equality over the slot would silently change.

ratified — see also: LERR.1.5, NCBK.10.4, RTCT.7.1

[LSTR.11.4] capacity is required only where a capability's own clause requires it. Under linear_scan: false, a String slot that a regex match or an ordering comparison reads must declare one ([LNLS.3.7], [LNLS.3.10]).

see also: LNLS.3.10, LNLS.3.7

Comparison and ordering [LSTR.3]#

[LSTR.3.1] String equality is codepoint-by-codepoint. Two strings are equal iff they have the same length and matching codepoints at every position. No normalisation is applied; the NFC and NFD forms of the same grapheme cluster are not equal: café spelled with U+00E9 is not equal to café spelled with U+0065 U+0301.

ratified — checks: idris×10

[LSTR.3.2] String ordering is lexicographic by codepoint value. This is not locale-aware ordering; it sorts by Unicode scalar value, which corresponds to UTF-8 byte ordering for any well-formed pair.

ratified — checks: idris×16

[LSTR.3.3] Authors needing locale-aware comparison or normalisation-insensitive equality compute the canonical form adapter-side and emit a normalised slot.


The Bytes type [LSTR.4]#

[LSTR.4.1] A Bytes value is an opaque sequence of octets. Bytes carry no encoding metadata, and the language does not interpret them as text ([LSTR.4.6]).

see also: LSTR.4.6

[LSTR.4.2] len(b) on a Bytes value returns the byte count, as Int.

ratified

[LSTR.4.3] Indexing a Bytes value by integer position returns the octet at that position as Int in [0, 255].

Out-of-range and negative indices follow [LSTR.2.2]: an out-of-range index raises a ComputationError and abandons the activation; a negative index is a compile error when literal and a ComputationError when computed. len(b) is the guard.

Deferred: not binding in this edition.

see also: LSTR.2.2

[LSTR.4.4] Bytes values are immutable; operations producing modified bytes return new Bytes values.

[LSTR.4.5] String and Bytes are distinct types. The language converts between them neither implicitly nor explicitly ([LSTR.4.6]).


ratified — see also: LSTR.4.6

[LSTR.4.6] Bytes pass through; the language converts them to nothing. A Bytes value arrives from an adapter, and the engine stores it in a slot, compares it for equality ([LEXP.7.19]) and passes it to a call as it is. Converting bytes to text, text to bytes or bytes to a number is the adapter's work, done before the fact is asserted. The compiler must refuse each of these, at the construct, with DS_VALIDATE_BYTES_CONVERSION_RETIRED:

  • b.decode(<encoding>), for every encoding;

  • bytes_to_utf8(b);

  • s.encode(<encoding>), for every encoding;

  • bytes_to_int(b, width) and bytes_to_int_le(b, width);

  • a :: cast from Bytes to String, or from String to Bytes ([LCST.4.5.2]).

The builtins these spellings named keep their numbers, reserved and never reissued ([STYL.2.8.7]). DS_VALIDATE_UNKNOWN_ENCODING stays allocated and is not raised in this edition.

ratified — see also: LCST.4.5.2, LEXP.7.19

Interaction with other features [LSTR.7]#

Regex [LSTR.7.1]#

[LSTR.7.1.1] Regex patterns and matches operate on String values at the codepoint level per [LREG]. A regex applied to a Bytes value is a compile error.

ratified

[LSTR.7.1.2] Regex character classes (\w, \d, \s) match as [LREG.8.5] states: ASCII. The u flag is reserved ([LREG.5.4]).

see also: LREG.5.4, LREG.8.5

JSON [LSTR.7.2]#

[LSTR.7.2.1] JSON strings (per [LEXP.7]) are String values under the LSTR contract. JSON itself mandates Unicode strings; the wire encoding for JSON values in DS is UTF-8 per JSON RFC 8259.

see also: LEXP.7

[LSTR.7.2.2] Path access on Json values that yields a string (json.field where field is a JSON string) returns a String per LSTR.1.

see also: LSTR.1

[LSTR.7.2.3] A \u escape in JSON text is RFC 8259's. Wherever a runtime or the compiler reads JSON text, a host's text for a Json value or a cache seed ([LSTR.8.5]) or a test's json() value ([LTSL.4.1]) alike, a \u escape names one UTF-16 code unit, as RFC 8259 §7 has it. A high-surrogate escape (\uD800 to \uDBFF) followed at once by a low-surrogate escape (\uDC00 to \uDFFF) names the one codepoint above U+FFFF that the pair encodes, and the reader must decode it so. A surrogate escape that is not the first or second half of such a pair (a lone high, a lone low, or a low before a high) names no codepoint a String can hold ([LSTR.1.1]): the reader must refuse the text as malformed JSON, as it refuses any text that is not JSON.

ratified — see also: LSTR.1.1, LSTR.8.5, LTSL.4.1

String interpolation [LSTR.7.3]#

[LSTR.7.3.1] Interpolation expressions ("value: {x}") produce a String per LSTR.1. Interpolated values are converted to their string representation by the language's standard rules; the result is a sequence of codepoints, not bytes.

see also: LSTR.1

distance.hamming and distance.levenshtein [LSTR.7.4]#

Deferred: not binding in this edition.

[LSTR.7.4.1] String overloads of distance.hamming and distance.levenshtein per [LEXP.5.6] count codepoint-level differences and edits, not byte-level.


see also: LEXP.5.6

Wire-format invariants [LSTR.8]#

[LSTR.8.1] Adapters delivering String slot values must deliver valid UTF-8. Adapters that source data in other encodings must decode adapter-side before emission.

[LSTR.8.2] Adapters delivering Bytes slot values must deliver octet sequences without any encoding interpretation. The receiving rule is responsible for explicit decode if a text interpretation is needed.

[LSTR.8.3] An adapter that violates LSTR.8.1 — emitting a String slot whose bytes are not valid UTF-8 — is a runtime adapter error per [LECC.3.7].

The obligation is the adapter's and the fact does not arrive. A value that does not fit a declared non-Json slot is [LADP.5.6.1]'s case: the adapter coerces or refuses to emit, and a metric increments so operators see the mismatch ([LADP.5.6.2]). Nothing reaches the engine, so no activation fires on the affected slot and no fault is raised on the rule's behalf. Where an adapter hands the engine such a value all the same, the engine refuses it ([LSTR.8.5]).

see also: LADP.5.6.1, LADP.5.6.2, LECC.3.7, LSTR.8.1, LSTR.8.5

[LSTR.8.4] Persistent storage of String and Bytes values follows the same wire invariants. Restored String values are valid UTF-8; restored Bytes values are byte-identical to what was persisted.

[LSTR.8.5] The engine refuses text that is not valid UTF-8. Text a host hands the engine must be well-formed UTF-8 (RFC 3629): a String slot value, JSON text to parse, a string or object key of a Json value the host builds, a cache seed, a call's response, and an engine's run and tier identifiers ([NLFC.1.2]). The engine must check each where it enters, and must refuse one that is not with DS_ERR_ENCODING, changing nothing. The status is the whole refusal: the engine records no event for it and raises no fault, as for a value over a Json limit ([LGRA.5.8.6]). The engine must not repair the text by replacing, dropping or truncating any of its bytes.

The engine must check JSON text whole, after [LGRA.5.8.6]'s text limit and before it parses any of it, so text that is both ill-formed and malformed JSON is DS_ERR_ENCODING.

JSON text is refused before the engine's state is judged. Where a host gives a seed or a Json slot as JSON text, the engine must judge the text first: its limit (DS_ERR_JSON_LIMIT, [LGRA.5.8.6]), then its encoding (DS_ERR_ENCODING), then its parse (DS_ERR_PARSE). Only text that passes all three becomes the value that [NFCT.2.11] and [NLFC.9.6] judge, so a text refusal comes before every step they order, a halted engine included. Where that value breaks this clause and another refusal, those two clauses say which status it returns.

So every operation behind the boundary meets only well-formed UTF-8, and every conforming runtime refuses the same inputs. A .dsc's strings are judged where the artefact is decoded ([RDSC.9.12]). A recorded fact stream and a FactExternal are trusted input ([RDSC.9.9]), and this clause does not reach the text they carry. A FactExternal whose text is not well-formed is a malformed record, and [NCBK.10.4] refuses it as one. A recorded fact body whose text is not well-formed is a malformed body, and [RFCS.6.3] refuses it as one.


ratified — see also: LGRA.5.8.6, NCBK.10.4, NFCT.2.11, NLFC.1.2, NLFC.9.6, RDSC.9.12, RDSC.9.9, RFCS.6.3

String commitments [LSTR.9]#

[LSTR.9.1] Strings are Unicode codepoint sequences, not byte sequences. Authors who need byte-level operations use Bytes. Strings are not UTF-16 code-unit sequences: surrogate pairs are never visible to authors, and codepoint semantics stay clean.

see also: LSTR.1.1

[LSTR.9.2] Length is codepoint count. An adapter that needs a string's byte count computes it ([LSTR.4.6]).

see also: LSTR.2.1, LSTR.4.6

[LSTR.9.3] NFC and NFD strings are distinct. Normalisation tables are large and revision-bound; keeping normalisation explicit keeps the spec stable.

see also: LSTR.3.1

[LSTR.9.6] No locale-aware operations and no case mapping. Locale support and case conversion are out of scope; rules that need either compute the converted form adapter-side. No builtin performs either, so a call to one is an unresolved name ([LGRA.22.7]), DS_VALIDATE_UNKNOWN_FUNCTION.


ratified — see also: LGRA.22.7, LSTR.3.3