Text
Text values
This type represents human-readable text as sequences of characters of type [Char
](Char.html) .
If t
is a value of type Text
, then:
* t.chars()
returns an iterator of type Iter<Char>
enumerating its characters from first to last.
* t.size()
returns the size (or length) of t
(and t.chars()
) as a Nat
.
This module defines additional operations on Text
values.
fromChar
let fromChar : (c : Char) -> Text
Conversion.
Returns the text value of size 1 containing the single character c
.
toIter
func toIter(t : Text) : Iter.Iter<Char>
Conversion.
Creates an iterator that traverses the characters of the text t
.
fromIter
func fromIter(cs : Iter.Iter<Char>) : Text
Conversion.
Returns the text value containing the sequence of characters in cs
.
hash
func hash(t : Text) : Hash.Hash
Returns a hash obtained by using the djb2
algorithm from http://www.cse.yorku.ca/~oz/hash.html
This function is good enough for use in a hash-table but it’s not a cryptographic hash function!
compare
func compare(t1 : Text, t2 : Text) : {#less; #equal; #greater}
Returns the order of t1
and t1
.
join
func join(sep : Text, ts : Iter.Iter<Text>) : Text
Returns the concatenation of text values in ts
, separated by sep
.
map
func map(t : Text, f : Char -> Char) : Text
Returns the result of applying f
to each character in ts
, concatenating the intermediate single-character text values.
translate
func translate(t : Text, f : Char -> Text) : Text
Returns the result of applying f
to each character in ts
, concatenating the intermediate text values.
Pattern
type Pattern = {#char : Char; #text : Text; #predicate : (Char -> Bool)}
A pattern p
describes a sequence of characters. A pattern has one of the following forms:
-
#char c
matches the single character sequence,c
. -
#predicate p
matches any single character sequencec
satisfying predicatep(c)
. -
#text t
matches multi-character text sequencet
.
A match for p
is any sequence of characters matching the pattern p
.
contains
func contains(t : Text, p : Pattern) : Bool
Returns true if t
contains a match for pattern p
.
startsWith
func startsWith(t : Text, p : Pattern) : Bool
Returns true
if t
starts with a prefix matching pattern p
, otherwise returns false
.
endsWith
func endsWith(t : Text, p : Pattern) : Bool
Returns true
if t
ends with a suffix matching pattern p
, otherwise returns false
.
replace
func replace(t : Text, p : Pattern, r : Text) : Text
Returns t
with all matches of pattern p
replaced by text r
.
stripStart
func stripStart(t : Text, p : Pattern) : ?Text
Returns the optioned suffix of t
obtained by eliding exactly one leading match of pattern p
, otherwise null
.
stripEnd
func stripEnd(t : Text, p : Pattern) : ?Text
Returns the optioned prefix of t
obtained by eliding exactly one trailing match of pattern p
, otherwise null
.
trimStart
func trimStart(t : Text, p : Pattern) : Text
Returns the suffix of t
obtained by eliding all leading matches of pattern p
.
trimEnd
func trimEnd(t : Text, p : Pattern) : Text
Returns the prefix of t
obtained by eliding all trailing matches of pattern p
.
trim
func trim(t : Text, p : Pattern) : Text
Returns the subtext of t
obtained by eliding all leading and trailing matches of pattern p
.