Data.String
String operations, byte-oriented and ASCII-accurate.
Built over the primitive UTF-8 string operations. Base includes this module.
Functions and Values
str_join
str_join : (String, List(String)) -> String
Join a list of strings, placing sep between adjacent elements.
str_join(", ", ["a", "b", "c"])
a, b, c
str_repeat
str_repeat : (String, Int) -> String
s repeated n times (the empty string when n <= 0).
str_repeat("ab", 3)
ababab
pad_left
pad_left : (String, Int) -> String
Right-align s to width w by prepending spaces (unchanged if already wider).
pad_left("42", 5)
42
pad_right
pad_right : (String, Int) -> String
Left-align s to width w by appending spaces (unchanged if already wider).
pad_right("42", 5)
42
lines_of
lines_of : (List(String)) -> String
Join a list of strings with newlines between them.
lines_of(["one", "two"])
one
two
occurs_at
occurs_at : (String, String, Int, Int) -> Bool
Helper for the substring queries: whether needle sits at byte offset j in s, comparing from position k.
starts_with
starts_with : (String, String) -> Bool
True when s begins with prefix.
starts_with("foo", "foobar")
true
ends_with
ends_with : (String, String) -> Bool
True when s ends with suffix.
ends_with("bar", "foobar")
true
index_of_go
index_of_go : (String, String, Int) -> Int
Helper for index_of: search for needle in s from byte offset j.
index_of
index_of : (String, String) -> Int
The byte offset of the first occurrence of needle in s, or -1 if absent.
index_of("bar", "foobar")
3
contains
contains : (String, String) -> Bool
True when needle occurs anywhere in s.
contains("oob", "foobar")
true
map_case
map_case : (String, Int, Buf, Bool) -> Buf
Helper for to_upper/to_lower: fold ASCII case mapping over s into a byte buffer (up selects upper- vs lower-casing).
to_upper
to_upper : (String) -> String
ASCII upper-case of s (non-letters unchanged).
to_upper("Hello")
HELLO
to_lower
to_lower : (String) -> String
ASCII lower-case of s (non-letters unchanged).
to_lower("Hello")
hello
ltrim_idx
ltrim_idx : (String, Int) -> Int
Helper for trim: the first non-whitespace byte index at or after i.
rtrim_idx
rtrim_idx : (String, Int) -> Int
Helper for trim: the index just past the last non-whitespace byte before i.
slice_bytes
slice_bytes : (String, Int, Int, Buf) -> Buf
Helper for trim: collect the bytes of s in [lo, hi) into buf.
trim
trim : (String) -> String
Strip leading and trailing ASCII whitespace.
trim(" hi ")
hi
index_of_from
index_of_from : (Int, String, Int) -> Int
The index of character c in s at or after position i, or -1 if absent.
split_from
split_from : (Int, String, Int) -> List(String)
Helper for split: split s on c, starting from position i.
split
split : (Int, String) -> List(String)
Split s into the pieces between each occurrence of character c.
split(char_at(",", 0), "a,b,c")
[a, b, c]
str_of_char
str_of_char : (Char) -> String
The single-character string containing c.
str_of_char(chr(65))
A
chars_from
chars_from : (String, Int) -> List(Char)
Helper for chars: the characters of s from position i onward.
chars
chars : (String) -> List(Char)
The list of characters in s.
chars("hi")
[104, 105]