package core:strings

Overview

simple procedures to manipulate UTF-8 encoded strings

Index

Constants (0)

This section is empty.

Variables (0)

This section is empty.

Procedures (164)

Types

Builder_Flush_Proc ¶

Builder_Flush_Proc :: proc "odin" (b: ^Builder) -> (do_reset: bool)

Builder ¶

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

dynamic byte buffer / string builder with helper procedures

the dynamic array is wrapped inside the struct to be more opaque
you can use `fmt.sbprint*` procedures with a `^strings.Builder` directly

Intern_Entry ¶

Intern_Entry :: struct {
	len: int,
	str: [1]u8,
}
 

custom string entry struct

Intern ¶

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

"intern" is a more memory efficient string map allocator is used to allocate the actual Intern_Entry strings

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`

Split_Multi ¶

Split_Multi :: struct {
	temp:     string,
	temp_old: string,
	substrs:  []string,
}
 

state for the split multi iterator

Constants

This section is empty.

Variables

This section is empty.

Procedures

builder_cap ¶

builder_cap :: proc "odin" (b: Builder) -> int {…}
 

return the cap of the builder byte buffer

builder_from_bytes ¶

builder_from_bytes :: proc "odin" (backing: []u8) -> Builder {…}
 

create an empty builder with the same slice length as its cap

uses the `mem.nil_allocator` to avoid allocation and keep a fixed length
used in `fmt.bprint*`

bytes: [8]byte // <-- gets filled
builder := strings.builder_from_bytes(bytes[:])
strings.write_byte(&builder, 'a') -> "a"
strings.write_byte(&builder, 'b') -> "ab"

builder_from_slice ¶

builder_from_slice :: builder_from_bytes
 

create an empty builder with the same slice length as its cap

uses the `mem.nil_allocator` to avoid allocation and keep a fixed length
used in `fmt.bprint*`

bytes: [8]byte // <-- gets filled
builder := strings.builder_from_bytes(bytes[:])
strings.write_byte(&builder, 'a') -> "a"
strings.write_byte(&builder, 'b') -> "ab"

builder_len ¶

builder_len :: proc "odin" (b: Builder) -> int {…}
 

return the length of the builder byte buffer

builder_space ¶

builder_space :: proc "odin" (b: Builder) -> int {…}
 

returns the space left in the builder byte buffer to use up

center_justify ¶

center_justify :: centre_justify
 

centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length

centre_justify ¶

centre_justify :: proc "odin" (str: string, length: int, pad: string, allocator := context.allocator) -> string {…}
 

centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length

clone ¶

clone :: proc "odin" (s: string, allocator := context.allocator, loc := #caller_location) -> string {…}
 

returns a clone of the string s allocated using the allocator

clone_from_bytes ¶

clone_from_bytes :: proc "odin" (s: []u8, allocator := context.allocator, loc := #caller_location) -> string {…}
 

returns a cloned string of the byte array s using the allocator appends a leading nul byte

clone_from_cstring_bounded ¶

clone_from_cstring_bounded :: proc "odin" (ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> string {…}
 

returns a cloned string from the cstring ptr and a byte length len using the allocator truncates till the first nul byte it finds or the byte len

clone_from_cstring ¶

clone_from_cstring :: proc "odin" (s: cstring, allocator := context.allocator, loc := #caller_location) -> string {…}
 

returns a clone of the cstring s using the allocator as a string

clone_from_ptr ¶

clone_from_ptr :: proc "odin" (ptr: ^u8, len: int, allocator := context.allocator, loc := #caller_location) -> string {…}
 

returns a cloned string from the pointer ptr and a byte length len using the allocator same to string_from_ptr but allocates

clone_safe ¶

clone_safe :: proc "odin" (s: string, allocator := context.allocator, loc := #caller_location) -> (str: string, err: runtime.Allocator_Error) {…}
 

returns a clone of the string s allocated using the allocator

clone_to_cstring ¶

clone_to_cstring :: proc "odin" (s: string, allocator := context.allocator, loc := #caller_location) -> cstring {…}
 

returns a clone of the string s allocated using the allocator as a cstring a nul byte is appended to the clone, to make the cstring safe

compare ¶

compare :: proc "odin" (a, b: string) -> int {…}
 

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

concatenate_safe ¶

concatenate_safe :: proc "odin" (a: []string, allocator := context.allocator) -> (res: string, err: runtime.Allocator_Error) {…}

concatenate ¶

concatenate :: proc "odin" (elems: []string, allocator := context.allocator) -> string {…}
 

returns a combined string from the slice of strings a without a seperator

allocates the string using the `allocator`


a := [?]string { "a", "b", "c" }
b := strings.concatenate(a[:]) -> "abc"

contains ¶

contains :: proc "odin" (s, substr: string) -> bool {…}
 

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

strings.contains("testing", "test") -> true
strings.contains("testing", "ing") -> true
strings.contains("testing", "text") -> false

contains_any ¶

contains_any :: proc "odin" (s, substr: string) -> bool {…}
 

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

strings.contains_any("test", "test") -> true
strings.contains_any("test", "ts") -> true
strings.contains_any("test", "et") -> true
strings.contains_any("test", "a") -> false

contains_rune ¶

contains_rune :: proc "odin" (s: string, r: rune) -> int {…}
 

returns the byte offset of the rune r in the string s, -1 when not found

count ¶

count :: proc "odin" (a, b: string) -> int {…}
 

returns the count of the string substr found in the string s

returns the rune_count + 1 of the string `s` on empty `substr`

strings.count("abbccc", "a") -> 1
strings.count("abbccc", "b") -> 2
strings.count("abbccc", "c") -> 3
strings.count("abbccc", "ab") -> 1
strings.count("abbccc", " ") -> 0

cut ¶

cut :: proc "odin" (s: string, rune_offset: int = int(0), rune_length: int = int(0), allocator := context.allocator) -> (res: string) {…}
 

rune_offset and rune_length are in runes, not bytes.

If `rune_length` <= 0, then it'll return the remainder of the string starting at `rune_offset`.

strings.cut("some example text", 0, 4) -> "some"
strings.cut("some example text", 2, 2) -> "me"
strings.cut("some example text", 5, 7) -> "example"

destroy_builder ¶

destroy_builder :: proc "odin" (b: ^Builder) {…}
 

delete and clear the builder byte buffer content

equal_fold ¶

equal_fold :: proc "odin" (s, substr: string) -> bool {…}
 

returns wether the strings u and v are the same alpha characters

works with utf8 string content and ignores different casings

strings.equal_fold("test", "test") -> true
strings.equal_fold("Test", "test") -> true
strings.equal_fold("Test", "tEsT") -> true
strings.equal_fold("test", "tes") -> false

expand_tabs ¶

expand_tabs :: proc "odin" (s: string, count: int, allocator := context.allocator) -> string {…}
 

expands the string to a grid spaced by tab_size whenever a \t character appears

returns the tabbed string, panics on tab_size <= 0

strings.expand_tabs("abc1\tabc2\tabc3", 4) -> abc1    abc2    abc3
strings.expand_tabs("abc1\tabc2\tabc3", 5) -> abc1 abc2 abc3
strings.expand_tabs("abc1\tabc2\tabc3", 6) -> abc1  abc2  abc3

fields ¶

fields :: proc "odin" (path: string, allocator := context.allocator) -> []string {…}
 

fields splits the string s around each instance of one or more consecutive white space character, defined by unicode.is_space returning a slice of substrings of s or an empty slice if s only contains white space

fields_iterator ¶

fields_iterator :: proc "odin" (s: ^string) -> (line: string, ok: bool) {…}
 

fields_iterator returns the first run of characters in s that does not contain white space, defined by unicode.is_space s will then start from any space after the substring, or be an empty string if the substring was the remaining characters

fields_proc ¶

fields_proc :: proc "odin" (s: string, f: proc "odin" (r: rune) -> bool, allocator := context.allocator) -> []string {…}
 

fields_proc splits the string s at each run of unicode code points ch satisfying f(ch) returns a slice of substrings of s If all code points in s satisfy f(ch) or string is empty, an empty slice is returned

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

grow_builder ¶

grow_builder :: proc "odin" (b: ^Builder, cap: int) {…}
 

reserve the builfer byte buffer to a specific cap, when it's higher than before

has_prefix ¶

has_prefix :: proc "odin" (s, substr: string) -> bool {…}
 

return true when the string prefix is contained at the start of the string s

strings.has_prefix("testing", "test") -> true
strings.has_prefix("testing", "te") -> true
strings.has_prefix("telephone", "te") -> true
strings.has_prefix("testing", "est") -> false

has_suffix ¶

has_suffix :: proc "odin" (s, substr: string) -> bool {…}
 

returns true when the string suffix is contained at the end of the string s

good example to use this is for file extensions

strings.has_suffix("todo.txt", ".txt") -> true
strings.has_suffix("todo.doc", ".txt") -> false
strings.has_suffix("todo.doc.txt", ".txt") -> true

index ¶

index :: proc "odin" (a, b: string) -> int {…}
 

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

strings.index("test", "t") -> 0
strings.index("test", "te") -> 0
strings.index("test", "st") -> 2
strings.index("test", "tt") -> -1

index_any ¶

index_any :: proc "odin" (a, b: string) -> int {…}
 

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

strings.index_any("test", "s") -> 2
strings.index_any("test", "se") -> 1
strings.index_any("test", "et") -> 0
strings.index_any("test", "set") -> 0
strings.index_any("test", "x") -> -1

index_byte ¶

index_byte :: proc "odin" (s: string, c: u8) -> int {…}
 

returns the byte offset of the first byte c in the string s it finds, -1 when not found

can't find utf8 based runes

strings.index_byte("test", 't') -> 0
strings.index_byte("test", 'e') -> 1
strings.index_byte("test", 'x') -> -1
strings.index_byte("teäst", 'ä') -> -1

index_proc ¶

index_proc :: proc "odin" (s: string, p: proc "odin" (r: rune) -> bool, truth: bool = true) -> int {…}
 

runs trough the s string linearly and watches wether the p procedure matches the truth bool

returns the rune offset or -1 when no match was found

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

index_proc_with_state ¶

index_proc_with_state :: proc "odin" (s: string, p: proc "odin" (state: rawptr, r: rune) -> bool, state: rawptr, truth: bool = true) -> int {…}
 

same as index_proc but with a p procedure taking a rawptr for state

index_rune ¶

index_rune :: proc "odin" (s: string, r: rune) -> int {…}
 

returns the byte offset of the first rune r in the string s it finds, -1 when not found

avoids invalid runes

strings.index_rune("abcädef", 'x') -> -1
strings.index_rune("abcädef", 'a') -> 0
strings.index_rune("abcädef", 'b') -> 1
strings.index_rune("abcädef", 'c') -> 2
strings.index_rune("abcädef", 'ä') -> 3
strings.index_rune("abcädef", 'd') -> 5
strings.index_rune("abcädef", 'e') -> 6
strings.index_rune("abcädef", 'f') -> 7

init_builder_len_cap ¶

init_builder_len_cap :: proc "odin" (b: ^Builder, len, cap: int, allocator := context.allocator) {…}
 

initialize a builder, with a set length len byte buffer and a custom cap replaces the existing buf

init_builder_len ¶

init_builder_len :: proc "odin" (b: ^Builder, len: int, allocator := context.allocator) {…}
 

initialize a builder, with a set length len and cap 16 byte buffer replaces the existing buf

init_builder_none ¶

init_builder_none :: proc "odin" (b: ^Builder, allocator := context.allocator) {…}
 

initialize a builder, default length 0 / cap 16 are done through make replaces the existing buf

intern_destroy ¶

intern_destroy :: proc "odin" (m: ^Intern) {…}
 

free the map and all its content allocated using the .allocator

intern_get ¶

intern_get :: proc "odin" (m: ^Intern, text: string) -> string {…}
 

returns the text string from the intern map - gets set if it didnt exist yet the returned string lives as long as the map entry lives

intern_get_cstring ¶

intern_get_cstring :: proc "odin" (m: ^Intern, text: string) -> cstring {…}
 

returns the text cstring from the intern map - gets set if it didnt exist yet the returned cstring lives as long as the map entry lives

intern_init ¶

intern_init :: proc "odin" (m: ^Intern, allocator := context.allocator, map_allocator := context.allocator) {…}
 

initialize the entries map and set the allocator for the string entries

is_ascii_space ¶

is_ascii_space :: proc "odin" (r: rune) -> bool {…}
 

return true when the r rune is '\t', '\n', '\v', '\f', '\r' or ' '

is_delimiter ¶

is_delimiter :: proc "odin" (r: rune) -> bool {…}
 

returns true when the c rune is a space, '-' or '_' useful when treating strings like words in a text editor or html paths

is_in_cutset ¶

is_in_cutset :: proc "odin" (state: rawptr, r: rune) -> bool {…}
 

procedure for trim_*_proc variants, which has a string rawptr cast + rune comparison

is_null ¶

is_null :: proc "odin" (r: rune) -> bool {…}
 

returns true when the r rune is a nul byte

is_separator ¶

is_separator :: proc "odin" (r: rune) -> bool {…}
 

returns true when the r rune is a non alpha or unicode.is_space rune

is_space ¶

is_space :: proc "odin" (r: rune) -> bool {…}
 

returns true when the r rune is any asci or utf8 based whitespace

join ¶

join :: proc "odin" (a: []string, sep: string, allocator := context.allocator) -> string {…}
 

returns a combined string from the slice of strings a seperated with the sep string

allocates the string using the `allocator`

a := [?]string { "a", "b", "c" }
b := strings.join(a[:], " ") -> "a b c"
c := strings.join(a[:], "-") -> "a-b-c"
d := strings.join(a[:], "...") -> "a...b...c"

join_safe ¶

join_safe :: proc "odin" (a: []string, sep: string, allocator := context.allocator) -> (str: string, err: runtime.Allocator_Error) {…}

last_index ¶

last_index :: proc "odin" (a, b: string) -> int {…}
 

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

strings.index("test", "t") -> 3
strings.index("test", "te") -> 0
strings.index("test", "st") -> 2
strings.index("test", "tt") -> -1

last_index_any ¶

last_index_any :: proc "odin" (a, b: string) -> int {…}
 

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

iterates the string in reverse

strings.index_any("test", "s") -> 2
strings.index_any("test", "se") -> 2
strings.index_any("test", "et") -> 1
strings.index_any("test", "set") -> 3
strings.index_any("test", "x") -> -1

last_index_byte ¶

last_index_byte :: proc "odin" (s: string, c: u8) -> int {…}
 

returns the byte offset of the last byte c in the string s it finds, -1 when not found

can't find utf8 based runes

strings.index_byte("test", 't') -> 3
strings.index_byte("test", 'e') -> 1
strings.index_byte("test", 'x') -> -1
strings.index_byte("teäst", 'ä') -> -1

last_index_proc_with_state ¶

last_index_proc_with_state :: proc "odin" (s: string, p: proc "odin" (state: rawptr, r: rune) -> bool, state: rawptr, truth: bool = true) -> int {…}
 

same as index_proc_with_state but runs through the string in reverse

last_index_proc ¶

last_index_proc :: proc "odin" (s: string, p: proc "odin" (r: rune) -> bool, truth: bool = true) -> int {…}
 

same as index_proc but runs through the string in reverse

left_justify ¶

left_justify :: proc "odin" (str: string, length: int, pad: string, allocator := context.allocator) -> string {…}
 

left_justify returns a string with a pad string at left side if the str's rune length is smaller than length

levenshtein_distance ¶

levenshtein_distance :: proc "odin" (a, b: string, allocator := context.allocator) -> int {…}
 

levenshtein_distance returns the Levenshtein edit distance between 2 strings. This is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl. Note: allocator isn't used if the length of string b in runes is smaller than 64.

make_builder_len ¶

make_builder_len :: proc "odin" (len: int, allocator := context.allocator) -> Builder {…}
 

return a builder, with a set length len and cap 16 byte buffer

make_builder_len_cap ¶

make_builder_len_cap :: proc "odin" (len, cap: int, allocator := context.allocator) -> Builder {…}
 

return a builder, with a set length len byte buffer and a custom cap

make_builder_none ¶

make_builder_none :: proc "odin" (allocator := context.allocator) -> Builder {…}
 

return a builder, default length 0 / cap 16 are done through make

partition ¶

partition :: proc "odin" (str, sep: string) -> (head, match, tail: string) {…}
 

splits the str string by the seperator sep string and returns 3 parts

`head`: before the split, `match`: the seperator, `tail`: the end of the split
returns the input string when the `sep` was not found

text := "testing this out"
strings.partition(text, " this ") -> head: "testing", match: " this ", tail: "out"
strings.partition(text, "hi") -> head: "testing t", match: "hi", tail: "s out"
strings.partition(text, "xyz") -> head: "testing this out", match: "", tail: ""

pop_byte ¶

pop_byte :: proc "odin" (b: ^Builder) -> (r: u8) {…}
 

pops and returns the last byte in the builder returns 0 when the builder is empty

pop_rune ¶

pop_rune :: proc "odin" (b: ^Builder) -> (r: rune, width: int) {…}
 

pops the last rune in the builder and returns the popped rune and its rune width returns 0, 0 when the builder is empty

prefix_length ¶

prefix_length :: proc "odin" (a, b: string) -> int {…}
 

return the prefix length common between strings a and b.

strings.prefix_length("testing", "test") -> 4
strings.prefix_length("testing", "te") -> 2
strings.prefix_length("telephone", "te") -> 2
strings.prefix_length("testing", "est") -> 0

ptr_from_string ¶

ptr_from_string :: proc "odin" (str: string) -> ^u8 {…}
 

returns the raw ^byte start of the string str

reader_init ¶

reader_init :: proc "odin" (r: ^Reader, s: string) {…}
 

init the reader to the string s

reader_length ¶

reader_length :: proc "odin" (r: ^Reader) -> int {…}
 

remaining length of the reader

reader_read_at ¶

reader_read_at :: proc "odin" (r: ^Reader, p: []u8, off: i64) -> (n: int, err: io.Error) {…}
 

reads len(p) bytes into the slice from the string in the reader at an offset returns n amount of read bytes and an io.Error

reader_read_byte ¶

reader_read_byte :: proc "odin" (r: ^Reader) -> (u8, io.Error) {…}
 

reads and returns a single byte - error when out of bounds

reader_read ¶

reader_read :: proc "odin" (r: ^Reader, p: []u8) -> (n: int, err: io.Error) {…}
 

reads len(p) bytes into the slice from the string in the reader returns n amount of read bytes and an io.Error

reader_read_rune ¶

reader_read_rune :: proc "odin" (r: ^Reader) -> (ch: rune, size: int, err: io.Error) {…}
 

reads and returns a single rune and the rune size - error when out bounds

reader_seek ¶

reader_seek :: proc "odin" (r: ^Reader, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {…}
 

seeks the reader offset to a wanted offset

reader_size ¶

reader_size :: proc "odin" (r: ^Reader) -> i64 {…}
 

returns the string length stored by the reader

reader_to_stream ¶

reader_to_stream :: proc "odin" (r: ^Reader) -> (s: io.Stream) {…}
 

returns a stream from the reader data

reader_unread_byte ¶

reader_unread_byte :: proc "odin" (r: ^Reader) -> io.Error {…}
 

decreases the reader offset - error when below 0

reader_unread_rune ¶

reader_unread_rune :: proc "odin" (r: ^Reader) -> io.Error {…}
 

decreases the reader offset by the last rune can only be used once and after a valid read_rune call

reader_write_to ¶

reader_write_to :: proc "odin" (r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {…}
 

writes the string content left to read into the io.Writer w

remove_all ¶

remove_all :: proc "odin" (s, key: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {…}
 

removes all the key string instanes from the s string

returns the `output` string and true when an a allocation through a remove happened

strings.remove("abcabc", "abc") -> "", true
strings.remove("abcabc", "a") -> "bcbc", true
strings.remove("abcabc", "x") -> "abcabc", false

remove ¶

remove :: proc "odin" (s, key: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {…}
 

removes the key string n times from the s string

if n < 0, no limit on the number of removes
returns the `output` string and true when an a allocation through a remove happened

strings.remove("abcabc", "abc", 1) -> "abc", true
strings.remove("abcabc", "abc", -1) -> "", true
strings.remove("abcabc", "a", -1) -> "bcbc", true
strings.remove("abcabc", "x", -1) -> "abcabc", false

repeat ¶

repeat :: proc "odin" (s: string, count: int, allocator := context.allocator) -> string {…}
 

repeats the string s multiple count times and returns the allocated string

panics when `count` is below 0

strings.repeat("abc", 2) -> "abcabc"

replace_all ¶

replace_all :: proc "odin" (s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {…}
 

replaces all instances of old in the string s with the new string

returns the `output` string and true when an a allocation through a replace happened

strings.replace_all("xyzxyz", "xyz", "abc") -> "abcabc", true
strings.replace_all("xyzxyz", "abc", "xyz") -> "xyzxyz", false
strings.replace_all("xyzxyz", "xy", "z") -> "zzzz", true

replace ¶

replace :: proc "odin" (s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {…}
 

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

if n < 0, no limit on the number of replacements
returns the `output` string and true when an a allocation through a replace happened

strings.replace("xyzxyz", "xyz", "abc", 2) -> "abcabc", true
strings.replace("xyzxyz", "xyz", "abc", 1) -> "abcxyz", true
strings.replace("xyzxyz", "abc", "xyz", -1) -> "xyzxyz", false
strings.replace("xyzxyz", "xy", "z", -1) -> "zzzz", true

reset_builder ¶

reset_builder :: proc "odin" (b: ^Builder) {…}
 

clear the builder byte buffer content

reverse ¶

reverse :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

returns a reversed version of the s string

a := "abcxyz"
b := strings.reverse(a)
fmt.eprintln(a, b) // abcxyz zyxcba

right_justify ¶

right_justify :: proc "odin" (str: string, length: int, pad: string, allocator := context.allocator) -> string {…}
 

right_justify returns a string with a pad string at right side if the str's rune length is smaller than length

rune_count ¶

rune_count :: proc "odin" (path: string) -> int {…}
 

returns the utf8 rune count of the string s

strings.rune_count("test") -> 4
strings.rune_count("testö") -> 5, where len("testö") -> 6

scrub ¶

scrub :: proc "odin" (s, replacement: string, allocator := context.allocator) -> string {…}
 

scrub scruvs invalid utf-8 characters and replaces them with the replacement string Adjacent invalid bytes are only replaced once

split_after ¶

split_after :: proc "odin" (s, sep: string, allocator := context.allocator) -> []string {…}
 

splits the string s after the seperator string sep appears

returns the slice of split strings allocated using `allocator`

a := "aaa.bbb.ccc.ddd.eee"
aa := strings.split_after(a, ".")
fmt.eprintln(aa) // [aaa., bbb., ccc., ddd., eee]

split_after_iterator ¶

split_after_iterator :: proc "odin" (s: ^string, sep: string) -> (string, bool) {…}
 

split the ^string s after every seperator string sep in an iterator fashion

consumes the original string till the end

text := "a.b.c.d.e"
for str in strings.split_after_iterator(&text, ".") {
	fmt.eprintln(str) // every loop -> a. b. c. d. e
}

split_after_n ¶

split_after_n :: proc "odin" (s, sep: string, n: int, allocator := context.allocator) -> []string {…}
 

splits the string s after the seperator string sep appears into a total of n parts

returns the slice of split strings allocated using `allocator`

a := "aaa.bbb.ccc.ddd.eee"
aa := strings.split_after(a, ".")
fmt.eprintln(aa) // [aaa., bbb., ccc., ddd., eee]

split_by_byte_iterator ¶

split_by_byte_iterator :: proc "odin" (s: ^string, sep: u8) -> (res: string, ok: bool) {…}
 

split the ^string s by the byte seperator sep in an iterator fashion

consumes the original string till the end, leaving the string `s` with len == 0

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

split_iterator ¶

split_iterator :: proc "odin" (s: ^string, sep: string) -> (string, bool) {…}
 

split the ^string s by the seperator string sep in an iterator fashion

consumes the original string till the end

text := "a.b.c.d.e"
for str in strings.split_iterator(&text, ".") {
	fmt.eprintln(str) // every loop -> a b c d e
}

split ¶

split :: proc "odin" (s, sep: string, allocator := context.allocator) -> []string {…}
 

Splits a string into parts, based on a separator.

Returned strings are substrings of 's'.
```
s := "aaa.bbb.ccc.ddd.eee" // 5 parts
ss := split(s, ".")
fmt.println(ss)            // [aaa, bbb, ccc, ddd, eee]
```

split_lines ¶

split_lines :: proc "odin" (path: string, allocator := context.allocator) -> []string {…}
 

split the string s at every line break '\n'

return an allocated slice of strings

a := "a\nb\nc\nd\ne"
b := strings.split_lines(a)
fmt.eprintln(b) // [a, b, c, d, e]

split_lines_after_iterator ¶

split_lines_after_iterator :: proc "odin" (s: ^string) -> (line: string, ok: bool) {…}
 

split the string s at every line break '\n'

returns the current split string every iteration till the string is consumed

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

split_lines_after ¶

split_lines_after :: proc "odin" (path: string, allocator := context.allocator) -> []string {…}
 

split the string s at every line break '\n' leaving the '\n' in the resulting strings

return an allocated slice of strings

a := "a\nb\nc\nd\ne"
b := strings.split_lines_after(a)
fmt.eprintln(b) // [a\n, b\n, c\n, d\n, e\n]

split_lines_after_n ¶

split_lines_after_n :: proc "odin" (s: string, n: int, allocator := context.allocator) -> []string {…}
 

split the string s at every line break '\n' leaving the '\n' in the resulting strings

only runs for `n` parts
return an allocated slice of strings

a := "a\nb\nc\nd\ne"
b := strings.split_lines_after_n(a, 3)
fmt.eprintln(b) // [a\n, b\n, c\n, d\ne\n]

split_lines_iterator ¶

split_lines_iterator :: proc "odin" (s: ^string) -> (line: string, ok: bool) {…}
 

split the string s at every line break '\n'

returns the current split string every iteration till the string is consumed

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

split_lines_n ¶

split_lines_n :: proc "odin" (s: string, n: int, allocator := context.allocator) -> []string {…}
 

split the string s at every line break '\n' for n parts

return an allocated slice of strings

a := "a\nb\nc\nd\ne"
b := strings.split_lines_n(a, 3)
fmt.eprintln(b) // [a, b, c, d\ne\n]

split_multi_init ¶

split_multi_init :: proc "odin" (s: string, substrs: []string) -> Split_Multi {…}
 

returns split multi state with sorted substrs

split_multi_iterate ¶

split_multi_iterate :: proc "odin" (using sm: ^Split_Multi) -> (res: string, ok: bool) {…}
 

splits the input string s by all possible substrs []string in an iterator fashion

returns the split string every iteration, the full string on no match

splits := [?]string { "---", "~~~", ".", "_", "," }
state := strings.split_multi_init("testing,this.out_nice---done~~~last", splits[:])
for str in strings.split_multi_iterate(&state) {
	fmt.eprintln(str) // every iteration -> [testing, this, out, nice, done, last]
}

split_multi ¶

split_multi :: proc "odin" (s: string, substrs: []string, allocator := context.allocator) -> (buf: []string) {…}
 

splits the input string s by all possible substrs []string

returns the allocated []string, nil on any empty substring or no matches

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

split_n ¶

split_n :: proc "odin" (s, sep: string, n: int, allocator := context.allocator) -> []string {…}
 

Splits a string into a total of 'n' parts, based on a separator.

Returns fewer parts if there wasn't enough occurrences of the separator.
Returned strings are substrings of 's'.
```
s := "aaa.bbb.ccc.ddd.eee" // 5 parts present
ss := split_n(s, ".", 3)   // total of 3 wanted
fmt.println(ss)            // [aaa, bbb, ccc.ddd.eee]
```

string_case_iterator ¶

string_case_iterator :: proc "odin" (w: io.Writer, s: string, callback: proc "odin" (w: io.Writer, prev, curr, next: rune)) {…}
 

iterator that loops through the string and calls the callback with the prev, curr and next rune

on empty string `s` the callback gets called once with empty runes

string_from_nul_terminated_ptr ¶

string_from_nul_terminated_ptr :: proc "odin" (ptr: ^u8, len: int) -> string {…}
 

returns a string from a byte pointer `ptr and byte length `len` searches for a nul byte from 0..len will be the end size

