core/strings
strings
Types
6Ascii_Set
Ascii_Set :: [8]u32SourceAscii_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,
}SourceA 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
Builder_Flush_Proc
Builder_Flush_Proc :: proc(b: ^Builder) -> (do_reset: bool)SourceType 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,
}SourceIntern 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
Intern_Entry
Intern_Entry :: struct {
len: int,
str: [1]u8,
}SourceCustom string entry struct
Reader
Reader :: struct {
s: string,
i: i64,
prev_rune: int,
}Sourceio 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
1compare
compare :: runtime.string_cmpSourceCompares two strings, returning a value representing which one comes first lexicographically. -1 for lhs; 1 for rhs, or 0 if they are equal.
Inputs:
- lhs: First string for comparison
- rhs: Second string for comparison
Returns:
- result:
-1iflhscomes first,1ifrhscomes first, or0if 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)SourceInternal 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)
Inputs:
- 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)
Returns:
- new_entry: The interned cstring
- err: An allocator error if one occured,
nilotherwise
ascii_set_contains
ascii_set_contains :: proc(as: Ascii_Set, c: u8) -> (res: bool)SourceDetermines 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
ascii_set_make :: proc(chars: string) -> (as: Ascii_Set, ok: bool)SourceCreates 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
builder_cap :: proc(b: Builder) -> (res: int)SourceReturns 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)SourceDeletes the Builder byte buffer content
Inputs:
- b: A pointer to the Builder
builder_from_bytes
builder_from_bytes :: proc(backing: []u8) -> (res: Builder)SourceCreates 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"
}Output:
a
abbuilder_from_slice
builder_from_slice :: proc(backing: []u8) -> (res: Builder)SourceAlias to builder_from_bytes
builder_grow
builder_grow :: proc(b: ^Builder, cap: int)SourceReserves 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: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)SourceInitializes 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: int, cap: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)SourceInitializes 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: mem.Allocator = context.allocator, loc = #caller_location) -> (res: ^Builder, err: runtime.Allocator_Error)SourceInitializes 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
builder_len :: proc(b: Builder) -> (res: int)SourceReturns 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
builder_make_len :: proc(len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)SourceProduces 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
builder_make_len_cap :: proc(len: int, cap: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)SourceProduces 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
builder_make_none :: proc(allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: Builder, err: runtime.Allocator_Error)SourceProduces 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: string, new: string, n: int, loc = #caller_location) -> (replaced: int, err: runtime.Allocator_Error)SourceReplaces 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: string, new: string) -> (replaced: int, err: runtime.Allocator_Error)SourceReplaces 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)SourceClears the Builder byte buffer content (sets len to zero)
Inputs:
- b: A pointer to the Builder
builder_space
builder_space :: proc(b: Builder) -> (res: int)SourceThe 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 :: proc(str: string, length: int, pad: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceAlias for centre_justify
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)SourceCenters 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
clone :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceClones 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
clone_from_bytes :: proc(s: []u8, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceClones 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
clone_from_cstring :: proc(s: cstring, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceClones 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
clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceClones 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
clone_from_ptr :: proc(ptr: ^u8, len: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceClones 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
clone_to_cstring :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: cstring, err: runtime.Allocator_Error)SourceClones 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
common_prefix :: proc(a: string, b: string) -> (string)SourceReturns 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"))
}Output:
test
te
teconcatenate
concatenate :: proc(a: []string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceReturns 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[:]))
}Output:
abccontains
contains :: proc(s: string, substr: string) -> (res: bool)SourceReturns 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"))
}Output:
true
true
falsecontains_any
contains_any :: proc(s: string, chars: string) -> (res: bool)SourceReturns 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"))
}Output:
true
true
true
falsecontains_rune
contains_rune :: proc(s: string, r: rune) -> (result: bool)SourceChecks 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
contains_space
contains_space :: proc(s: string) -> (res: bool)Sourcecount
count :: proc(s: string, substr: string) -> (res: int)SourceCounts 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", " "))
}Output:
1
2
3
1
0cut
cut :: proc(s: string, rune_offset: int = int(0), rune_length: int = int(0)) -> (res: string)SourceReturns 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"
}Output:
some
me
examplecut_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)SourceReturns 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"
}Output:
some
me
exampleends_with
ends_with :: proc(s: string, suffix: string) -> (result: bool)Sourceequal_fold
equal_fold :: proc(u: string, v: string) -> (res: bool)SourceReturns 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"))
}Output:
true
true
true
falseexpand_tabs
expand_tabs :: proc(s: string, tab_size: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceExpands 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))
}Output:
abc1 abc2 abc3fields
fields :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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
fields_iterator
fields_iterator :: proc(s: ^string) -> (field: string, ok: bool)SourceRetrieves 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
fields_proc :: proc(s: string, f: proc() -> (bool), allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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
has_prefix :: proc(s: string, prefix: string) -> (result: bool)SourceDetermines 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"))
}Output:
true
true
true
falsehas_suffix
has_suffix :: proc(s: string, suffix: string) -> (result: bool)SourceDetermines 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"))
}Output:
true
false
trueindex
index :: proc(s: string, substr: string) -> (res: int)SourceReturns 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"))
}Output:
0
0
2
-1index_any
index_any :: proc(s: string, chars: string) -> (res: int)SourceReturns 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"))
}Output:
2
1
0
0
-1index_byte
index_byte :: proc(s: string, c: u8) -> (res: int)SourceReturns 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", 'ä'))
}Output:
0
1
-1
-1index_multi
index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int)SourceFinds 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
index_proc :: proc(s: string, p: proc() -> (bool), truth: untyped boolean = true) -> (res: int)SourceFind 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))
}Output:
0
2
0
1
-1index_proc_with_state
index_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr, truth: untyped boolean = true) -> (res: int)SourceSame as index_proc, but the procedure p takes a raw pointer for state
index_rune
index_rune :: proc(s: string, r: rune) -> (res: int)SourceReturns 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'))
}Output:
-1
0
1
2
3
5
6
7intern_destroy
intern_destroy :: proc(m: ^Intern)SourceFrees 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)SourceReturns 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)SourceReturns 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: mem.Allocator = context.allocator, map_allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (err: runtime.Allocator_Error)SourceInitializes 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
is_ascii_space :: proc(r: rune) -> (res: bool)SourceReturns 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
is_delimiter :: proc(r: rune) -> (res: bool)SourceChecks 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
is_in_cutset :: proc(state: rawptr, r: rune) -> (res: bool)SourceProcedure for trim_*_proc variants, which has a string rawptr cast + rune comparison
is_null
is_null :: proc(r: rune) -> (res: bool)SourceReturns 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
is_separator :: proc(r: rune) -> (res: bool)SourceChecks 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
is_space :: proc(r: rune) -> (res: bool)SourceReturns 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
join :: proc(a: []string, sep: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceJoins 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[:], "..."))
}Output:
a b c
a-b-c
a...b...clast_index
last_index :: proc(s: string, substr: string) -> (res: int)SourceReturns 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"))
}Output:
3
0
2
-1last_index_any
last_index_any :: proc(s: string, chars: string) -> (res: int)SourceFinds 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"))
}Output:
2
2
3
3
-1last_index_byte
last_index_byte :: proc(s: string, c: u8) -> (res: int)SourceReturns 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", 'ä'))
}Output:
3
1
-1
-1last_index_proc
last_index_proc :: proc(s: string, p: proc() -> (bool), truth: untyped boolean = true) -> (res: int)SourceFinds 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
last_index_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr, truth: untyped boolean = true) -> (res: int)SourceSame as index_proc_with_state, runs through the string in reverse
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)SourceLeft-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
levenshtein_distance :: proc(a: string, b: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: int, err: runtime.Allocator_Error)SourceComputes 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
partition :: proc(str: string, sep: string) -> (head: string, match: string, tail: string)SourceSplits 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 == "")
}Output:
testing this out
testing t hi s out
testing this out
true
truepop_byte
pop_byte :: proc(b: ^Builder) -> (r: u8)SourcePops 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
pop_rune :: proc(b: ^Builder) -> (r: rune, width: int)SourcePops 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
prefix_length :: proc(a: string, b: string) -> (n: int)SourceReturns 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"))
}Output:
4
2
2
0reader_init
reader_init :: proc(r: ^Reader, s: string)SourceInitializes a string Reader with the provided string
Inputs:
- r: A pointer to a Reader struct
- s: The input string to be read
reader_length
reader_length :: proc(r: ^Reader) -> (res: int)SourceReturns the remaining length of the Reader
Inputs:
- r: A pointer to a Reader struct
Returns:
- res: The remaining length of the Reader
reader_read
reader_read :: proc(r: ^Reader, p: []u8) -> (n: int, err: io.Error)SourceReads 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
reader_read_at :: proc(r: ^Reader, p: []u8, off: i64) -> (n: int, err: io.Error)SourceReads 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
reader_read_byte :: proc(r: ^Reader) -> (res: u8, err: io.Error)SourceReads 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
reader_read_rune :: proc(r: ^Reader) -> (rr: rune, size: int, err: io.Error)SourceReads 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)SourceSeeks 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
reader_size :: proc(r: ^Reader) -> (res: i64)SourceReturns 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
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream)SourceConverts 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
reader_unread_byte :: proc(r: ^Reader) -> (err: io.Error)SourceDecrements 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
reader_unread_rune :: proc(r: ^Reader) -> (err: io.Error)SourceDecrements 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
reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error)SourceWrites 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
remove :: proc(s: string, key: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)SourceRemoves 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))
}Output:
abc true
true
bcbc true
abcabc falseremove_all
remove_all :: proc(s: string, key: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)SourceRemoves 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"))
}Output:
true
bcbc true
abcabc falserepeat
repeat :: proc(s: string, count: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceRepeats 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))
}Output:
abcabcreplace
replace :: proc(
s: string,
old: string,
new: string,
n: int,
allocator: mem.Allocator = context.allocator,
loc: _ = #caller_location,
) -> (output: string, was_allocation: bool)SourceReplaces 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))
}Output:
abcabc true
abcxyz true
xyzxyz false
zzzz truereplace_all
replace_all :: proc(s: string, old: string, new: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (output: string, was_allocation: bool)SourceReplaces all occurrences of old in s with new
Allocates Using Provided Allocator
Inputs:
- s: The string to modify
- old: The substring to replace
- new: The substring to replace
oldwith - allocator: The allocator to use for the new string (default is 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_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 truereverse
reverse :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceReverses 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)
}Output:
abcxyz zyxcbaright_justify
right_justify :: proc(str: string, length: int, pad: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceRight-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
rune_count :: proc(s: string) -> (res: int)SourceReturns 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
}Output:
4
5scrub
scrub :: proc(s: string, replacement: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: string, err: runtime.Allocator_Error)SourceReplaces 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"
}Output:
Hello?split
split :: proc(s: string, sep: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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)
}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)SourceSplits 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)
}Output:
["aaa.", "bbb.", "ccc.", "ddd.", "eee"]split_after_iterator
split_after_iterator :: proc(s: ^string, sep: string) -> (res: string, ok: bool)SourceSplits 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)
}
}Output:
a.
b.
c.
d.
esplit_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)SourceSplits 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)
}Output:
["aaa.", "bbb.", "ccc.ddd.eee"]split_by_byte_iterator
split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool)SourceSplits 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
}
}Output:
a
b
c
d
esplit_iterator
split_iterator :: proc(s: ^string, sep: string) -> (res: string, ok: bool)SourceSplits 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)
}
}Output:
a
b
c
d
esplit_lines
split_lines :: proc(s: string, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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)
}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)SourceSplits 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)
}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)SourceSplits 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
}
}Output:
a
b
c
d
esplit_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)SourceSplits 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)
}Output:
["a\n", "b\n", "c\nd\ne"]split_lines_iterator
split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool)SourceSplits 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")
}Output:
abcdesplit_lines_n
split_lines_n :: proc(s: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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)
}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)SourceSplits 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]
}Output:
["testing", "this", "out", "nice", "done", "last"]split_multi_iterate
split_multi_iterate :: proc(it: ^string, substrs: []string) -> (res: string, ok: bool)SourceSplits 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)
}
}Output:
testing
this
out
nice
done
lastsplit_n
split_n :: proc(s: string, sep: string, n: int, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (res: []string, err: runtime.Allocator_Error)SourceSplits 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)
}Output:
["aaa", "bbb", "ccc.ddd.eee"]starts_with
starts_with :: proc(s: string, prefix: string) -> (result: bool)Sourcestring_case_iterator
string_case_iterator :: proc(w: io.Writer, s: string, callback: proc(w: io.Writer, prev: rune, curr: rune, next: rune))SourceIterates 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)
}Output:
my_callback h
my_callback e
my_callback l
my_callback l
my_callback ostring_from_null_terminated_ptr
string_from_null_terminated_ptr :: proc(ptr: [^]u8, len: int) -> (res: string)SourceTransmutes 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 lengthstring_from_ptr
string_from_ptr :: proc(ptr: ^u8, len: int) -> (res: string)SourceTransmutes 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
substring :: proc(s: string, rune_start: int, rune_end: int) -> (sub: string, ok: bool)SourceReturns 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
substring_from :: proc(s: string, rune_start: int) -> (sub: string, ok: bool)SourceReturns 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
substring_to :: proc(s: string, rune_end: int) -> (sub: string, ok: bool)SourceReturns 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: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
Hello_Worldto_camel_case
to_camel_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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
to_cstring :: proc(b: ^Builder, loc = #caller_location) -> (res: cstring, err: runtime.Allocator_Error)SourceAppends 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: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceReturns 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))
}Output:
hello_world
HELLO WORLD
a_bcto_kebab_case
to_kebab_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
hello-worldto_lower
to_lower :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
testto_lower_camel_case
to_lower_camel_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceAlias to to_camel_case
to_pascal_case
to_pascal_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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
to_reader :: proc(r: ^Reader, s: string) -> (res: io.Reader)SourceInitializes 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
to_reader_at :: proc(r: ^Reader, s: string) -> (res: io.Reader_At)SourceInitializes 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 :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceAlias for to_upper_snake_case
to_snake_case
to_snake_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
hello_world
hello_worldto_stream
to_stream :: proc(b: ^Builder) -> (res: io.Stream)SourceReturns an io.Stream from a Builder
Inputs:
- b: A pointer to the Builder
Returns:
- res: the io.Stream
to_string
to_string :: proc(b: Builder) -> (res: string)SourceCasts 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: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
TESTto_upper_camel_case
to_upper_camel_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceAlias to to_pascal_case
to_upper_kebab_case
to_upper_kebab_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
HELLO-WORLDto_upper_snake_case
to_upper_snake_case :: proc(s: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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"))
}Output:
HELLO_WORLDto_valid_utf8
to_valid_utf8 :: proc(s: string, replacement: string, allocator: mem.Allocator = context.allocator) -> (res: string, err: runtime.Allocator_Error)SourceConverts 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
to_writer :: proc(b: ^Builder) -> (res: io.Writer)SourceReturns an io.Writer from a Builder
Inputs:
- b: A pointer to the Builder
Returns:
- res: The io.Writer
trim
trim :: proc(s: string, cutset: string) -> (res: string)SourceTrims 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
trim_left :: proc(s: string, cutset: string) -> (res: string)SourceTrims 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
trim_left_null :: proc(s: string) -> (res: string)SourceTrims 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
trim_left_proc :: proc(s: string, p: proc() -> (bool)) -> (res: string)SourceTrims 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))
}Output:
testingtrim_left_proc_with_state
trim_left_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr) -> (res: string)SourceTrims 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
trim_left_space :: proc(s: string) -> (res: string)SourceTrims 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
trim_null :: proc(s: string) -> (res: string)SourceTrims 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
trim_prefix :: proc(s: string, prefix: string) -> (res: string)SourceTrims 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"))
}Output:
ing
testingtrim_right
trim_right :: proc(s: string, cutset: string) -> (res: string)SourceTrims 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
trim_right_null :: proc(s: string) -> (res: string)SourceTrims 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
trim_right_proc :: proc(s: string, p: proc() -> (bool)) -> (res: string)SourceTrims 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))
}Output:
testtrim_right_proc_with_state
trim_right_proc_with_state :: proc(s: string, p: proc() -> (bool), state: rawptr) -> (res: string)SourceTrims 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
trim_right_space :: proc(s: string) -> (res: string)SourceTrims 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
trim_space :: proc(s: string) -> (res: string)SourceTrims 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
trim_suffix :: proc(s: string, suffix: string) -> (res: string)SourceTrims 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"))
}Output:
todo
todo.doctruncate_to_byte
truncate_to_byte :: proc(str: string, b: u8) -> (res: string)SourceTruncates 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
truncate_to_rune :: proc(str: string, r: rune) -> (res: string)SourceTruncates 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
unsafe_string_to_cstring :: proc(str: string) -> (res: cstring)SourceConverts 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
unsafe_to_cstring :: proc(b: ^Builder, loc = #caller_location) -> (res: cstring)SourceAppends 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)SourceAppends 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
}Output:
abwrite_bytes
write_bytes :: proc(b: ^Builder, x: []u8, loc = #caller_location) -> (n: int)SourceAppends 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
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote: untyped boolean = true) -> (n: int)SourceAppends 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))
}Output:
a'"'xwrite_escaped_rune
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: u8, html_safe: untyped boolean = false) -> (n: int)SourceAppends 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
write_f16 :: proc(b: ^Builder, f: f16, fmt: u8, always_signed: untyped boolean = false) -> (n: int)SourceWrites 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
write_f32 :: proc(b: ^Builder, f: f32, fmt: u8, always_signed: untyped boolean = false) -> (n: int)SourceWrites 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
}Output:
3.14159012 - -1.23000003e-01write_f64
write_f64 :: proc(b: ^Builder, f: f64, fmt: u8, always_signed: untyped boolean = false) -> (n: int)SourceWrites 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: int,
bit_size: int,
always_signed: untyped boolean = false,
) -> (n: int)SourceWrites 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
write_i64 :: proc(b: ^Builder, i: i64, base: int) -> (n: int)SourceWrites 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
write_int :: proc(b: ^Builder, i: int, base: int) -> (n: int)SourceWrites 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
write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int)SourceAppends 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
}Output:
abc'ä'abcwrite_quoted_string
write_quoted_string :: proc()SourceInputs:
- 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))
}Output:
"a"'bc'"xyz"write_rune
write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error)SourceAppends 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
}Output:
äbwrite_string
write_string :: proc(b: ^Builder, s: string, loc = #caller_location) -> (n: int)SourceAppends 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
}Output:
abcwrite_u64
write_u64 :: proc(b: ^Builder, i: u64, base: int) -> (n: int)SourceWrites 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
write_uint :: proc(b: ^Builder, i: uint, base: int) -> (n: int)SourceWrites 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
3builder_init
builder_init :: proc{builder_init_none, builder_init_len, builder_init_len_cap}SourceOverload simple builder_init_* with or without len / ap parameters
builder_make
builder_make :: proc{builder_make_none, builder_make_len, builder_make_len_cap}SourceProduces 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°clone_from
clone_from :: proc{clone, clone_from_bytes, clone_from_cstring, clone_from_ptr}SourceOverloaded procedure to clone from a string, []byte, cstring or a ^byte + length