string
More of strings than the prelude’s: what is replaced in one, what is
cut from either end, cases, padding, and integers and floats written
in other ways than debug writes them (another base, a fixed number
of places). The prelude has the rest (split, joinWith, trim,
indexOf, startsWith, parseInt). Import it by a name, import string as String, as its names are common ones: String.replace,
String.repeat (the alias comes before the type, so String alone
still names the type). As in the prelude, the string worked on comes
last: String.replace "a" "b" s.
Cases are ASCII’s, as rune’s toUpper has them; a width, a padding’s,
is counted in runes, as rune’s count counts them.
replace
replace : String -> String -> String -> Strings with each old in it, from the first, new, none of them
overlapping (Go’s strings.ReplaceAll); an empty old, s as it
is.
>>> replace "ab" "x" "abcabab""xcxx">>> replace "aa" "b" "aaa""ba">>> replace "" "x" "abc""abc"repeat
repeat : Int -> String -> Stringn of s, one after another; fewer than none, none.
>>> List.map (\n -> repeat n "ab") [3, 0, -1]Cons "ababab" (Cons "" (Cons "" Nil))cut
cut : String -> String -> Maybe (String, String)What is before the first sep in s and what is after it, if sep
is in it (Go’s strings.Cut).
>>> cut "=" "key=value=more"Just (MkPair "key" "value=more")>>> cut "=" "key"NothinglastIndexOf
lastIndexOf : String -> String -> Maybe IntWhere needle is last in s, if it is.
>>> lastIndexOf "ab" "abcab"Just 3>>> lastIndexOf "" "abc"Just 3>>> lastIndexOf "x" "abc"Nothingoccurrences
occurrences : String -> String -> IntHow many needles are in s, none overlapping (Go’s
strings.Count): of an empty one, one more than its runes, as
there is one before each and one at the end.
>>> List.map (\n -> occurrences n "cheese") ["e", "ee", ""]Cons 3 (Cons 1 (Cons 7 Nil))trimPrefix
trimPrefix : String -> String -> Strings without prefix at its start, if it is there; else as it is.
>>> List.map (trimPrefix "--") ["--all", "-a"]Cons "all" (Cons "-a" Nil)trimSuffix
trimSuffix : String -> String -> Strings without suffix at its end, if it is there; else as it is.
>>> trimSuffix ".hf" "main.hf""main"trimStart
trimStart : String -> StringWithout the spaces at its start, as trim takes those at either
end.
>>> trimStart " a b ""a b "trimEnd
trimEnd : String -> StringWithout the spaces at its end.
>>> trimEnd " a b "" a b"padStart
padStart : Int -> Rune -> String -> StringOf at least width runes: fill put before it as many times as it
is short.
>>> List.map (padStart 3 '0') ["7", "1234"]Cons "007" (Cons "1234" Nil)padEnd
padEnd : Int -> Rune -> String -> StringOf at least width runes: fill put after it as many times as it is
short.
>>> padEnd 4 '.' "h\u{e9}""h\u{e9}.."toUpper
toUpper : String -> StringEach of a to z in upper case, any other rune as it is.
>>> toUpper "heff, \u{e9}!""HEFF, \u{e9}!"toLower
toLower : String -> StringEach of A to Z in lower case, any other rune as it is.
>>> toLower "HeFF""heff"equalIgnoringCase
equalIgnoringCase : String -> String -> BoolWhether they are the same but for the cases of a to z.
>>> equalIgnoringCase "Heff" "hEFF" && not (equalIgnoringCase "heff" "hef")TrueshowIntBase
showIntBase : Int -> (base : Int) -> Stringn in base, from 2 to 36, its digits after 9 a to z, a -
before it if it is below 0 (Go’s strconv.FormatInt); of another
base, in base 10.
>>> List.map (\b -> showIntBase 255 b) [2, 16, 36, 1]Cons "11111111" (Cons "ff" (Cons "73" (Cons "255" Nil)))>>> showIntBase (-4096) (base := 16)"-1000"parseIntBase
parseIntBase : String -> (base : Int) -> Maybe IntThe integer s is, whole, in base, from 2 to 36 — digits of
either case, perhaps a - before them, as showIntBase writes it;
of another base, Nothing.
>>> List.map (\s -> parseIntBase s 16) ["ff", "-FF", "", "-", "fg"]Cons (Just 255) (Cons (Just (-255)) (Cons Nothing (Cons Nothing (Cons Nothing Nil))))>>> parseIntBase "101" (base := 2)Just 5showFixed
showFixed : Float64 -> (places : Int) -> Stringx with places digits after its point (none, of fewer than
none), exactly: the decimal of so many places nearest it, a tie to
the even (Go’s strconv.FormatFloat(x, 'f', places, 64)); a -
before a negative one, even one that is 0 to so many places; of what
is not finite, as debug writes it.
>>> List.map (\x -> showFixed x 2) [3.14159, 2.5, -0.001, 1.0e21]Cons "3.14" (Cons "2.50" (Cons "-0.00" (Cons "1000000000000000000000.00" Nil)))>>> List.map (\p -> showFixed 0.125 p) [0, 1, 2, 3, -1]Cons "0" (Cons "0.1" (Cons "0.12" (Cons "0.125" (Cons "0" Nil))))>>> List.map (\x -> showFixed x (places := 1)) [0.05, 0.15, 0.25]Cons "0.1" (Cons "0.1" (Cons "0.2" Nil))>>> List.map (\x -> showFixed x 1) [1.0 /. 0.0, 0.0 /. 0.0]Cons "Infinity" (Cons "NaN" Nil)