string_from_ptr ¶

string_from_ptr :: proc "odin" (ptr: ^u8, len: int) -> string {…}
 

returns a string from a byte pointer ptr and byte length len the string is valid as long as the parameters stay alive

to_ada_case ¶

to_ada_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "Ada_case"

to_byte_reader ¶

to_byte_reader :: proc "odin" (r: ^Reader, s: string) -> io.Byte_Reader {…}
 

init a reader to the string s and return an io.Byte_Reader

to_camel_case ¶

to_camel_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "lowerCamelCase"

to_delimiter_case ¶

to_delimiter_case :: proc "odin" (s: string, delimiter: rune, all_upper_case: bool, allocator := context.allocator) -> string {…}
 

returns the s string to words seperated by the given delimiter rune

all runes will be upper or lowercased based on the `all_uppercase` bool

strings.to_delimiter_case("Hello World", '_', false) -> hello_world
strings.to_delimiter_case("Hello World", ' ', true) -> HELLO WORLD
strings.to_delimiter_case("Hello World", ' ', true) -> HELLO WORLD
strings.to_delimiter_case("aBC", '_', false) -> a_b_c

to_kebab_case ¶

to_kebab_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "kebab-case" with all runes lowercased

to_lower ¶

to_lower :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

returns the input string s with all runes set to lowered case

