Strings

Module strings

import strings

Text operations that count characters, not bytes.

strings works on Like — a prelude.String or a single prelude.Char — so a one-character value can be passed anywhere text is expected.

Positions here are character positions. count returns the number of UTF-8 code points, which is not the same as len(): len("café") is 5 bytes, count("café") is 4 characters. Indexing a String directly gives bytes; charAt gives characters.

Dependencies


Contents


Unions

Like

`strings/strings.zirr:6`
union Like {
	Char
	String
}

A single character or a sequence of characters.

Cases

Case Interpretation
Char A single character from a string.
String A regular String. Can be indexed to get the Byte. Iterates over Char.

Functions

charAt

`strings/strings.zirr:19`
extern fn charAt(v: Like, index: Int) -> Char

Returns the character at index (0-based, counting characters, not bytes).


concat

`strings/strings.zirr:102`
extern fn concat(parts: [Like]) -> String

Concatenates every part into a single String, in order.


contains

`strings/strings.zirr:55`
extern fn contains(v: Like, needle: Like) -> Bool

Returns whether needle occurs anywhere in v.


count

`strings/strings.zirr:16`
extern fn count(v: Like) -> Int

Returns the number of characters (runes) in v. Unlike len(), which counts
bytes, this counts UTF-8 code points — len(“café”) is 5, count(“café”) is 4.


firstIndexOf

`strings/strings.zirr:67`
fn firstIndexOf(v: Like, needle: Like) -> Option

Returns the character index of needle’s first occurrence in v, or None if absent.


from

`strings/strings.zirr:12`
extern fn from(v: Like) -> String

Normalizes a Char or String to String.


hasPrefix

`strings/strings.zirr:58`
extern fn hasPrefix(v: Like, prefix: Like) -> Bool

Returns whether v starts with prefix.


hasSuffix

`strings/strings.zirr:61`
extern fn hasSuffix(v: Like, suffix: Like) -> Bool

Returns whether v ends with suffix.


isDigit

`strings/strings.zirr:44`
extern fn isDigit(v: Like) -> Bool

Returns whether v is not empty and every character is a decimal digit.
Unicode-aware — isDigit(‘٣’) is true, isDigit(“”) is false.


isEmpty

`strings/strings.zirr:38`
fn isEmpty(v: Like) -> Bool

Returns whether v has no characters.


isLetter

`strings/strings.zirr:48`
extern fn isLetter(v: Like) -> Bool

Returns whether v is not empty and every character is a letter.
Unicode-aware — isLetter(‘é’) is true, isLetter(“”) is false.


isSpace

`strings/strings.zirr:52`
extern fn isSpace(v: Like) -> Bool

Returns whether v is not empty and every character is whitespace.
Unicode-aware — a non-breaking space counts, isSpace(“”) is false.


join

`strings/strings.zirr:99`
extern fn join(parts: [Like], separator: Like) -> String

Joins parts into a single String, with separator between each.


lastIndexOf

`strings/strings.zirr:77`
fn lastIndexOf(v: Like, needle: Like) -> Option

Returns the character index of needle’s last occurrence in v, or None if absent.


quote

`strings/strings.zirr:127`
extern fn quote(v: Like) -> String

Returns v as a double-quoted string literal, escaping the characters that
need it, so the result reads back as Zirric source. A Char is quoted as a
String — quote(‘a’) is the same as quote(“a”).


range

`strings/strings.zirr:26`
fn range(v: Like, r: ranges.Like) -> String

Returns the characters of v selected by r.


repeat

`strings/strings.zirr:105`
extern fn repeat(v: Like, n: Int) -> String

Returns v repeated n times.


replace

`strings/strings.zirr:87`
extern fn replace(v: Like, target: Like, replacement: Like) -> String

Replaces every occurrence of target in v with replacement.


replaceFirst

`strings/strings.zirr:90`
extern fn replaceFirst(v: Like, target: Like, replacement: Like) -> String

Replaces only the first occurrence of target in v with replacement.


replaceLast

`strings/strings.zirr:93`
extern fn replaceLast(v: Like, target: Like, replacement: Like) -> String

Replaces only the last occurrence of target in v with replacement.


slice

`strings/strings.zirr:23`
extern fn slice(v: Like, start: Int, end: Int) -> String

Returns the characters of v from start (inclusive) to end (exclusive),
counting characters, not bytes.


split

`strings/strings.zirr:96`
extern fn split(v: Like, separator: Like) -> [String]

Splits v on every occurrence of separator.


toLower

`strings/strings.zirr:113`
extern fn toLower(v: Like) -> Like

Returns v with every letter converted to lowercase. Preserves whether v
was a Char or a String — toLower(‘A’) is ‘a’, toLower(“A”) is “a”.


toUpper

`strings/strings.zirr:109`
extern fn toUpper(v: Like) -> Like

Returns v with every letter converted to uppercase. Preserves whether v
was a Char or a String — toUpper(‘a’) is ‘A’, toUpper(“a”) is “A”.


trim

`strings/strings.zirr:116`
extern fn trim(v: Like) -> String

Returns v with leading and trailing whitespace removed.


trimPrefix

`strings/strings.zirr:119`
extern fn trimPrefix(v: Like, prefix: Like) -> String

Returns v with a leading prefix removed, if present.


trimSuffix

`strings/strings.zirr:122`
extern fn trimSuffix(v: Like, suffix: Like) -> String

Returns v with a trailing suffix removed, if present.


unquote

`strings/strings.zirr:132`
extern fn unquote(v: Like) -> Result

Reads a quoted literal, returning Ok with the text it denotes and Err when v
is not one. Both literal forms are accepted, “"hi"” and “‘a’”, and quote
is inverted exactly.