Acton
Tolk standard library

strings

strings.tolk Tolk standard library file

Definitions

string.depth

@pure
fun string.depth(self): int

Returns snake depth of a string. Example: "abcd" has depth 0, it's a flat string. Example: "ab" + (ref "cd") has depth 1. Example: "a" + (ref "bc" + (ref "d" + (ref ""))) has depth 3.

Source code

string.calculateLength

@pure
@inline_ref
fun string.calculateLength(self): int

Calculates length of a string in bytes (not unicode symbols, just bytes). Since strings are stored in snake format (chain of cells), this requires traversing all cells.

Source code

string.hash

@pure
@inline_ref
fun string.hash(self): uint256

Computes a content hash of a string, traversing all snake cells. The hash is the same regardless of snake layout: "abcd", "ab" + (ref "cd"), "a" + (ref "bcd" + (ref "")) all produce the same hash. Uses FNV-1a hashing with standard 128-bit parameters.

Source code

string.equalTo

@pure
fun string.equalTo(self, another: string): bool

Compares two strings, considering snake cells. For example, "abcd" is equal to "ab" + (ref "cd") is equal to "a" + (ref "bc" + (ref "d")).

Source code

int.toDecimalString

fun int.toDecimalString(self): string

Converts an integer to its decimal string representation. Returns a string (a TVM cell on a stack).

Source code

StringBuilder

struct StringBuilder {
    private chunks: array<string> = []
}

A utility for building strings at runtime by appending multiple parts. The result is a valid snake-format string.

var s = StringBuilder.create().append("Hello, ").append(userName).append("!").build();

After this, s will be a chain of cells: "Hello, " + (ref userName + (ref "!"))

Source code

StringBuilder.create

fun StringBuilder.create(): StringBuilder

Creates an empty StringBuilder.

var s = StringBuilder.create().append(...).build();
Source code

StringBuilder.append

fun StringBuilder.append(mutate self, s: string): self

Appends a string to the builder. The string may itself be a snake chain.

Source code

StringBuilder.appendInt

@inline_ref
fun StringBuilder.appendInt(mutate self, n: int): self

Appends an integer as its decimal representation. For example, sb.appendInt(-42) appends "-42".

Source code

StringBuilder.build

@inline_ref
fun StringBuilder.build(self): string

Builds the final string by concatenating all appended parts into a snake chain. The result is a valid snake-format string where each cell contains data and optionally a ref to next. Throws an exception if append() was not called (a builder is empty).

Source code

string_prefixed0x

type string_prefixed0x = string

A string, which contents starts with 0x00/0x01 byte. By standard, offchain explorers/APIs may treat contents of a string based on 0xXX prefixes:

  • 0x00 + "str" — means "this is a regular snake-encoded string"
  • 0x01 + "str" — means "this is an off-chain content URL" Used in jettons and NFTs for responses from GET methods to comply with TEPs (token standards).
Source code

string.prefixWith00

fun string.prefixWith00(self): string_prefixed0x

Given "str", prefix it with 0x00 byte. Logically it makes a string "\x00str"; physically, it's 0x00 + ref "str". See string_prefixed0x.

Source code

string.prefixWith01

fun string.prefixWith01(self): string_prefixed0x

Given "str", prefix it with 0x01 byte. Logically it makes a string "\x01str"; physically, it's 0x01 + ref "str". See string_prefixed0x.

Source code

Last updated on

On this page