always allocates using the `allocator`

strings.to_lower("test") -> test
strings.to_lower("Test") -> test

to_lower_camel_case ¶

to_lower_camel_case :: to_camel_case
 

converts the s string to "lowerCamelCase"

to_pascal_case ¶

to_pascal_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "PascalCase"

to_reader_at ¶

to_reader_at :: proc "odin" (r: ^Reader, s: string) -> io.Reader_At {…}
 

init a reader to the string s and return an io.Reader_At

to_reader ¶

to_reader :: proc "odin" (r: ^Reader, s: string) -> io.Reader {…}
 

init a reader to the string s and return an io.Reader

to_rune_reader ¶

to_rune_reader :: proc "odin" (r: ^Reader, s: string) -> io.Rune_Reader {…}
 

init a reader to the string s and return an io.Rune_Reader

to_screaming_snake_case ¶

to_screaming_snake_case :: to_upper_snake_case
 

converts the s string to "SNAKE_CASE" with all runes uppercased

to_snake_case ¶

to_snake_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "snake_case" with all runes lowercased

strings.to_snake_case("HelloWorld") -> hello_world
strings.to_snake_case("Hello World") -> hello_world

to_stream ¶

to_stream :: proc "odin" (b: ^Builder) -> io.Stream {…}
 

return an io.Stream from a builder

