vyrn

std/strings

19 exports, from std/strings.vyrn.

String helpers in Vyrn, over slice and bytes().

Indices and lengths are byte offsets. The case and whitespace helpers are ASCII-only and pass other bytes through, so multi-byte text is never corrupted. Every ?? panic(...) below cuts at an offset the function found by scanning s, at an ASCII byte or at a needle match, so it lies on a character boundary; grep -n "?? panic" lists these claims. substring, padStart and padEnd can receive a bad range and say so.

Dependencies

The import graph shows the whole library.

Imports
fn fromBytesOr(b: Array<UInt8>, fallback: String) -> String

stringFromBytes(b) for bytes known to be valid UTF-8, such as an ASCII transform of valid text. fallback exists only to keep the function total. Decode a fallible byte source with stringFromBytes and handle the Err.

fn substring(s: String, start: Int64, end: Int64) -> String

A byte-range substring. An out-of-range offset or a cut inside a multi-byte character panics, naming the offset. The one place in std/ that turns a SliceError into a crash; a caller that can receive a bad range calls slice and matches.

fn lastIndexOf(s: String, needle: String) -> Option<Int64>

The byte offset of the last occurrence of needle in s, or None. An empty needle matches at s.byteLength.

fn lines(s: String) -> Array<String>

Splits s into lines on \n, stripping a trailing \r from each, as readLine() does. A trailing newline makes no final empty line.

fn replace(s: String, from: String, to: String) -> String

Every non-overlapping occurrence of from in s replaced with to. An empty from returns s unchanged.

fn padEnd(s: String, len: Int64, fill: String) -> String

s right-padded with fill to at least len bytes. Panics when a multi-byte fill does not tile the padding width.

fn editDistance(a: String, b: String) -> Int64

The optimal-string-alignment edit distance between a and b, in bytes: insertions, deletions, substitutions and adjacent transpositions, each costing 1.

Tests

1 test block in std/strings.vyrn. CI runs all of them.

Test names
  • toHex writes sixteen zero-padded nibbles, low byte last

Examples

11 programs in examples/ import this module. Each runs as WebAssembly and as a native binary.