Strings and bytes#
String#
String values#
A String value is an immutable sequence of Unicode codepoints.
Every codepoint in U+0001 to U+10FFFF is admissible except the surrogates U+D800 to U+DFFF. An operation that produces modified text returns a new String. The empty string is admissible and is present, with length zero, as distinct from a missing slot.
U+0000#
U+0000 is refused where it would enter a String, a Json string or a Json object key.
A literal that spells it is a compile error. A host call that supplies it, JSON text holding it raw or as \u0000, a recording and an artefact are each refused. A value is never truncated at a U+0000, and a U+0000 is never dropped.
String encoding#
The wire form of a String between runtime, host and storage is UTF-8.
(Spec: [LSTR.1.2])
String length#
len(s)
len on a String returns its number of Unicode codepoints, as Int.
café with a precomposed é has length 4. U+1F44B U+1F3FD has length 2.
len(r.label)
String equality#
a == b
Two strings are equal iff they have the same length and matching codepoints at every position.
No normalisation applies: café with U+00E9 is not equal to café with U+0065 U+0301.
String ordering#
a < b
String ordering is lexicographic by codepoint value, which is UTF-8 byte order for a well-formed pair, and is not locale-aware.
r.label == "a"
r.label < "b"
(Spec: [LSTR.3.2])
Regex on strings#
Regex patterns and matches operate on String values at the codepoint level.
A regex on a Bytes value is refused with DS_VALIDATE_REGEX_ON_BYTES.
(Spec: [LSTR.7.1.1], Source: compiler/mercury/src/dsc.typecheck.m)
JSON strings#
A JSON string is a String; a path read that yields one returns a String. The wire encoding is UTF-8.
(Spec: [LSTR.7.2.1], [LSTR.7.2.2])
JSON text escapes#
A \u escape in JSON text names one UTF-16 code unit.
A high-surrogate escape followed at once by a low-surrogate escape names the one codepoint above U+FFFF the pair encodes. Any other surrogate escape makes the text malformed JSON, and the reader refuses it.
(Spec: [LSTR.7.2.3])
Interpolation result#
An interpolation expression produces a String, a sequence of codepoints.
(Spec: [LSTR.7.3.1])
Bytes#
Bytes values#
A Bytes value is an immutable sequence of octets with no encoding metadata.
The language does not interpret it as text. An operation that produces modified bytes returns a new Bytes value. A rule stores one in a slot, compares it for equality and passes it to a call.
Bytes equality#
a == b, a != b
Two Bytes operands are equal iff they have the same length and the same octet at every position.
<, <=, > and >= with either operand Bytes are refused at the operator with DS_VALIDATE_UNORDERED_TYPE. In a comparison chain each step is judged on its own two operands. A null operand propagates.
(Spec: [LEXP.7.19])
Bytes length#
len(b)
len on a Bytes value returns its byte count, as Int.
(Spec: [LSTR.4.2])
Bytes and String conversion#
String and Bytes are distinct types with no conversion in either direction.
== between a String and a Bytes is refused with DS_VALIDATE_TYPE_MISMATCH. A :: cast from Bytes to String, or from String to Bytes, is refused with DS_VALIDATE_BYTES_CONVERSION_RETIRED.
(Spec: [LSTR.4.5], [LSTR.4.6], Source: compiler/mercury/src/dsc.typecheck.m)
Capacity#
String capacity#
<slot>: String capacity N
capacity N bounds a String slot to at most N codepoints, counted as len counts them; N is a positive integer, and a slot without it is unbounded.
A slot of any other type is refused with
DS_VALIDATE_CAPACITY_BAD_TYPE.An
Nbelow 1 is refused withDS_VALIDATE_CAPACITY_NOT_POSITIVE.A string literal written to a slot it exceeds is refused with
DS_VALIDATE_CAPACITY_LITERAL_EXCEEDED.capacityis required only where a capability’s clause requires it: underlinear_scan: false, on aStringslot read by a regex match or an ordering comparison.
schema Line scope logical:
id: String key
text: String capacity 8 mutable
Capacity at run time#
A longer value that reaches the slot at run time, in an asserted or an emitted fact, is a bounds refusal.
The value is not stored or truncated, and the assert or the emit does not happen. The BoundsError names CAP_VALUE_CAPACITY, 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.
(Spec: [LSTR.11.3])