to_string ¶

to_string :: proc "odin" (b: Builder) -> string {…}
 

cast the builder byte buffer to a string and return it

to_upper_camel_case ¶

to_upper_camel_case :: to_pascal_case
 

converts the s string to "PascalCase"

to_upper ¶

to_upper :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

returns the input string s with all runes set to upper case

always allocates using the `allocator`

strings.to_lower("test") -> TEST
strings.to_lower("Test") -> TEST

to_upper_kebab_case ¶

to_upper_kebab_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "KEBAB-CASE" with all runes uppercased

to_upper_snake_case ¶

to_upper_snake_case :: proc "odin" (path: string, allocator := context.allocator) -> string {…}
 

converts the s string to "SNAKE_CASE" with all runes uppercased

to_valid_utf8 ¶

to_valid_utf8 :: proc "odin" (s, replacement: string, allocator := context.allocator) -> string {…}

to_writer ¶

to_writer :: proc "odin" (b: ^Builder) -> io.Writer {…}
 

return an io.Writer from a builder

trim_left ¶

trim_left :: proc "odin" (s: string, cutset: string) -> string {…}
 

trims the cutset string from the s string

trim_left_null ¶

trim_left_null :: proc "odin" (path: string) -> string {…}
 

trims nul runes from the left: "\x00\x00testing\x00\x00" -> "testing\x00\x00"

