Skip to content

stdlib/stringView source ↗

string

41 items · generated by scribe fromstdlib/string.fd· synced 2026-11-15

stdlib/string41 items

Contents

str_join

Join a Vec of strings with a separator. str_join([“a”, “b”, “c”], ”-”) → “a-b-c”

Example: var parts: Vec= Vec::new(); parts.push(“a”); parts.push(“b”); parts.push(“c”); var s = str_join(parts, ”-”); println(s); assert_eq(s, “a-b-c”);

pub fn str_join(parts: Vec<str>, sep: str) -> str {

Parameters:

  • parts: Vec<str>
  • sep: str

Returns: str

Defined at line 25


str_join_fast

Efficient join that avoids O(n^2) memory by building in batches. Concatenates groups of 1000 parts, then concatenates the groups. This reduces the number of large intermediate strings.

Example: var parts: Vec= Vec::new(); parts.push(“x”); parts.push(“y”); assert_eq(str_join_fast(parts, ”, ”), “x, y”);

pub fn str_join_fast(parts: Vec<str>, sep: str) -> str {

Parameters:

  • parts: Vec<str>
  • sep: str

Returns: str

Defined at line 48


str_join_ints

Join a Vec of i64 values with a separator.

Example: var nums: Vec= Vec::new(); nums.push(1); nums.push(2); nums.push(3); var s = str_join_ints(nums, ”+”); println(s); assert_eq(s, “1+2+3”);

pub fn str_join_ints(nums: Vec<i64>, sep: str) -> str {

Parameters:

  • nums: Vec<i64>
  • sep: str

Returns: str

Defined at line 79


str_reverse

Reverse a string (byte-level).

Example: assert_eq(str_reverse(“abc”), “cba”); assert_eq(str_reverse(""), "");

pub fn str_reverse(s: str) -> str {

Parameters:

  • s: str

Returns: str

Defined at line 100


str_capitalize

Capitalize the first character of a string.

Example: assert_eq(str_capitalize(“hello”), “Hello”);

pub fn str_capitalize(s: str) -> str {

Parameters:

  • s: str

Returns: str

Defined at line 118


str_title

Convert a string to title case (first char of each word uppercase).

Example: var t = str_title(“the quick fox”); println(t); assert_eq(t, “The Quick Fox”);

pub fn str_title(s: str) -> str {

Parameters:

  • s: str

Returns: str

Defined at line 142


str_pad_left

Pad a string on the left with a character to reach the target length. str_pad_left(“42”, 5, ‘0’) → “00042”

Example: assert_eq(str_pad_left(“42”, 5, ‘0’), “00042”); assert_eq(str_pad_left(“toolong”, 3, ’ ’), “toolong”);

pub fn str_pad_left(s: str, width: i64, ch: i64) -> str {

Parameters:

  • s: str
  • width: i64
  • ch: i64

Returns: str

Defined at line 176


str_pad_right

Pad a string on the right with a character to reach the target length. str_pad_right(“42”, 5, ’ ’) → “42 ”

Example: assert_eq(str_pad_right(“42”, 5, ’.’), “42…”);

pub fn str_pad_right(s: str, width: i64, ch: i64) -> str {

Parameters:

  • s: str
  • width: i64
  • ch: i64

Returns: str

Defined at line 190


str_center

Center a string within a given width, padding with the given character. str_center(“hi”, 6, ’ ’) → ” hi ”

Example: assert_eq(str_center(“hi”, 6, ’*’), “hi”);

pub fn str_center(s: str, width: i64, ch: i64) -> str {

Parameters:

  • s: str
  • width: i64
  • ch: i64

Returns: str

Defined at line 204


str_count

Count occurrences of a substring in a string.

Example: assert_eq(str_count(“abcabc”, “bc”), 2); assert_eq(str_count(“aaa”, “x”), 0);

pub fn str_count(s: str, sub: str) -> i64 {

Parameters:

  • s: str
  • sub: str

Returns: i64

Defined at line 225


str_count_char

Count occurrences of a byte (character) in a string.

Example: assert_eq(str_count_char(“banana”, ‘a’), 3);

pub fn str_count_char(s: str, ch: i64) -> i64 {

Parameters:

  • s: str
  • ch: i64

Returns: i64

Defined at line 248


str_find_nth

Find the nth occurrence of a substring. Returns index or -1.

Example: assert_eq(str_find_nth(“a-b-c”, ”-”, 1), 1); assert_eq(str_find_nth(“a-b-c”, ”-”, 2), 3); assert_eq(str_find_nth(“a-b-c”, ”-”, 3), -1);

pub fn str_find_nth(s: str, sub: str, n: i64) -> i64 {

Parameters:

  • s: str
  • sub: str
  • n: i64

Returns: i64

Defined at line 266


str_is_blank

Check if a string is empty.

Example: assert(str_is_blank(” ”)); assert(!str_is_blank(” x ”));

pub fn str_is_blank(s: str) -> bool {

Parameters:

  • s: str

Returns: bool

Defined at line 295


str_is_numeric

Check if a string consists only of digits.

Example: assert(str_is_numeric(“12345”)); assert(!str_is_numeric(“12a”));

pub fn str_is_numeric(s: str) -> bool {

Parameters:

  • s: str

Returns: bool

Defined at line 311


str_is_alpha

Check if a string consists only of alphabetic characters.

Example: assert(str_is_alpha(“abcXYZ”)); assert(!str_is_alpha(“abc1”));

pub fn str_is_alpha(s: str) -> bool {

Parameters:

  • s: str

Returns: bool

Defined at line 330


str_is_alphanumeric

Check if a string consists only of alphanumeric characters.

Example: assert(str_is_alphanumeric(“abc123”)); assert(!str_is_alphanumeric(“a b”));

pub fn str_is_alphanumeric(s: str) -> bool {

Parameters:

  • s: str

Returns: bool

Defined at line 349


str_strip_char

Strip a specific character from both ends of a string.

Example: assert_eq(str_strip_char(--x--, ’-’), “x”);

pub fn str_strip_char(s: str, ch: i64) -> str {

Parameters:

  • s: str
  • ch: i64

Returns: str

Defined at line 370


str_remove_char

Remove all occurrences of a character from a string.

Example: assert_eq(str_remove_char(“a,b,c”, ’,’), “abc”);

pub fn str_remove_char(s: str, ch: i64) -> str {

Parameters:

  • s: str
  • ch: i64

Returns: str

Defined at line 386


bool_to_str

Convert a bool to a string.

Example: assert_eq(bool_to_str(true), “true”); assert_eq(bool_to_str(false), “false”);

pub fn bool_to_str(b: bool) -> str {

Parameters:

  • b: bool

Returns: str

Defined at line 406


int_to_str_padded

Convert an i64 to a string with a minimum width (zero-padded).

Example: assert_eq(int_to_str_padded(7, 3), “007”);

pub fn int_to_str_padded(n: i64, width: i64) -> str {

Parameters:

  • n: i64
  • width: i64

Returns: str

Defined at line 417


int_to_hex

Convert an i64 to a hex string (lowercase, no prefix).

Example: assert_eq(int_to_hex(255), “ff”); assert_eq(int_to_hex(0), “0”);

pub fn int_to_hex(n: i64) -> str {

Parameters:

  • n: i64

Returns: str

Defined at line 428


int_to_bin

Convert an i64 to a binary string (no prefix).

Example: assert_eq(int_to_bin(5), “101”); assert_eq(int_to_bin(0), “0”);

pub fn int_to_bin(n: i64) -> str {

Parameters:

  • n: i64

Returns: str

Defined at line 454


smallstr_max_inline

Maximum length for inline storage.

pub fn smallstr_max_inline() -> i64 {

Returns: i64

Defined at line 504


smallstr_from_str

Create a SmallStr from a regular string.

pub fn smallstr_from_str(s: str) -> SmallStr {

Parameters:

  • s: str

Returns: SmallStr

Defined at line 509


smallstr_new

Alias for smallstr_from_str.

Example: var s = smallstr_new(“hello”); assert_eq(smallstr_len(s), 5); assert(!smallstr_is_inline(s));

pub fn smallstr_new(s: str) -> SmallStr {

Parameters:

  • s: str

Returns: SmallStr

Defined at line 523


smallstr_len

Get the length of a SmallStr.

pub fn smallstr_len(s: SmallStr) -> i64 {

Parameters:

  • s: SmallStr

Returns: i64

Defined at line 528


smallstr_to_str

Convert a SmallStr to a regular heap string.

pub fn smallstr_to_str(s: SmallStr) -> str {

Parameters:

  • s: SmallStr

Returns: str

Defined at line 533


smallstr_is_inline

Check if a SmallStr is stored inline (no heap allocation).

pub fn smallstr_is_inline(s: SmallStr) -> bool {

Parameters:

  • s: SmallStr

Returns: bool

Defined at line 538


smallstr_eq

Compare two SmallStrs for equality.

pub fn smallstr_eq(a: SmallStr, b: SmallStr) -> bool {

Parameters:

  • a: SmallStr
  • b: SmallStr

Returns: bool

Defined at line 543


rope_new

Create a rope leaf from a string.

Example: var r = rope_new(“Hello”); assert_eq(rope_len(r), 5); assert_eq(rope_to_str(r), “Hello”);

pub fn rope_new(s: str) -> Rope {

Parameters:

  • s: str

Returns: Rope

Defined at line 587


rope_concat

Concatenate two ropes. O(1) — just creates a new internal node. The resulting rope shares structure with the inputs (no copying).

pub fn rope_concat(left: Rope, right: Rope) -> Rope {

Parameters:

  • left: Rope
  • right: Rope

Returns: Rope

Defined at line 595


rope_len

Get the total length of a rope. O(1) — stored in the node.

pub fn rope_len(r: Rope) -> i64 {

Parameters:

  • r: Rope

Returns: i64

Defined at line 604


rope_to_str

Convert a rope to a regular string. O(n) — flattens the tree.

pub fn rope_to_str(r: Rope) -> str {

Parameters:

  • r: Rope

Returns: str

Defined at line 609


rope_substring

Create a rope from a substring of another rope. This is O(log n) for balanced ropes.

pub fn rope_substring(r: Rope, start: i64, end: i64) -> Rope {

Parameters:

  • r: Rope
  • start: i64
  • end: i64

Returns: Rope

Defined at line 622


StrView

pub struct StrView {

Fields:

NameType
basestr
starti64
leni64

Defined at line 639


str_view

The bytes start..end of s (clamped to the string), not copied.

Example: var v = str_view(“key=value”, 4, 9); assert_eq(v.length(), 5); assert(v.eq_str(“value”)); assert_eq(v.to_str(), “value”);

pub fn str_view(s: str, start: i64, end: i64) -> StrView {

Parameters:

  • s: str
  • start: i64
  • end: i64

Returns: StrView

Defined at line 652


str_view_all

A view of the whole string.

Example: var v = str_view_all(” a=1 “).trim(); var parts: Vec= v.split(”=”); assert_eq(parts.len(), 2); assert(parts.get(0).eq_str(“a”)); assert_eq(parts.get(1).parse_int(-1), 1);

pub fn str_view_all(s: str) -> StrView {

Parameters:

  • s: str

Returns: StrView

Defined at line 678


StrView

impl StrView {

Defined at line 684


Generated by scribe, the Fides documentation generator