Text Module
The aivi.text module provides core string and character utilities for Text and Char. It focuses on predictable, Unicode-aware behavior, and uses Option/Result instead of sentinel values like -1.
Overview
aivi
use aivi.text
greeting = "Hello, AIVI!"
len = length greeting
words = split " " greeting
upper = toUpper greetingTypes
aivi
type Bytes
type Encoding = Utf8 | Utf16 | Utf32 | Latin1
type TextError = InvalidEncoding EncodingCore API (v0.1)
Length and inspection
| Function | Explanation |
|---|---|
length text | Returns the number of Unicode scalar values in text. |
isEmpty text | Returns true when text has zero length. |
Character predicates
| Function | Explanation |
|---|---|
isDigit char | Returns whether char is a Unicode digit. |
isAlpha char | Returns whether char is a Unicode letter. |
isAlnum char | Returns whether char is a Unicode letter or digit. |
isSpace char | Returns whether char is a Unicode whitespace. |
isUpper char | Returns whether char is uppercase. |
isLower char | Returns whether char is lowercase. |
Search and comparison
| Function | Explanation |
|---|---|
contains haystack needle | Returns whether needle occurs in haystack. |
startsWith text prefix | Returns whether text starts with prefix. |
endsWith text suffix | Returns whether text ends with suffix. |
indexOf haystack needle | Returns the first index of needle, or None when not found. |
lastIndexOf haystack needle | Returns the last index of needle, or None when not found. |
count haystack needle | Returns the number of non-overlapping occurrences. |
compare a b | Returns -1, 0, or 1 in Unicode codepoint order (not locale-aware). |
Notes:
indexOfandlastIndexOfreturnNonewhen not found.
Slicing and splitting
| Function | Explanation |
|---|---|
slice start end text | Returns the substring from start (inclusive) to end (exclusive). |
split sep text | Splits text on sep. |
splitLines text | Splits on line endings. |
chunk size text | Splits into codepoint chunks of length size. |
Notes:
slice start end textis half-open (startinclusive,endexclusive) and clamps out-of-range indices.chunksplits by codepoint count, not bytes.
Trimming and padding
| Function | Explanation |
|---|---|
trim text | Removes Unicode whitespace from both ends. |
trimStart text | Removes Unicode whitespace from the start. |
trimEnd text | Removes Unicode whitespace from the end. |
padStart width fill text | Pads on the left to reach width using repeated fill. |
padEnd width fill text | Pads on the right to reach width using repeated fill. |
Notes:
padStart width fill textrepeatsfillas needed and truncates extra.
Modification
| Function | Explanation |
|---|---|
replace text needle replacement | Replaces the first occurrence of needle. |
replaceAll text needle replacement | Replaces all occurrences of needle. |
remove text needle | Removes all occurrences of needle. |
repeat count text | Repeats text count times. |
reverse text | Reverses grapheme clusters. |
concat parts | Concatenates all parts into one Text. |
Notes:
replacechanges the first occurrence only.removeisreplaceAll needle "".reverseis grapheme-aware and may be linear-time with extra allocations.
Case and normalization
| Function | Explanation |
|---|---|
toLower text | Converts to lowercase using Unicode rules. |
toUpper text | Converts to uppercase using Unicode rules. |
capitalize text | Uppercases the first grapheme and lowercases the rest. |
titleCase text | Converts to title case using Unicode rules. |
caseFold text | Produces a case-folded form for case-insensitive comparisons. |
normalizeNFC text | Normalizes to NFC. |
normalizeNFD text | Normalizes to NFD. |
normalizeNFKC text | Normalizes to NFKC. |
normalizeNFKD text | Normalizes to NFKD. |
Encoding / decoding
| Function | Explanation |
|---|---|
toBytes encoding text | Encodes text into Bytes using encoding. |
fromBytes encoding bytes | Decodes bytes and returns TextError on invalid input. |
Formatting and conversion
| Function | Explanation |
|---|---|
toText value | Formats any Show instance into Text. |
parseInt text | Parses a decimal integer, returning None on failure. |
parseFloat text | Parses a decimal float, returning None on failure. |
Usage Examples
aivi
use aivi.text
slug = " Hello World "
clean = slug |> trim |> toLower |> replaceAll " " "-"
maybePort = parseInt "8080"