trim_left_proc_with_state ¶

trim_left_proc_with_state :: proc "odin" (s: string, p: proc "odin" (state: rawptr, r: rune) -> bool, state: rawptr) -> string {…}
 

trims the input string s until the procedure p with state returns false

returns an empty string when no match was found at all

trim_left_proc ¶

trim_left_proc :: proc "odin" (s: string, p: proc "odin" (r: rune) -> bool) -> string {…}
 

trims the input string s until the procedure p returns false

does not allocate - only returns a cut variant of the input string
returns an empty string when no match was found at all

find :: proc(r: rune) -> bool {
	return r != 'i'
}
strings.trim_left_proc("testing", find) -> "ing"

trim_left_space ¶

trim_left_space :: proc "odin" (path: string) -> string {…}
 

trims until a valid non space rune: "\t\txyz\t\t" -> "xyz\t\t"

trim_null ¶

trim_null :: proc "odin" (path: string) -> string {…}
 

trims nul runes from both sides: "\x00\x00testing\x00\x00" -> "testing"

trim_prefix ¶

trim_prefix :: proc "odin" (s: string, cutset: string) -> string {…}
 

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

returns the input string `s` when no prefix was found

strings.trim_prefix("testing", "test") -> "ing"
strings.trim_prefix("testing", "abc") -> "testing"

