core/strings

strings

Types

6

Ascii_Set

Ascii_Set :: [8]u32Source

Ascii_Set is designed to store ASCII characters efficiently as a bit-array Each bit in the array corresponds to a specific ASCII character, where the value of the bit (0 or 1) indicates if the character is present in the set or not.

Builder

Builder :: struct { buf: [dynamic]u8, }Source

A dynamic byte buffer / string builder with helper procedures The dynamic array is wrapped inside the struct to be more opaque You can use fmt.sbprint* procedures with a ^strings.Builder directly

Intern

Intern :: struct { allocator: runtime.Allocator, entries: map[string]^Intern_Entry, }Source

Intern is a more memory efficient string map

Uses Specified Allocator for Intern_Entry strings

Fields:

  • allocator: The allocator used for the Intern_Entry strings
  • entries: A map of strings to interned string entries

Reader

Reader :: struct { s: string, i: i64, prev_rune: int, }Source

io stream data for a string reader that can read based on bytes or runes implements the vtable when using the io.Reader variants "read" calls advance the current reading offset i

Constants

1

compare

compare :: runtime.string_cmpSource

Compares two strings, returning a value representing which one comes first lexicographically. -1 for lhs; 1 for rhs, or 0 if they are equal.

  • lhs: First string for comparison
  • rhs: Second string for comparison
  • result: -1 if lhs comes first, 1 if rhs comes first, or 0 if they are equal

Procedures

171

_intern_get_entry

