.nib std
nib 0.3.1-pre

Pre-release documentation. This describes nib 0.3.1-pre, which is on main and has no release yet: what it documents beyond 0.3.0 may change before it ships.

module always in scope

String

Immutable text, written "…", with {expr} interpolation and ++ to join. Lengths and offsets count bytes.

String is nib's text type. A literal is written in double quotes, and {expr} inside one splices in the value of an Int, Float, Bool or String expression: "at {i}". Strings join with ++. Nothing converts to a String implicitly — "a" ++ 1 is a type error, and interpolation is the way to render a number.

let name = "nib";
let n = 3;
"{name} has {n} letters: " ++ name ++ "!"
nib has 3 letters: nib!

Escapes inside a literal are \n, \t, \r, \\, \" and \xNN for a single byte by its two hex digits. Because { always opens a splice, a literal brace is written \x7b.

"line one\nline two \x7bbraced}"
line one
line two {braced}

A String is bytes

String.length counts bytes, and String.slice and String.index_of work in byte offsets. nib does not decode UTF-8, so a character outside ASCII takes more than one position, and a slice can cut through the middle of one. Bytes is the type for data that is not text at all.

(String.length("cafe"), String.length("café"))
(4, 5)

Functions

funclength
func length :: String -> Int

The number of bytes in the string.

(String.length("nib"), String.length(""))
(3, 0)
funcslice
func slice :: String, Int, Int -> String raises Err.IndexOutOfRange

The bytes from offset lo up to, but not including, hi. Raises Err.IndexOutOfRange when lo is negative, hi is past the end, or lo is greater than hi; an empty range such as slice(s, 2, 2) is fine and answers "".

(String.slice("hello world", 0, 5), String.slice("hello", 2, 2))
("hello", "")

Out-of-range offsets are refused rather than clamped:

try String.slice("abc", 1, 9)
rescue Err.IndexOutOfRange(i, msg) => "refused at {i}: {msg}" end
refused at 1: String.slice(1, 9) is not inside a String of length 3
func index_of :: String, String -> Int

The byte offset of the first occurrence of needle, or -1 if it does not occur. An empty needle is found at 0.

(String.index_of("a/b/c", "/"), String.index_of("abc", "z"), String.index_of("abc", ""))
(1, -1, 0)
funcsplit
func split :: String, String -> Vec<String>

The pieces of the string between occurrences of sep, in order. Adjacent separators, or one at either end, produce empty pieces, and a string without sep splits into one piece — itself. The empty string splits into one empty piece.

(String.split("a,b,,c", ","), String.split(",x,", ","), String.split("no-sep", ","), String.split("", ","))
(#["a", "b", "", "c"], #["", "x", ""], #["no-sep"], #[""])

An empty sep splits the string into one piece per byte:

String.split("nib", "")
#["n", "i", "b"]
func starts_with :: String, String -> Bool

true if the string begins with prefix. Every string starts with "".

(String.starts_with("/api/users", "/api"), String.starts_with("api", "/api"), String.starts_with("x", ""))
(true, false, true)