Data.String

lib/Data/String.gob

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.

fun byteLength : fun(String) -> Int

O(1). The only cheap count a string has.

fun isEmpty : fun(String) -> Bool
fun codePointCount : fun(String) -> Int

Deliberately O(n) and deliberately spelled out, so that it can never be mistaken for the cheap one.

type Bytes
type CodePoints
fun bytes : fun(String) -> Bytes
fun codePoints : fun(String) -> CodePoints
type Index : Ord

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.

fun start : fun(String) -> Index
fun end : fun(String) -> Index
fun atEnd : fun(String, Index) -> Bool
fun step : fun(String, Index) -> Option (Char, Index)

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.

fun decode : fun(String, Index) -> Option Char

The character at an index, without moving. What a lexer's peek is.

fun slice : fun(String, Index, Index) -> String
fun find : fun(String, String, Index) -> Option Index
fun toBytes : fun(String) -> Array Byte

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.

fun fromBytes : [Index c, Length c, Key c ~ Int, Value c ~ Byte] fun(c) -> Option String
fun fromBytesLossy : [Index c, Length c, Key c ~ Int, Value c ~ Byte] fun(c) -> String

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.

type Builder
fun builder : fun() -> Data.String.Builder
fun push : fun(Data.String.Builder, String) -> Unit
fun build : fun(Data.String.Builder) -> String

One allocation, however many parts: the lengths first, then the copy.

fun drain : fun(Data.String.Builder) -> String

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.

fun toCodePoints : fun(String) -> Array Char

Returns the code points of s as an array: the one-call form of a grid line. fromCodePoints is the inverse. Allocates O(n).

fun fromCodePoints : [Iterator c, Item c ~ Char] fun(c) -> String
fun join : [Iterator c, Item c ~ String] fun(c, String) -> String
fun repeat : fun(String, Int) -> String
fun padStart : fun(String, Int, Char) -> String

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.

fun padEnd : fun(String, Int, Char) -> String

As padStart, with the fill after s.

fun startsWith : fun(String, String) -> Bool
fun endsWith : fun(String, String) -> Bool
fun contains : fun(String, String) -> Bool
fun stripPrefix : fun(String, String) -> Option String
fun stripSuffix : fun(String, String) -> Option String
fun split : fun(String, String) -> Vec String
fun splitOnce : fun(String, String) -> Option (String, String)
fun rsplitOnce : fun(String, String) -> Option (String, String)

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.

fun replace : fun(String, String, String) -> String
fun lines : fun(String) -> Vec String

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.

fun unlines : [Iterator c, Item c ~ String] fun(c) -> String

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".

fun words : fun(String) -> Vec String

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.

fun trimStart : fun(String) -> String
fun trimEnd : fun(String) -> String
fun trim : fun(String) -> String
type String

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.

fun view : fun(String) -> ByteView

Returns a view of the bytes of s, without copying them. A string's bytes are never written, so the view never changes.

fun concat : fun(String, String) -> String
instance Iterator Bytes
instance Iterator CodePoints
instance Eq Data.String.Builder
instance Eq String
instance Ord String

By bytes, which for well-formed UTF-8 is the order of the code points.

instance Add String
instance Show String
instance Semigroup String
instance Monoid String
instance Hash String