_intern_get_entry :: proc(m: ^Intern, text: string, loc = #caller_location) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error)Source

Internal function to lookup whether the text string exists in the map, returns the entry Sets and allocates the entry if it wasn't set yet

Allocate using the Intern's Allocator (First time string is seen only)

  • m: A pointer to the Intern struct
  • text: The string to be looked up or interned
  • loc: The caller location for debugging purposes (default: #caller_location)
  • new_entry: The interned cstring
  • err: An allocator error if one occured, nil otherwise

ascii_set_contains

ascii_set_contains :: proc(as: Ascii_Set, c: u8) -> (res: bool)Source

Determines if a given char is contained within an Ascii_Set.

  • as: The Ascii_Set to search.
  • c: The char to check for in the Ascii_Set.
  • res: A boolean indicating if the byte is contained in the Ascii_Set (true) or not (false).

ascii_set_make

ascii_set_make :: proc(chars: string) -> (as: Ascii_Set, ok: bool)Source

Creates an Ascii_Set with unique characters from the input string.

  • chars: A string containing characters to include in the Ascii_Set.
  • as: An Ascii_Set with unique characters from the input string.
  • ok: false if any character in the input string is not a valid ASCII character.

builder_from_bytes

builder_from_bytes :: proc(backing: []u8) -> (res: Builder)Source

Creates a Builder from a slice of bytes with the same slice length as its capacity. Used in fmt.bprint*

Uses Nil Allocator - Does NOT allocate

  • backing: A slice of bytes to be used as the backing buffer
  • res: The new Builder

Example:

import "core:fmt"
import "core:strings"
builder_from_bytes_example :: proc() {
	bytes: [8]byte // <-- gets filled
	builder := strings.builder_from_bytes(bytes[:])
	strings.write_byte(&builder, 'a')
	fmt.println(strings.to_string(builder)) // -> "a"
	strings.write_byte(&builder, 'b')
	fmt.println(strings.to_string(builder)) // -> "ab"
}

Output:

a
ab

builder_grow

builder_grow :: proc(b: ^Builder, cap: int)Source

Reserves the Builder byte buffer to a specific capacity, when it's higher than before

  • b: A pointer to the Builder
  • cap: The desired capacity for the Builder's buffer

builder_init_len

builder_init_len :: proc(b: ^Builder, len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)Source

Initializes a Builder with specified length and capacity len. It replaces the existing buf

Allocates Using Provided Allocator

  • b: A pointer to the Builder
  • len: The desired length of the Builder's buffer
  • allocator: (default is context.allocator)
  • res: A pointer to the initialized Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_init_len_cap

builder_init_len_cap :: proc(b: ^Builder, len: int, cap: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)Source

Initializes a Builder with specified length len and capacity cap. It replaces the existing buf

  • b: A pointer to the Builder
  • len: The desired length of the Builder's buffer
  • cap: The desired capacity of the Builder's buffer, actual max(len,cap)
  • allocator: (default is context.allocator)
  • res: A pointer to the initialized Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_init_none

builder_init_none :: proc(b: ^Builder, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)Source

Initializes an empty Builder It replaces the existing buf

Allocates Using Provided Allocator

  • b: A pointer to the Builder
  • allocator: (default is context.allocator)
  • res: A pointer to the initialized Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_make_len

builder_make_len :: proc(len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)Source

Produces a Builder with specified length and capacity len.

Allocates Using Provided Allocator

  • len: The desired length of the Builder's buffer
  • allocator: (default is context.allocator)
  • res: The new Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_make_len_cap

builder_make_len_cap :: proc(len: int, cap: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)Source

Produces a Builder with specified length len and capacity cap.

Allocates Using Provided Allocator

  • len: The desired length of the Builder's buffer
  • cap: The desired capacity of the Builder's buffer, cap is max(cap, len)
  • allocator: (default is context.allocator)
  • res: The new Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_make_none

builder_make_none :: proc(allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)Source

Produces an empty Builder

Allocates Using Provided Allocator

  • allocator: (default is context.allocator)
  • res: The new Builder
  • err: An optional allocator error if one occured, nil otherwise

builder_replace

builder_replace :: proc(b: ^Builder, old: string, new: string, n: int, loc = #caller_location) -> (replaced: int, err: runtime.Allocator_Error)Source

Replaces n instances of old in the string in a Builder b with the new string

Allocates Using The Allocator On The Builder

  • b: The input Builder
  • old: The substring to be replaced
  • new: The replacement string
  • n: The number of instances to replace (if n < 0, no limit on the number of replacements)
  • replaced: The number of replacements
  • err: if any allocation errors occurred

centre_justify

centre_justify :: proc(str: string, length: int, pad: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Centers the input string within a field of specified length by adding pad string on both sides, if its length is less than the target length.

Allocates Using Provided Allocator

  • str: The input string
  • length: The desired length of the centered string, in runes
  • pad: The string used for padding on both sides
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A new string centered within a field of the specified length
  • err: An optional allocator error if one occured, nil otherwise

clone

clone :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Clones a string

Allocates Using Provided Allocator

  • s: The string to be cloned
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The cloned string
  • err: An optional allocator error if one occured, nil otherwise

clone_from_bytes

clone_from_bytes :: proc(s: []u8, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Clones a byte array s and appends a null-byte

Allocates Using Provided Allocator

  • s: The byte array to be cloned
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The cloned string from the byte array with a null-byte
  • err: An optional allocator error if one occured, nil otherwise

clone_from_cstring

clone_from_cstring :: proc(s: cstring, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Clones a cstring s as a string

Allocates Using Provided Allocator

  • s: The cstring to be cloned
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The cloned string from the cstring
  • err: An optional allocator error if one occured, nil otherwise

clone_from_cstring_bounded

clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Clones a string from a null-terminated cstring ptr and a byte length len

Allocates Using Provided Allocator

  • ptr: A pointer to the start of the null-terminated cstring
  • len: The byte length of the cstring
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)

NOTE: Truncates at the first null-byte encountered or the byte length.

  • res: The cloned string from the null-terminated cstring and byte length
  • err: An optional allocator error if one occured, nil otherwise

clone_from_ptr

clone_from_ptr :: proc(ptr: ^u8, len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Clones a string from a byte pointer ptr and a byte length len

Allocates Using Provided Allocator

  • ptr: A pointer to the start of the byte sequence
  • len: The length of the byte sequence
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)

NOTE: Same as string_from_ptr, but perform an additional clone operation

  • res: The cloned string from the byte pointer and length
  • err: An optional allocator error if one occured, nil otherwise

clone_to_cstring

clone_to_cstring :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: cstring, err: runtime.Allocator_Error)Source

Clones a string and appends a null-byte to make it a cstring

Allocates Using Provided Allocator

  • s: The string to be cloned
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A cloned cstring with an appended null-byte
  • err: An optional allocator error if one occured, nil otherwise

common_prefix

common_prefix :: proc(a: string, b: string) -> (string)Source

Returns the common prefix between strings a and b

  • a: The first input string
  • b: The second input string
  • n: The string prefix common between strings a and b

Example:

import "core:fmt"
import "core:strings"

common_prefix_example :: proc() {
	fmt.println(strings.common_prefix("testing", "test"))
	fmt.println(strings.common_prefix("testing", "te"))
	fmt.println(strings.common_prefix("telephone", "te"))
}

Output:

test
te
te

concatenate

concatenate :: proc(a: []string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Returns a combined string from the slice of strings a without a separator

Allocates Using Provided Allocator

  • a: A slice of strings to concatenate
  • allocator: (default is context.allocator)
  • res: The concatenated string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

concatenate_example :: proc() {
	a := [?]string { "a", "b", "c" }
	fmt.println(strings.concatenate(a[:]))
}

Output:

abc

contains

contains :: proc(s: string, substr: string) -> (res: bool)Source

Returns true when the string substr is contained inside the string s

  • s: The input string
  • substr: The substring to search for
  • res: true if substr is contained inside the string s, false otherwise

Example:

import "core:fmt"
import "core:strings"

contains_example :: proc() {
	fmt.println(strings.contains("testing", "test"))
	fmt.println(strings.contains("testing", "ing"))
	fmt.println(strings.contains("testing", "text"))
}

Output:

true
true
false

contains_any

contains_any :: proc(s: string, chars: string) -> (res: bool)Source

Returns true when the string s contains any of the characters inside the string chars

  • s: The input string
  • chars: The characters to search for
  • res: true if the string s contains any of the characters in chars, false otherwise

Example:

import "core:fmt"
import "core:strings"

contains_any_example :: proc() {
	fmt.println(strings.contains_any("test", "test"))
	fmt.println(strings.contains_any("test", "ts"))
	fmt.println(strings.contains_any("test", "et"))
	fmt.println(strings.contains_any("test", "a"))
}

Output:

true
true
true
false

count

count :: proc(s: string, substr: string) -> (res: int)Source

Counts the number of non-overlapping occurrences of substr in s

  • s: The string to search in
  • substr: The substring to count
  • res: The number of occurrences of substr in s, returns the rune_count + 1 of the string s on empty substr

Example:

import "core:fmt"
import "core:strings"

count_example :: proc() {
	fmt.println(strings.count("abbccc", "a"))
	fmt.println(strings.count("abbccc", "b"))
	fmt.println(strings.count("abbccc", "c"))
	fmt.println(strings.count("abbccc", "ab"))
	fmt.println(strings.count("abbccc", " "))
}

Output:

1
2
3
1
0

cut

cut :: proc(s: string, rune_offset: int = int(0), rune_length: int = int(0)) -> (res: string)Source

Returns a substring of the input string s with the specified rune offset and length

  • s: The input string to cut
  • rune_offset: The starting rune index (default is 0). In runes, not bytes.
  • rune_length: The number of runes to include in the substring (default is 0, which returns the remainder of the string). In runes, not bytes.
  • res: The substring

Example:

import "core:fmt"
import "core:strings"

cut_example :: proc() {
	fmt.println(strings.cut("some example text", 0, 4)) // -> "some"
	fmt.println(strings.cut("some example text", 2, 2)) // -> "me"
	fmt.println(strings.cut("some example text", 5, 7)) // -> "example"
}

Output:

some
me
example

cut_clone

cut_clone :: proc(s: string, rune_offset: int = int(0), rune_length: int = int(0), allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Returns a substring of the input string s with the specified rune offset and length

Allocates Using Provided Allocator

  • s: The input string to cut
  • rune_offset: The starting rune index (default is 0). In runes, not bytes.
  • rune_length: The number of runes to include in the substring (default is 0, which returns the remainder of the string). In runes, not bytes.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The substring
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

cut_clone_example :: proc() {
	fmt.println(strings.cut_clone("some example text", 0, 4)) // -> "some"
	fmt.println(strings.cut_clone("some example text", 2, 2)) // -> "me"
	fmt.println(strings.cut_clone("some example text", 5, 7)) // -> "example"
}

Output:

some
me
example

equal_fold

equal_fold :: proc(u: string, v: string) -> (res: bool)Source

Returns whether the strings u and v are the same alpha characters, ignoring different casings Works with UTF-8 string content

  • u: The first string for comparison
  • v: The second string for comparison
  • res: true if the strings u and v are the same alpha characters (ignoring case)

Example:

import "core:fmt"
import "core:strings"

equal_fold_example :: proc() {
	fmt.println(strings.equal_fold("test", "test"))
	fmt.println(strings.equal_fold("Test", "test"))
	fmt.println(strings.equal_fold("Test", "tEsT"))
	fmt.println(strings.equal_fold("test", "tes"))
}

Output:

true
true
true
false

expand_tabs

expand_tabs :: proc(s: string, tab_size: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Expands the input string by replacing tab characters with spaces to align to a specified tab size

Allocates Using Provided Allocator

  • s: The input string
  • tab_size: The number of spaces to use for each tab character
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A new string with tab characters expanded to the specified tab size
  • err: An optional allocator error if one occured, nil otherwise

WARNING: Panics if tab_size <= 0

Example:

import "core:fmt"
import "core:strings"

expand_tabs_example :: proc() {
	text := "abc1\tabc2\tabc3"
	fmt.println(strings.expand_tabs(text, 4))
}

Output:

abc1    abc2    abc3

fields

fields :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into a slice of substrings at each instance of one or more consecutive white space characters, as defined by unicode.is_space

Allocates Using Provided Allocator

  • s: The input string
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A slice of substrings of the input string, or an empty slice if the input string only contains white space
  • err: An optional allocator error if one occured, nil otherwise

fields_iterator

fields_iterator :: proc(s: ^string) -> (field: string, ok: bool)Source

Retrieves the first non-space substring from a mutable string reference and advances the reference. s is advanced from any space after the substring, or be an empty string if the substring was the remaining characters

  • s: A mutable string reference to be iterated
  • field: The first non-space substring found
  • ok: A boolean indicating if a non-space substring was found

fields_proc

fields_proc :: proc(s: string, f: proc() -> (bool), allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into a slice of substrings at each run of unicode code points r satisfying the predicate f(r)

Allocates Using Provided Allocator

  • s: The input string
  • f: A predicate function to determine the split points
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)

NOTE: fields_proc makes no guarantee about the order in which it calls f(r), it assumes that f always returns the same value for a given r

  • res: A slice of substrings of the input string, or an empty slice if all code points in the input string satisfy the predicate or if the input string is empty
  • err: An optional allocator error if one occured, nil otherwise

has_prefix

has_prefix :: proc(s: string, prefix: string) -> (result: bool)Source

Determines if a string s starts with a given prefix

  • s: The string to check for the prefix
  • prefix: The prefix to look for
  • result: true if the string s starts with the prefix, otherwise false

Example:

import "core:fmt"
import "core:strings"

has_prefix_example :: proc() {
	fmt.println(strings.has_prefix("testing", "test"))
	fmt.println(strings.has_prefix("testing", "te"))
	fmt.println(strings.has_prefix("telephone", "te"))
	fmt.println(strings.has_prefix("testing", "est"))
}

Output:

true
true
true
false

has_suffix

has_suffix :: proc(s: string, suffix: string) -> (result: bool)Source

Determines if a string s ends with a given suffix

  • s: The string to check for the suffix
  • suffix: The suffix to look for
  • result: true if the string s ends with the suffix, otherwise false

Example:

import "core:fmt"
import "core:strings"

has_suffix_example :: proc() {
	fmt.println(strings.has_suffix("todo.txt", ".txt"))
	fmt.println(strings.has_suffix("todo.doc", ".txt"))
	fmt.println(strings.has_suffix("todo.doc.txt", ".txt"))
}

Output:

true
false
true

index

index :: proc(s: string, substr: string) -> (res: int)Source

Returns the byte offset of the string substr in the string s, -1 when not found.

  • s: The input string to search in.
  • substr: The substring to search for.
  • res: The byte offset of the first occurrence of substr in s, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

index_example :: proc() {
	fmt.println(strings.index("test", "t"))
	fmt.println(strings.index("test", "te"))
	fmt.println(strings.index("test", "st"))
	fmt.println(strings.index("test", "tt"))
}

Output:

0
0
2
-1

index_any

index_any :: proc(s: string, chars: string) -> (res: int)Source

Returns the index of any first char of chars found in s, -1 if not found.

  • s: The input string to search in.
  • chars: The characters to look for
  • res: The index of the first character of chars found in s, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

index_any_example :: proc() {
	fmt.println(strings.index_any("test", "s"))
	fmt.println(strings.index_any("test", "se"))
	fmt.println(strings.index_any("test", "et"))
	fmt.println(strings.index_any("test", "set"))
	fmt.println(strings.index_any("test", "x"))
}

Output:

2
1
0
0
-1

index_byte

index_byte :: proc(s: string, c: u8) -> (res: int)Source

Returns the byte offset of the first byte c in the string s it finds, -1 when not found. NOTE: Can't find UTF-8 based runes.

  • s: The input string to search in.
  • c: The byte to search for.
  • res: The byte offset of the first occurrence of c in s, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

index_byte_example :: proc() {
	fmt.println(strings.index_byte("test", 't'))
	fmt.println(strings.index_byte("test", 'e'))
	fmt.println(strings.index_byte("test", 'x'))
	fmt.println(strings.index_byte("teäst", 'ä'))
}

Output:

0
1
-1
-1

index_multi

index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int)Source

Finds the first occurrence of any substring in substrs within s

  • s: The string to search in
  • substrs: The substrings to look for
  • idx: the index of the first matching substring
  • width: the length of the found substring

index_proc

index_proc :: proc(s: string, p: proc() -> (bool), truth: untyped boolean = true) -> (res: int)Source

Find the index of the first rune r in string s for which procedure p returns the same as truth, or -1 if no such rune appears.

  • s: The input string
  • p: A procedure that takes a rune and returns a boolean
  • truth: The boolean value to be matched (default: true)
  • res: The index of the first matching rune, or -1 if no match was found

Example:

import "core:fmt"
import "core:strings"

index_proc_example :: proc() {
	call :: proc(r: rune) -> bool {
		return r == 'a'
	}
	fmt.println(strings.index_proc("abcabc", call))
	fmt.println(strings.index_proc("cbacba", call))
	fmt.println(strings.index_proc("cbacba", call, false))
	fmt.println(strings.index_proc("abcabc", call, false))
	fmt.println(strings.index_proc("xyz", call))
}

Output:

0
2
0
1
-1

index_rune

index_rune :: proc(s: string, r: rune) -> (res: int)Source

Returns the byte offset of the first rune r in the string s it finds, -1 when not found. Invalid runes return -1

  • s: The input string to search in.
  • r: The rune to search for.
  • res: The byte offset of the first occurrence of r in s, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

index_rune_example :: proc() {
	fmt.println(strings.index_rune("abcädef", 'x'))
	fmt.println(strings.index_rune("abcädef", 'a'))
	fmt.println(strings.index_rune("abcädef", 'b'))
	fmt.println(strings.index_rune("abcädef", 'c'))
	fmt.println(strings.index_rune("abcädef", 'ä'))
	fmt.println(strings.index_rune("abcädef", 'd'))
	fmt.println(strings.index_rune("abcädef", 'e'))
	fmt.println(strings.index_rune("abcädef", 'f'))
}

Output:

-1
0
1
2
3
5
6
7

intern_get

intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error)Source

Returns an interned copy of the given text, adding it to the map if not already present.

Allocate using the Intern's Allocator (First time string is seen only)

  • m: A pointer to the Intern struct
  • text: The string to be interned

NOTE: The returned string lives as long as the map entry lives.

  • str: The interned string
  • err: An allocator error if one occured, nil otherwise

intern_get_cstring

intern_get_cstring :: proc(m: ^Intern, text: string, loc = #caller_location) -> (str: cstring, err: runtime.Allocator_Error)Source

Returns an interned copy of the given text as a cstring, adding it to the map if not already present.

Allocate using the Intern's Allocator (First time string is seen only)

  • m: A pointer to the Intern struct
  • text: The string to be interned
  • loc: The caller location for debugging purposes (default: #caller_location)

NOTE: The returned cstring lives as long as the map entry lives

  • str: The interned cstring
  • err: An allocator error if one occured, nil otherwise

intern_init

intern_init :: proc(m: ^Intern, allocator: mem.Allocator = context.allocator, map_allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (err: runtime.Allocator_Error)Source

Initializes the entries map and sets the allocator for the string entries

Allocates Using Provided Allocators

  • m: A pointer to the Intern struct to be initialized
  • allocator: The allocator for the Intern_Entry strings (Default: context.allocator)
  • map_allocator: The allocator for the map of entries (Default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • err: An allocator error if one occured, nil otherwise

is_ascii_space

is_ascii_space :: proc(r: rune) -> (res: bool)Source

Returns true when the r rune is an ASCII whitespace character.

  • r: the rune to test

is_delimiter

is_delimiter :: proc(r: rune) -> (res: bool)Source

Checks if the rune r is a delimiter (' ', '-', or '_').

  • r: Rune to check for delimiter status.
  • res: True if r is a delimiter, false otherwise.

is_null

is_null :: proc(r: rune) -> (res: bool)Source

Returns true when the r rune is 0x0

  • r: the rune to test

is_separator

is_separator :: proc(r: rune) -> (res: bool)Source

Checks if the rune r is a non-alphanumeric or space character.

  • r: Rune to check for separator status.
  • res: True if r is a non-alpha or unicode.is_space rune.

is_space

is_space :: proc(r: rune) -> (res: bool)Source

Returns true when the r rune is an ASCII or UTF-8 whitespace character.

  • r: the rune to test

join

join :: proc(a: []string, sep: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Joins a slice of strings a with a sep string

Allocates Using Provided Allocator

  • a: A slice of strings to join
  • sep: The separator string
  • allocator: (default is context.allocator)
  • res: A combined string from the slice of strings a separated with the sep string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

join_example :: proc() {
	a := [?]string { "a", "b", "c" }
	fmt.println(strings.join(a[:], " "))
	fmt.println(strings.join(a[:], "-"))
	fmt.println(strings.join(a[:], "..."))
}

Output:

a b c
a-b-c
a...b...c

last_index

last_index :: proc(s: string, substr: string) -> (res: int)Source

Returns the last byte offset of the string substr in the string s, -1 when not found.

  • s: The input string to search in.
  • substr: The substring to search for.
  • res: The byte offset of the last occurrence of substr in s, or -1 if not found.

Example:

import "core:fmt"
import "core:strings"

last_index_example :: proc() {
	fmt.println(strings.last_index("test", "t"))
	fmt.println(strings.last_index("test", "te"))
	fmt.println(strings.last_index("test", "st"))
	fmt.println(strings.last_index("test", "tt"))
}

Output:

3
0
2
-1

last_index_any

last_index_any :: proc(s: string, chars: string) -> (res: int)Source

Finds the last occurrence of any character in chars within s. Iterates in reverse.

  • s: The string to search in
  • chars: The characters to look for
  • res: The index of the last matching character, or -1 if not found

Example:

import "core:fmt"
import "core:strings"

last_index_any_example :: proc() {
	fmt.println(strings.last_index_any("test", "s"))
	fmt.println(strings.last_index_any("test", "se"))
	fmt.println(strings.last_index_any("test", "et"))
	fmt.println(strings.last_index_any("test", "set"))
	fmt.println(strings.last_index_any("test", "x"))
}

Output:

2
2
3
3
-1

last_index_byte

last_index_byte :: proc(s: string, c: u8) -> (res: int)Source

Returns the byte offset of the last byte c in the string s, -1 when not found.

  • s: The input string to search in.
  • c: The byte to search for.
  • res: The byte offset of the last occurrence of c in s, or -1 if not found.

NOTE: Can't find UTF-8 based runes.

Example:

import "core:fmt"
import "core:strings"

last_index_byte_example :: proc() {
	fmt.println(strings.last_index_byte("test", 't'))
	fmt.println(strings.last_index_byte("test", 'e'))
	fmt.println(strings.last_index_byte("test", 'x'))
	fmt.println(strings.last_index_byte("teäst", 'ä'))
}

Output:

3
1
-1
-1

left_justify

left_justify :: proc(str: string, length: int, pad: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Left-justifies the input string within a field of specified length by adding pad string on the right side, if its length is less than the target length.

Allocates Using Provided Allocator

  • str: The input string
  • length: The desired length of the left-justified string
  • pad: The string used for padding on the right side
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A new string left-justified within a field of the specified length
  • err: An optional allocator error if one occured, nil otherwise

levenshtein_distance

levenshtein_distance :: proc(a: string, b: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: int, err: runtime.Allocator_Error)Source

Computes the Levenshtein edit distance between two strings

Allocates Using Provided Allocator (deletion occurs internal to proc)

NOTE: Does not perform internal allocation if length of string b, in runes, is smaller than 64

  • a, b: The two strings to compare
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The Levenshtein edit distance between the two strings
  • err: An optional allocator error if one occured, nil otherwise

NOTE: This implementation is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl.

partition

partition :: proc(str: string, sep: string) -> (head: string, match: string, tail: string)Source

Splits the input string str by the separator sep string and returns 3 parts. The values are slices of the original string.

  • str: The input string
  • sep: The separator string
  • head: the string before the split
  • match: the seperator string
  • tail: the string after the split

Example:

import "core:fmt"
import "core:strings"

partition_example :: proc() {
	text := "testing this out"
	head, match, tail := strings.partition(text, " this ") // -> head: "testing", match: " this ", tail: "out"
	fmt.println(head, match, tail)
	head, match, tail = strings.partition(text, "hi") // -> head: "testing t", match: "hi", tail: "s out"
	fmt.println(head, match, tail)
	head, match, tail = strings.partition(text, "xyz")    // -> head: "testing this out", match: "", tail: ""
	fmt.println(head)
	fmt.println(match == "")
	fmt.println(tail == "")
}

Output:

testing  this  out
testing t hi s out
testing this out
true
true

pop_byte

pop_byte :: proc(b: ^Builder) -> (r: u8)Source

Pops and returns the last byte in the Builder or 0 when the Builder is empty

  • b: A pointer to the Builder
  • r: The last byte in the Builder or 0 if empty

pop_rune

pop_rune :: proc(b: ^Builder) -> (r: rune, width: int)Source

Pops the last rune in the Builder and returns the popped rune and its rune width or (0, 0) if empty

  • b: A pointer to the Builder
  • r: The popped rune
  • width: The rune width or 0 if the builder was empty

prefix_length

prefix_length :: proc(a: string, b: string) -> (n: int)Source

Returns the prefix length common between strings a and b

  • a: The first input string
  • b: The second input string
  • n: The prefix length common between strings a and b

Example:

import "core:fmt"
import "core:strings"

prefix_length_example :: proc() {
	fmt.println(strings.prefix_length("testing", "test"))
	fmt.println(strings.prefix_length("testing", "te"))
	fmt.println(strings.prefix_length("telephone", "te"))
	fmt.println(strings.prefix_length("testing", "est"))
}

Output:

4
2
2
0

reader_read

reader_read :: proc(r: ^Reader, p: []u8) -> (n: int, err: io.Error)Source

Reads len(p) bytes from the Reader's string and copies into the provided slice.

  • r: A pointer to a Reader struct
  • p: A byte slice to copy data into
  • n: The number of bytes read
  • err: An io.Error if an error occurs while reading, including .EOF, otherwise nil denotes success.

reader_read_at

reader_read_at :: proc(r: ^Reader, p: []u8, off: i64) -> (n: int, err: io.Error)Source

Reads len(p) bytes from the Reader's string and copies into the provided slice, at the specified offset from the current index.

  • r: A pointer to a Reader struct
  • p: A byte slice to copy data into
  • off: The offset from which to read
  • n: The number of bytes read
  • err: An io.Error if an error occurs while reading, including .EOF, otherwise nil denotes success.

reader_read_byte

reader_read_byte :: proc(r: ^Reader) -> (res: u8, err: io.Error)Source

Reads and returns a single byte from the Reader's string

  • r: A pointer to a Reader struct
  • The byte read from the Reader
  • err: An io.Error if an error occurs while reading, including .EOF, otherwise nil denotes success.

reader_read_rune

reader_read_rune :: proc(r: ^Reader) -> (rr: rune, size: int, err: io.Error)Source

Reads and returns a single rune and its size from the Reader's string

  • r: A pointer to a Reader struct
  • rr: The rune read from the Reader
  • size: The size of the rune in bytes
  • err: An io.Error if an error occurs while reading

reader_seek

reader_seek :: proc(r: ^Reader, offset: i64, whence: io.Seek_From) -> (res: i64, err: io.Error)Source

Seeks the Reader's index to a new position

  • r: A pointer to a Reader struct
  • offset: The new offset position
  • whence: The reference point for the new position (.Start, .Current, or .End)
  • The absolute offset after seeking
  • err: An io.Error if an error occurs while seeking (.Invalid_Whence, .Invalid_Offset)

reader_size

reader_size :: proc(r: ^Reader) -> (res: i64)Source

Returns the length of the string stored in the Reader

  • r: A pointer to a Reader struct
  • res: The length of the string stored in the Reader

reader_unread_rune

reader_unread_rune :: proc(r: ^Reader) -> (err: io.Error)Source

Decrements the Reader's index (i) by the size of the last read rune

  • r: A pointer to a Reader struct

WARNING: May only be used once and after a valid read_rune call

  • err: An io.Error if an error occurs while unreading (.Invalid_Unread), else nil denotes success.

reader_write_to

reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error)Source

Writes the remaining content of the Reader's string into the provided io.Writer

  • r: A pointer to a Reader struct
  • w: The io.Writer to write the remaining content into

WARNING: Panics if writer writes more bytes than remainig length of string.

  • n: The number of bytes written
  • err: An io.Error if an error occurs while writing (.Short_Write)

remove

remove :: proc(s: string, key: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)Source

Removes the key string n times from the s string

Allocates Using Provided Allocator

  • s: The input string
  • key: The substring to be removed
  • n: The number of instances to remove (if n < 0, no limit on the number of removes)
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • output: The modified string
  • was_allocation: true if an allocation occurred during the replacement, false otherwise

Example:

import "core:fmt"
import "core:strings"

remove_example :: proc() {
	fmt.println(strings.remove("abcabc", "abc", 1))
	fmt.println(strings.remove("abcabc", "abc", -1))
	fmt.println(strings.remove("abcabc", "a", -1))
	fmt.println(strings.remove("abcabc", "x", -1))
}

Output:

abc true
 true
bcbc true
abcabc false

remove_all

remove_all :: proc(s: string, key: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)Source

Removes all the key string instances from the s string

Allocates Using Provided Allocator

  • s: The input string
  • key: The substring to be removed
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • output: The modified string
  • was_allocation: true if an allocation occurred during the replacement, false otherwise

Example:

import "core:fmt"
import "core:strings"

remove_all_example :: proc() {
	fmt.println(strings.remove_all("abcabc", "abc"))
	fmt.println(strings.remove_all("abcabc", "a"))
	fmt.println(strings.remove_all("abcabc", "x"))
}

Output:

 true
bcbc true
abcabc false

repeat

repeat :: proc(s: string, count: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Repeats the string s count times, concatenating the result

Allocates Using Provided Allocator

  • s: The string to repeat
  • count: The number of times to repeat s
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The concatenated repeated string
  • err: An optional allocator error if one occured, nil otherwise

WARNING: Panics if count < 0

Example:

import "core:fmt"
import "core:strings"

repeat_example :: proc() {
	fmt.println(strings.repeat("abc", 2))
}

Output:

abcabc

replace

replace :: proc( s: string, old: string, new: string, n: int, allocator: mem.Allocator = context.allocator, loc: _ = #caller_location, ) -> (output: string, was_allocation: bool)Source

Replaces n instances of old in the string s with the new string

Allocates Using Provided Allocator

  • s: The input string
  • old: The substring to be replaced
  • new: The replacement string
  • n: The number of instances to replace (if n < 0, no limit on the number of replacements)
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • output: The modified string
  • was_allocation: true if an allocation occurred during the replacement, false otherwise

Example:

import "core:fmt"
import "core:strings"

replace_example :: proc() {
	fmt.println(strings.replace("xyzxyz", "xyz", "abc", 2))
	fmt.println(strings.replace("xyzxyz", "xyz", "abc", 1))
	fmt.println(strings.replace("xyzxyz", "abc", "xyz", -1))
	fmt.println(strings.replace("xyzxyz", "xy", "z", -1))
}

Output:

abcabc true
abcxyz true
xyzxyz false
zzzz true

replace_all

replace_all :: proc(s: string, old: string, new: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)Source

Replaces all occurrences of old in s with new

Allocates Using Provided Allocator

  • s: The string to modify
  • old: The substring to replace
  • new: The substring to replace old with
  • allocator: The allocator to use for the new string (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • output: The modified string
  • was_allocation: true if an allocation occurred during the replacement, false otherwise

Example:

import "core:fmt"
import "core:strings"

replace_all_example :: proc() {
	fmt.println(strings.replace_all("xyzxyz", "xyz", "abc"))
	fmt.println(strings.replace_all("xyzxyz", "abc", "xyz"))
	fmt.println(strings.replace_all("xyzxyz", "xy", "z"))
}

Output:

abcabc true
xyzxyz false
zzzz true

reverse

reverse :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Reverses the input string s

Allocates Using Provided Allocator

  • s: The input string
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A reversed version of the input string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

reverse_example :: proc() {
	a := "abcxyz"
	b := strings.reverse(a)
	fmt.println(a, b)
}

Output:

abcxyz zyxcba

right_justify

right_justify :: proc(str: string, length: int, pad: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Right-justifies the input string within a field of specified length by adding pad string on the left side, if its length is less than the target length.

Allocates Using Provided Allocator

  • str: The input string
  • length: The desired length of the right-justified string
  • pad: The string used for padding on the left side
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A new string right-justified within a field of the specified length
  • err: An optional allocator error if one occured, nil otherwise

rune_count

rune_count :: proc(s: string) -> (res: int)Source

Returns the UTF-8 rune count of the string s

  • s: The input string
  • res: The UTF-8 rune count of the string s

Example:

import "core:fmt"
import "core:strings"

rune_count_example :: proc() {
	fmt.println(strings.rune_count("test"))
	fmt.println(strings.rune_count("testö")) // where len("testö") == 6
}

Output:

4
5

scrub

scrub :: proc(s: string, replacement: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)Source

Replaces invalid UTF-8 characters in the input string with a specified replacement string. Adjacent invalid bytes are only replaced once.

Allocates Using Provided Allocator

  • s: The input string
  • replacement: The string used to replace invalid UTF-8 characters
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: A new string with invalid UTF-8 characters replaced
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

scrub_example :: proc() {
	text := "Hello\xC0\x80World"
	fmt.println(strings.scrub(text, "?")) // -> "Hello?World"
}

Output:

Hello?

split

split :: proc(s: string, sep: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into parts based on a separator.

Allocates Using Provided Allocator

  • s: The string to split.
  • sep: The separator string used to split the input string.
  • allocator: (default is context.allocator).
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice of strings, each representing a part of the split string.
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_example :: proc() {
	s := "aaa.bbb.ccc.ddd.eee"    // 5 parts
	ss := strings.split(s, ".")
	fmt.println(ss)
}

Output:

["aaa", "bbb", "ccc", "ddd", "eee"]

split_after

split_after :: proc(s: string, sep: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into parts after the separator, retaining it in the substrings.

Allocates Using Provided Allocator

  • s: The string to split.
  • sep: The separator string used to split the input string.
  • allocator: (default is context.allocator).
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice of strings, each representing a part of the split string after the separator
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_after_example :: proc() {
	a := "aaa.bbb.ccc.ddd.eee"         // 5 parts
	aa := strings.split_after(a, ".")
	fmt.println(aa)
}

Output:

["aaa.", "bbb.", "ccc.", "ddd.", "eee"]

split_after_iterator

split_after_iterator :: proc(s: ^string, sep: string) -> (res: string, ok: bool)Source

Splits the input string after every separator string in an iterator fashion.

  • s: Pointer to the input string, which is modified during the search.
  • sep: The separator string to search for.
  • res: The resulting substring
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_after_iterator_example :: proc() {
	text := "a.b.c.d.e"
	for str in strings.split_after_iterator(&text, ".") {
		fmt.println(str)
	}
}

Output:

a.
b.
c.
d.
e

split_after_n

split_after_n :: proc(s: string, sep: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into a total of n parts after the separator.

Allocates Using Provided Allocator

  • s: The string to split.
  • sep: The separator string used to split the input string.
  • n: The maximum number of parts to split the string into.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice of strings with n parts or fewer if there weren't
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_after_n_example :: proc() {
	a := "aaa.bbb.ccc.ddd.eee"
	aa := strings.split_after_n(a, ".", 3)
	fmt.println(aa)
}

Output:

["aaa.", "bbb.", "ccc.ddd.eee"]

split_by_byte_iterator

split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool)Source

Splits the input string by the byte separator in an iterator fashion.

  • s: Pointer to the input string, which is modified during the search.
  • sep: The byte separator to search for.
  • res: The resulting substring
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_by_byte_iterator_example :: proc() {
	text := "a.b.c.d.e"
	for str in strings.split_by_byte_iterator(&text, '.') {
		fmt.println(str) // every loop -> a b c d e
	}
}

Output:

a
b
c
d
e

split_iterator

split_iterator :: proc(s: ^string, sep: string) -> (res: string, ok: bool)Source

Splits the input string by the separator string in an iterator fashion.

  • s: Pointer to the input string, which is modified during the search.
  • sep: The separator string to search for.
  • res: The resulting substring
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_iterator_example :: proc() {
	text := "a.b.c.d.e"
	for str in strings.split_iterator(&text, ".") {
		fmt.println(str)
	}
}

Output:

a
b
c
d
e

split_lines

split_lines :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits the input string at every line break \n.

Allocates Using Provided Allocator

  • s: The input string to split.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice (allocated) of the split string (slices into original string)
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

split_lines_example :: proc() {
	a := "a\nb\nc\nd\ne"
	b := strings.split_lines(a)
	fmt.println(b)
}

Output:

["a", "b", "c", "d", "e"]

split_lines_after

split_lines_after :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits the input string at every line break \n leaving the \n in the resulting strings.

Allocates Using Provided Allocator

  • s: The input string to split.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice (allocated) of the split string (slices into original string), with \n included
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_lines_after_example :: proc() {
	a := "a\nb\nc\nd\ne"
	b := strings.split_lines_after(a)
	fmt.println(b)
}

Output:

["a\n", "b\n", "c\n", "d\n", "e"]

split_lines_after_iterator

split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool)Source

Splits the input string at every line break \n. Returns the current split string with line breaks included every iteration until the string is consumed.

  • s: Pointer to the input string, which is modified during the search.
  • line: The resulting substring with line breaks included
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_lines_after_iterator_example :: proc() {
	text := "a\nb\nc\nd\ne\n"
	for str in strings.split_lines_after_iterator(&text) {
		fmt.print(str) // every loop -> a\n b\n c\n d\n e\n
	}
}

Output:

a
b
c
d
e

split_lines_after_n

split_lines_after_n :: proc(s: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits the input string at every line break \n leaving the \n in the resulting strings. Only runs for n parts.

Allocates Using Provided Allocator

  • s: The input string to split.
  • n: The number of parts to split into.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice (allocated) of the split string (slices into original string), with \n included
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_lines_after_n_example :: proc() {
	a := "a\nb\nc\nd\ne"
	b := strings.split_lines_after_n(a, 3)
	fmt.println(b)
}

Output:

["a\n", "b\n", "c\nd\ne"]

split_lines_iterator

split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool)Source

Splits the input string at every line break \n. Returns the current split string every iteration until the string is consumed.

  • s: Pointer to the input string, which is modified during the search.
  • line: The resulting substring
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_lines_iterator_example :: proc() {
	text := "a\nb\nc\nd\ne"
	for str in strings.split_lines_iterator(&text) {
		fmt.print(str)    // every loop -> a b c d e
	}
	fmt.print("\n")
}

Output:

abcde

split_lines_n

split_lines_n :: proc(s: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits the input string at every line break \n for n parts.

Allocates Using Provided Allocator

  • s: The input string to split.
  • n: The number of parts to split into.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice (allocated) of the split string (slices into original string)
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_lines_n_example :: proc() {
	a := "a\nb\nc\nd\ne"
	b := strings.split_lines_n(a, 3)
	fmt.println(b)
}

Output:

["a", "b", "c\nd\ne"]

split_multi

split_multi :: proc(s: string, substrs: []string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits the input string s by all possible substrs and returns an allocated array of strings

Allocates Using Provided Allocator

  • s: The input string
  • substrs: An array of substrings used for splitting
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: An array of strings, or nil on empty substring or no matches
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_multi_example :: proc() {
	splits := [?]string { "---", "~~~", ".", "_", "," }
	res := strings.split_multi("testing,this.out_nice---done~~~last", splits[:])
	fmt.println(res) // -> [testing, this, out, nice, done, last]
}

Output:

["testing", "this", "out", "nice", "done", "last"]

split_multi_iterate

split_multi_iterate :: proc(it: ^string, substrs: []string) -> (res: string, ok: bool)Source

Splits the input string s by all possible substrs in an iterator fashion. The full string is returned if no match.

  • it: A pointer to the input string
  • substrs: An array of substrings used for splitting
  • res: The split string
  • ok: true if an iteration result was returned, false if the iterator has reached the end

Example:

import "core:fmt"
import "core:strings"

split_multi_iterate_example :: proc() {
	it := "testing,this.out_nice---done~~~last"
	splits := [?]string { "---", "~~~", ".", "_", "," }
	for str in strings.split_multi_iterate(&it, splits[:]) {
		fmt.println(str)
	}
}

Output:

testing
this
out
nice
done
last

split_n

split_n :: proc(s: string, sep: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)Source

Splits a string into parts based on a separator. If n < count of seperators, the remainder of the string is returned in the last entry.

Allocates Using Provided Allocator

  • s: The string to split.
  • sep: The separator string used to split the input string.
  • n: The maximum amount of parts to split the string into.
  • allocator: (default is context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • res: The slice of strings, each representing a part of the split string.
  • err: An optional allocator error if one occured, nil otherwise

NOTE: Allocation occurs for the array, the splits are all views of the original string.

Example:

import "core:fmt"
import "core:strings"

split_n_example :: proc() {
	s := "aaa.bbb.ccc.ddd.eee"  // 5 parts present
	ss := strings.split_n(s, ".",3) // total of 3 wanted
	fmt.println(ss)
}

Output:

["aaa", "bbb", "ccc.ddd.eee"]

string_case_iterator

string_case_iterator :: proc(w: io.Writer, s: string, callback: proc(w: io.Writer, prev: rune, curr: rune, next: rune))Source

Iterates over a string, calling a callback for each rune with the previous, current, and next runes as arguments.

  • w: An io.Writer to be used by the callback for writing output.
  • s: The input string to be iterated over.
  • callback: A procedure to be called for each rune in the string, with arguments (w: io.Writer, prev, curr, next: rune).

The callback can utilize the provided io.Writer to write output during the iteration.

Example:

import "core:fmt"
import "core:strings"
import "core:io"

string_case_iterator_example :: proc() {
	my_callback :: proc(w: io.Writer, prev, curr, next: rune) {
		fmt.println("my_callback", curr) // <-- Custom logic here
	}
	s := "hello"
	b: strings.Builder
	strings.builder_init_len(&b, len(s))
	w := strings.to_writer(&b)
	strings.string_case_iterator(w, s, my_callback)
}

Output:

my_callback h
my_callback e
my_callback l
my_callback l
my_callback o

string_from_null_terminated_ptr

string_from_null_terminated_ptr :: proc(ptr: [^]u8, len: int) -> (res: string)Source

Transmutes a raw pointer (null-terminated) into a string. Non-allocating. Searches for a null-byte from 0..<len, otherwise len will be the end size

NOTE: The created string is only valid as long as the pointer and length are valid.

  The string is truncated at the first null-byte encountered.

Inputs:
- ptr: A pointer to the start of the null-terminated byte sequence
- len: The length of the byte sequence

Returns:
- res: A string created from the null-terminated byte pointer and length

string_from_ptr

string_from_ptr :: proc(ptr: ^u8, len: int) -> (res: string)Source

Transmutes a raw pointer into a string. Non-allocating.

  • ptr: A pointer to the start of the byte sequence
  • len: The length of the byte sequence

NOTE: The created string is only valid as long as the pointer and length are valid.

  • res: A string created from the byte pointer and length

substring

substring :: proc(s: string, rune_start: int, rune_end: int) -> (sub: string, ok: bool)Source

Returns a substring of s that starts at rune index rune_start and goes up to rune_end.

Think of it as slicing s[rune_start:rune_end] but rune-wise.

  • s: the string to substring
  • rune_start: the start (inclusive) rune
  • rune_end: the end (exclusive) rune
  • sub: the substring
  • ok: whether the rune indexes where in bounds of the original string

substring_from

substring_from :: proc(s: string, rune_start: int) -> (sub: string, ok: bool)Source

Returns a substring of s that starts at rune index rune_start and goes up to the end of the string.

Think of it as slicing s[rune_start:] but rune-wise.

  • s: the string to substring
  • rune_start: the start (inclusive) rune
  • sub: the substring
  • ok: whether the rune indexes where in bounds of the original string

substring_to

substring_to :: proc(s: string, rune_end: int) -> (sub: string, ok: bool)Source

Returns a substring of s that goes up to rune index rune_end.

Think of it as slicing s[:rune_end] but rune-wise.

  • s: the string to substring
  • rune_end: the end (exclusive) rune
  • sub: the substring
  • ok: whether the rune indexes where in bounds of the original string

to_ada_case

to_ada_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts a string to "Ada_Case"

Allocates Using Provided Allocator

  • s: The input string to be converted
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_ada_case_example :: proc() {
	fmt.println(strings.to_ada_case("HelloWorld"))
}

Output:

Hello_World

to_camel_case

to_camel_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts the input string s to "lowerCamelCase".

Allocates Using Provided Allocator

  • s: Input string to be converted.
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

to_cstring

to_cstring :: proc(b: ^Builder, loc = #caller_location) -> (res: cstring, err: runtime.Allocator_Error)Source

Appends a trailing null byte after the end of the current Builder byte buffer and then casts it to a cstring

  • b: A pointer to builder
  • res: A cstring of the Builder's buffer upon success
  • err: An optional allocator error if one occured, nil otherwise

to_delimiter_case

to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Returns a string converted to a delimiter-separated case with configurable casing

Allocates Using Provided Allocator

  • s: The input string to be converted
  • delimiter: The rune to be used as the delimiter between words
  • all_upper_case: A boolean indicating if the output should be all uppercased (true) or lowercased (false)
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_delimiter_case_example :: proc() {
	fmt.println(strings.to_delimiter_case("Hello World", '_', false))
	fmt.println(strings.to_delimiter_case("Hello World", ' ', true))
	fmt.println(strings.to_delimiter_case("aBC", '_', false))
}

Output:

hello_world
HELLO WORLD
a_bc

to_kebab_case

to_kebab_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts a string to "kebab-case" with all runes lowercased

Allocates Using Provided Allocator

  • s: The input string to be converted
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_kebab_case_example :: proc() {
	fmt.println(strings.to_kebab_case("HelloWorld"))
}

Output:

hello-world

to_lower

to_lower :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts the input string s to all lowercase characters.

Allocates Using Provided Allocator

  • s: Input string to be converted.
  • allocator: (default: context.allocator).
  • res: The new string with all characters converted to lowercase
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_lower_example :: proc() {
	fmt.println(strings.to_lower("TeST"))
}

Output:

test

to_pascal_case

to_pascal_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts the input string s to "UpperCamelCase" (PascalCase).

Allocates Using Provided Allocator

  • s: Input string to be converted.
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

to_reader

to_reader :: proc(r: ^Reader, s: string) -> (res: io.Reader)Source

Initializes a string Reader and returns an io.Reader for the given string

  • r: A pointer to a Reader struct
  • s: The input string to be read
  • res: An io.Reader for the given string

to_snake_case

to_snake_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts a string to "snake_case" with all runes lowercased

Allocates Using Provided Allocator

  • s: The input string to be converted
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_snake_case_example :: proc() {
	fmt.println(strings.to_snake_case("HelloWorld"))
	fmt.println(strings.to_snake_case("Hello World"))
}

Output:

hello_world
hello_world

to_string

to_string :: proc(b: Builder) -> (res: string)Source

Casts the Builder byte buffer to a string and returns it

  • b: A Builder
  • res: The contents of the Builder's buffer, as a string

to_upper

to_upper :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts the input string s to all uppercase characters.

Allocates Using Provided Allocator

  • s: Input string to be converted.
  • allocator: (default: context.allocator).
  • res: The new string with all characters converted to uppercase
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_upper_example :: proc() {
	fmt.println(strings.to_upper("Test"))
}

Output:

TEST

to_upper_kebab_case

to_upper_kebab_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts a string to "KEBAB-CASE" with all runes uppercased

Allocates Using Provided Allocator

  • s: The input string to be converted
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_upper_kebab_case_example :: proc() {
	fmt.println(strings.to_upper_kebab_case("HelloWorld"))
}

Output:

HELLO-WORLD

to_upper_snake_case

to_upper_snake_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts a string to "SNAKE_CASE" with all runes uppercased

Allocates Using Provided Allocator

  • s: The input string to be converted
  • allocator: (default: context.allocator).
  • res: The converted string
  • err: An optional allocator error if one occured, nil otherwise

Example:

import "core:fmt"
import "core:strings"

to_upper_snake_case_example :: proc() {
	fmt.println(strings.to_upper_snake_case("HelloWorld"))
}

Output:

HELLO_WORLD

to_valid_utf8

to_valid_utf8 :: proc(s: string, replacement: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)Source

Converts invalid UTF-8 sequences in the input string s to the replacement string.

Allocates Using Provided Allocator

  • s: Input string that may contain invalid UTF-8 sequences.
  • replacement: String to replace invalid UTF-8 sequences with.
  • allocator: (default: context.allocator).

WARNING: Allocation does not occur when len(s) == 0

  • res: A valid UTF-8 string with invalid sequences replaced by replacement.
  • err: An optional allocator error if one occured, nil otherwise

trim

trim :: proc(s: string, cutset: string) -> (res: string)Source

Trims the cutset string from the s string, both from left and right

  • s: The input string
  • cutset: The set of characters to be trimmed from both sides of the input string
  • res: The trimmed string as a slice of the original

trim_left

trim_left :: proc(s: string, cutset: string) -> (res: string)Source

Trims the cutset string from the s string

  • s: The input string
  • cutset: The set of characters to be trimmed from the left of the input string
  • res: The trimmed string as a slice of the original

trim_left_null

trim_left_null :: proc(s: string) -> (res: string)Source

Trims null runes from the left, "\x00\x00testing\x00\x00" -> "testing\x00\x00"

  • s: The input string
  • res: The trimmed string as a slice of the original

trim_left_proc

trim_left_proc :: proc(s: string, p: proc() -> (bool)) -> (res: string)Source

Trims the input string s from the left until the procedure p returns false

  • s: The input string
  • p: A procedure that takes a rune and returns a boolean
  • res: The trimmed string as a slice of the original

Example:

import "core:fmt"
import "core:strings"

trim_left_proc_example :: proc() {
	find :: proc(r: rune) -> bool {
		return r == 'x'
	}
	fmt.println(strings.trim_left_proc("xxxxxxtesting", find))
}

Output:

testing

trim_left_proc_with_state

trim_left_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr) -> (res: string)Source

Trims the input string s from the left until the procedure p with state returns false

  • s: The input string
  • p: A procedure that takes a raw pointer and a rune and returns a boolean
  • state: The raw pointer to be passed to the procedure p
  • res: The trimmed string as a slice of the original

trim_null

trim_null :: proc(s: string) -> (res: string)Source

Trims null runes from both sides, "\x00\x00testing\x00\x00" -> "testing"

  • s: The input string
  • res: The trimmed string as a slice of the original

trim_prefix

trim_prefix :: proc(s: string, prefix: string) -> (res: string)Source

Trims a prefix string from the start of the s string and returns the trimmed string

  • s: The input string
  • prefix: The prefix string to be removed
  • res: The trimmed string as a slice of original, or the input string if no prefix was found

Example:

import "core:fmt"
import "core:strings"

trim_prefix_example :: proc() {
	fmt.println(strings.trim_prefix("testing", "test"))
	fmt.println(strings.trim_prefix("testing", "abc"))
}

Output:

ing
testing

trim_right

trim_right :: proc(s: string, cutset: string) -> (res: string)Source

Trims the cutset string from the s string from the right

  • s: The input string
  • cutset: The set of characters to be trimmed from the right of the input string
  • res: The trimmed string as a slice of the original

trim_right_proc

trim_right_proc :: proc(s: string, p: proc() -> (bool)) -> (res: string)Source

Trims the input string s from the right until the procedure p returns false

  • s: The input string
  • p: A procedure that takes a rune and returns a boolean
  • res: The trimmed string as a slice of the original

Example:

import "core:fmt"
import "core:strings"

trim_right_proc_example :: proc() {
	find :: proc(r: rune) -> bool {
		return r != 't'
	}
	fmt.println(strings.trim_right_proc("testing", find))
}

Output:

test

trim_right_proc_with_state

trim_right_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr) -> (res: string)Source

Trims the input string s from the right until the procedure p with state returns false

  • s: The input string
  • p: A procedure that takes a raw pointer and a rune and returns a boolean
  • state: The raw pointer to be passed to the procedure p
  • res: The trimmed string as a slice of the original, empty when no match

trim_space

trim_space :: proc(s: string) -> (res: string)Source

Trims from both sides until a valid non-space rune, "\t\txyz\t\t" -> "xyz"

  • s: The input string
  • res: The trimmed string as a slice of the original

trim_suffix

trim_suffix :: proc(s: string, suffix: string) -> (res: string)Source

Trims a suffix string from the end of the s string and returns the trimmed string

  • s: The input string
  • suffix: The suffix string to be removed
  • res: The trimmed string as a slice of original, or the input string if no suffix was found

Example:

import "core:fmt"
import "core:strings"

trim_suffix_example :: proc() {
	fmt.println(strings.trim_suffix("todo.txt", ".txt"))
	fmt.println(strings.trim_suffix("todo.doc", ".txt"))
}

Output:

todo
todo.doc

truncate_to_byte

truncate_to_byte :: proc(str: string, b: u8) -> (res: string)Source

Truncates a string str at the first occurrence of char/byte b

  • str: The input string
  • b: The byte to truncate the string at

NOTE: Failure to find the byte results in returning the entire string.

  • res: The truncated string

truncate_to_rune

truncate_to_rune :: proc(str: string, r: rune) -> (res: string)Source

Truncates a string str at the first occurrence of rune r as a slice of the original, entire string if not found

  • str: The input string
  • r: The rune to truncate the string at
  • res: The truncated string

unsafe_to_cstring

unsafe_to_cstring :: proc(b: ^Builder, loc = #caller_location) -> (res: cstring)Source

Appends a trailing null byte after the end of the current Builder byte buffer and then casts it to a cstring

NOTE: This procedure will not check if the backing buffer has enough space to include the extra null byte.

  • b: A pointer to builder
  • res: A cstring of the Builder's buffer

write_byte

write_byte :: proc(b: ^Builder, x: u8, loc = #caller_location) -> (n: int)Source

Appends a byte to the Builder and returns the number of bytes appended

  • b: A pointer to the Builder
  • x: The byte to be appended
  • n: The number of bytes appended

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_byte_example :: proc() {
	builder := strings.builder_make()
	strings.write_byte(&builder, 'a')        // 1
	strings.write_byte(&builder, 'b')        // 1
	fmt.println(strings.to_string(builder))  // -> ab
}

Output:

ab

write_bytes

write_bytes :: proc(b: ^Builder, x: []u8, loc = #caller_location) -> (n: int)Source

Appends a slice of bytes to the Builder and returns the number of bytes appended

  • b: A pointer to the Builder
  • x: The slice of bytes to be appended

Example:

import "core:fmt"
import "core:strings"

write_bytes_example :: proc() {
	builder := strings.builder_make()
	bytes := [?]byte { 'a', 'b', 'c' }
	strings.write_bytes(&builder, bytes[:]) // 3
	fmt.println(strings.to_string(builder)) // -> abc
}

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of bytes appended

write_encoded_rune

write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote: untyped boolean = true) -> (n: int)Source

Appends a rune to the Builder and returns the number of bytes written

  • b: A pointer to the Builder
  • r: The rune to be appended
  • write_quote: Optional boolean flag to wrap in single-quotes (') (default is true)
  • n: The number of bytes written

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_encoded_rune_example :: proc() {
	builder := strings.builder_make()
	strings.write_encoded_rune(&builder, 'a', false) // 1
	strings.write_encoded_rune(&builder, '\"', true) // 3
	strings.write_encoded_rune(&builder, 'x', false) // 1
	fmt.println(strings.to_string(builder))
}

Output:

a'"'x

write_escaped_rune

write_escaped_rune :: proc(b: ^Builder, r: rune, quote: u8, html_safe: untyped boolean = false) -> (n: int)Source

Appends an escaped rune to the Builder and returns the number of bytes written

  • b: A pointer to the Builder
  • r: The rune to be appended
  • quote: The quote character
  • html_safe: Optional boolean flag to encode '<', '>', '&' as digits (default is false)

Usage

  • '\a' will be written as such
  • r and quote match and quote is \\ - they will be written as two slashes
  • html_safe flag in case the runes '<', '>', '&' should be encoded as digits e.g. \u0026

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of bytes written

write_f16

write_f16 :: proc(b: ^Builder, f: f16, fmt: u8, always_signed: untyped boolean = false) -> (n: int)Source

Writes a f16 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • f: The f16 value to be appended
  • fmt: The format byte
  • always_signed: Optional boolean flag to always include the sign

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_f32

write_f32 :: proc(b: ^Builder, f: f32, fmt: u8, always_signed: untyped boolean = false) -> (n: int)Source

Writes a f32 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • f: The f32 value to be appended
  • fmt: The format byte
  • always_signed: Optional boolean flag to always include the sign
  • n: The number of characters written

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_f32_example :: proc() {
	builder := strings.builder_make()
	strings.write_f32(&builder, 3.14159, 'f') // 6
	strings.write_string(&builder, " - ")     // 3
	strings.write_f32(&builder, -0.123, 'e')  // 8
	fmt.println(strings.to_string(builder))   // -> 3.14159012 - -1.23000003e-01
}

Output:

3.14159012 - -1.23000003e-01

write_f64

write_f64 :: proc(b: ^Builder, f: f64, fmt: u8, always_signed: untyped boolean = false) -> (n: int)Source

Writes a f64 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • f: The f64 value to be appended
  • fmt: The format byte
  • always_signed: Optional boolean flag to always include the sign

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_float

write_float :: proc( b: ^Builder, f: f64, fmt: u8, prec: int, bit_size: int, always_signed: untyped boolean = false, ) -> (n: int)Source

Writes a f64 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • f: The f64 value to be appended
  • fmt: The format byte
  • prec: The precision
  • bit_size: The bit size
  • always_signed: Optional boolean flag to always include the sign (default is false)

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_i64

write_i64 :: proc(b: ^Builder, i: i64, base: int) -> (n: int)Source

Writes a i64 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • i: The i64 value to be appended
  • base: The optional base for the numeric representation

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_int

write_int :: proc(b: ^Builder, i: int, base: int) -> (n: int)Source

Writes a int value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • i: The int value to be appended
  • base: The optional base for the numeric representation

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_quoted_rune

write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int)Source

Appends a quoted rune to the Builder and returns the number of bytes written

  • b: A pointer to the Builder
  • r: The rune to be appended
  • n: The number of bytes written

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_quoted_rune_example :: proc() {
	builder := strings.builder_make()
	strings.write_string(&builder, "abc")      // 3
	strings.write_quoted_rune(&builder, 'ä') // 4
	strings.write_string(&builder, "abc")      // 3
	fmt.println(strings.to_string(builder))    // -> abc'ä'abc
}

Output:

abc'ä'abc

write_quoted_string

write_quoted_string :: proc()Source
  • b: A pointer to the Builder
  • str: The string to be quoted and appended
  • quote: The optional quote character (default is double quotes)
  • n: The number of bytes written

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_quoted_string_example :: proc() {
	builder := strings.builder_make()
	strings.write_quoted_string(&builder, "a")        // 3
	strings.write_quoted_string(&builder, "bc", '\'') // 4
	strings.write_quoted_string(&builder, "xyz")      // 5
	fmt.println(strings.to_string(builder))
}

Output:

"a"'bc'"xyz"

write_rune

write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error)Source

Appends a single rune to the Builder and returns the number of bytes written and an io.Error

  • b: A pointer to the Builder
  • r: The rune to be appended
  • res: The number of bytes written
  • err: An io.Error if one occured, nil otherwise

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_rune_example :: proc() {
	builder := strings.builder_make()
	strings.write_rune(&builder, 'ä')     // 2 None
	strings.write_rune(&builder, 'b')       // 1 None
	fmt.println(strings.to_string(builder)) // -> äb
}

Output:

äb

write_string

write_string :: proc(b: ^Builder, s: string, loc = #caller_location) -> (n: int)Source

Appends a string to the Builder and returns the number of bytes written

  • b: A pointer to the Builder
  • s: The string to be appended
  • n: The number of bytes written

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

Example:

import "core:fmt"
import "core:strings"

write_string_example :: proc() {
	builder := strings.builder_make()
	strings.write_string(&builder, "a")     // 1
	strings.write_string(&builder, "bc")    // 2
	fmt.println(strings.to_string(builder)) // -> abc
}

Output:

abc

write_u64

write_u64 :: proc(b: ^Builder, i: u64, base: int) -> (n: int)Source

Writes a u64 value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • i: The u64 value to be appended
  • base: The optional base for the numeric representation

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

write_uint

write_uint :: proc(b: ^Builder, i: uint, base: int) -> (n: int)Source

Writes a uint value to the Builder and returns the number of characters written

  • b: A pointer to the Builder
  • i: The uint value to be appended
  • base: The optional base for the numeric representation

NOTE: The backing dynamic array may be fixed in capacity or fail to resize, n states the number actually written.

  • n: The number of characters written

Procedure Groups

3

builder_make

builder_make :: proc{builder_make_none, builder_make_len, builder_make_len_cap}Source

Produces a String Builder

Allocates Using Provided Allocator

Example:

import "core:fmt"
import "core:strings"
builder_make_example :: proc() {
	sb := strings.builder_make()
	strings.write_byte(&sb, 'a')
	strings.write_string(&sb, " slice of ")
	strings.write_f64(&sb, 3.14,'g',true) // See `fmt.fmt_float` byte codes
	strings.write_string(&sb, " is ")
	strings.write_int(&sb, 180)
	strings.write_rune(&sb,'°')
	the_string :=strings.to_string(sb)
	fmt.println(the_string)
}

Output:

a slice of +3.14 is 180°

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.