Text
Release v0.4.0
Edition 0.2 adds functions that take a text apart, search it, change it and parse it (spec/22). Each is a prelude function called with method syntax.
- Positions count Unicode scalar values from 0, as
.lendoes; there are no byte offsets or graphemes. - The functions:
split(on:), which keeps empty parts, andjoin(separator:), its inverse.find(text:),Someof the first position orNone, which isSomeexactly whencontains_text(part:)holds; the position and the match lie in the text, so it bounds aslice.ends_with(suffix:).replace(pattern:, by:), every non-overlapping occurrence, left to right.slice(start:, end:), whose precondition0 <= start and start <= end and end <= t.lenyou prove at each call.chars(), one text per scalar value;bytes(), the UTF-8 encoding asList[U8].to_upper()andto_lower(), which change ASCII letters only and keep the length.trim_start()andtrim_end(), astrimat one end.parse_int(), which fails withParseError:Empty,NoDigitsfor-alone,NotADigit(at: i)at the first wrong position, orTooLongfor more than 4300 digits. Handle it as any failure. The prelude'sto_int(t)gives anOption[Int]instead,Nonefor any text that is not an integer or has more than 4300 digits.
- Cost:
find,splitandreplaceare linear in the text;.lenis O(1). - Proofs: the contracts are hypotheses after each call, such as
split'sresult.len >= 1andslice'sresult.len == end - start. There is no theory of strings beyond them. - Labels: every result carries every label of every argument, including the top and the elements of
split's andchars' lists. Whether a secret parses is secret too.
una
module csv
--- The fields of a line, without the spaces around them.
pub fields(line: Text) -> List[Text]
=
let comma = ","
let parts = line.split(on: comma)
parts.map(each: fn(part) -> part.trim())
--- The number at the start of a field, or 0.
pub leading_number(field: Text) -> Int
=
let space = " "
let end = field.find(text: space)
if none use field.len
let digits = field.slice(start: 0, end: end)
let n = digits.parse_int()
if error use 0
n