trim ¶

trim :: proc "odin" (s: string, cutset: string) -> string {…}
 

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

trim_right ¶

trim_right :: proc "odin" (s: string, cutset: string) -> string {…}
 

trims the cutset string from the s string from the right

trim_right_null ¶

trim_right_null :: proc "odin" (path: string) -> string {…}
 

trims nul runes from the right: "\x00\x00testing\x00\x00" -> "\x00\x00testing"

trim_right_proc_with_state ¶

trim_right_proc_with_state :: proc "odin" (s: string, p: proc "odin" (state: rawptr, r: rune) -> bool, state: rawptr) -> string {…}
 

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

returns an empty string when no match was found at all

trim_right_proc ¶

trim_right_proc :: proc "odin" (s: string, p: proc "odin" (r: rune) -> bool) -> string {…}
 

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

does not allocate - only returns a cut variant of the input string
returns an empty string when no match was found at all

find :: proc(r: rune) -> bool {
	return r != 't'
}
strings.trim_left_proc("testing", find) -> "test"

trim_right_space ¶

trim_right_space :: proc "odin" (path: string) -> string {…}
 

trims from the right until a valid non space rune: "\t\txyz\t\t" -> "\t\txyz"

trim_space ¶

trim_space :: proc "odin" (path: string) -> string {…}
 

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

