Data.String
A String is an immutable sequence of bytes that is guaranteed to be
well-formed UTF-8. That is the whole model, and every operation here is
judged by whether it preserves the invariant. It is not "a sequence of
characters" and not "a sequence of code points" -- the code points are a
view, and so are the bytes.
Note what is absent. There is no length, because "how long is this
string" has three defensible answers and a length would silently pick
one; there is no Index String and no Length String, for
the same reason; and no byte offset ever reaches the surface language, so
there is no way to construct a slice that cuts a character in half.
O(1). The only cheap count a string has.
Deliberately O(n) and deliberately spelled out, so that it can never be mistaken for the cheap one.
An opaque position in a string, for a reader that needs to look ahead and
to remember where a token started. The two alternatives are both wrong at
scale: materialize an Array Char,
which costs four bytes per code point and an allocation per file, or drive
the CodePoints view, which cannot look ahead and cannot say where a token
started.
The invariant holds by construction, which is the whole design. An Index
is only ever obtained from start, end, step or find, so it always
names a character boundary, and there is no arithmetic on one -- no
addition, no toInt, nothing that could compute an offset that is not a
boundary. slice therefore neither validates nor admits an ill-formed
result; the question cannot be asked. Exposing a raw Int offset instead
would be the decision that cannot be taken back, since slice would then
have to validate every call or admit ill-formed strings.
An Index belongs to the string it came from. Using one with a different
string is not caught -- it would cost a tag per index to catch -- and lands
on a boundary of the wrong string or panics. That is the same discipline
Iterator's Cursor already runs on.
Ordering is by position in the string, which is what a slice's two ends have to be compared for. It is not a comparison between strings.
Decode one scalar value and answer where the next one begins. This is the
whole of forward motion: None at the end, and otherwise a character and
an index strictly greater than the one handed in, so a loop written on it
terminates.
The character at an index, without moving. What a lexer's peek is.
Both copy. A String is never written after it is made and an Array can
be, so the two must not share storage in either direction.
The total partner of fromBytes: well-formed characters are kept, and each
maximal subpart of an ill-formed sequence -- the longest run that some
well-formed sequence begins with, or one byte -- becomes one U+FFFD. So
E1 80 41 is U+FFFD and A, and E0 80 41 is two U+FFFD and A, since no
well-formed sequence begins E0 80.
A U+FFFD is three bytes and stands for at least one, so the result is at most three times the input: it is written into scratch storage that long and copied once to its length. Input that is well-formed is the string as it is.
One allocation, however many parts: the lengths first, then the copy.
Everything pushed so far, as build joins it, and the builder emptied: how
a long output is written in pieces instead of held whole until the end.
Returns the code points of s as an array: the one-call form of a grid
line. fromCodePoints is the inverse. Allocates O(n).
Returns s with fill repeated in front of it until it has width code
points. Returns s unchanged when it already has width or more.
As padStart, with the fill after s.
The same, splitting at the last occurrence. "M#C.eq" split on "#" is
how an internal name is taken apart, and the qualification is the part after
the last separator whatever the name before it contains.
Split on newlines, and neither invent a last line nor lose one. A file that
ends with a newline has no empty line after it -- the newline terminates the
last line rather than separating it from another -- while a file that does
not end with one still has its final line. split cannot tell those apart,
which is why this is not written on it.
A carriage return immediately before the newline goes with it, so a file written on Windows reads the same as one written anywhere else. A lone carriage return is not a line break: treating it as one is a Classic MacOS convention that now only ever misreads data.
Each line followed by a newline, so unlines(lines(s)) == s for text that
ends in one. A newline ends a line rather than separating two, which is the
difference from joining with "\n".
Split on runs of whitespace, discarding them. Unlike split(s, " ") this
yields no empty strings: two spaces separate two words rather than fencing
an empty one between them, and leading or trailing space produces nothing at
all.
Its representation is its bytes: an exact-sized Prim.Array Byte, never written
after the string is made, and always well-formed UTF-8. Both backends erase
a single-field newtype, so at run time a String is the byte array -- no
wrapper object, no header of its own.
Nothing outside lib/ may hold that array. Prim.Array has SetIndex, so
whoever holds a string's bytes can write them, and literals are shared
constants: one write would change every use of the literal, or leave bytes
that are not UTF-8. So the constructor and bytesOf are exported only from
this module, which only a library module may import.
A string is a value, though its bytes are an array: literals are shared
constants, so two strings built apart may be one array. !Object keeps
identical off it, and off every wrapper over it.
Returns a view of the bytes of s, without copying them. A string's bytes
are never written, so the view never changes.
By bytes, which for well-formed UTF-8 is the order of the code points.