package core:strings
Overview
A string builder, as well as procedures to manipulate UTF-8 encoded strings.
External Examples 1 ¶
The programs in odin-lang/examples about this package, and how many of its declarations each uses.
Index
Types (6)
Constants (0)
This section is empty.
Variables (0)
This section is empty.
Procedures (171)
- ascii_set_contains
- ascii_set_make
- center_justify
- centre_justify
- clone
- common_prefix
- compare
- concatenate
- contains
contains_… (3)
- count
- cut
- cut_clone
- ends_with
- equal_fold
- expand_tabs
- fields
- fields_iterator
- fields_proc
- has_prefix
- has_suffix
- index
intern_… (4)
- join
- left_justify
- levenshtein_distance
- partition
- pop_byte
- pop_rune
- prefix_length
- remove
- remove_all
- repeat
- replace
- replace_all
- reverse
- right_justify
- rune_count
- scrub
- split
- starts_with
- substring
- substring_from
- substring_to
- trim
- truncate_to_byte
- truncate_to_rune
- unsafe_string_to_cstring
- unsafe_to_cstring
Procedure Groups (3)
Types
Ascii_Set ¶
Ascii_Set :: distinct [8]u32
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, }
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
Related Procedures With Parameters
- builder_cap
- builder_destroy
- builder_grow
- builder_init_len
- builder_init_len_cap
- builder_init_none
- builder_len
- builder_replace
- builder_replace_all
- builder_reset
- builder_space
- pop_byte
- pop_rune
- to_cstring
- to_stream
- to_string
- to_writer
- unsafe_to_cstring
- write_byte
- write_bytes
- write_encoded_rune
- write_escaped_rune
- write_f16
- write_f32
- write_f64
- write_float
- write_i64
- write_int
- write_quoted_rune
- write_quoted_string
- write_rune
- write_string
- write_u64
- write_uint
- builder_init (procedure groups)
Related Procedures With Returns
- builder_from_bytes
- builder_make_len
- builder_make_len_cap
- builder_make_none
- builder_make (procedure groups)
Builder_Flush_Proc ¶
Type definition for a procedure that flushes a Builder
Inputs:
- b A pointer to the Builder
Returns:
A boolean indicating whether the Builder should be reset
Intern ¶
Intern :: struct { allocator: runtime.Allocator, entries: map[string]^Intern_Entry, }
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
Related Procedures With Parameters
Intern_Entry ¶
Custom string entry struct
Reader ¶
Reader :: struct { s: string, // read-only buffer i: i64, // current reading index prev_rune: int, }
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
Related Procedures With Parameters
Constants
This section is empty.
Variables
This section is empty.
Procedures
ascii_set_contains ¶
Determines if a given char is contained within an Ascii_Set.
Inputs:
- as The Ascii_Set to search.
- c The char to check for in the Ascii_Set.
Returns:
- res A boolean indicating if the byte is contained in the Ascii_Set (true) or not (false).
ascii_set_make ¶
Creates an Ascii_Set with unique characters from the input string.
Inputs:
- chars A string containing characters to include in the Ascii_Set.
Returns:
- 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_cap ¶
Returns the capacity of the Builder's buffer, in bytes
Inputs:
- b A Builder
Returns:
- res The capacity of the Builder's buffer
builder_destroy ¶
builder_destroy :: proc(b: ^Builder) {…}
Deletes the Builder byte buffer content
Inputs:
- b A pointer to the Builder
builder_from_bytes ¶
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
Inputs:
- backing A slice of bytes to be used as the backing buffer
Returns:
- 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"
}
a ab
builder_from_slice ¶
builder_from_slice :: builder_from_bytes
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
Inputs:
- backing A slice of bytes to be used as the backing buffer
Returns:
- 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"
}
a ab
builder_grow ¶
Reserves the Builder byte buffer to a specific capacity, when it's higher than before
Inputs:
- 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 := context.allocator, loc := #caller_location, ) -> (res: ^Builder, err: runtime.Allocator_Error) #optional_ok {…}
Initializes a Builder with specified length and capacity len.
It replaces the existing buf
Allocates Using Provided Allocator
Inputs:
- b A pointer to the Builder
- len The desired length of the Builder's buffer
- allocator (default is context.allocator)
Returns:
- res A pointer to the initialized Builder
- err An optional allocator error if one occured,
nilotherwise
builder_init_len_cap ¶
builder_init_len_cap :: proc( b: ^Builder, len, cap: int, allocator := context.allocator, loc := #caller_location, ) -> (res: ^Builder, err: runtime.Allocator_Error) #optional_ok {…}
Initializes a Builder with specified length len and capacity cap.
It replaces the existing buf
Inputs:
- 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)
Returns:
- res A pointer to the initialized Builder
- err An optional allocator error if one occured,
nilotherwise
builder_init_none ¶
builder_init_none :: proc( b: ^Builder, allocator := context.allocator, loc := #caller_location, ) -> (res: ^Builder, err: runtime.Allocator_Error) #optional_ok {…}
Initializes an empty Builder
It replaces the existing buf
Allocates Using Provided Allocator
Inputs:
- b A pointer to the Builder
- allocator (default is context.allocator)
Returns:
- res A pointer to the initialized Builder
- err An optional allocator error if one occured,
nilotherwise
builder_len ¶
Returns the length of the Builder's buffer, in bytes
Inputs:
- b A Builder
Returns:
- res The length of the Builder's buffer
builder_make_len ¶
@(require_results) builder_make_len :: proc( len: int, allocator := context.allocator, loc := #caller_location, ) -> (res: Builder, err: runtime.Allocator_Error) #optional_ok {…}
Produces a Builder with specified length and capacity len.
Allocates Using Provided Allocator
Inputs:
- len The desired length of the Builder's buffer
- allocator (default is context.allocator)
Returns:
- res The new Builder
- err An optional allocator error if one occured,
nilotherwise
builder_make_len_cap ¶
@(require_results) builder_make_len_cap :: proc( len, cap: int, allocator := context.allocator, loc := #caller_location, ) -> (res: Builder, err: runtime.Allocator_Error) #optional_ok {…}
Produces a Builder with specified length len and capacity cap.
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The new Builder
- err An optional allocator error if one occured,
nilotherwise
builder_make_none ¶
@(require_results) builder_make_none :: proc( allocator := context.allocator, loc := #caller_location, ) -> (res: Builder, err: runtime.Allocator_Error) #optional_ok {…}
Produces an empty Builder
Allocates Using Provided Allocator
Inputs:
- allocator (default is context.allocator)
Returns:
- res The new Builder
- err An optional allocator error if one occured,
nilotherwise
builder_replace ¶
builder_replace :: proc( b: ^Builder, old, new: string, n: int, loc := #caller_location, ) -> (replaced: int, err: runtime.Allocator_Error) {…}
Replaces n instances of old in the string in a Builder b with the new string
Allocates Using The Allocator On The Builder
Inputs:
- 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)
Returns:
- replaced The number of replacements
- err if any allocation errors occurred
builder_replace_all ¶
builder_replace_all :: proc( b: ^Builder, old, new: string, ) -> (replaced: int, err: runtime.Allocator_Error) {…}
Replaces all instances of old in the string in a Builder b with the new string
Allocates Using The Allocator On The Builder
Inputs:
- b The input
Builder - old The substring to be replaced
- new The replacement string
Returns:
- replaced The number of replacements
- err if any allocation errors occurred
builder_reset ¶
builder_reset :: proc(b: ^Builder) {…}
Clears the Builder byte buffer content (sets len to zero)
Inputs:
- b A pointer to the Builder
builder_space ¶
The free space left in the Builder's buffer, in bytes
Inputs:
- b A Builder
Returns:
- res The available space left in the Builder's buffer
center_justify ¶
center_justify :: centre_justify
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
Inputs:
- 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)
Returns:
- res A new string centered within a field of the specified length
- err An optional allocator error if one occured,
nilotherwise
centre_justify ¶
@(require_results) centre_justify :: proc( str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res A new string centered within a field of the specified length
- err An optional allocator error if one occured,
nilotherwise
clone ¶
@(require_results) clone :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Clones a string
Allocates Using Provided Allocator
Inputs:
- s The string to be cloned
- allocator (default: context.allocator)
- loc The caller location for debugging purposes (default: #caller_location)
Returns:
- res The cloned string
- err An optional allocator error if one occured,
nilotherwise
clone_from_bytes ¶
@(require_results) clone_from_bytes :: proc( s: []u8, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Clones a byte array s and appends a null-byte
Allocates Using Provided Allocator
Inputs:
- s The byte array to be cloned
- allocator (default: context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res The cloned string from the byte array with a null-byte
- err An optional allocator error if one occured,
nilotherwise
clone_from_cstring ¶
@(require_results) clone_from_cstring :: proc( s: cstring, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Clones a cstring s as a string
Allocates Using Provided Allocator
Inputs:
- s The cstring to be cloned
- allocator (default: context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res The cloned string from the cstring
- err An optional allocator error if one occured,
nilotherwise
clone_from_cstring_bounded ¶
@(require_results) clone_from_cstring_bounded :: proc( ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Clones a string from a null-terminated cstring ptr and a byte length len
Allocates Using Provided Allocator
Inputs:
- 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.
Returns:
- res The cloned string from the null-terminated cstring and byte length
- err An optional allocator error if one occured,
nilotherwise
clone_from_ptr ¶
@(require_results) clone_from_ptr :: proc( ptr: ^u8, len: int, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Clones a string from a byte pointer ptr and a byte length len
Allocates Using Provided Allocator
Inputs:
- 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
Returns:
- res The cloned string from the byte pointer and length
- err An optional allocator error if one occured,
nilotherwise
clone_to_cstring ¶
@(require_results) clone_to_cstring :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: cstring, err: runtime.Allocator_Error) #optional_ok {…}
Clones a string and appends a null-byte to make it a cstring
Allocates Using Provided Allocator
Inputs:
- s The string to be cloned
- allocator (default: context.allocator)
- loc The caller location for debugging purposes (default: #caller_location)
Returns:
- res A cloned cstring with an appended null-byte
- err An optional allocator error if one occured,
nilotherwise
common_prefix ¶
Returns the common prefix between strings a and b
Inputs:
- a The first input string
- b The second input string
Returns:
- n The string prefix common between strings
aandb
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"))
}
test te te
concatenate ¶
@(require_results) concatenate :: proc( a: []string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Returns a combined string from the slice of strings a without a separator
Allocates Using Provided Allocator
Inputs:
- a A slice of strings to concatenate
- allocator (default is context.allocator)
Returns:
- res The concatenated string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
concatenate_example :: proc() {
a := [?]string { "a", "b", "c" }
fmt.println(strings.concatenate(a[:]))
}
abc
External examples strings
contains ¶
Returns true when the string substr is contained inside the string s
Inputs:
- s The input string
- substr The substring to search for
Returns:
- res
trueifsubstris contained inside the strings,falseotherwise
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"))
}
true true false
External examples strings
contains_any ¶
Returns true when the string s contains any of the characters inside the string chars
Inputs:
- s The input string
- chars The characters to search for
Returns:
- res
trueif the stringscontains any of the characters inchars,falseotherwise
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"))
}
true true true false
contains_rune ¶
Checks if rune r in the string s
Inputs:
- s The input string
- r The rune to search for
Returns:
- result
trueif the runerin the strings,falseotherwise
External examples strings
count ¶
Counts the number of non-overlapping occurrences of substr in s
Inputs:
- s The string to search in
- substr The substring to count
Returns:
- res The number of occurrences of
substrins, returns the rune_count + 1 of the stringson emptysubstr
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", " "))
}
1 2 3 1 0
cut ¶
@(require_results) cut :: proc(s: string, rune_offset: int = int(0), rune_length: int = int(0)) -> (res: string) {…}
Returns a substring of the input string s with the specified rune offset and length
Inputs:
- 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.
Returns:
- 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"
}
some me example
cut_clone ¶
@(require_results) cut_clone :: proc( s: string, rune_offset: int = int(0), rune_length: int = int(0), allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Returns a substring of the input string s with the specified rune offset and length
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The substring
- err An optional allocator error if one occured,
nilotherwise
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"
}
some me example
ends_with ¶
ends_with :: has_suffix
Determines if a string s ends with a given suffix
Inputs:
- s The string to check for the
suffix - suffix The suffix to look for
Returns:
- result
trueif the stringsends with thesuffix, otherwisefalse
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"))
}
true false true
equal_fold ¶
Returns whether the strings u and v are the same alpha characters, ignoring different casings
Works with UTF-8 string content
Inputs:
- u The first string for comparison
- v The second string for comparison
Returns:
- res
trueif the stringsuandvare 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"))
}
true true true false
expand_tabs ¶
@(require_results) expand_tabs :: proc( s: string, tab_size: int, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Expands the input string by replacing tab characters with spaces to align to a specified tab size
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res A new string with tab characters expanded to the specified tab size
- err An optional allocator error if one occured,
nilotherwise
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))
}
abc1 abc2 abc3
fields ¶
@(require_results) fields :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- s The input string
- allocator (default is context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- 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,
nilotherwise
External examples strings
fields_iterator ¶
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
Inputs:
- s A mutable string reference to be iterated
Returns:
- field The first non-space substring found
- ok A boolean indicating if a non-space substring was found
fields_proc ¶
@(require_results) fields_proc :: proc( s: string, f: proc(rune) -> bool, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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
Returns:
- 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,
nilotherwise
has_prefix ¶
Determines if a string s starts with a given prefix
Inputs:
- s The string to check for the
prefix - prefix The prefix to look for
Returns:
- result
trueif the stringsstarts with theprefix, otherwisefalse
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"))
}
true true true false
has_suffix ¶
Determines if a string s ends with a given suffix
Inputs:
- s The string to check for the
suffix - suffix The suffix to look for
Returns:
- result
trueif the stringsends with thesuffix, otherwisefalse
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"))
}
true false true
index ¶
Returns the byte offset of the string substr in the string s, -1 when not found.
Inputs:
- s The input string to search in.
- substr The substring to search for.
Returns:
- res The byte offset of the first occurrence of
substrins, 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"))
}
0 0 2 -1
External examples strings
index_any ¶
Returns the index of any first char of chars found in s, -1 if not found.
Inputs:
- s The input string to search in.
- chars The characters to look for
Returns:
- res The index of the first character of
charsfound ins, 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"))
}
2 1 0 0 -1
index_byte ¶
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.
Inputs:
- s The input string to search in.
- c The byte to search for.
Returns:
- res The byte offset of the first occurrence of
cins, 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", 'ä'))
}
0 1 -1 -1
index_multi ¶
Finds the first occurrence of any substring in substrs within s
Inputs:
- s The string to search in
- substrs The substrings to look for
Returns:
- idx the index of the first matching substring
- width the length of the found substring
index_proc ¶
@(require_results) index_proc :: proc(s: string, p: proc(rune) -> bool, truth: bool = true) -> (res: int) {…}
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.
Inputs:
- s The input string
- p A procedure that takes a rune and returns a boolean
- truth The boolean value to be matched (default:
true)
Returns:
- 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))
}
0 2 0 1 -1
index_proc_with_state ¶
@(require_results) index_proc_with_state :: proc( s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth: bool = true, ) -> (res: int) {…}
Same as index_proc, but the procedure p takes a raw pointer for state
index_rune ¶
Returns the byte offset of the first rune r in the string s it finds, -1 when not found.
Invalid runes return -1
Inputs:
- s The input string to search in.
- r The rune to search for.
Returns:
- res The byte offset of the first occurrence of
rins, 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'))
}
-1 0 1 2 3 5 6 7
intern_destroy ¶
intern_destroy :: proc(m: ^Intern) {…}
Frees the map and all its content allocated using the .allocator.
Inputs:
- m A pointer to the Intern struct to be destroyed
intern_get ¶
intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {…}
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)
Inputs:
- 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.
Returns:
- str The interned string
- err An allocator error if one occured,
nilotherwise
intern_get_cstring ¶
intern_get_cstring :: proc( m: ^Intern, text: string, loc := #caller_location, ) -> (str: cstring, err: runtime.Allocator_Error) {…}
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)
Inputs:
- 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
Returns:
- str The interned cstring
- err An allocator error if one occured,
nilotherwise
intern_init ¶
intern_init :: proc( m: ^Intern, allocator := context.allocator, map_allocator := context.allocator, loc := #caller_location, ) -> (err: runtime.Allocator_Error) {…}
Initializes the entries map and sets the allocator for the string entries
Allocates Using Provided Allocators
Inputs:
- 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)
Returns:
- err An allocator error if one occured,
nilotherwise
is_ascii_space ¶
Returns true when the r rune is an ASCII whitespace character.
Inputs:
- r the rune to test
Returns:
-res: true if r is a whitespace character, false if otherwise
is_delimiter ¶
Checks if the rune r is a delimiter (' ', '-', or '_').
Inputs:
- r Rune to check for delimiter status.
Returns:
- res True if
ris a delimiter, false otherwise.
is_in_cutset ¶
Procedure for trim_*_proc variants, which has a string rawptr cast + rune comparison
is_null ¶
Returns true when the r rune is 0x0
Inputs:
- r the rune to test
Returns:
-res: true if r is 0x0, false if otherwise
is_separator ¶
Checks if the rune r is a non-alphanumeric or space character.
Inputs:
- r Rune to check for separator status.
Returns:
- res True if
ris a non-alpha orunicode.is_spacerune.
is_space ¶
Returns true when the r rune is an ASCII or UTF-8 whitespace character.
Inputs:
- r the rune to test
Returns:
-res: true if r is a whitespace character, false if otherwise
join ¶
@(require_results) join :: proc( a: []string, sep: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Joins a slice of strings a with a sep string
Allocates Using Provided Allocator
Inputs:
- a A slice of strings to join
- sep The separator string
- allocator (default is context.allocator)
Returns:
- res A combined string from the slice of strings
aseparated with thesepstring - err An optional allocator error if one occured,
nilotherwise
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[:], "..."))
}
a b c a-b-c a...b...c
External examples strings
last_index ¶
Returns the last byte offset of the string substr in the string s, -1 when not found.
Inputs:
- s The input string to search in.
- substr The substring to search for.
Returns:
- res The byte offset of the last occurrence of
substrins, 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"))
}
3 0 2 -1
last_index_any ¶
Finds the last occurrence of any character in chars within s. Iterates in reverse.
Inputs:
- s The string to search in
- chars The characters to look for
Returns:
- 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"))
}
2 2 3 3 -1
last_index_byte ¶
Returns the byte offset of the last byte c in the string s, -1 when not found.
Inputs:
- s The input string to search in.
- c The byte to search for.
Returns:
- res The byte offset of the last occurrence of
cins, 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", 'ä'))
}
3 1 -1 -1
last_index_proc ¶
@(require_results) last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth: bool = true) -> (res: int) {…}
Finds the index of the last rune in the string s for which the procedure p returns the same value as truth
last_index_proc_with_state ¶
@(require_results) last_index_proc_with_state :: proc( s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth: bool = true, ) -> (res: int) {…}
Same as index_proc_with_state, runs through the string in reverse
left_justify ¶
@(require_results) left_justify :: proc( str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res A new string left-justified within a field of the specified length
- err An optional allocator error if one occured,
nilotherwise
levenshtein_distance ¶
@(require_results) levenshtein_distance :: proc( a, b: string, allocator := context.allocator, loc := #caller_location, ) -> (res: int, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- a, b The two strings to compare
- allocator (default is context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res The Levenshtein edit distance between the two strings
- err An optional allocator error if one occured,
nilotherwise
NOTE: This implementation is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl.
partition ¶
Splits the input string str by the separator sep string and returns 3 parts. The values are slices of the original string.
Inputs:
- str The input string
- sep The separator string
Returns:
- 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 == "")
}
testing this out testing t hi s out testing this out true true
pop_byte ¶
Pops and returns the last byte in the Builder or 0 when the Builder is empty
Inputs:
- b A pointer to the Builder
Returns:
- r The last byte in the Builder or 0 if empty
pop_rune ¶
Pops the last rune in the Builder and returns the popped rune and its rune width or (0, 0) if empty
Inputs:
- b A pointer to the Builder
Returns:
- r The popped rune
- width The rune width or 0 if the builder was empty
prefix_length ¶
Returns the prefix length common between strings a and b
Inputs:
- a The first input string
- b The second input string
Returns:
- n The prefix length common between strings
aandb
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"))
}
4 2 2 0
reader_init ¶
Initializes a string Reader with the provided string
Inputs:
- r A pointer to a Reader struct
- s The input string to be read
reader_length ¶
Returns the remaining length of the Reader
Inputs:
- r A pointer to a Reader struct
Returns:
- res The remaining length of the Reader
reader_read ¶
Reads len(p) bytes from the Reader's string and copies into the provided slice.
Inputs:
- r A pointer to a Reader struct
- p A byte slice to copy data into
Returns:
- n The number of bytes read
- err An
io.Errorif an error occurs while reading, including.EOF, otherwisenildenotes success.
reader_read_at ¶
Reads len(p) bytes from the Reader's string and copies into the provided slice, at the specified offset from the current index.
Inputs:
- r A pointer to a Reader struct
- p A byte slice to copy data into
- off The offset from which to read
Returns:
- n The number of bytes read
- err An
io.Errorif an error occurs while reading, including.EOF, otherwisenildenotes success.
reader_read_byte ¶
Reads and returns a single byte from the Reader's string
Inputs:
- r A pointer to a Reader struct
Returns:
- The byte read from the Reader
- err: An
io.Errorif an error occurs while reading, including.EOF, otherwisenildenotes success.
reader_read_rune ¶
Reads and returns a single rune and its size from the Reader's string
Inputs:
- r A pointer to a Reader struct
Returns:
- rr The rune read from the Reader
- size The size of the rune in bytes
- err An
io.Errorif an error occurs while reading
reader_seek ¶
reader_seek :: proc( r: ^Reader, offset: i64, whence: io.Seek_From, ) -> (res: i64, err: io.Error) {…}
Seeks the Reader's index to a new position
Inputs:
- r A pointer to a Reader struct
- offset The new offset position
- whence The reference point for the new position (
.Start,.Current, or.End)
Returns:
- The absolute offset after seeking
- err: An
io.Errorif an error occurs while seeking (.Invalid_Whence,.Invalid_Offset)
reader_size ¶
Returns the length of the string stored in the Reader
Inputs:
- r A pointer to a Reader struct
Returns:
- res The length of the string stored in the Reader
reader_to_stream ¶
Converts a Reader into an io.Stream
Inputs:
- r A pointer to a Reader struct
Returns:
- s An io.Stream for the given Reader
reader_unread_byte ¶
Decrements the Reader's index (i) by 1
Inputs:
- r A pointer to a Reader struct
Returns:
- err An
io.Errorifr.i <= 0(.Invalid_Unread), otherwisenildenotes success.
reader_unread_rune ¶
Decrements the Reader's index (i) by the size of the last read rune
Inputs:
- r A pointer to a Reader struct
WARNING: May only be used once and after a valid read_rune call
Returns:
- err An
io.Errorif an error occurs while unreading (.Invalid_Unread), elsenildenotes success.
reader_write_to ¶
Writes the remaining content of the Reader's string into the provided io.Writer
Inputs:
- 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.
Returns:
- n The number of bytes written
- err An io.Error if an error occurs while writing (
.Short_Write)
remove ¶
@(require_results) remove :: proc( s, key: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool) {…}
Removes the key string n times from the s string
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- output The modified string
- was_allocation
trueif an allocation occurred during the replacement,falseotherwise
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))
}
abc true true bcbc true abcabc false
remove_all ¶
@(require_results) remove_all :: proc( s, key: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool) {…}
Removes all the key string instances from the s string
Allocates Using Provided Allocator
Inputs:
- s The input string
- key The substring to be removed
- allocator (default: context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- output The modified string
- was_allocation
trueif an allocation occurred during the replacement,falseotherwise
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"))
}
true bcbc true abcabc false
repeat ¶
@(require_results) repeat :: proc( s: string, count: int, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Repeats the string s count times, concatenating the result
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The concatenated repeated string
- err An optional allocator error if one occured,
nilotherwise
WARNING: Panics if count < 0
Example:
import "core:fmt"
import "core:strings"
repeat_example :: proc() {
fmt.println(strings.repeat("abc", 2))
}
abcabc
replace ¶
@(require_results) replace :: proc( s, old, new: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool) {…}
Replaces n instances of old in the string s with the new string
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- output The modified string
- was_allocation
trueif an allocation occurred during the replacement,falseotherwise
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))
}
abcabc true abcxyz true xyzxyz false zzzz true
replace_all ¶
@(require_results) replace_all :: proc( s, old, new: string, allocator := context.allocator, loc := #caller_location, ) -> (output: string, was_allocation: bool) {…}
reverse ¶
@(require_results) reverse :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Reverses the input string s
Allocates Using Provided Allocator
Inputs:
- s The input string
- allocator (default is context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res A reversed version of the input string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
reverse_example :: proc() {
a := "abcxyz"
b := strings.reverse(a)
fmt.println(a, b)
}
abcxyz zyxcba
right_justify ¶
@(require_results) right_justify :: proc( str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res A new string right-justified within a field of the specified length
- err An optional allocator error if one occured,
nilotherwise
rune_count ¶
Returns the UTF-8 rune count of the string s
Inputs:
- s The input string
Returns:
- 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
}
4 5
External examples strings
scrub ¶
@(require_results) scrub :: proc( s: string, replacement: string, allocator := context.allocator, loc := #caller_location, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res A new string with invalid UTF-8 characters replaced
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
scrub_example :: proc() {
text := "Hello\xC0\x80World"
fmt.println(strings.scrub(text, "?")) // -> "Hello?World"
}
Hello?
split ¶
@(require_results) split :: proc( s, sep: string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits a string into parts based on a separator.
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The slice of strings, each representing a part of the split string.
- err An optional allocator error if one occured,
nilotherwise
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)
}
["aaa", "bbb", "ccc", "ddd", "eee"]
External examples strings
split_after ¶
@(require_results) split_after :: proc( s, sep: string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits a string into parts after the separator, retaining it in the substrings.
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The slice of strings, each representing a part of the split string after the separator
- err An optional allocator error if one occured,
nilotherwise
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)
}
["aaa.", "bbb.", "ccc.", "ddd.", "eee"]
split_after_iterator ¶
@(require_results) split_after_iterator :: proc(s: ^string, sep: string) -> (res: string, ok: bool) {…}
Splits the input string after every separator string in an iterator fashion.
Inputs:
- s Pointer to the input string, which is modified during the search.
- sep The separator string to search for.
Returns:
- res The resulting substring
- ok
trueif an iteration result was returned,falseif 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)
}
}
a. b. c. d. e
split_after_n ¶
@(require_results) split_after_n :: proc( s, sep: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits a string into a total of n parts after the separator.
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The slice of strings with
nparts or fewer if there weren't - err An optional allocator error if one occured,
nilotherwise
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)
}
["aaa.", "bbb.", "ccc.ddd.eee"]
split_by_byte_iterator ¶
@(require_results) split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool) {…}
Splits the input string by the byte separator in an iterator fashion.
Inputs:
- s Pointer to the input string, which is modified during the search.
- sep The byte separator to search for.
Returns:
- res The resulting substring
- ok
trueif an iteration result was returned,falseif 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
}
}
a b c d e
split_iterator ¶
Splits the input string by the separator string in an iterator fashion.
Inputs:
- s Pointer to the input string, which is modified during the search.
- sep The separator string to search for.
Returns:
- res The resulting substring
- ok
trueif an iteration result was returned,falseif 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)
}
}
a b c d e
split_lines ¶
@(require_results) split_lines :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits the input string at every line break \n.
Allocates Using Provided Allocator
Inputs:
- s The input string to split.
- allocator (default is context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res The slice (allocated) of the split string (slices into original string)
- err An optional allocator error if one occured,
nilotherwise
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)
}
["a", "b", "c", "d", "e"]
split_lines_after ¶
@(require_results) split_lines_after :: proc( s: string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits the input string at every line break \n leaving the \n in the resulting strings.
Allocates Using Provided Allocator
Inputs:
- s The input string to split.
- allocator (default is context.allocator)
- loc The caller location for debugging purposes (default:
#caller_location)
Returns:
- res The slice (allocated) of the split string (slices into original string), with
\nincluded - err An optional allocator error if one occured,
nilotherwise
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)
}
["a\n", "b\n", "c\n", "d\n", "e"]
split_lines_after_iterator ¶
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.
Inputs:
- s Pointer to the input string, which is modified during the search.
Returns:
- line The resulting substring with line breaks included
- ok
trueif an iteration result was returned,falseif 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
}
}
a b c d e
split_lines_after_n ¶
@(require_results) split_lines_after_n :: proc( s: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res The slice (allocated) of the split string (slices into original string), with
\nincluded - err An optional allocator error if one occured,
nilotherwise
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)
}
["a\n", "b\n", "c\nd\ne"]
split_lines_iterator ¶
Splits the input string at every line break \n.
Returns the current split string every iteration until the string is consumed.
Inputs:
- s Pointer to the input string, which is modified during the search.
Returns:
- line The resulting substring
- ok
trueif an iteration result was returned,falseif 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")
}
abcde
split_lines_n ¶
@(require_results) split_lines_n :: proc( s: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits the input string at every line break \n for n parts.
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res The slice (allocated) of the split string (slices into original string)
- err An optional allocator error if one occured,
nilotherwise
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)
}
["a", "b", "c\nd\ne"]
split_multi ¶
@(require_results) split_multi :: proc( s: string, substrs: []string, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
Splits the input string s by all possible substrs and returns an allocated array of strings
Allocates Using Provided Allocator
Inputs:
- 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)
Returns:
- res An array of strings, or nil on empty substring or no matches
- err An optional allocator error if one occured,
nilotherwise
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]
}
["testing", "this", "out", "nice", "done", "last"]
split_multi_iterate ¶
@(require_results) split_multi_iterate :: proc(it: ^string, substrs: []string) -> (res: string, ok: bool) {…}
Splits the input string s by all possible substrs in an iterator fashion. The full string is returned if no match.
Inputs:
- it A pointer to the input string
- substrs An array of substrings used for splitting
Returns:
- res The split string
- ok
trueif an iteration result was returned,falseif 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)
}
}
testing this out nice done last
split_n ¶
@(require_results) split_n :: proc( s, sep: string, n: int, allocator := context.allocator, loc := #caller_location, ) -> (res: []string, err: runtime.Allocator_Error) #optional_ok {…}
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
Inputs:
- 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)
Returns:
- res The slice of strings, each representing a part of the split string.
- err An optional allocator error if one occured,
nilotherwise
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)
}
["aaa", "bbb", "ccc.ddd.eee"]
starts_with ¶
starts_with :: has_prefix
Determines if a string s starts with a given prefix
Inputs:
- s The string to check for the
prefix - prefix The prefix to look for
Returns:
- result
trueif the stringsstarts with theprefix, otherwisefalse
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"))
}
true true true false
string_case_iterator ¶
string_case_iterator :: proc( w: io.Stream, s: string, callback: proc(w: io.Stream, prev, curr, next: rune), ) {…}
Iterates over a string, calling a callback for each rune with the previous, current, and next runes as arguments.
Inputs:
- 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)
}
my_callback h my_callback e my_callback l my_callback l my_callback o
string_from_null_terminated_ptr ¶
@(require_results) string_from_null_terminated_ptr :: proc "contextless" (ptr: [^]u8, len: int) -> (res: string) {…}
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 ¶
Transmutes a raw pointer into a string. Non-allocating.
Inputs:
- 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.
Returns:
- res A string created from the byte pointer and length
substring ¶
@(require_results) substring :: proc(s: string, rune_start: int, rune_end: int) -> (sub: string, ok: bool) {…}
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.
Inputs:
- s the string to substring
- rune_start the start (inclusive) rune
- rune_end the end (exclusive) rune
Returns:
- sub the substring
- ok whether the rune indexes where in bounds of the original string
substring_from ¶
@(require_results) substring_from :: proc(s: string, rune_start: int) -> (sub: string, ok: bool) {…}
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.
Inputs:
- s the string to substring
- rune_start the start (inclusive) rune
Returns:
- sub the substring
- ok whether the rune indexes where in bounds of the original string
substring_to ¶
Returns a substring of s that goes up to rune index rune_end.
Think of it as slicing s[:rune_end] but rune-wise.
Inputs:
- s the string to substring
- rune_end the end (exclusive) rune
Returns:
- 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 := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts a string to "Ada_Case"
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_ada_case_example :: proc() {
fmt.println(strings.to_ada_case("HelloWorld"))
}
Hello_World
to_camel_case ¶
to_camel_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts the input string s to "lowerCamelCase".
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
to_cstring ¶
@(require_results) to_cstring :: proc( b: ^Builder, loc := #caller_location, ) -> (res: cstring, err: runtime.Allocator_Error) #optional_ok {…}
Appends a trailing null byte after the end of the current Builder byte buffer and then casts it to a cstring
Inputs:
- b A pointer to builder
Returns:
- res A cstring of the Builder's buffer upon success
- err An optional allocator error if one occured,
nilotherwise
to_delimiter_case ¶
to_delimiter_case :: proc( s: string, delimiter: rune, all_upper_case: bool, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Returns a string converted to a delimiter-separated case with configurable casing
Allocates Using Provided Allocator
Inputs:
- 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).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
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))
}
hello_world HELLO WORLD a_bc
to_kebab_case ¶
to_kebab_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts a string to "kebab-case" with all runes lowercased
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_kebab_case_example :: proc() {
fmt.println(strings.to_kebab_case("HelloWorld"))
}
hello-world
to_lower ¶
to_lower :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts the input string s to all lowercase characters.
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The new string with all characters converted to lowercase
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_lower_example :: proc() {
fmt.println(strings.to_lower("TeST"))
}
test
to_lower_camel_case ¶
to_lower_camel_case :: to_camel_case
Converts the input string s to "lowerCamelCase".
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
to_pascal_case ¶
to_pascal_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts the input string s to "UpperCamelCase" (PascalCase).
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
to_reader ¶
Initializes a string Reader and returns an io.Reader for the given string
Inputs:
- r A pointer to a Reader struct
- s The input string to be read
Returns:
- res An io.Reader for the given string
to_reader_at ¶
Initializes a string Reader and returns an io.Reader_At for the given string
Inputs:
- r A pointer to a Reader struct
- s The input string to be read
Returns:
- res An
io.Reader_Atfor the given string
to_screaming_snake_case ¶
to_screaming_snake_case :: to_upper_snake_case
Converts a string to "SNAKE_CASE" with all runes uppercased
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_upper_snake_case_example :: proc() {
fmt.println(strings.to_upper_snake_case("HelloWorld"))
}
HELLO_WORLD
to_snake_case ¶
to_snake_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts a string to "snake_case" with all runes lowercased
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
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"))
}
hello_world hello_world
to_stream ¶
Returns an io.Stream from a Builder
Inputs:
- b A pointer to the Builder
Returns:
- res the io.Stream
to_string ¶
Casts the Builder byte buffer to a string and returns it
Inputs:
- b A Builder
Returns:
- res The contents of the Builder's buffer, as a string
to_upper ¶
to_upper :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts the input string s to all uppercase characters.
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The new string with all characters converted to uppercase
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_upper_example :: proc() {
fmt.println(strings.to_upper("Test"))
}
TEST
to_upper_camel_case ¶
to_upper_camel_case :: to_pascal_case
Converts the input string s to "UpperCamelCase" (PascalCase).
Allocates Using Provided Allocator
Inputs:
- s Input string to be converted.
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
to_upper_kebab_case ¶
to_upper_kebab_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts a string to "KEBAB-CASE" with all runes uppercased
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_upper_kebab_case_example :: proc() {
fmt.println(strings.to_upper_kebab_case("HelloWorld"))
}
HELLO-WORLD
to_upper_snake_case ¶
to_upper_snake_case :: proc( s: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts a string to "SNAKE_CASE" with all runes uppercased
Allocates Using Provided Allocator
Inputs:
- s The input string to be converted
- allocator (default: context.allocator).
Returns:
- res The converted string
- err An optional allocator error if one occured,
nilotherwise
Example:
import "core:fmt"
import "core:strings"
to_upper_snake_case_example :: proc() {
fmt.println(strings.to_upper_snake_case("HelloWorld"))
}
HELLO_WORLD
to_valid_utf8 ¶
to_valid_utf8 :: proc( s, replacement: string, allocator := context.allocator, ) -> (res: string, err: runtime.Allocator_Error) #optional_ok {…}
Converts invalid UTF-8 sequences in the input string s to the replacement string.
Allocates Using Provided Allocator
Inputs:
- 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
Returns:
- res A valid UTF-8 string with invalid sequences replaced by
replacement. - err An optional allocator error if one occured,
nilotherwise
to_writer ¶
Returns an io.Writer from a Builder
Inputs:
- b A pointer to the Builder
Returns:
- res The io.Writer
trim ¶
Trims the cutset string from the s string, both from left and right
Inputs:
- s The input string
- cutset The set of characters to be trimmed from both sides of the input string
Returns:
- res The trimmed string as a slice of the original
trim_left ¶
Trims the cutset string from the s string
Inputs:
- s The input string
- cutset The set of characters to be trimmed from the left of the input string
Returns:
- res The trimmed string as a slice of the original
trim_left_null ¶
Trims null runes from the left, "\x00\x00testing\x00\x00" -> "testing\x00\x00"
Inputs:
- s The input string
Returns:
- res The trimmed string as a slice of the original
trim_left_proc ¶
Trims the input string s from the left until the procedure p returns false
Inputs:
- s The input string
- p A procedure that takes a rune and returns a boolean
Returns:
- 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))
}
testing
trim_left_proc_with_state ¶
@(require_results) trim_left_proc_with_state :: proc( s: string, p: proc(rawptr, rune) -> bool, state: rawptr, ) -> (res: string) {…}
Trims the input string s from the left until the procedure p with state returns false
Inputs:
- 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
Returns:
- res The trimmed string as a slice of the original
trim_left_space ¶
Trims until a valid non-space rune from the left, "\t\txyz\t\t" -> "xyz\t\t"
Inputs:
- s The input string
Returns:
- res The trimmed string as a slice of the original
trim_null ¶
Trims null runes from both sides, "\x00\x00testing\x00\x00" -> "testing"
Inputs:
- s The input string Returns:
- res The trimmed string as a slice of the original
trim_prefix ¶
Trims a prefix string from the start of the s string and returns the trimmed string
Inputs:
- s The input string
- prefix The prefix string to be removed
Returns:
- 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"))
}
ing testing
trim_right ¶
Trims the cutset string from the s string from the right
Inputs:
- s The input string
- cutset The set of characters to be trimmed from the right of the input string
Returns:
- res The trimmed string as a slice of the original
trim_right_null ¶
Trims null runes from the right, "\x00\x00testing\x00\x00" -> "\x00\x00testing"
Inputs:
- s The input string
Returns:
- res The trimmed string as a slice of the original
trim_right_proc ¶
Trims the input string s from the right until the procedure p returns false
Inputs:
- s The input string
- p A procedure that takes a rune and returns a boolean
Returns:
- 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))
}
test
trim_right_proc_with_state ¶
@(require_results) trim_right_proc_with_state :: proc( s: string, p: proc(rawptr, rune) -> bool, state: rawptr, ) -> (res: string) {…}
Trims the input string s from the right until the procedure p with state returns false
Inputs:
- 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
Returns:
- res The trimmed string as a slice of the original, empty when no match
trim_right_space ¶
Trims from the right until a valid non-space rune, "\t\txyz\t\t" -> "\t\txyz"
Inputs:
- s The input string
Returns:
- res The trimmed string as a slice of the original
trim_space ¶
Trims from both sides until a valid non-space rune, "\t\txyz\t\t" -> "xyz"
Inputs:
- s The input string
Returns:
- res The trimmed string as a slice of the original
trim_suffix ¶
Trims a suffix string from the end of the s string and returns the trimmed string
Inputs:
- s The input string
- suffix The suffix string to be removed
Returns:
- 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"))
}
todo todo.doc
truncate_to_byte ¶
Truncates a string str at the first occurrence of char/byte b
Inputs:
- str The input string
- b The byte to truncate the string at
NOTE: Failure to find the byte results in returning the entire string.
Returns:
- res The truncated string
truncate_to_rune ¶
Truncates a string str at the first occurrence of rune r as a slice of the original, entire string if not found
Inputs:
- str The input string
- r The rune to truncate the string at
Returns:
- res The truncated string
unsafe_string_to_cstring ¶
Converts a string str to a cstring
Inputs:
- str The input string
WARNING: This is unsafe because the original string may not contain a null-byte.
Returns:
- res The converted cstring
unsafe_to_cstring ¶
@(require_results) unsafe_to_cstring :: proc(b: ^Builder, loc := #caller_location) -> (res: cstring) {…}
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.
Inputs:
- b A pointer to builder
Returns:
- res A cstring of the Builder's buffer
write_byte ¶
write_byte :: proc(b: ^Builder, x: u8, loc := #caller_location) -> (n: int) {…}
Appends a byte to the Builder and returns the number of bytes appended
Inputs:
- b A pointer to the Builder
- x The byte to be appended
Returns:
- 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
}
ab
write_bytes ¶
write_bytes :: proc(b: ^Builder, x: []u8, loc := #caller_location) -> (n: int) {…}
Appends a slice of bytes to the Builder and returns the number of bytes appended
Inputs:
- 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.
Returns:
- n The number of bytes appended
write_encoded_rune ¶
Appends a rune to the Builder and returns the number of bytes written
Inputs:
- 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)
Returns:
- 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))
}
a'"'x
write_escaped_rune ¶
write_escaped_rune :: proc( b: ^Builder, r: rune, quote: u8, html_safe: bool = false, ) -> (n: int) {…}
Appends an escaped rune to the Builder and returns the number of bytes written
Inputs:
- 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
randquotematch andquoteis\\- they will be written as two slasheshtml_safeflag 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.
Returns:
- n The number of bytes written
write_f16 ¶
Writes a f16 value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_f32 ¶
Writes a f32 value to the Builder and returns the number of characters written
Inputs:
- 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
Returns:
- 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
}
3.14159012 - -1.23000003e-01
write_f64 ¶
Writes a f64 value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_float ¶
write_float :: proc( b: ^Builder, f: f64, fmt: u8, prec, bit_size: int, always_signed: bool = false, ) -> (n: int) {…}
Writes a f64 value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_i64 ¶
Writes a i64 value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_int ¶
Writes a int value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_quoted_rune ¶
Appends a quoted rune to the Builder and returns the number of bytes written
Inputs:
- b A pointer to the Builder
- r The rune to be appended
Returns:
- 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
}
abc'ä'abc
write_quoted_string ¶
Inputs:
- b A pointer to the Builder
- str The string to be quoted and appended
- quote The optional quote character (default is double quotes)
Returns:
- 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))
}
"a"'bc'"xyz"
write_rune ¶
write_rune :: proc(b: ^Builder, r: rune, loc := #caller_location) -> (n: int) {…}
Appends a single rune to the Builder and returns the number of bytes written and an io.Error
Inputs:
- b A pointer to the Builder
- r The rune to be appended
Returns:
- res The number of bytes written
- err An io.Error if one occured,
nilotherwise
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
}
äb
write_string ¶
write_string :: proc(b: ^Builder, s: string, loc := #caller_location) -> (n: int) {…}
Appends a string to the Builder and returns the number of bytes written
Inputs:
- b A pointer to the Builder
- s The string to be appended
Returns:
- 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
}
abc
write_u64 ¶
Writes a u64 value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
write_uint ¶
Writes a uint value to the Builder and returns the number of characters written
Inputs:
- 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.
Returns:
- n The number of characters written
Procedure Groups
builder_init ¶
builder_init :: proc{ builder_init_none, builder_init_len, builder_init_len_cap, }
Overload simple builder_init_* with or without len / ap parameters
builder_make ¶
builder_make :: proc{ builder_make_none, builder_make_len, builder_make_len_cap, }
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)
}
a slice of +3.14 is 180°
clone_from ¶
clone_from :: proc{ clone, clone_from_bytes, clone_from_cstring, clone_from_ptr, }
Overloaded procedure to clone from a string, []byte, cstring or a ^byte + length
Source Files
Generation Information
Generated with odin version dev-2026-10 (vendor "odin") Windows_amd64 @ 2026-10-23 23:43:08.089658900 +0000 UTC