trim_suffix ¶

trim_suffix :: proc "odin" (s: string, cutset: string) -> string {…}
 

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

returns the input string `s` when no suffix was found

strings.trim_suffix("todo.txt", ".txt") -> "todo"
strings.trim_suffix("todo.doc", ".txt") -> "todo.doc"

truncate_to_byte ¶

truncate_to_byte :: proc "odin" (str: string, b: u8) -> string {…}
 

returns a string truncated to the first time it finds the byte b uses the len of the string str when it couldn't find the input

truncate_to_rune ¶

truncate_to_rune :: proc "odin" (str: string, r: rune) -> string {…}
 

returns a string truncated to the first time it finds the rune r uses the len of the string str when it couldn't find the input

unsafe_string_to_cstring ¶

unsafe_string_to_cstring :: proc "odin" (str: string) -> cstring {…}
 

returns the transmute of string str to a cstring not safe since the origin string may not contain a nul byte

write_byte ¶

write_byte :: proc "odin" (b: ^Builder, x: u8) -> (n: int) {…}
 

appends a byte to the builder, returns the append diff

builder := strings.make_builder()
strings.write_byte(&builder, 'a') // 1
strings.write_byte(&builder, 'b') // 1
strings.write_byte(&builder, 'c') // 1
fmt.println(strings.to_string(builder)) // -> abc

