diff options
| author | Lucas Perlind <perlindluca@gmail.com> | 2023-04-03 08:33:24 +1000 |
|---|---|---|
| committer | Lucas Perlind <perlindluca@gmail.com> | 2023-04-03 08:44:14 +1000 |
| commit | 67e6f571925a07d25b5b3bef2ecb22a30afb6508 (patch) | |
| tree | b12a01775f35d2aaec40265092f91b4fbd0430c9 /core/strings/strings.odin | |
| parent | 6ff0cc0b400524232cdce44f5b2a68b399ba4adc (diff) | |
Small improvements strings documentation
* Use new 'Returns:' and 'Inputs:' keywords used by the website generator
* Make order item order resemble website, i.e. 'Returns:' comes before
'Example:'
* Add a few missing input items
* Add a few missing return items
Diffstat (limited to 'core/strings/strings.odin')
| -rw-r--r-- | core/strings/strings.odin | 542 |
1 files changed, 305 insertions, 237 deletions
diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 8cb046bd6..3c55374b7 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -11,12 +11,12 @@ Clones a string *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to be cloned - allocator: (default: context.allocator) - loc: The caller location for debugging purposes (default: #caller_location) -**Returns** +Returns: A cloned string */ clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> string { @@ -29,12 +29,12 @@ Clones a string safely (returns early with an allocation error on failure) *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to be cloned - allocator: (default: context.allocator) - loc: The caller location for debugging purposes (default: #caller_location) -**Returns** +Returns: - str: A cloned string - err: A mem.Allocator_Error if an error occurs during allocation */ @@ -48,12 +48,12 @@ Clones a string and appends a null-byte to make it a cstring *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to be cloned - allocator: (default: context.allocator) - loc: The caller location for debugging purposes (default: #caller_location) -**Returns** +Returns: A cloned cstring with an appended null-byte */ clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> cstring { @@ -65,29 +65,29 @@ clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #call /* Transmutes a raw pointer into a string. Non-allocating. -**Inputs** +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** +Returns: A string created from the byte pointer and length */ string_from_ptr :: proc(ptr: ^byte, len: int) -> string { return transmute(string)mem.Raw_String{ptr, len} } /* -Transmutes a raw pointer (null-terminated) into a string. Non-allocating. Searches for a null-byte from `0..<len`, otherwhise `len` will be the end size +Transmutes a raw pointer (null-terminated) into a string. Non-allocating. Searches for a null-byte from `0..<len`, otherwise `len` will be the end size NOTE: The created string is only valid as long as the pointer and length are valid. The string is truncated at the first null-byte encountered. -**Inputs** +Inputs: - ptr: A pointer to the start of the null-terminated byte sequence - len: The length of the byte sequence -**Returns** +Returns: A string created from the null-terminated byte pointer and length */ string_from_null_terminated_ptr :: proc(ptr: ^byte, len: int) -> string { @@ -98,10 +98,10 @@ string_from_null_terminated_ptr :: proc(ptr: ^byte, len: int) -> string { /* Gets the raw byte pointer for the start of a string `str` -**Inputs** +Inputs: - str: The input string -**Returns** +Returns: A pointer to the start of the string's bytes */ ptr_from_string :: proc(str: string) -> ^byte { @@ -111,12 +111,12 @@ ptr_from_string :: proc(str: string) -> ^byte { /* Converts a string `str` to a cstring -**Inputs** +Inputs: - str: The input string WARNING: This is unsafe because the original string may not contain a null-byte. -**Returns** +Returns: The converted cstring */ unsafe_string_to_cstring :: proc(str: string) -> cstring { @@ -126,13 +126,13 @@ unsafe_string_to_cstring :: proc(str: string) -> cstring { /* Truncates a string `str` at the first occurrence of char/byte `b` -**Inputs** +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** +Returns: The truncated string */ truncate_to_byte :: proc(str: string, b: byte) -> string { @@ -145,11 +145,11 @@ truncate_to_byte :: proc(str: string, b: byte) -> string { /* Truncates a string `str` at the first occurrence of rune `r` as a slice of the original, entire string if not found -**Inputs** +Inputs: - str: The input string - r: The rune to truncate the string at -**Returns** +Returns: The truncated string */ truncate_to_rune :: proc(str: string, r: rune) -> string { @@ -164,12 +164,12 @@ Clones a byte array `s` and appends a null-byte *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The byte array to be cloned - allocator: (default: context.allocator) - loc: The caller location for debugging purposes (default: `#caller_location`) -**Returns** +Returns: A cloned string from the byte array with a null-byte */ clone_from_bytes :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> string { @@ -183,12 +183,12 @@ Clones a cstring `s` as a string *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The cstring to be cloned - allocator: (default: context.allocator) - loc: The caller location for debugging purposes (default: `#caller_location`) -**Returns** +Returns: A cloned string from the cstring */ clone_from_cstring :: proc(s: cstring, allocator := context.allocator, loc := #caller_location) -> string { @@ -199,7 +199,7 @@ Clones a string from a byte pointer `ptr` and a byte length `len` *Allocates Using Provided Allocator* -**Inputs** +Inputs: - ptr: A pointer to the start of the byte sequence - len: The length of the byte sequence - allocator: (default: context.allocator) @@ -207,7 +207,7 @@ Clones a string from a byte pointer `ptr` and a byte length `len` NOTE: Same as `string_from_ptr`, but perform an additional `clone` operation -**Returns** +Returns: A cloned string from the byte pointer and length */ clone_from_ptr :: proc(ptr: ^byte, len: int, allocator := context.allocator, loc := #caller_location) -> string { @@ -226,7 +226,7 @@ Clones a string from a null-terminated cstring `ptr` and a byte length `len` *Allocates Using Provided Allocator* -**Inputs** +Inputs: - ptr: A pointer to the start of the null-terminated cstring - len: The byte length of the cstring - allocator: (default: context.allocator) @@ -234,7 +234,7 @@ Clones a string from a null-terminated cstring `ptr` and a byte length `len` NOTE: Truncates at the first null-byte encountered or the byte length. -**Returns** +Returns: A cloned string from the null-terminated cstring and byte length */ clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> string { @@ -246,11 +246,11 @@ clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context. Compares two strings, returning a value representing which one comes first lexicographically. -1 for `lhs`; 1 for `rhs`, or 0 if they are equal. -**Inputs** +Inputs: - lhs: First string for comparison - rhs: Second string for comparison -**Returns** +Returns: -1 if `lhs` comes first, 1 if `rhs` comes first, or 0 if they are equal */ compare :: proc(lhs, rhs: string) -> int { @@ -259,11 +259,11 @@ compare :: proc(lhs, rhs: string) -> int { /* Returns the byte offset of the rune `r` in the string `s`, -1 when not found -**Inputs** +Inputs: - s: The input string - r: The rune to search for -**Returns** +Returns: The byte offset of the rune `r` in the string `s`, or -1 if not found */ contains_rune :: proc(s: string, r: rune) -> int { @@ -277,10 +277,13 @@ contains_rune :: proc(s: string, r: rune) -> int { /* Returns true when the string `substr` is contained inside the string `s` -**Inputs** +Inputs: - s: The input string - substr: The substring to search for +Returns: +`true` if `substr` is contained inside the string `s`, `false` otherwise + Example: import "core:fmt" @@ -298,8 +301,6 @@ Output: true false -**Returns** -`true` if `substr` is contained inside the string `s`, `false` otherwise */ contains :: proc(s, substr: string) -> bool { return index(s, substr) >= 0 @@ -307,10 +308,13 @@ contains :: proc(s, substr: string) -> bool { /* Returns `true` when the string `s` contains any of the characters inside the string `chars` -**Inputs** +Inputs: - s: The input string - chars: The characters to search for +Returns: +`true` if the string `s` contains any of the characters in `chars`, `false` otherwise + Example: import "core:fmt" @@ -330,8 +334,6 @@ Output: true false -**Returns** -`true` if the string `s` contains any of the characters in `chars`, `false` otherwise */ contains_any :: proc(s, chars: string) -> bool { return index_any(s, chars) >= 0 @@ -339,9 +341,12 @@ contains_any :: proc(s, chars: string) -> bool { /* Returns the UTF-8 rune count of the string `s` -**Inputs** +Inputs: - s: The input string +Returns: +The UTF-8 rune count of the string `s` + Example: import "core:fmt" @@ -357,8 +362,6 @@ Output: 4 5 -**Returns** -The UTF-8 rune count of the string `s` */ rune_count :: proc(s: string) -> int { return utf8.rune_count_in_string(s) @@ -367,10 +370,13 @@ rune_count :: proc(s: string) -> int { Returns whether the strings `u` and `v` are the same alpha characters, ignoring different casings Works with UTF-8 string content -**Inputs** +Inputs: - u: The first string for comparison - v: The second string for comparison +Returns: +`true` if the strings `u` and `v` are the same alpha characters (ignoring case) + Example: import "core:fmt" @@ -390,8 +396,6 @@ Output: true false -**Returns** -`true` if the strings `u` and `v` are the same alpha characters (ignoring case) */ equal_fold :: proc(u, v: string) -> bool { s, t := u, v @@ -438,10 +442,13 @@ equal_fold :: proc(u, v: string) -> bool { /* Returns the prefix length common between strings `a` and `b` -**Inputs** +Inputs: - a: The first input string - b: The second input string +Returns: +The prefix length common between strings `a` and `b` + Example: import "core:fmt" @@ -461,8 +468,6 @@ Output: 2 0 -**Returns** -The prefix length common between strings `a` and `b` */ prefix_length :: proc(a, b: string) -> (n: int) { _len := min(len(a), len(b)) @@ -490,10 +495,13 @@ prefix_length :: proc(a, b: string) -> (n: int) { /* Determines if a string `s` starts with a given `prefix` -**Inputs** +Inputs: - s: The string to check for the `prefix` - prefix: The prefix to look for +Returns: +`true` if the string `s` starts with the `prefix`, otherwise `false` + Example: import "core:fmt" @@ -513,8 +521,6 @@ Output: true false -**Returns** -`true` if the string `s` starts with the `prefix`, otherwise `false` */ has_prefix :: proc(s, prefix: string) -> bool { return len(s) >= len(prefix) && s[0:len(prefix)] == prefix @@ -522,6 +528,13 @@ has_prefix :: proc(s, prefix: string) -> bool { /* Determines if a string `s` ends with a given `suffix` +Inputs: +- s: The string to check for the `suffix` +- suffix: The suffix to look for + +Returns: +`true` if the string `s` ends with the `suffix`, otherwise `false` + Example: import "core:fmt" @@ -539,12 +552,6 @@ Output: false true -**Inputs** -- s: The string to check for the `suffix` -- suffix: The suffix to look for - -**Returns** -`true` if the string `s` ends with the `suffix`, otherwise `false` */ has_suffix :: proc(s, suffix: string) -> bool { return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix @@ -554,6 +561,14 @@ Joins a slice of strings `a` with a `sep` string *Allocates Using Provided Allocator* +Inputs: +- a: A slice of strings to join +- sep: The separator string +- allocator: (default is context.allocator) + +Returns: +A combined string from the slice of strings `a` separated with the `sep` string + Example: import "core:fmt" @@ -572,13 +587,6 @@ Output: a-b-c a...b...c -**Inputs** -- a: A slice of strings to join -- sep: The separator string -- allocator: (default is context.allocator) - -**Returns** -A combined string from the slice of strings `a` separated with the `sep` string */ join :: proc(a: []string, sep: string, allocator := context.allocator) -> string { if len(a) == 0 { @@ -603,12 +611,12 @@ Joins a slice of strings `a` with a `sep` string, returns an error on allocation *Allocates Using Provided Allocator* -**Inputs** +Inputs: - a: A slice of strings to join - sep: The separator string - allocator: (default is context.allocator) -**Returns** +Returns: - str: A combined string from the slice of strings `a` separated with the `sep` string - err: An error if allocation failed, otherwise `nil` */ @@ -635,10 +643,13 @@ Returns a combined string from the slice of strings `a` without a separator *Allocates Using Provided Allocator* -**Inputs** +Inputs: - a: A slice of strings to concatenate - allocator: (default is context.allocator) +Returns: +The concatenated string + Example: import "core:fmt" @@ -653,8 +664,6 @@ Output: abc -**Returns** -The concatenated string */ concatenate :: proc(a: []string, allocator := context.allocator) -> string { if len(a) == 0 { @@ -677,11 +686,11 @@ Returns a combined string from the slice of strings `a` without a separator, or *Allocates Using Provided Allocator* -**Inputs** +Inputs: - a: A slice of strings to concatenate - allocator: (default is context.allocator) -**Returns** +Returns: The concatenated string, and an error if allocation fails */ concatenate_safe :: proc(a: []string, allocator := context.allocator) -> (res: string, err: mem.Allocator_Error) { @@ -705,12 +714,15 @@ Returns a substring of the input string `s` with the specified rune offset and l *Allocates Using Provided Allocator* -**Inputs** +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) +Returns: +The substring + Example: import "core:fmt" @@ -728,8 +740,6 @@ Output: me example -**Returns** -The substring */ cut :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator) -> (res: string) { s := s; rune_length := rune_length @@ -789,7 +799,7 @@ Splits the input string `s` into a slice of substrings separated by the specifie *Used Internally - Private Function* -**Inputs** +Inputs: - s: The input string to split - sep: The separator string - sep_save: A flag determining if the separator should be saved in the resulting substrings @@ -798,7 +808,7 @@ Splits the input string `s` into a slice of substrings separated by the specifie NOTE: Allocation occurs for the array, the splits are all views of the original string. -**Returns** +Returns: A slice of substrings */ @private @@ -853,11 +863,15 @@ Splits a string into parts based on a separator. *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to split. - sep: The separator string used to split the input string. - allocator: (default is context.allocator). +Returns: A slice of strings, each representing a part of the split string. + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -873,9 +887,6 @@ Output: ["aaa", "bbb", "ccc", "ddd", "eee"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** A slice of strings, each representing a part of the split string. */ split :: proc(s, sep: string, allocator := context.allocator) -> []string { return _split(s, sep, 0, -1, allocator) @@ -885,11 +896,15 @@ Splits a string into parts based on a separator. If n < count of seperators, the *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to split. - sep: The separator string used to split the input string. - allocator: (default is context.allocator) +Returns: A slice of strings, each representing a part of the split string. + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -905,9 +920,6 @@ Output: ["aaa", "bbb", "ccc.ddd.eee"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** A slice of strings, each representing a part of the split string. */ split_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string { return _split(s, sep, 0, n, allocator) @@ -917,11 +929,16 @@ Splits a string into parts after the separator, retaining it in the substrings. *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to split. - sep: The separator string used to split the input string. - allocator: (default is context.allocator). +Returns: +A slice of strings, each representing a part of the split string after the separator. + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -937,10 +954,6 @@ Output: ["aaa.", "bbb.", "ccc.", "ddd.", "eee"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** -A slice of strings, each representing a part of the split string after the separator. */ split_after :: proc(s, sep: string, allocator := context.allocator) -> []string { return _split(s, sep, len(sep), -1, allocator) @@ -950,12 +963,17 @@ Splits a string into a total of `n` parts after the separator. *Allocates Using Provided Allocator* -**Inputs** +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) +Returns: +A slice of strings with `n` parts or fewer if there weren't + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -971,10 +989,6 @@ Output: ["aaa.", "bbb.", "ccc.ddd.eee"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** -A slice of strings with `n` parts or fewer if there weren't */ split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string { return _split(s, sep, len(sep), n, allocator) @@ -985,12 +999,12 @@ up to (but not including) the separator, as well as a boolean indicating success *Used Internally - Private Function* -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. - sep: The separator string to search for. - sep_save: Number of characters from the separator to include in the result. -**Returns** +Returns: A tuple containing the resulting substring and a boolean indicating success. */ @private @@ -1023,10 +1037,13 @@ _split_iterator :: proc(s: ^string, sep: string, sep_save: int) -> (res: string, /* Splits the input string by the byte separator in an iterator fashion. -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. - sep: The byte separator to search for. +Returns: +A tuple containing the resulting substring and a boolean indicating success. + Example: import "core:fmt" @@ -1047,8 +1064,6 @@ Output: d e -**Returns** -A tuple containing the resulting substring and a boolean indicating success. */ split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool) { m := index_byte(s^, sep) @@ -1068,10 +1083,13 @@ split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool) { Splits the input string by the separator string in an iterator fashion. Destructively consumes the original string until the end. -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. - sep: The separator string to search for. +Returns: +A tuple containing the resulting substring and a boolean indicating success. + Example: import "core:fmt" @@ -1092,8 +1110,6 @@ Output: d e -**Returns** -A tuple containing the resulting substring and a boolean indicating success. */ split_iterator :: proc(s: ^string, sep: string) -> (string, bool) { return _split_iterator(s, sep, 0) @@ -1102,10 +1118,13 @@ split_iterator :: proc(s: ^string, sep: string) -> (string, bool) { Splits the input string after every separator string in an iterator fashion. Destructively consumes the original string until the end. -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. - sep: The separator string to search for. +Returns: +A tuple containing the resulting substring and a boolean indicating success. + Example: import "core:fmt" @@ -1126,8 +1145,6 @@ Output: d. e -**Returns** -A tuple containing the resulting substring and a boolean indicating success. */ split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) { return _split_iterator(s, sep, len(sep)) @@ -1137,10 +1154,10 @@ Trims the carriage return character from the end of the input string. *Used Internally - Private Function* -**Inputs** +Inputs: - s: The input string to trim. -**Returns** +Returns: The trimmed string as a slice of the original. */ @(private) @@ -1158,10 +1175,13 @@ Splits the input string at every line break `\n`. *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string to split. - allocator: (default is context.allocator) +Returns: +A slice (allocated) of the split string (slices into original string) + Example: import "core:fmt" @@ -1177,8 +1197,6 @@ Output: ["a", "b", "c", "d", "e"] -**Returns** -A slice (allocated) of the split string (slices into original string) */ split_lines :: proc(s: string, allocator := context.allocator) -> []string { sep :: "\n" @@ -1193,11 +1211,16 @@ Splits the input string at every line break `\n` for `n` parts. *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string to split. - n: The number of parts to split into. - allocator: (default is context.allocator) +Returns: +A slice (allocated) of the split string (slices into original string) + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -1213,10 +1236,6 @@ Output: ["a", "b", "c\nd\ne"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** -A slice (allocated) of the split string (slices into original string) */ split_lines_n :: proc(s: string, n: int, allocator := context.allocator) -> []string { sep :: "\n" @@ -1231,10 +1250,15 @@ Splits the input string at every line break `\n` leaving the `\n` in the resulti *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string to split. - allocator: (default is context.allocator) +Returns: +A slice (allocated) of the split string (slices into original string), with `\n` included. + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -1250,10 +1274,6 @@ Output: ["a\n", "b\n", "c\n", "d\n", "e"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** -A slice (allocated) of the split string (slices into original string), with `\n` included. */ split_lines_after :: proc(s: string, allocator := context.allocator) -> []string { sep :: "\n" @@ -1269,11 +1289,16 @@ Only runs for n parts. *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string to split. - n: The number of parts to split into. - allocator: (default is context.allocator) +Returns: +A slice (allocated) of the split string (slices into original string), with `\n` included. + +NOTE: Allocation occurs for the array, the splits are all views of the original string. + Example: import "core:fmt" @@ -1289,10 +1314,6 @@ Output: ["a\n", "b\n", "c\nd\ne"] -NOTE: Allocation occurs for the array, the splits are all views of the original string. - -**Returns** -A slice (allocated) of the split string (slices into original string), with `\n` included. */ split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) -> []string { sep :: "\n" @@ -1306,9 +1327,12 @@ split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) - Splits the input string at every line break `\n`. Returns the current split string every iteration until the string is consumed. -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. +Returns: +A tuple containing the resulting substring and a boolean indicating success. + Example: import "core:fmt" @@ -1326,8 +1350,6 @@ Output: abcde -**Returns** -A tuple containing the resulting substring and a boolean indicating success. */ split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) { sep :: "\n" @@ -1338,9 +1360,12 @@ split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) { Splits the input string at every line break `\n`. Returns the current split string with line breaks included every iteration until the string is consumed. -**Inputs** +Inputs: - s: Pointer to the input string, which is modified during the search. +Returns: +A tuple containing the resulting substring with line breaks included and a boolean indicating success. + Example: import "core:fmt" @@ -1361,8 +1386,6 @@ Output: d e -**Returns** -A tuple containing the resulting substring with line breaks included and a boolean indicating success. */ split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) { sep :: "\n" @@ -1373,10 +1396,13 @@ split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) { Returns the byte offset of the first byte `c` in the string s it finds, -1 when not found. NOTE: Can't find UTF-8 based runes. -**Inputs** +Inputs: - s: The input string to search in. - c: The byte to search for. +Returns: +The byte offset of the first occurrence of `c` in `s`, or -1 if not found. + Example: import "core:fmt" @@ -1396,8 +1422,6 @@ Output: -1 -1 -**Returns** -The byte offset of the first occurrence of `c` in `s`, or -1 if not found. */ index_byte :: proc(s: string, c: byte) -> int { for i := 0; i < len(s); i += 1 { @@ -1409,6 +1433,14 @@ index_byte :: proc(s: string, c: byte) -> int { } /* Returns the byte offset of the last byte `c` in the string `s`, -1 when not found. + +Inputs: +- s: The input string to search in. +- c: The byte to search for. + +Returns: +The byte offset of the last occurrence of `c` in `s`, or -1 if not found. + NOTE: Can't find UTF-8 based runes. Example: @@ -1430,8 +1462,6 @@ Output: -1 -1 -**Returns** -The byte offset of the last occurrence of `c` in `s`, or -1 if not found. */ last_index_byte :: proc(s: string, c: byte) -> int { for i := len(s)-1; i >= 0; i -= 1 { @@ -1445,6 +1475,13 @@ last_index_byte :: proc(s: string, c: byte) -> int { Returns the byte offset of the first rune `r` in the string `s` it finds, -1 when not found. Invalid runes return -1 +Inputs: +- s: The input string to search in. +- r: The rune to search for. + +Returns: +The byte offset of the first occurrence of `r` in `s`, or -1 if not found. + Example: import "core:fmt" @@ -1472,8 +1509,6 @@ Output: 6 7 -**Returns** -The byte offset of the first occurrence of `r` in `s`, or -1 if not found. */ index_rune :: proc(s: string, r: rune) -> int { switch { @@ -1500,6 +1535,13 @@ index_rune :: proc(s: string, r: rune) -> int { /* Returns the byte offset of the string `substr` in the string `s`, -1 when not found. +Inputs: +- s: The input string to search in. +- substr: The substring to search for. + +Returns: +The byte offset of the first occurrence of `substr` in `s`, or -1 if not found. + Example: import "core:fmt" @@ -1519,8 +1561,6 @@ Output: 2 -1 -**Returns** -The byte offset of the first occurrence of `substr` in `s`, or -1 if not found. */ index :: proc(s, substr: string) -> int { hash_str_rabin_karp :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) { @@ -1574,6 +1614,13 @@ index :: proc(s, substr: string) -> int { /* Returns the last byte offset of the string `substr` in the string `s`, -1 when not found. +Inputs: +- s: The input string to search in. +- substr: The substring to search for. + +Returns: +The byte offset of the last occurrence of `substr` in `s`, or -1 if not found. + Example: import "core:fmt" @@ -1593,8 +1640,6 @@ Output: 2 -1 -**Returns** -The byte offset of the last occurrence of `substr` in `s`, or -1 if not found. */ last_index :: proc(s, substr: string) -> int { hash_str_rabin_karp_reverse :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) { @@ -1646,6 +1691,13 @@ last_index :: proc(s, substr: string) -> int { /* Returns the index of any first char of `chars` found in `s`, -1 if not found. +Inputs: +- s: The input string to search in. +- chars: The characters to look for + +Returns: +The index of the first character of `chars` found in `s`, or -1 if not found. + Example: import "core:fmt" @@ -1667,8 +1719,6 @@ Output: 0 -1 -**Returns** -The index of the first character of `chars` found in `s`, or -1 if not found. */ index_any :: proc(s, chars: string) -> int { if chars == "" { @@ -1704,10 +1754,13 @@ index_any :: proc(s, chars: string) -> int { /* Finds the last occurrence of any character in `chars` within `s`. Iterates in reverse. -**Inputs** +Inputs: - s: The string to search in - chars: The characters to look for +Returns: +The index of the last matching character, or -1 if not found + Example: import "core:fmt" @@ -1729,8 +1782,6 @@ Output: 3 -1 -**Returns** -The index of the last matching character, or -1 if not found */ last_index_any :: proc(s, chars: string) -> int { if chars == "" { @@ -1783,11 +1834,11 @@ last_index_any :: proc(s, chars: string) -> int { /* Finds the first occurrence of any substring in `substrs` within `s` -**Inputs** +Inputs: - s: The string to search in - substrs: The substrings to look for -**Returns** +Returns: A tuple containing the index of the first matching substring, and its length (width) */ index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int) { @@ -1822,10 +1873,13 @@ index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int) { /* Counts the number of non-overlapping occurrences of `substr` in `s` -**Inputs** +Inputs: - s: The string to search in - substr: The substring to count +Returns: +The number of occurrences of `substr` in `s`, returns the rune_count + 1 of the string `s` on empty `substr` + Example: import "core:fmt" @@ -1847,8 +1901,6 @@ Output: 1 0 -**Returns** -The number of occurrences of `substr` in `s`, returns the rune_count + 1 of the string `s` on empty `substr` */ count :: proc(s, substr: string) -> int { if len(substr) == 0 { // special case @@ -1889,11 +1941,14 @@ Repeats the string `s` `count` times, concatenating the result *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to repeat - count: The number of times to repeat `s` - allocator: (default is context.allocator) +Returns: +The concatenated repeated string + WARNING: Panics if count < 0 Example: @@ -1909,8 +1964,6 @@ Output: abcabc -**Returns** -The concatenated repeated string */ repeat :: proc(s: string, count: int, allocator := context.allocator) -> string { if count < 0 { @@ -1932,12 +1985,15 @@ Replaces all occurrences of `old` in `s` with `new` *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The string to modify - old: The substring to replace - new: The substring to replace `old` with - allocator: The allocator to use for the new string (default is context.allocator) +Returns: +A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement + Example: import "core:fmt" @@ -1955,8 +2011,6 @@ Output: xyzxyz false zzzz true -**Returns** -A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement */ replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) { return replace(s, old, new, -1, allocator) @@ -1966,13 +2020,16 @@ Replaces n instances of old in the string s with the new string *Allocates Using Provided Allocator* -**Inputs** +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) +Returns: +A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement + Example: import "core:fmt" @@ -1992,8 +2049,6 @@ Output: xyzxyz false zzzz true -**Returns** -A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement */ replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) { if old == new || n == 0 { @@ -2039,12 +2094,15 @@ Removes the key string `n` times from the `s` string *Allocates Using Provided Allocator* -**Inputs** +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) +Returns: +A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal + Example: import "core:fmt" @@ -2064,8 +2122,6 @@ Output: bcbc true abcabc false -**Returns** -A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal */ remove :: proc(s, key: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) { return replace(s, key, "", n, allocator) @@ -2075,11 +2131,14 @@ Removes all the `key` string instances from the `s` string *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - key: The substring to be removed - allocator: (default: context.allocator) +Returns: +A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal + Example: import "core:fmt" @@ -2097,8 +2156,6 @@ Output: bcbc true abcabc false -**Returns** -A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal */ remove_all :: proc(s, key: string, allocator := context.allocator) -> (output: string, was_allocation: bool) { return remove(s, key, -1, allocator) @@ -2138,11 +2195,14 @@ is_null :: proc(r: rune) -> bool { /* Find the index of the first rune `r` in string `s` for which procedure `p` returns the same as truth, or -1 if no such rune appears. -**Inputs** +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: +The index of the first matching rune, or -1 if no match was found + Example: import "core:fmt" @@ -2167,8 +2227,6 @@ Output: 1 -1 -**Returns** -The index of the first matching rune, or -1 if no match was found */ index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int { for r, i in s { @@ -2214,10 +2272,13 @@ last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, sta /* Trims the input string `s` from the left until the procedure `p` returns false -**Inputs** +Inputs: - s: The input string - p: A procedure that takes a rune and returns a boolean +Returns: +The trimmed string as a slice of the original + Example: import "core:fmt" @@ -2234,8 +2295,6 @@ Output: testing -**Returns** -The trimmed string as a slice of the original */ trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string { i := index_proc(s, p, false) @@ -2247,12 +2306,12 @@ trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string { /* Trims the input string `s` from the left until the procedure `p` with state returns false -**Inputs** +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** +Returns: The trimmed string as a slice of the original */ trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string { @@ -2265,10 +2324,13 @@ trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, stat /* Trims the input string `s` from the right until the procedure `p` returns `false` -**Inputs** +Inputs: - s: The input string - p: A procedure that takes a rune and returns a boolean +Returns: +The trimmed string as a slice of the original + Example: import "core:fmt" @@ -2285,8 +2347,6 @@ Output: test -**Returns** -The trimmed string as a slice of the original */ trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string { i := last_index_proc(s, p, false) @@ -2301,12 +2361,12 @@ trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string { /* Trims the input string `s` from the right until the procedure `p` with state returns `false` -**Inputs** +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** +Returns: The trimmed string as a slice of the original, empty when no match */ trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string { @@ -2335,11 +2395,11 @@ is_in_cutset :: proc(state: rawptr, r: rune) -> bool { /* Trims the cutset string from the `s` string -**Inputs** +Inputs: - s: The input string - cutset: The set of characters to be trimmed from the left of the input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_left :: proc(s: string, cutset: string) -> string { @@ -2352,11 +2412,11 @@ trim_left :: proc(s: string, cutset: string) -> string { /* Trims the cutset string from the `s` string from the right -**Inputs** +Inputs: - s: The input string - cutset: The set of characters to be trimmed from the right of the input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_right :: proc(s: string, cutset: string) -> string { @@ -2369,11 +2429,11 @@ trim_right :: proc(s: string, cutset: string) -> string { /* Trims the cutset string from the `s` string, both from left and right -**Inputs** +Inputs: - s: The input string - cutset: The set of characters to be trimmed from both sides of the input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim :: proc(s: string, cutset: string) -> string { @@ -2382,10 +2442,10 @@ trim :: proc(s: string, cutset: string) -> string { /* Trims until a valid non-space rune from the left, "\t\txyz\t\t" -> "xyz\t\t" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_left_space :: proc(s: string) -> string { @@ -2394,10 +2454,10 @@ trim_left_space :: proc(s: string) -> string { /* Trims from the right until a valid non-space rune, "\t\txyz\t\t" -> "\t\txyz" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_right_space :: proc(s: string) -> string { @@ -2406,10 +2466,10 @@ trim_right_space :: proc(s: string) -> string { /* Trims from both sides until a valid non-space rune, "\t\txyz\t\t" -> "xyz" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_space :: proc(s: string) -> string { @@ -2418,10 +2478,10 @@ trim_space :: proc(s: string) -> string { /* Trims null runes from the left, "\x00\x00testing\x00\x00" -> "testing\x00\x00" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_left_null :: proc(s: string) -> string { @@ -2430,10 +2490,10 @@ trim_left_null :: proc(s: string) -> string { /* Trims null runes from the right, "\x00\x00testing\x00\x00" -> "\x00\x00testing" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_right_null :: proc(s: string) -> string { @@ -2442,9 +2502,9 @@ trim_right_null :: proc(s: string) -> string { /* Trims null runes from both sides, "\x00\x00testing\x00\x00" -> "testing" -**Inputs** +Inputs: - s: The input string -**Returns** +Returns: The trimmed string as a slice of the original */ trim_null :: proc(s: string) -> string { @@ -2453,10 +2513,13 @@ trim_null :: proc(s: string) -> string { /* Trims a `prefix` string from the start of the `s` string and returns the trimmed string -**Inputs** +Inputs: - s: The input string - prefix: The prefix string to be removed +Returns: +The trimmed string as a slice of original, or the input string if no prefix was found + Example: import "core:fmt" @@ -2472,8 +2535,6 @@ Output: ing testing -**Returns** -The trimmed string as a slice of original, or the input string if no prefix was found */ trim_prefix :: proc(s, prefix: string) -> string { if has_prefix(s, prefix) { @@ -2484,10 +2545,13 @@ trim_prefix :: proc(s, prefix: string) -> string { /* Trims a `suffix` string from the end of the `s` string and returns the trimmed string -**Inputs** +Inputs: - s: The input string - suffix: The suffix string to be removed +Returns: +The trimmed string as a slice of original, or the input string if no suffix was found + Example: import "core:fmt" @@ -2503,8 +2567,6 @@ Output: todo todo.doc -**Returns** -The trimmed string as a slice of original, or the input string if no suffix was found */ trim_suffix :: proc(s, suffix: string) -> string { if has_suffix(s, suffix) { @@ -2517,11 +2579,14 @@ Splits the input string `s` by all possible `substrs` and returns an allocated a *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - substrs: An array of substrings used for splitting - allocator: (default is context.allocator) +Returns: +An array of strings, or nil on empty substring or no matches + NOTE: Allocation occurs for the array, the splits are all views of the original string. Example: @@ -2539,8 +2604,6 @@ Output: ["testing", "this", "out", "nice", "done", "last"] -**Returns** -An array of strings, or nil on empty substring or no matches */ split_multi :: proc(s: string, substrs: []string, allocator := context.allocator) -> []string #no_bounds_check { if s == "" || len(substrs) <= 0 { @@ -2585,10 +2648,13 @@ split_multi :: proc(s: string, substrs: []string, allocator := context.allocator /* Splits the input string `s` by all possible `substrs` in an iterator fashion. The full string is returned if no match. -**Inputs** +Inputs: - it: A pointer to the input string - substrs: An array of substrings used for splitting +Returns: +A tuple containing the split string and a boolean indicating success or failure + Example: import "core:fmt" @@ -2611,8 +2677,6 @@ Output: done last -**Returns** -A tuple containing the split string and a boolean indicating success or failure */ split_multi_iterate :: proc(it: ^string, substrs: []string) -> (res: string, ok: bool) #no_bounds_check { if it == nil || len(it) == 0 || len(substrs) <= 0 { @@ -2644,11 +2708,14 @@ Replaces invalid UTF-8 characters in the input string with a specified replaceme *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - replacement: The string used to replace invalid UTF-8 characters - allocator: (default is context.allocator) +Returns: +A new string with invalid UTF-8 characters replaced + Example: import "core:fmt" @@ -2663,8 +2730,6 @@ Output: Hello? -**Returns** -A new string with invalid UTF-8 characters replaced */ scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string { str := s @@ -2702,10 +2767,13 @@ Reverses the input string `s` *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - allocator: (default is context.allocator) +Returns: +A reversed version of the input string + Example: import "core:fmt" @@ -2721,8 +2789,6 @@ Output: abcxyz zyxcba -**Returns** -A reversed version of the input string */ reverse :: proc(s: string, allocator := context.allocator) -> string { str := s @@ -2743,11 +2809,16 @@ Expands the input string by replacing tab characters with spaces to align to a s *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - tab_size: The number of spaces to use for each tab character - allocator: (default is context.allocator) +Returns: +A new string with tab characters expanded to the specified tab size + +WARNING: Panics if tab_size <= 0 + Example: import "core:fmt" @@ -2762,10 +2833,6 @@ Output: abc1 abc2 abc3 -WARNING: Panics if tab_size <= 0 - -**Returns** -A new string with tab characters expanded to the specified tab size */ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> string { if tab_size <= 0 { @@ -2811,10 +2878,13 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> /* Splits the input string `str` by the separator `sep` string and returns 3 parts. The values are slices of the original string. -**Inputs** +Inputs: - str: The input string - sep: The separator string +Returns: +A tuple with `head` (before the split), `match` (the separator), and `tail` (the end of the split) strings + Example: import "core:fmt" @@ -2840,8 +2910,6 @@ Output: true true -**Returns** -A tuple with `head` (before the split), `match` (the separator), and `tail` (the end of the split) strings */ partition :: proc(str, sep: string) -> (head, match, tail: string) { i := index(str, sep) @@ -2862,13 +2930,13 @@ Centers the input string within a field of specified length by adding pad string *Allocates Using Provided Allocator* -**Inputs** +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) -**Returns** +Returns: A new string centered within a field of the specified length */ centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string { @@ -2897,13 +2965,13 @@ Left-justifies the input string within a field of specified length by adding pad *Allocates Using Provided Allocator* -**Inputs** +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) -**Returns** +Returns: A new string left-justified within a field of the specified length */ left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string { @@ -2931,13 +2999,13 @@ Right-justifies the input string within a field of specified length by adding pa *Allocates Using Provided Allocator* -**Inputs** +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) -**Returns** +Returns: A new string right-justified within a field of the specified length */ right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string { @@ -2963,7 +3031,7 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex /* Writes a given pad string a specified number of times to an `io.Writer` -**Inputs** +Inputs: - w: The io.Writer to write the pad string to - pad: The pad string to be written - pad_len: The length of the pad string, in runes @@ -2991,11 +3059,11 @@ Splits a string into a slice of substrings at each instance of one or more conse *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - allocator: (default is context.allocator) -**Returns** +Returns: A slice of substrings of the input string, or an empty slice if the input string only contains white space */ fields :: proc(s: string, allocator := context.allocator) -> []string #no_bounds_check { @@ -3051,14 +3119,14 @@ Splits a string into a slice of substrings at each run of unicode code points `r *Allocates Using Provided Allocator* -**Inputs** +Inputs: - s: The input string - f: A predicate function to determine the split points - allocator: (default is context.allocator) 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** +Returns: 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 */ fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator) -> []string #no_bounds_check { @@ -3090,10 +3158,10 @@ fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.alloc /* Retrieves the first non-space substring from a mutable string reference and advances the reference. `s` is advanced from any space after the substring, or be an empty string if the substring was the remaining characters -**Inputs** +Inputs: - s: A mutable string reference to be iterated -**Returns** +Returns: - field: The first non-space substring found - ok: A boolean indicating if a non-space substring was found */ @@ -3132,11 +3200,11 @@ Computes the Levenshtein edit distance between two strings NOTE: Does not perform internal allocation if length of string `b`, in runes, is smaller than 64 -**Inputs** +Inputs: - a, b: The two strings to compare - allocator: (default is context.allocator) -**Returns** +Returns: The Levenshtein edit distance between the two strings NOTE: This implementation is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl. |