Skip to content
Kavachकवच

Standard Library: std.text and std.collections

● Intermediate5 min

Every type, method and function of std.text and std.collections: what the spec lists, then each declaration in its source, with its examples and the line that declares it.

Spec 14.9 · learn it in Text and Collections.

A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.

What the spec lists

FunctionGives
text.pad_left(s, width, fill), text.pad_right(s, width, fill)s filled with fill (a Char, default ' ') up to width characters
text.words(s)The words of s: the parts between runs of spaces, tabs and line breaks
text.capitalize(s)s with its first character in upper case
text.truncate(s, max)At most max characters of s
text.is_blank(s)Whether s has only spaces, tabs and line breaks
text.find_ints(s)Every whole number written inside s, in order: "a12b34" gives [12, 34]
text.find_int(s)The first of them, or none: "sd23" gives 23
text.find_float(s)The first number with its fraction, or none: "pi is 3.14!" gives 3.14
text.nfc(s), text.nfd(s), text.nfkc(s), text.nfkd(s)s in a Unicode normal form (UAX #15), so texts written with different code points compare equal: nfc joins a letter and its accent into one code point, nfd splits them, and the k forms also turn compatibility characters into plain ones (fi into fi, full-width 1 into 1). The runtime gives these four (decision 0102); the rest of std.text is written in Kavach
collections.chunks(items, size)The items in lists of size (the last may be shorter)
collections.zip(a, b)Pairs of items at the same position, as long as the shorter list
collections.unique(items)Each item once, in the order first seen
collections.counts(items)How many times each item appears, in the order first seen
collections.flatten(lists)The items of all the lists, in order

std.text · text.kav

use std.text: 0 types and 9 functions. stdlib/text/text.kav

pad_left function

s with fill added in front until it is width characters long.

pub fn pad_left(s: String, width: Int, fill: Char = ' ') -> String
    require width >= 0
    require width <= 1_000_000
    ensure result.len() >= s.len()
    example pad_left("7", 3, '0') == "007"
    example pad_left("hello", 3) == "hello"

pad_right function

s with fill added after it until it is width characters long.

pub fn pad_right(s: String, width: Int, fill: Char = ' ') -> String
    require width >= 0
    require width <= 1_000_000
    ensure result.len() >= s.len()
    example pad_right("ab", 4, '.') == "ab.."

words function

The parts of s between runs of spaces, tabs and line breaks.

pub fn words(s: String) -> [String]
    example words("  to be\tor\nnot ") == ["to", "be", "or", "not"]
    example words("") == []

capitalize function

s with its first character in upper case.

pub fn capitalize(s: String) -> String
    ensure result.len() == s.len() or s.is_empty()
    example capitalize("asha") == "Asha"
    example capitalize("") == ""

truncate function

At most max characters of s.

pub fn truncate(s: String, max: Int) -> String
    require max >= 0
    ensure result.len() <= max
    example truncate("Kavach", 3) == "Kav"
    example truncate("hi", 5) == "hi"

is_blank function

Whether s has only spaces, tabs and line breaks.

pub fn is_blank(s: String) -> Bool
    example is_blank(" \t\n")
    example not is_blank(" a ")

find_ints function

Every whole number written in s, in order ("a12b-3" gives [12, -3]). A comma or any other mark ends a number, and one too big for Int is left out. Unlike Int(s), which reads the whole text strictly (spec 2.1), this looks for numbers inside other text.

pub fn find_ints(s: String) -> [Int]
    example find_ints("a12b34") == [12, 34]
    example find_ints("pages 10-20") == [10, 20]
    example find_ints("none here") == []

find_int function

The first whole number written in s, as find_ints reads them, or none when there is none.

pub fn find_int(s: String) -> Int?
    example find_int("12sdsfs") == 12
    example find_int("sd23") == 23
    example find_int("sd3f") == 3
    example find_int("-5 °C") == -5
    example find_int("item-3") == 3
    example find_int("₹1,299") == 1
    example find_int("no digits") == none

find_float function

The first number written in s, with a fraction if it has one ("v2.5b" is 2.5, "x3" is 3.0), or none when there is none.

pub fn find_float(s: String) -> Float?
    example find_float("pi is 3.14!") == 3.14
    example find_float("x3") == 3.0
    example find_float("-0.5 m") == -0.5
    example find_float("end.") == none

std.collections · collections.kav

use std.collections: 0 types and 5 functions. stdlib/collections/collections.kav

chunks function

The items in lists of size; the last list may be shorter.

pub fn chunks<T>(items: [T], size: Int) -> [[T]]
    require size > 0
    example chunks([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4], [5]]
    example chunks([1], 3) == [[1]]

zip function

Pairs of items at the same position, as long as the shorter list.

pub fn zip<A, B>(a: [A], b: [B]) -> [(A, B)]
    example zip([1, 2, 3], ["a", "b"]) == [(1, "a"), (2, "b")]

unique function

Each item once, in the order first seen.

pub fn unique<T>(items: [T]) -> [T]
    example unique([3, 1, 3, 2, 1]) == [3, 1, 2]

counts function

How many times each item appears, in the order first seen.

pub fn counts<T>(items: [T]) -> [T: Int]
    example counts(["a", "b", "a"]) == ["a": 2, "b": 1]

flatten function

The items of all the lists, in order.

pub fn flatten<T>(lists: [[T]]) -> [T]
    example flatten([[1, 2], [], [3]]) == [1, 2, 3]