write_bytes ¶

write_bytes :: proc "odin" (b: ^Builder, x: []u8) -> (n: int) {…}
 

appends a slice of bytes to the builder, returns the append diff

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

write_encoded_rune_builder ¶

write_encoded_rune_builder :: proc "odin" (b: ^Builder, r: rune, write_quote: bool = true) -> (n: int) {…}
 

appends a rune to the builder, optional write_quote boolean tag, returns the written rune size

write_encoded_rune_writer ¶

write_encoded_rune_writer :: proc "odin" (w: io.Writer, r: rune, write_quote: bool = true) -> (n: int) {…}

write_escaped_rune_builder ¶

write_escaped_rune_builder :: proc "odin" (b: ^Builder, r: rune, quote: u8, html_safe: bool = false) -> (n: int) {…}
 

appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such when r and quote match and quote is \\ - they will be written as two slashes html_safe flag in case the runes '<', '>', '&' should be encoded as digits e.g. \u0026

write_escaped_rune_writer ¶

write_escaped_rune_writer :: proc "odin" (w: io.Writer, r: rune, quote: u8, html_safe: bool = false) -> (n: int) {…}

write_i64 ¶

write_i64 :: proc "odin" (b: ^Builder, i: i64, base: int = 10) -> (n: int) {…}
 

writes a i64 value i in base = 10 into the builder, returns the written amount of characters

write_int ¶

write_int :: proc "odin" (b: ^Builder, i: int, base: int = 10) -> (n: int) {…}
 

writes a int value i in base = 10 into the builder, returns the written amount of characters

write_quoted_rune_builder ¶

write_quoted_rune_builder :: proc "odin" (b: ^Builder, r: rune) -> (n: int) {…}
 

appends a quoted rune into the builder, returns written size

builder := strings.make_builder()
strings.write_string(&builder, "abc") // 3
strings.write_quoted_rune_builder(&builder, 'ä') // 4
strings.write_string(&builder, "abc") // 3
fmt.println(strings.to_string(builder)) // -> abc'ä'abc

write_quoted_rune ¶

write_quoted_rune :: proc "odin" (w: io.Writer, r: rune) -> (n: int) {…}
 

writer append a quoted rune into the byte buffer, return the written size

write_quoted_string_builder ¶

write_quoted_string_builder :: proc "odin" (b: ^Builder, str: string, quote: u8 = '"') -> (n: int) {…}
 

append a quoted string into the builder, return the written byte size

builder := strings.make_builder()
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_quoted_string_writer ¶

write_quoted_string_writer :: proc "odin" (w: io.Writer, str: string, quote: u8 = '"') -> (n: int) {…}

write_rune_builder ¶

write_rune_builder :: proc "odin" (b: ^Builder, r: rune) -> (int, io.Error) {…}
 

appends a single rune into the builder, returns written rune size and an io.Error

builder := strings.make_builder()
strings.write_rune_builder(&builder, 'ä') // 2 None
strings.write_rune_builder(&builder, 'b') // 1 None
strings.write_rune_builder(&builder, 'c') // 1 None
fmt.println(strings.to_string(builder)) // -> äbc

write_string_builder ¶

write_string_builder :: proc "odin" (b: ^Builder, s: string) -> (n: int) {…}
 

appends a string to the builder, return the written byte size

builder := strings.make_builder()
strings.write_string(&builder, "a") // 1
strings.write_string(&builder, "bc") // 2
strings.write_string(&builder, "xyz") // 3
fmt.println(strings.to_string(builder)) // -> abcxyz

write_string_writer ¶

write_string_writer :: proc "odin" (w: io.Writer, s: string) -> (n: int) {…}
 

appends a string to the writer

write_u64 ¶

write_u64 :: proc "odin" (b: ^Builder, i: u64, base: int = 10) -> (n: int) {…}
 

writes a u64 value i in base = 10 into the builder, returns the written amount of characters

write_uint ¶

write_uint :: proc "odin" (b: ^Builder, i: uint, base: int = 10) -> (n: int) {…}
 

writes a uint value i in base = 10 into the builder, returns the written amount of characters

Procedure Groups

clone_from ¶

 

overload to clone from a string, []byte, cstring or a ^byte + length to a string

init_builder ¶

 

overload simple init_builder_* with or without len / ap parameters

make_builder ¶

 

overload simple make_builder_* with or without len / cap parameters

write_encoded_rune ¶

write_encoded_rune :: proc{
	write_encoded_rune_builder,
	write_encoded_rune_writer,
}
 

overload for write_encoded_rune_*

write_escaped_rune ¶

write_escaped_rune :: proc{
	write_escaped_rune_builder,
	write_escaped_rune_writer,
}
 

overload for write_escaped_rune_*

write_quoted_string ¶

write_quoted_string :: proc{
	write_quoted_string_builder,
	write_quoted_string_writer,
}
 

overload for write_quoted_string_* variants

write_string ¶

write_string :: proc{
	write_string_builder,
	write_string_writer,
}
 

overload for write_string_* variants

Source Files

Generation Information

Generated with odin version dev-2022-07 (vendor "odin") Windows_amd64 @ 2022-07-01 21:08:58.656203900 +0000 UTC