vera:string

String and byte sequence operations.

import "vera:string"

Overview

All functions operate on (ptr, length) byte buffers. VERA has no dedicated string type — strings are byte sequences with explicit lengths. Null terminators are only relevant when interfacing with C-style APIs.

Dependencies: None (pure VERA)

Functions

Length


string_length

function string_length(buf: ptr) -> i64

Get the length of a null-terminated string (not counting the null byte). Scans forward from buf until a zero byte is found.

Parameters:

  • buf (ptr) — pointer to a null-terminated string

Returns: number of bytes before the null terminator


byte_find

function byte_find(buf: ptr, len: i64, needle: i8) -> i64

Find the first occurrence of a single byte in a buffer.

Parameters:

  • buf (ptr) — pointer to the buffer to search
  • len (i64) — length of the buffer
  • needle (i8) — byte value to search for

Returns: index of the first occurrence, or -1 if not found


bytes_find

function bytes_find(haystack: ptr, haystack_len: i64, needle: ptr, needle_len: i64) -> i64

Find the first occurrence of a byte subsequence in a buffer. If the needle is empty, returns 0.

Parameters:

  • haystack (ptr) — pointer to the buffer to search
  • haystack_len (i64) — length of the buffer
  • needle (ptr) — pointer to the subsequence to find
  • needle_len (i64) — length of the subsequence

Returns: starting index of the first match, or -1 if not found

Comparison


bytes_equal

function bytes_equal(a: ptr, b: ptr, len: i64) -> i32

Compare two byte sequences for equality.

Parameters:

  • a (ptr) — pointer to the first buffer
  • b (ptr) — pointer to the second buffer
  • len (i64) — number of bytes to compare

Returns: 1 if all bytes are equal, 0 otherwise


bytes_compare

function bytes_compare(a: ptr, a_len: i64, b: ptr, b_len: i64) -> i32

Lexicographic comparison of two byte sequences. Compares byte-by-byte up to the shorter length; if all compared bytes are equal, the shorter sequence is considered "less".

Parameters:

  • a (ptr) — pointer to the first buffer
  • a_len (i64) — length of the first buffer
  • b (ptr) — pointer to the second buffer
  • b_len (i64) — length of the second buffer

Returns: -1 if a < b, 0 if equal, 1 if a > b


starts_with

function starts_with(buf: ptr, buf_len: i64, prefix: ptr, prefix_len: i64) -> i32

Check if a buffer starts with a given prefix.

Parameters:

  • buf (ptr) — pointer to the buffer
  • buf_len (i64) — length of the buffer
  • prefix (ptr) — pointer to the prefix
  • prefix_len (i64) — length of the prefix

Returns: 1 if buf[0..prefix_len) equals prefix, 0 otherwise

Parsing


parse_int

function parse_int(buf: ptr, len: i64) -> i64

Parse ASCII decimal digits into an i64. Stops at the first non-digit character. Handles an optional leading sign: "-123" yields -123, "+456" yields 456.

Parameters:

  • buf (ptr) — pointer to the ASCII string
  • len (i64) — length of the string

Returns: the parsed integer value (0 for empty input)

Formatting


int_to_string

function int_to_string(value: i64, buf: ptr) -> i64

Convert an i64 to its ASCII decimal representation. Writes the digits into buf and returns the number of characters written. Handles negative numbers (writes a leading '-'). The buffer must be at least 20 bytes.

Parameters:

  • value (i64) — the integer to convert
  • buf (ptr) — output buffer (must be at least 20 bytes)

Returns: number of characters written

Case Conversion


to_lowercase

function to_lowercase(buf: ptr, len: i64)

Convert ASCII uppercase letters (A-Z) to lowercase in-place. Non-ASCII bytes and non-letter bytes are left unchanged.

Parameters:

  • buf (ptr) — pointer to the buffer to modify in-place
  • len (i64) — length of the buffer

to_uppercase

function to_uppercase(buf: ptr, len: i64)

Convert ASCII lowercase letters (a-z) to uppercase in-place. Non-ASCII bytes and non-letter bytes are left unchanged.

Parameters:

  • buf (ptr) — pointer to the buffer to modify in-place
  • len (i64) — length of the buffer