mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
markdown compliant spaces
This commit is contained in:
@@ -37,7 +37,7 @@ Determines if a given char is contained within an Ascii_Set.
|
|||||||
- as: The Ascii_Set to search.
|
- as: The Ascii_Set to search.
|
||||||
- c: The char to check for in the Ascii_Set.
|
- c: The char to check for in the Ascii_Set.
|
||||||
|
|
||||||
**Returns** A boolean indicating if the byte is contained in the Ascii_Set (true) or not (false).
|
**Returns** A boolean indicating if the byte is contained in the Ascii_Set (true) or not (false).
|
||||||
*/
|
*/
|
||||||
ascii_set_contains :: proc(as: Ascii_Set, c: byte) -> bool #no_bounds_check {
|
ascii_set_contains :: proc(as: Ascii_Set, c: byte) -> bool #no_bounds_check {
|
||||||
return as[c>>5] & (1<<(c&31)) != 0
|
return as[c>>5] & (1<<(c&31)) != 0
|
||||||
|
|||||||
+33
-33
@@ -10,7 +10,7 @@ Type definition for a procedure that flushes a Builder
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
|
|
||||||
**Returns** A boolean indicating whether the Builder should be reset
|
**Returns** A boolean indicating whether the Builder should be reset
|
||||||
*/
|
*/
|
||||||
Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)
|
Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)
|
||||||
/*
|
/*
|
||||||
@@ -29,7 +29,7 @@ Produces a Builder with a default length of 0 and cap of 16
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new Builder
|
**Returns** A new Builder
|
||||||
*/
|
*/
|
||||||
builder_make_none :: proc(allocator := context.allocator) -> Builder {
|
builder_make_none :: proc(allocator := context.allocator) -> Builder {
|
||||||
return Builder{buf=make([dynamic]byte, allocator)}
|
return Builder{buf=make([dynamic]byte, allocator)}
|
||||||
@@ -43,7 +43,7 @@ Produces a Builder with a specified length and cap of max(16,len) byte buffer
|
|||||||
- len: The desired length of the Builder's buffer
|
- len: The desired length of the Builder's buffer
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new Builder
|
**Returns** A new Builder
|
||||||
*/
|
*/
|
||||||
builder_make_len :: proc(len: int, allocator := context.allocator) -> Builder {
|
builder_make_len :: proc(len: int, allocator := context.allocator) -> Builder {
|
||||||
return Builder{buf=make([dynamic]byte, len, allocator)}
|
return Builder{buf=make([dynamic]byte, len, allocator)}
|
||||||
@@ -58,7 +58,7 @@ Produces a Builder with a specified length and cap
|
|||||||
- cap: The desired capacity of the Builder's buffer, cap is max(cap, len)
|
- cap: The desired capacity of the Builder's buffer, cap is max(cap, len)
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new Builder
|
**Returns** A new Builder
|
||||||
*/
|
*/
|
||||||
builder_make_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
|
builder_make_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
|
||||||
return Builder{buf=make([dynamic]byte, len, cap, allocator)}
|
return Builder{buf=make([dynamic]byte, len, cap, allocator)}
|
||||||
@@ -79,7 +79,7 @@ It replaces the existing `buf`
|
|||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** initialized ^Builder
|
**Returns** initialized ^Builder
|
||||||
*/
|
*/
|
||||||
builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> ^Builder {
|
builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> ^Builder {
|
||||||
b.buf = make([dynamic]byte, allocator)
|
b.buf = make([dynamic]byte, allocator)
|
||||||
@@ -96,7 +96,7 @@ It replaces the existing `buf`
|
|||||||
- len: The desired length of the Builder's buffer
|
- len: The desired length of the Builder's buffer
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** Initialized ^Builder
|
**Returns** Initialized ^Builder
|
||||||
*/
|
*/
|
||||||
builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> ^Builder {
|
builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> ^Builder {
|
||||||
b.buf = make([dynamic]byte, len, allocator)
|
b.buf = make([dynamic]byte, len, allocator)
|
||||||
@@ -112,7 +112,7 @@ It replaces the existing `buf`
|
|||||||
- cap: The desired capacity of the Builder's buffer, actual max(len,cap)
|
- cap: The desired capacity of the Builder's buffer, actual max(len,cap)
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A pointer to the initialized Builder
|
**Returns** A pointer to the initialized Builder
|
||||||
*/
|
*/
|
||||||
builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> ^Builder {
|
builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> ^Builder {
|
||||||
b.buf = make([dynamic]byte, len, cap, allocator)
|
b.buf = make([dynamic]byte, len, cap, allocator)
|
||||||
@@ -162,7 +162,7 @@ Returns an io.Stream from a Builder
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
|
|
||||||
**Returns** An io.Stream
|
**Returns** An io.Stream
|
||||||
*/
|
*/
|
||||||
to_stream :: proc(b: ^Builder) -> io.Stream {
|
to_stream :: proc(b: ^Builder) -> io.Stream {
|
||||||
return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
|
return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
|
||||||
@@ -173,7 +173,7 @@ Returns an io.Writer from a Builder
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
|
|
||||||
**Returns** An io.Writer
|
**Returns** An io.Writer
|
||||||
*/
|
*/
|
||||||
to_writer :: proc(b: ^Builder) -> io.Writer {
|
to_writer :: proc(b: ^Builder) -> io.Writer {
|
||||||
return io.to_writer(to_stream(b))
|
return io.to_writer(to_stream(b))
|
||||||
@@ -231,7 +231,7 @@ Output:
|
|||||||
a
|
a
|
||||||
ab
|
ab
|
||||||
|
|
||||||
**Returns** A new Builder
|
**Returns** A new Builder
|
||||||
*/
|
*/
|
||||||
builder_from_bytes :: proc(backing: []byte) -> Builder {
|
builder_from_bytes :: proc(backing: []byte) -> Builder {
|
||||||
s := transmute(runtime.Raw_Slice)backing
|
s := transmute(runtime.Raw_Slice)backing
|
||||||
@@ -253,7 +253,7 @@ Casts the Builder byte buffer to a string and returns it
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A Builder
|
- b: A Builder
|
||||||
|
|
||||||
**Returns** The contents of the Builder's buffer, as a string
|
**Returns** The contents of the Builder's buffer, as a string
|
||||||
*/
|
*/
|
||||||
to_string :: proc(b: Builder) -> string {
|
to_string :: proc(b: Builder) -> string {
|
||||||
return string(b.buf[:])
|
return string(b.buf[:])
|
||||||
@@ -264,7 +264,7 @@ Returns the length of the Builder's buffer, in bytes
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A Builder
|
- b: A Builder
|
||||||
|
|
||||||
**Returns** The length of the Builder's buffer
|
**Returns** The length of the Builder's buffer
|
||||||
*/
|
*/
|
||||||
builder_len :: proc(b: Builder) -> int {
|
builder_len :: proc(b: Builder) -> int {
|
||||||
return len(b.buf)
|
return len(b.buf)
|
||||||
@@ -275,7 +275,7 @@ Returns the capacity of the Builder's buffer, in bytes
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A Builder
|
- b: A Builder
|
||||||
|
|
||||||
**Returns** The capacity of the Builder's buffer
|
**Returns** The capacity of the Builder's buffer
|
||||||
*/
|
*/
|
||||||
builder_cap :: proc(b: Builder) -> int {
|
builder_cap :: proc(b: Builder) -> int {
|
||||||
return cap(b.buf)
|
return cap(b.buf)
|
||||||
@@ -286,7 +286,7 @@ The free space left in the Builder's buffer, in bytes
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A Builder
|
- b: A Builder
|
||||||
|
|
||||||
**Returns** The available space left in the Builder's buffer
|
**Returns** The available space left in the Builder's buffer
|
||||||
*/
|
*/
|
||||||
builder_space :: proc(b: Builder) -> int {
|
builder_space :: proc(b: Builder) -> int {
|
||||||
return cap(b.buf) - len(b.buf)
|
return cap(b.buf) - len(b.buf)
|
||||||
@@ -310,13 +310,13 @@ Example:
|
|||||||
fmt.println(strings.to_string(builder)) // -> ab
|
fmt.println(strings.to_string(builder)) // -> ab
|
||||||
}
|
}
|
||||||
|
|
||||||
**Output**
|
Output:
|
||||||
|
|
||||||
ab
|
ab
|
||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes appended
|
**Returns** The number of bytes appended
|
||||||
*/
|
*/
|
||||||
write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
|
write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
|
||||||
n0 := len(b.buf)
|
n0 := len(b.buf)
|
||||||
@@ -345,7 +345,7 @@ Example:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes appended
|
**Returns** The number of bytes appended
|
||||||
*/
|
*/
|
||||||
write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
|
write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
|
||||||
n0 := len(b.buf)
|
n0 := len(b.buf)
|
||||||
@@ -378,7 +378,7 @@ Output:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written and an io.Error (if any)
|
**Returns** The number of bytes written and an io.Error (if any)
|
||||||
*/
|
*/
|
||||||
write_rune :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
|
write_rune :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
|
||||||
return io.write_rune(to_writer(b), r)
|
return io.write_rune(to_writer(b), r)
|
||||||
@@ -409,7 +409,7 @@ Output:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written
|
**Returns** The number of bytes written
|
||||||
*/
|
*/
|
||||||
write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
|
write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
|
||||||
return io.write_quoted_rune(to_writer(b), r)
|
return io.write_quoted_rune(to_writer(b), r)
|
||||||
@@ -439,7 +439,7 @@ Output:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written
|
**Returns** The number of bytes written
|
||||||
*/
|
*/
|
||||||
write_string :: proc(b: ^Builder, s: string) -> (n: int) {
|
write_string :: proc(b: ^Builder, s: string) -> (n: int) {
|
||||||
n0 := len(b.buf)
|
n0 := len(b.buf)
|
||||||
@@ -453,7 +453,7 @@ Pops and returns the last byte in the Builder or 0 when the Builder is empty
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
|
|
||||||
**Returns** The last byte in the Builder or 0 if empty
|
**Returns** The last byte in the Builder or 0 if empty
|
||||||
*/
|
*/
|
||||||
pop_byte :: proc(b: ^Builder) -> (r: byte) {
|
pop_byte :: proc(b: ^Builder) -> (r: byte) {
|
||||||
if len(b.buf) == 0 {
|
if len(b.buf) == 0 {
|
||||||
@@ -471,7 +471,7 @@ Pops the last rune in the Builder and returns the popped rune and its rune width
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- b: A pointer to the Builder
|
- b: A pointer to the Builder
|
||||||
|
|
||||||
**Returns** The popped rune and its rune width or (0, 0) if empty
|
**Returns** The popped rune and its rune width or (0, 0) if empty
|
||||||
*/
|
*/
|
||||||
pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
|
pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
|
||||||
if len(b.buf) == 0 {
|
if len(b.buf) == 0 {
|
||||||
@@ -510,7 +510,7 @@ Output:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written
|
**Returns** The number of bytes written
|
||||||
*/
|
*/
|
||||||
write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
|
write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
|
||||||
n, _ = io.write_quoted_string(to_writer(b), str, quote)
|
n, _ = io.write_quoted_string(to_writer(b), str, quote)
|
||||||
@@ -526,7 +526,7 @@ Appends an encoded rune to the Builder and returns the number of bytes written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written
|
**Returns** The number of bytes written
|
||||||
*/
|
*/
|
||||||
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
|
write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
|
||||||
n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
|
n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
|
||||||
@@ -549,7 +549,7 @@ Appends an escaped rune to the Builder and returns the number of bytes written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of bytes written
|
**Returns** The number of bytes written
|
||||||
*/
|
*/
|
||||||
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
|
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
|
||||||
n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
|
n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
|
||||||
@@ -568,7 +568,7 @@ Writes a f64 value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
|
write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
|
||||||
buf: [384]byte
|
buf: [384]byte
|
||||||
@@ -591,7 +591,7 @@ Writes a f16 value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
|
write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
|
||||||
buf: [384]byte
|
buf: [384]byte
|
||||||
@@ -629,7 +629,7 @@ Output:
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
|
write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
|
||||||
buf: [384]byte
|
buf: [384]byte
|
||||||
@@ -650,7 +650,7 @@ Writes a f32 value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
|
write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
|
||||||
buf: [384]byte
|
buf: [384]byte
|
||||||
@@ -670,7 +670,7 @@ Writes a u64 value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
|
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
|
||||||
buf: [32]byte
|
buf: [32]byte
|
||||||
@@ -687,7 +687,7 @@ Writes a i64 value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
|
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
|
||||||
buf: [32]byte
|
buf: [32]byte
|
||||||
@@ -704,7 +704,7 @@ Writes a uint value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
|
write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
|
||||||
return write_u64(b, u64(i), base)
|
return write_u64(b, u64(i), base)
|
||||||
@@ -719,7 +719,7 @@ Writes a int value to the Builder and returns the number of characters written
|
|||||||
|
|
||||||
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
|
||||||
|
|
||||||
**Returns** The number of characters written
|
**Returns** The number of characters written
|
||||||
*/
|
*/
|
||||||
write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
|
write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
|
||||||
return write_i64(b, i64(i), base)
|
return write_i64(b, i64(i), base)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ Converts invalid UTF-8 sequences in the input string `s` to the `replacement` st
|
|||||||
|
|
||||||
WARNING: Allocation does not occur when len(s) == 0
|
WARNING: Allocation does not occur when len(s) == 0
|
||||||
|
|
||||||
**Returns** A valid UTF-8 string with invalid sequences replaced by `replacement`.
|
**Returns** A valid UTF-8 string with invalid sequences replaced by `replacement`.
|
||||||
*/
|
*/
|
||||||
to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> string {
|
to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> string {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
@@ -93,7 +93,7 @@ Output:
|
|||||||
|
|
||||||
test
|
test
|
||||||
|
|
||||||
**Returns** A new string with all characters converted to lowercase.
|
**Returns** A new string with all characters converted to lowercase.
|
||||||
*/
|
*/
|
||||||
to_lower :: proc(s: string, allocator := context.allocator) -> string {
|
to_lower :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
b: Builder
|
b: Builder
|
||||||
@@ -125,7 +125,7 @@ Output:
|
|||||||
|
|
||||||
TEST
|
TEST
|
||||||
|
|
||||||
**Returns** A new string with all characters converted to uppercase.
|
**Returns** A new string with all characters converted to uppercase.
|
||||||
*/
|
*/
|
||||||
to_upper :: proc(s: string, allocator := context.allocator) -> string {
|
to_upper :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
b: Builder
|
b: Builder
|
||||||
@@ -141,7 +141,7 @@ Checks if the rune `c` is a delimiter (' ', '-', or '_').
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- c: Rune to check for delimiter status.
|
- c: Rune to check for delimiter status.
|
||||||
|
|
||||||
**Returns** True if `c` is a delimiter, false otherwise.
|
**Returns** True if `c` is a delimiter, false otherwise.
|
||||||
*/
|
*/
|
||||||
is_delimiter :: proc(c: rune) -> bool {
|
is_delimiter :: proc(c: rune) -> bool {
|
||||||
return c == '-' || c == '_' || is_space(c)
|
return c == '-' || c == '_' || is_space(c)
|
||||||
@@ -152,7 +152,7 @@ Checks if the rune `r` is a non-alphanumeric or space character.
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- r: Rune to check for separator status.
|
- r: Rune to check for separator status.
|
||||||
|
|
||||||
**Returns** True if `r` is a non-alpha or `unicode.is_space` rune.
|
**Returns** True if `r` is a non-alpha or `unicode.is_space` rune.
|
||||||
*/
|
*/
|
||||||
is_separator :: proc(r: rune) -> bool {
|
is_separator :: proc(r: rune) -> bool {
|
||||||
if r <= 0x7f {
|
if r <= 0x7f {
|
||||||
@@ -245,7 +245,7 @@ Converts the input string `s` to "lowerCamelCase".
|
|||||||
- s: Input string to be converted.
|
- s: Input string to be converted.
|
||||||
- allocator: (default: context.allocator).
|
- allocator: (default: context.allocator).
|
||||||
|
|
||||||
**Returns** A "lowerCamelCase" formatted string.
|
**Returns** A "lowerCamelCase" formatted string.
|
||||||
*/
|
*/
|
||||||
to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
s := s
|
s := s
|
||||||
@@ -279,7 +279,7 @@ Converts the input string `s` to "UpperCamelCase" (PascalCase).
|
|||||||
- s: Input string to be converted.
|
- s: Input string to be converted.
|
||||||
- allocator: (default: context.allocator).
|
- allocator: (default: context.allocator).
|
||||||
|
|
||||||
**Returns** A "PascalCase" formatted string.
|
**Returns** A "PascalCase" formatted string.
|
||||||
*/
|
*/
|
||||||
to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
s := s
|
s := s
|
||||||
@@ -330,7 +330,7 @@ Output:
|
|||||||
HELLO WORLD
|
HELLO WORLD
|
||||||
a_b_c
|
a_b_c
|
||||||
|
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_delimiter_case :: proc(
|
to_delimiter_case :: proc(
|
||||||
s: string,
|
s: string,
|
||||||
@@ -400,7 +400,7 @@ Output:
|
|||||||
hello_world
|
hello_world
|
||||||
|
|
||||||
```
|
```
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_snake_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_snake_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
return to_delimiter_case(s, '_', false, allocator)
|
return to_delimiter_case(s, '_', false, allocator)
|
||||||
@@ -429,7 +429,7 @@ Output:
|
|||||||
|
|
||||||
HELLO_WORLD
|
HELLO_WORLD
|
||||||
|
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_upper_snake_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_upper_snake_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
return to_delimiter_case(s, '_', true, allocator)
|
return to_delimiter_case(s, '_', true, allocator)
|
||||||
@@ -456,7 +456,7 @@ Output:
|
|||||||
|
|
||||||
hello-world
|
hello-world
|
||||||
|
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
return to_delimiter_case(s, '-', false, allocator)
|
return to_delimiter_case(s, '-', false, allocator)
|
||||||
@@ -483,7 +483,7 @@ Output:
|
|||||||
|
|
||||||
HELLO-WORLD
|
HELLO-WORLD
|
||||||
|
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_upper_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_upper_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
return to_delimiter_case(s, '-', true, allocator)
|
return to_delimiter_case(s, '-', true, allocator)
|
||||||
@@ -510,7 +510,7 @@ Output:
|
|||||||
|
|
||||||
Hello_World
|
Hello_World
|
||||||
|
|
||||||
**Returns** The converted string
|
**Returns** The converted string
|
||||||
*/
|
*/
|
||||||
to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
|
to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
s := s
|
s := s
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ Returns the interned string for the given text, is set in the map if it didnt ex
|
|||||||
|
|
||||||
NOTE: The returned string lives as long as the map entry lives.
|
NOTE: The returned string lives as long as the map entry lives.
|
||||||
|
|
||||||
**Returns** The interned string and an allocator error if any
|
**Returns** The interned string and an allocator error if any
|
||||||
*/
|
*/
|
||||||
intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {
|
intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) {
|
||||||
entry := _intern_get_entry(m, text) or_return
|
entry := _intern_get_entry(m, text) or_return
|
||||||
@@ -74,7 +74,7 @@ Returns the interned C-String for the given text, is set in the map if it didnt
|
|||||||
|
|
||||||
NOTE: The returned C-String lives as long as the map entry lives
|
NOTE: The returned C-String lives as long as the map entry lives
|
||||||
|
|
||||||
**Returns** The interned C-String and an allocator error if any
|
**Returns** The interned C-String and an allocator error if any
|
||||||
*/
|
*/
|
||||||
intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) {
|
intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) {
|
||||||
entry := _intern_get_entry(m, text) or_return
|
entry := _intern_get_entry(m, text) or_return
|
||||||
@@ -90,7 +90,7 @@ Sets and allocates the entry if it wasn't set yet
|
|||||||
- m: A pointer to the Intern struct
|
- m: A pointer to the Intern struct
|
||||||
- text: The string to be looked up or interned
|
- text: The string to be looked up or interned
|
||||||
|
|
||||||
**Returns** The new or existing interned entry and an allocator error if any
|
**Returns** The new or existing interned entry and an allocator error if any
|
||||||
*/
|
*/
|
||||||
_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
|
_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check {
|
||||||
if prev, ok := m.entries[text]; ok {
|
if prev, ok := m.entries[text]; ok {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ Converts a Reader into an io.Stream
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
|
|
||||||
**Returns** An io.Stream for the given Reader
|
**Returns** An io.Stream for the given Reader
|
||||||
*/
|
*/
|
||||||
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
|
||||||
s.stream_data = r
|
s.stream_data = r
|
||||||
@@ -45,7 +45,7 @@ Initializes a string Reader and returns an io.Reader for the given string
|
|||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
- s: The input string to be read
|
- s: The input string to be read
|
||||||
|
|
||||||
**Returns** An io.Reader for the given string
|
**Returns** An io.Reader for the given string
|
||||||
*/
|
*/
|
||||||
to_reader :: proc(r: ^Reader, s: string) -> io.Reader {
|
to_reader :: proc(r: ^Reader, s: string) -> io.Reader {
|
||||||
reader_init(r, s)
|
reader_init(r, s)
|
||||||
@@ -59,7 +59,7 @@ Initializes a string Reader and returns an io.Reader_At for the given string
|
|||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
- s: The input string to be read
|
- s: The input string to be read
|
||||||
|
|
||||||
**Returns** An io.Reader_At for the given string
|
**Returns** An io.Reader_At for the given string
|
||||||
*/
|
*/
|
||||||
to_reader_at :: proc(r: ^Reader, s: string) -> io.Reader_At {
|
to_reader_at :: proc(r: ^Reader, s: string) -> io.Reader_At {
|
||||||
reader_init(r, s)
|
reader_init(r, s)
|
||||||
@@ -72,7 +72,7 @@ Returns the remaining length of the Reader
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
|
|
||||||
**Returns** The remaining length of the Reader
|
**Returns** The remaining length of the Reader
|
||||||
*/
|
*/
|
||||||
reader_length :: proc(r: ^Reader) -> int {
|
reader_length :: proc(r: ^Reader) -> int {
|
||||||
if r.i >= i64(len(r.s)) {
|
if r.i >= i64(len(r.s)) {
|
||||||
@@ -86,7 +86,7 @@ Returns the length of the string stored in the Reader
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
|
|
||||||
**Returns** The length of the string stored in the Reader
|
**Returns** The length of the string stored in the Reader
|
||||||
*/
|
*/
|
||||||
reader_size :: proc(r: ^Reader) -> i64 {
|
reader_size :: proc(r: ^Reader) -> i64 {
|
||||||
return i64(len(r.s))
|
return i64(len(r.s))
|
||||||
@@ -161,7 +161,7 @@ Decrements the Reader's index (i) by 1
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- r: A pointer to a Reader struct
|
- r: A pointer to a Reader struct
|
||||||
|
|
||||||
**Returns** An io.Error if `r.i <= 0` (.Invalid_Unread), otherwise nil denotes success.
|
**Returns** An io.Error if `r.i <= 0` (.Invalid_Unread), otherwise nil denotes success.
|
||||||
*/
|
*/
|
||||||
reader_unread_byte :: proc(r: ^Reader) -> io.Error {
|
reader_unread_byte :: proc(r: ^Reader) -> io.Error {
|
||||||
if r.i <= 0 {
|
if r.i <= 0 {
|
||||||
@@ -204,7 +204,7 @@ Decrements the Reader's index (i) by the size of the last read rune
|
|||||||
|
|
||||||
WARNING: May only be used once and after a valid `read_rune` call
|
WARNING: May only be used once and after a valid `read_rune` call
|
||||||
|
|
||||||
**Returns** An io.Error if an error occurs while unreading (.Invalid_Unread), else nil denotes success.
|
**Returns** An io.Error if an error occurs while unreading (.Invalid_Unread), else nil denotes success.
|
||||||
*/
|
*/
|
||||||
reader_unread_rune :: proc(r: ^Reader) -> io.Error {
|
reader_unread_rune :: proc(r: ^Reader) -> io.Error {
|
||||||
if r.i <= 0 {
|
if r.i <= 0 {
|
||||||
|
|||||||
+83
-83
@@ -16,7 +16,7 @@ Clones a string
|
|||||||
- allocator: (default: context.allocator)
|
- allocator: (default: context.allocator)
|
||||||
- loc: The caller location for debugging purposes (default: #caller_location)
|
- loc: The caller location for debugging purposes (default: #caller_location)
|
||||||
|
|
||||||
**Returns** A cloned string
|
**Returns** A cloned string
|
||||||
*/
|
*/
|
||||||
clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> string {
|
clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> string {
|
||||||
c := make([]byte, len(s), allocator, loc)
|
c := make([]byte, len(s), allocator, loc)
|
||||||
@@ -52,7 +52,7 @@ Clones a string and appends a nul byte to make it a cstring
|
|||||||
- allocator: (default: context.allocator)
|
- allocator: (default: context.allocator)
|
||||||
- loc: The caller location for debugging purposes (default: #caller_location)
|
- loc: The caller location for debugging purposes (default: #caller_location)
|
||||||
|
|
||||||
**Returns** A cloned cstring with an appended nul byte
|
**Returns** A cloned cstring with an appended nul byte
|
||||||
*/
|
*/
|
||||||
clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> cstring {
|
clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> cstring {
|
||||||
c := make([]byte, len(s)+1, allocator, loc)
|
c := make([]byte, len(s)+1, allocator, loc)
|
||||||
@@ -69,7 +69,7 @@ Transmutes a raw pointer into a string. Non-allocating.
|
|||||||
|
|
||||||
NOTE: The created string is only valid as long as the pointer and length are valid.
|
NOTE: The created string is only valid as long as the pointer and length are valid.
|
||||||
|
|
||||||
**Returns** A string created from the byte pointer and length
|
**Returns** A string created from the byte pointer and length
|
||||||
*/
|
*/
|
||||||
string_from_ptr :: proc(ptr: ^byte, len: int) -> string {
|
string_from_ptr :: proc(ptr: ^byte, len: int) -> string {
|
||||||
return transmute(string)mem.Raw_String{ptr, len}
|
return transmute(string)mem.Raw_String{ptr, len}
|
||||||
@@ -84,7 +84,7 @@ NOTE: The created string is only valid as long as the pointer and length are val
|
|||||||
- ptr: A pointer to the start of the nul-terminated byte sequence
|
- ptr: A pointer to the start of the nul-terminated byte sequence
|
||||||
- len: The length of the byte sequence
|
- len: The length of the byte sequence
|
||||||
|
|
||||||
**Returns** A string created from the nul-terminated byte pointer and length
|
**Returns** A string created from the nul-terminated byte pointer and length
|
||||||
*/
|
*/
|
||||||
string_from_nul_terminated_ptr :: proc(ptr: ^byte, len: int) -> string {
|
string_from_nul_terminated_ptr :: proc(ptr: ^byte, len: int) -> string {
|
||||||
s := transmute(string)mem.Raw_String{ptr, len}
|
s := transmute(string)mem.Raw_String{ptr, len}
|
||||||
@@ -97,7 +97,7 @@ Gets the raw byte pointer for the start of a string `str`
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- str: The input string
|
- str: The input string
|
||||||
|
|
||||||
**Returns** A pointer to the start of the string's bytes
|
**Returns** A pointer to the start of the string's bytes
|
||||||
*/
|
*/
|
||||||
ptr_from_string :: proc(str: string) -> ^byte {
|
ptr_from_string :: proc(str: string) -> ^byte {
|
||||||
d := transmute(mem.Raw_String)str
|
d := transmute(mem.Raw_String)str
|
||||||
@@ -111,7 +111,7 @@ Converts a string `str` to a cstring
|
|||||||
|
|
||||||
WARNING: This is unsafe because the original string may not contain a nul byte.
|
WARNING: This is unsafe because the original string may not contain a nul byte.
|
||||||
|
|
||||||
**Returns** The converted cstring
|
**Returns** The converted cstring
|
||||||
*/
|
*/
|
||||||
unsafe_string_to_cstring :: proc(str: string) -> cstring {
|
unsafe_string_to_cstring :: proc(str: string) -> cstring {
|
||||||
d := transmute(mem.Raw_String)str
|
d := transmute(mem.Raw_String)str
|
||||||
@@ -126,7 +126,7 @@ Truncates a string `str` at the first occurrence of char/byte `b`
|
|||||||
|
|
||||||
NOTE: Failure to find the byte results in returning the entire string.
|
NOTE: Failure to find the byte results in returning the entire string.
|
||||||
|
|
||||||
**Returns** The truncated string
|
**Returns** The truncated string
|
||||||
*/
|
*/
|
||||||
truncate_to_byte :: proc(str: string, b: byte) -> string {
|
truncate_to_byte :: proc(str: string, b: byte) -> string {
|
||||||
n := index_byte(str, b)
|
n := index_byte(str, b)
|
||||||
@@ -142,7 +142,7 @@ Truncates a string str at the first occurrence of rune r as a slice of the origi
|
|||||||
- str: The input string
|
- str: The input string
|
||||||
- r: The rune to truncate the string at
|
- r: The rune to truncate the string at
|
||||||
|
|
||||||
**Returns** The truncated string
|
**Returns** The truncated string
|
||||||
*/
|
*/
|
||||||
truncate_to_rune :: proc(str: string, r: rune) -> string {
|
truncate_to_rune :: proc(str: string, r: rune) -> string {
|
||||||
n := index_rune(str, r)
|
n := index_rune(str, r)
|
||||||
@@ -161,7 +161,7 @@ Clones a byte array s and appends a nul byte
|
|||||||
- allocator: (default: context.allocator)
|
- allocator: (default: context.allocator)
|
||||||
- loc: The caller location for debugging purposes (default: #caller_location)
|
- loc: The caller location for debugging purposes (default: #caller_location)
|
||||||
|
|
||||||
**Returns** A cloned string from the byte array with a nul byte
|
**Returns** A cloned string from the byte array with a nul byte
|
||||||
*/
|
*/
|
||||||
clone_from_bytes :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> string {
|
clone_from_bytes :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> string {
|
||||||
c := make([]byte, len(s)+1, allocator, loc)
|
c := make([]byte, len(s)+1, allocator, loc)
|
||||||
@@ -179,7 +179,7 @@ Clones a cstring s as a string
|
|||||||
- allocator: (default: context.allocator)
|
- allocator: (default: context.allocator)
|
||||||
- loc: The caller location for debugging purposes (default: #caller_location)
|
- loc: The caller location for debugging purposes (default: #caller_location)
|
||||||
|
|
||||||
**Returns** A cloned string from the cstring
|
**Returns** A cloned string from the cstring
|
||||||
*/
|
*/
|
||||||
clone_from_cstring :: proc(s: cstring, allocator := context.allocator, loc := #caller_location) -> string {
|
clone_from_cstring :: proc(s: cstring, allocator := context.allocator, loc := #caller_location) -> string {
|
||||||
return clone(string(s), allocator, loc)
|
return clone(string(s), allocator, loc)
|
||||||
@@ -197,7 +197,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
|
NOTE: Same as `string_from_ptr`, but perform an additional `clone` operation
|
||||||
|
|
||||||
**Returns** A cloned string from the byte pointer and length
|
**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 {
|
clone_from_ptr :: proc(ptr: ^byte, len: int, allocator := context.allocator, loc := #caller_location) -> string {
|
||||||
s := string_from_ptr(ptr, len)
|
s := string_from_ptr(ptr, len)
|
||||||
@@ -223,7 +223,7 @@ Clones a string from a nul-terminated cstring ptr and a byte length len
|
|||||||
|
|
||||||
NOTE: Truncates at the first nul byte encountered or the byte length.
|
NOTE: Truncates at the first nul byte encountered or the byte length.
|
||||||
|
|
||||||
**Returns** A cloned string from the nul-terminated cstring and byte length
|
**Returns** A cloned string from the nul-terminated cstring and byte length
|
||||||
*/
|
*/
|
||||||
clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> string {
|
clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> string {
|
||||||
s := string_from_ptr((^u8)(ptr), len)
|
s := string_from_ptr((^u8)(ptr), len)
|
||||||
@@ -238,7 +238,7 @@ Compares two strings, returning a value representing which one comes first lexic
|
|||||||
- lhs: First string for comparison
|
- lhs: First string for comparison
|
||||||
- rhs: Second string for comparison
|
- rhs: Second string for comparison
|
||||||
|
|
||||||
**Returns** -1 if lhs comes first, 1 if rhs comes first, or 0 if they are equal
|
**Returns** -1 if lhs comes first, 1 if rhs comes first, or 0 if they are equal
|
||||||
*/
|
*/
|
||||||
compare :: proc(lhs, rhs: string) -> int {
|
compare :: proc(lhs, rhs: string) -> int {
|
||||||
return mem.compare(transmute([]byte)lhs, transmute([]byte)rhs)
|
return mem.compare(transmute([]byte)lhs, transmute([]byte)rhs)
|
||||||
@@ -250,7 +250,7 @@ Returns the byte offset of the rune r in the string s, -1 when not found
|
|||||||
- s: The input string
|
- s: The input string
|
||||||
- r: The rune to search for
|
- r: The rune to search for
|
||||||
|
|
||||||
**Returns** The byte offset of the rune r in the string s, or -1 if not found
|
**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 {
|
contains_rune :: proc(s: string, r: rune) -> int {
|
||||||
for c, offset in s {
|
for c, offset in s {
|
||||||
@@ -296,7 +296,7 @@ Output:
|
|||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
|
||||||
**Returns** true if substr is contained inside the string s, false otherwise
|
**Returns** true if substr is contained inside the string s, false otherwise
|
||||||
*/
|
*/
|
||||||
contains :: proc(s, substr: string) -> bool {
|
contains :: proc(s, substr: string) -> bool {
|
||||||
return index(s, substr) >= 0
|
return index(s, substr) >= 0
|
||||||
@@ -327,7 +327,7 @@ Output:
|
|||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
|
||||||
**Returns** true if the string s contains any of the characters in chars, false otherwise
|
**Returns** true if the string s contains any of the characters in chars, false otherwise
|
||||||
*/
|
*/
|
||||||
contains_any :: proc(s, chars: string) -> bool {
|
contains_any :: proc(s, chars: string) -> bool {
|
||||||
return index_any(s, chars) >= 0
|
return index_any(s, chars) >= 0
|
||||||
@@ -353,7 +353,7 @@ Output:
|
|||||||
4
|
4
|
||||||
5
|
5
|
||||||
|
|
||||||
**Returns** The UTF-8 rune count of the string s
|
**Returns** The UTF-8 rune count of the string s
|
||||||
*/
|
*/
|
||||||
rune_count :: proc(s: string) -> int {
|
rune_count :: proc(s: string) -> int {
|
||||||
return utf8.rune_count_in_string(s)
|
return utf8.rune_count_in_string(s)
|
||||||
@@ -385,7 +385,7 @@ Output:
|
|||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
|
||||||
**Returns** True if the strings u and v are the same alpha characters (ignoring case), false
|
**Returns** True if the strings u and v are the same alpha characters (ignoring case), false
|
||||||
*/
|
*/
|
||||||
equal_fold :: proc(u, v: string) -> bool {
|
equal_fold :: proc(u, v: string) -> bool {
|
||||||
s, t := u, v
|
s, t := u, v
|
||||||
@@ -455,7 +455,7 @@ Output:
|
|||||||
2
|
2
|
||||||
0
|
0
|
||||||
|
|
||||||
**Returns** The prefix length common between strings a and b
|
**Returns** The prefix length common between strings a and b
|
||||||
*/
|
*/
|
||||||
prefix_length :: proc(a, b: string) -> (n: int) {
|
prefix_length :: proc(a, b: string) -> (n: int) {
|
||||||
_len := min(len(a), len(b))
|
_len := min(len(a), len(b))
|
||||||
@@ -506,7 +506,7 @@ Output:
|
|||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
|
||||||
**Returns** true if the string s starts with the prefix, otherwise false
|
**Returns** true if the string s starts with the prefix, otherwise false
|
||||||
*/
|
*/
|
||||||
has_prefix :: proc(s, prefix: string) -> bool {
|
has_prefix :: proc(s, prefix: string) -> bool {
|
||||||
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
|
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
|
||||||
@@ -535,7 +535,7 @@ Output:
|
|||||||
- s: The string to check for the suffix
|
- s: The string to check for the suffix
|
||||||
- suffix: The suffix to look for
|
- suffix: The suffix to look for
|
||||||
|
|
||||||
**Returns** true if the string s ends with the suffix, otherwise false
|
**Returns** true if the string s ends with the suffix, otherwise false
|
||||||
*/
|
*/
|
||||||
has_suffix :: proc(s, suffix: string) -> bool {
|
has_suffix :: proc(s, suffix: string) -> bool {
|
||||||
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
|
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
|
||||||
@@ -568,7 +568,7 @@ Output:
|
|||||||
- sep: The separator string
|
- sep: The separator string
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A combined string from the slice of strings a separated with the sep string
|
**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 {
|
join :: proc(a: []string, sep: string, allocator := context.allocator) -> string {
|
||||||
if len(a) == 0 {
|
if len(a) == 0 {
|
||||||
@@ -643,7 +643,7 @@ Output:
|
|||||||
|
|
||||||
abc
|
abc
|
||||||
|
|
||||||
**Returns** The concatenated string
|
**Returns** The concatenated string
|
||||||
*/
|
*/
|
||||||
concatenate :: proc(a: []string, allocator := context.allocator) -> string {
|
concatenate :: proc(a: []string, allocator := context.allocator) -> string {
|
||||||
if len(a) == 0 {
|
if len(a) == 0 {
|
||||||
@@ -670,7 +670,7 @@ Returns a combined string from the slice of strings `a` without a separator, or
|
|||||||
- a: A slice of strings to concatenate
|
- a: A slice of strings to concatenate
|
||||||
- allocator: An optional custom allocator (default is context.allocator)
|
- allocator: An optional custom allocator (default is context.allocator)
|
||||||
|
|
||||||
**Returns** The concatenated string, and an error if allocation fails
|
**Returns** The concatenated string, and an error if allocation fails
|
||||||
*/
|
*/
|
||||||
concatenate_safe :: proc(a: []string, allocator := context.allocator) -> (res: string, err: mem.Allocator_Error) {
|
concatenate_safe :: proc(a: []string, allocator := context.allocator) -> (res: string, err: mem.Allocator_Error) {
|
||||||
if len(a) == 0 {
|
if len(a) == 0 {
|
||||||
@@ -716,7 +716,7 @@ Output:
|
|||||||
me
|
me
|
||||||
example
|
example
|
||||||
|
|
||||||
**Returns** The substring
|
**Returns** The substring
|
||||||
*/
|
*/
|
||||||
cut :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator) -> (res: string) {
|
cut :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator) -> (res: string) {
|
||||||
s := s; rune_length := rune_length
|
s := s; rune_length := rune_length
|
||||||
@@ -781,7 +781,7 @@ Splits the input string `s` into a slice of substrings separated by the specifie
|
|||||||
- n: The maximum number of substrings to return, returns nil without alloc when n=0
|
- n: The maximum number of substrings to return, returns nil without alloc when n=0
|
||||||
- allocator: An optional custom allocator (default is context.allocator)
|
- allocator: An optional custom allocator (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A slice of substrings
|
**Returns** A slice of substrings
|
||||||
*/
|
*/
|
||||||
@private
|
@private
|
||||||
_split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator) -> []string {
|
_split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator) -> []string {
|
||||||
@@ -855,7 +855,7 @@ Output:
|
|||||||
|
|
||||||
["aaa", "bbb", "ccc", "ddd", "eee"]
|
["aaa", "bbb", "ccc", "ddd", "eee"]
|
||||||
|
|
||||||
**Returns** A slice of strings, each representing a part of the split string.
|
**Returns** A slice of strings, each representing a part of the split string.
|
||||||
*/
|
*/
|
||||||
split :: proc(s, sep: string, allocator := context.allocator) -> []string {
|
split :: proc(s, sep: string, allocator := context.allocator) -> []string {
|
||||||
return _split(s, sep, 0, -1, allocator)
|
return _split(s, sep, 0, -1, allocator)
|
||||||
@@ -885,7 +885,7 @@ Output:
|
|||||||
|
|
||||||
["aaa", "bbb", "ccc.ddd.eee"]
|
["aaa", "bbb", "ccc.ddd.eee"]
|
||||||
|
|
||||||
**Returns** A slice of strings, each representing a part of the split 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 {
|
split_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
|
||||||
return _split(s, sep, 0, n, allocator)
|
return _split(s, sep, 0, n, allocator)
|
||||||
@@ -915,7 +915,7 @@ Output:
|
|||||||
|
|
||||||
["aaa.", "bbb.", "ccc.", "ddd.", "eee"]
|
["aaa.", "bbb.", "ccc.", "ddd.", "eee"]
|
||||||
|
|
||||||
**Returns** A slice of strings, each representing a part of the split string after the separator.
|
**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 {
|
split_after :: proc(s, sep: string, allocator := context.allocator) -> []string {
|
||||||
return _split(s, sep, len(sep), -1, allocator)
|
return _split(s, sep, len(sep), -1, allocator)
|
||||||
@@ -946,7 +946,7 @@ Output:
|
|||||||
|
|
||||||
["aaa.", "bbb.", "ccc.ddd.eee"]
|
["aaa.", "bbb.", "ccc.ddd.eee"]
|
||||||
|
|
||||||
**Returns** A slice of strings with 'n' parts or fewer if there weren't
|
**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 {
|
split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
|
||||||
return _split(s, sep, len(sep), n, allocator)
|
return _split(s, sep, len(sep), n, allocator)
|
||||||
@@ -964,7 +964,7 @@ up to (but not including) the separator, as well as a boolean indicating success
|
|||||||
|
|
||||||
NOTE: Destructively consumes the string
|
NOTE: Destructively consumes the string
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
||||||
*/
|
*/
|
||||||
@private
|
@private
|
||||||
_split_iterator :: proc(s: ^string, sep: string, sep_save: int) -> (res: string, ok: bool) {
|
_split_iterator :: proc(s: ^string, sep: string, sep_save: int) -> (res: string, ok: bool) {
|
||||||
@@ -1021,7 +1021,7 @@ Output:
|
|||||||
d
|
d
|
||||||
e
|
e
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
**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) {
|
split_by_byte_iterator :: proc(s: ^string, sep: u8) -> (res: string, ok: bool) {
|
||||||
m := index_byte(s^, sep)
|
m := index_byte(s^, sep)
|
||||||
@@ -1065,7 +1065,7 @@ Output:
|
|||||||
d
|
d
|
||||||
e
|
e
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
||||||
*/
|
*/
|
||||||
split_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
|
split_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
|
||||||
return _split_iterator(s, sep, 0)
|
return _split_iterator(s, sep, 0)
|
||||||
@@ -1098,7 +1098,7 @@ Output:
|
|||||||
d.
|
d.
|
||||||
e
|
e
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
||||||
*/
|
*/
|
||||||
split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
|
split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
|
||||||
return _split_iterator(s, sep, len(sep))
|
return _split_iterator(s, sep, len(sep))
|
||||||
@@ -1111,7 +1111,7 @@ Trims the carriage return character from the end of the input string.
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string to trim.
|
- s: The input string to trim.
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice.
|
**Returns** The trimmed string as a slice.
|
||||||
*/
|
*/
|
||||||
@(private)
|
@(private)
|
||||||
_trim_cr :: proc(s: string) -> string {
|
_trim_cr :: proc(s: string) -> string {
|
||||||
@@ -1147,7 +1147,7 @@ Output:
|
|||||||
|
|
||||||
["a", "b", "c", "d", "e"]
|
["a", "b", "c", "d", "e"]
|
||||||
|
|
||||||
**Returns** An allocated slice of strings split by line breaks.
|
**Returns** An allocated slice of strings split by line breaks.
|
||||||
*/
|
*/
|
||||||
split_lines :: proc(s: string, allocator := context.allocator) -> []string {
|
split_lines :: proc(s: string, allocator := context.allocator) -> []string {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1182,7 +1182,7 @@ Output:
|
|||||||
|
|
||||||
["a", "b", "c\nd\ne"]
|
["a", "b", "c\nd\ne"]
|
||||||
|
|
||||||
**Returns** An allocated array of strings split by line breaks.
|
**Returns** An allocated array of strings split by line breaks.
|
||||||
*/
|
*/
|
||||||
split_lines_n :: proc(s: string, n: int, allocator := context.allocator) -> []string {
|
split_lines_n :: proc(s: string, n: int, allocator := context.allocator) -> []string {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1216,7 +1216,7 @@ Output:
|
|||||||
|
|
||||||
["a\n", "b\n", "c\n", "d\n", "e"]
|
["a\n", "b\n", "c\n", "d\n", "e"]
|
||||||
|
|
||||||
**Returns** An allocated slice of strings split by line breaks with line breaks included.
|
**Returns** An allocated slice of strings split by line breaks with line breaks included.
|
||||||
*/
|
*/
|
||||||
split_lines_after :: proc(s: string, allocator := context.allocator) -> []string {
|
split_lines_after :: proc(s: string, allocator := context.allocator) -> []string {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1252,7 +1252,7 @@ Output:
|
|||||||
|
|
||||||
["a\n", "b\n", "c\nd\ne"]
|
["a\n", "b\n", "c\nd\ne"]
|
||||||
|
|
||||||
**Returns** An allocated slice of strings split by line breaks with line breaks included.
|
**Returns** An allocated slice of strings split by line breaks with line breaks included.
|
||||||
*/
|
*/
|
||||||
split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) -> []string {
|
split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) -> []string {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1285,7 +1285,7 @@ Output:
|
|||||||
|
|
||||||
abcde
|
abcde
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
**Returns** A tuple containing the resulting substring and a boolean indicating success.
|
||||||
*/
|
*/
|
||||||
split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
|
split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1319,7 +1319,7 @@ Output:
|
|||||||
d
|
d
|
||||||
e
|
e
|
||||||
|
|
||||||
**Returns** A tuple containing the resulting substring with line breaks included and a boolean indicating success.
|
**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) {
|
split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) {
|
||||||
sep :: "\n"
|
sep :: "\n"
|
||||||
@@ -1353,7 +1353,7 @@ Output:
|
|||||||
-1
|
-1
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The byte offset of the first occurrence of c in s, or -1 if not found.
|
**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 {
|
index_byte :: proc(s: string, c: byte) -> int {
|
||||||
for i := 0; i < len(s); i += 1 {
|
for i := 0; i < len(s); i += 1 {
|
||||||
@@ -1386,7 +1386,7 @@ Output:
|
|||||||
-1
|
-1
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The byte offset of the last occurrence of `c` in `s`, or -1 if not found.
|
**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 {
|
last_index_byte :: proc(s: string, c: byte) -> int {
|
||||||
for i := len(s)-1; i >= 0; i -= 1 {
|
for i := len(s)-1; i >= 0; i -= 1 {
|
||||||
@@ -1426,7 +1426,7 @@ Output:
|
|||||||
6
|
6
|
||||||
7
|
7
|
||||||
|
|
||||||
**Returns** The byte offset of the first occurrence of `r` in `s`, or -1 if not found.
|
**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 {
|
index_rune :: proc(s: string, r: rune) -> int {
|
||||||
switch {
|
switch {
|
||||||
@@ -1472,7 +1472,7 @@ Output:
|
|||||||
2
|
2
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The byte offset of the first occurrence of `substr` in `s`, or -1 if not found.
|
**Returns** The byte offset of the first occurrence of `substr` in `s`, or -1 if not found.
|
||||||
*/
|
*/
|
||||||
index :: proc(s, substr: string) -> int {
|
index :: proc(s, substr: string) -> int {
|
||||||
hash_str_rabin_karp :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
|
hash_str_rabin_karp :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
|
||||||
@@ -1545,7 +1545,7 @@ Output:
|
|||||||
2
|
2
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The byte offset of the last occurrence of `substr` in `s`, or -1 if not found.
|
**Returns** The byte offset of the last occurrence of `substr` in `s`, or -1 if not found.
|
||||||
*/
|
*/
|
||||||
last_index :: proc(s, substr: string) -> int {
|
last_index :: proc(s, substr: string) -> int {
|
||||||
hash_str_rabin_karp_reverse :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
|
hash_str_rabin_karp_reverse :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
|
||||||
@@ -1618,7 +1618,7 @@ Output:
|
|||||||
0
|
0
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The index of the first character of `chars` found in `s`, or -1 if not found.
|
**Returns** The index of the first character of `chars` found in `s`, or -1 if not found.
|
||||||
*/
|
*/
|
||||||
index_any :: proc(s, chars: string) -> int {
|
index_any :: proc(s, chars: string) -> int {
|
||||||
if chars == "" {
|
if chars == "" {
|
||||||
@@ -1679,7 +1679,7 @@ Output:
|
|||||||
3
|
3
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The index of the last matching character, or -1 if not found
|
**Returns** The index of the last matching character, or -1 if not found
|
||||||
*/
|
*/
|
||||||
last_index_any :: proc(s, chars: string) -> int {
|
last_index_any :: proc(s, chars: string) -> int {
|
||||||
if chars == "" {
|
if chars == "" {
|
||||||
@@ -1736,7 +1736,7 @@ Finds the first occurrence of any substring in 'substrs' within 's'
|
|||||||
- s: The string to search in
|
- s: The string to search in
|
||||||
- substrs: The substrings to look for
|
- substrs: The substrings to look for
|
||||||
|
|
||||||
**Returns** A tuple containing the index of the first matching substring, and its length (width)
|
**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) {
|
index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int) {
|
||||||
idx = -1
|
idx = -1
|
||||||
@@ -1795,7 +1795,7 @@ Output:
|
|||||||
1
|
1
|
||||||
0
|
0
|
||||||
|
|
||||||
**Returns** The number of occurrences of 'substr' in 's', returns the rune_count + 1 of the string `s` on empty `substr`
|
**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 {
|
count :: proc(s, substr: string) -> int {
|
||||||
if len(substr) == 0 { // special case
|
if len(substr) == 0 { // special case
|
||||||
@@ -1856,7 +1856,7 @@ Output:
|
|||||||
|
|
||||||
abcabc
|
abcabc
|
||||||
|
|
||||||
**Returns** The concatenated repeated string
|
**Returns** The concatenated repeated string
|
||||||
*/
|
*/
|
||||||
repeat :: proc(s: string, count: int, allocator := context.allocator) -> string {
|
repeat :: proc(s: string, count: int, allocator := context.allocator) -> string {
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
@@ -1901,7 +1901,7 @@ Output:
|
|||||||
xyzxyz false
|
xyzxyz false
|
||||||
zzzz true
|
zzzz true
|
||||||
|
|
||||||
**Returns** A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement
|
**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) {
|
replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
|
||||||
return replace(s, old, new, -1, allocator)
|
return replace(s, old, new, -1, allocator)
|
||||||
@@ -1937,7 +1937,7 @@ Output:
|
|||||||
xyzxyz false
|
xyzxyz false
|
||||||
zzzz true
|
zzzz true
|
||||||
|
|
||||||
**Returns** A tuple containing the modified string and a boolean indicating if an allocation occurred during the replacement
|
**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) {
|
replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
|
||||||
if old == new || n == 0 {
|
if old == new || n == 0 {
|
||||||
@@ -2008,7 +2008,7 @@ Output:
|
|||||||
bcbc true
|
bcbc true
|
||||||
abcabc false
|
abcabc false
|
||||||
|
|
||||||
**Returns** A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal
|
**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) {
|
remove :: proc(s, key: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
|
||||||
return replace(s, key, "", n, allocator)
|
return replace(s, key, "", n, allocator)
|
||||||
@@ -2040,7 +2040,7 @@ Output:
|
|||||||
bcbc true
|
bcbc true
|
||||||
abcabc false
|
abcabc false
|
||||||
|
|
||||||
**Returns** A tuple containing the modified string and a boolean indicating if an allocation occurred during the removal
|
**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) {
|
remove_all :: proc(s, key: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
|
||||||
return remove(s, key, -1, allocator)
|
return remove(s, key, -1, allocator)
|
||||||
@@ -2109,7 +2109,7 @@ Output:
|
|||||||
1
|
1
|
||||||
-1
|
-1
|
||||||
|
|
||||||
**Returns** The index of the first matching rune, or -1 if no match was found
|
**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 {
|
index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
|
||||||
for r, i in s {
|
for r, i in s {
|
||||||
@@ -2175,7 +2175,7 @@ Output:
|
|||||||
|
|
||||||
ing
|
ing
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
|
trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
|
||||||
i := index_proc(s, p, false)
|
i := index_proc(s, p, false)
|
||||||
@@ -2192,7 +2192,7 @@ Trims the input string s from the left until the procedure p with state returns
|
|||||||
- p: A procedure that takes a raw pointer and a rune and returns a boolean
|
- 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
|
- state: The raw pointer to be passed to the procedure p
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**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 {
|
trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
|
||||||
i := index_proc_with_state(s, p, state, false)
|
i := index_proc_with_state(s, p, state, false)
|
||||||
@@ -2224,7 +2224,7 @@ Output:
|
|||||||
|
|
||||||
test
|
test
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
|
trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
|
||||||
i := last_index_proc(s, p, false)
|
i := last_index_proc(s, p, false)
|
||||||
@@ -2244,7 +2244,7 @@ Trims the input string s from the right until the procedure p with state returns
|
|||||||
- p: A procedure that takes a raw pointer and a rune and returns a boolean
|
- 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
|
- state: The raw pointer to be passed to the procedure p
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original, empty when no match
|
**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 {
|
trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
|
||||||
i := last_index_proc_with_state(s, p, state, false)
|
i := last_index_proc_with_state(s, p, state, false)
|
||||||
@@ -2276,7 +2276,7 @@ Trims the cutset string from the s string
|
|||||||
- s: The input string
|
- s: The input string
|
||||||
- cutset: The set of characters to be trimmed from the left of the input string
|
- cutset: The set of characters to be trimmed from the left of the input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_left :: proc(s: string, cutset: string) -> string {
|
trim_left :: proc(s: string, cutset: string) -> string {
|
||||||
if s == "" || cutset == "" {
|
if s == "" || cutset == "" {
|
||||||
@@ -2292,7 +2292,7 @@ Trims the cutset string from the s string from the right
|
|||||||
- s: The input string
|
- s: The input string
|
||||||
- cutset: The set of characters to be trimmed from the right of the input string
|
- cutset: The set of characters to be trimmed from the right of the input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_right :: proc(s: string, cutset: string) -> string {
|
trim_right :: proc(s: string, cutset: string) -> string {
|
||||||
if s == "" || cutset == "" {
|
if s == "" || cutset == "" {
|
||||||
@@ -2308,7 +2308,7 @@ Trims the cutset string from the s string, both from left and right
|
|||||||
- s: The input string
|
- s: The input string
|
||||||
- cutset: The set of characters to be trimmed from both sides of the input string
|
- cutset: The set of characters to be trimmed from both sides of the input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim :: proc(s: string, cutset: string) -> string {
|
trim :: proc(s: string, cutset: string) -> string {
|
||||||
return trim_right(trim_left(s, cutset), cutset)
|
return trim_right(trim_left(s, cutset), cutset)
|
||||||
@@ -2319,7 +2319,7 @@ Trims until a valid non-space rune from the left, "\t\txyz\t\t" -> "xyz\t\t"
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_left_space :: proc(s: string) -> string {
|
trim_left_space :: proc(s: string) -> string {
|
||||||
return trim_left_proc(s, is_space)
|
return trim_left_proc(s, is_space)
|
||||||
@@ -2330,7 +2330,7 @@ Trims from the right until a valid non-space rune, "\t\txyz\t\t" -> "\t\txyz"
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_right_space :: proc(s: string) -> string {
|
trim_right_space :: proc(s: string) -> string {
|
||||||
return trim_right_proc(s, is_space)
|
return trim_right_proc(s, is_space)
|
||||||
@@ -2341,7 +2341,7 @@ Trims from both sides until a valid non-space rune, "\t\txyz\t\t" -> "xyz"
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_space :: proc(s: string) -> string {
|
trim_space :: proc(s: string) -> string {
|
||||||
return trim_right_space(trim_left_space(s))
|
return trim_right_space(trim_left_space(s))
|
||||||
@@ -2352,7 +2352,7 @@ Trims null runes from the left, "\x00\x00testing\x00\x00" -> "testing\x00\x00"
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_left_null :: proc(s: string) -> string {
|
trim_left_null :: proc(s: string) -> string {
|
||||||
return trim_left_proc(s, is_null)
|
return trim_left_proc(s, is_null)
|
||||||
@@ -2363,7 +2363,7 @@ Trims null runes from the right, "\x00\x00testing\x00\x00" -> "\x00\x00testing"
|
|||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_right_null :: proc(s: string) -> string {
|
trim_right_null :: proc(s: string) -> string {
|
||||||
return trim_right_proc(s, is_null)
|
return trim_right_proc(s, is_null)
|
||||||
@@ -2373,7 +2373,7 @@ Trims null runes from both sides, "\x00\x00testing\x00\x00" -> "testing"
|
|||||||
|
|
||||||
**Inputs**
|
**Inputs**
|
||||||
- s: The input string
|
- s: The input string
|
||||||
**Returns** The trimmed string as a slice of the original
|
**Returns** The trimmed string as a slice of the original
|
||||||
*/
|
*/
|
||||||
trim_null :: proc(s: string) -> string {
|
trim_null :: proc(s: string) -> string {
|
||||||
return trim_right_null(trim_left_null(s))
|
return trim_right_null(trim_left_null(s))
|
||||||
@@ -2400,7 +2400,7 @@ Output:
|
|||||||
ing
|
ing
|
||||||
testing
|
testing
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of original, or the input string if no prefix was found
|
**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 {
|
trim_prefix :: proc(s, prefix: string) -> string {
|
||||||
if has_prefix(s, prefix) {
|
if has_prefix(s, prefix) {
|
||||||
@@ -2430,7 +2430,7 @@ Output:
|
|||||||
todo
|
todo
|
||||||
todo.doc
|
todo.doc
|
||||||
|
|
||||||
**Returns** The trimmed string as a slice of original, or the input string if no suffix was found
|
**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 {
|
trim_suffix :: proc(s, suffix: string) -> string {
|
||||||
if has_suffix(s, suffix) {
|
if has_suffix(s, suffix) {
|
||||||
@@ -2465,7 +2465,7 @@ Output:
|
|||||||
|
|
||||||
["testing", "this", "out", "nice", "done", "last"]
|
["testing", "this", "out", "nice", "done", "last"]
|
||||||
|
|
||||||
**Returns** An array of strings, or nil on empty substring or no matches
|
**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 {
|
split_multi :: proc(s: string, substrs: []string, allocator := context.allocator) -> []string #no_bounds_check {
|
||||||
if s == "" || len(substrs) <= 0 {
|
if s == "" || len(substrs) <= 0 {
|
||||||
@@ -2536,7 +2536,7 @@ Output:
|
|||||||
done
|
done
|
||||||
last
|
last
|
||||||
|
|
||||||
**Returns** A tuple containing the split string and a boolean indicating success or failure
|
**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 {
|
split_multi_iterate :: proc(it: ^string, substrs: []string) -> (res: string, ok: bool) #no_bounds_check {
|
||||||
if it == nil || len(it) == 0 || len(substrs) <= 0 {
|
if it == nil || len(it) == 0 || len(substrs) <= 0 {
|
||||||
@@ -2587,7 +2587,7 @@ Output:
|
|||||||
|
|
||||||
Hello?
|
Hello?
|
||||||
|
|
||||||
**Returns** A new string with invalid UTF-8 characters replaced
|
**Returns** A new string with invalid UTF-8 characters replaced
|
||||||
*/
|
*/
|
||||||
scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
|
scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
|
||||||
str := s
|
str := s
|
||||||
@@ -2644,7 +2644,7 @@ Output:
|
|||||||
|
|
||||||
abcxyz zyxcba
|
abcxyz zyxcba
|
||||||
|
|
||||||
**Returns** A reversed version of the input string
|
**Returns** A reversed version of the input string
|
||||||
*/
|
*/
|
||||||
reverse :: proc(s: string, allocator := context.allocator) -> string {
|
reverse :: proc(s: string, allocator := context.allocator) -> string {
|
||||||
str := s
|
str := s
|
||||||
@@ -2686,7 +2686,7 @@ Output:
|
|||||||
|
|
||||||
WARNING: Panics if tab_size <= 0
|
WARNING: Panics if tab_size <= 0
|
||||||
|
|
||||||
**Returns** A new string with tab characters expanded to the specified tab size
|
**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 {
|
expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> string {
|
||||||
if tab_size <= 0 {
|
if tab_size <= 0 {
|
||||||
@@ -2754,7 +2754,7 @@ Output:
|
|||||||
testing t hi s out
|
testing t hi s out
|
||||||
testing this out
|
testing this out
|
||||||
|
|
||||||
**Returns** A tuple with head (before the split), match (the separator), and tail (the end of the split) strings
|
**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) {
|
partition :: proc(str, sep: string) -> (head, match, tail: string) {
|
||||||
i := index(str, sep)
|
i := index(str, sep)
|
||||||
@@ -2781,7 +2781,7 @@ Centers the input string within a field of specified length by adding pad string
|
|||||||
- pad: The string used for padding on both sides
|
- pad: The string used for padding on both sides
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new string centered within a field of the specified length
|
**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 {
|
centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
|
||||||
n := rune_count(str)
|
n := rune_count(str)
|
||||||
@@ -2815,7 +2815,7 @@ Left-justifies the input string within a field of specified length by adding pad
|
|||||||
- pad: The string used for padding on the right side
|
- pad: The string used for padding on the right side
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new string left-justified within a field of the specified length
|
**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 {
|
left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
|
||||||
n := rune_count(str)
|
n := rune_count(str)
|
||||||
@@ -2848,7 +2848,7 @@ Right-justifies the input string within a field of specified length by adding pa
|
|||||||
- pad: The string used for padding on the left side
|
- pad: The string used for padding on the left side
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A new string right-justified within a field of the specified length
|
**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 {
|
right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
|
||||||
n := rune_count(str)
|
n := rune_count(str)
|
||||||
@@ -2905,7 +2905,7 @@ Splits a string into a slice of substrings at each instance of one or more conse
|
|||||||
- s: The input string
|
- s: The input string
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** A slice of substrings of the input string, or an empty slice if the input string only contains white space
|
**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 {
|
fields :: proc(s: string, allocator := context.allocator) -> []string #no_bounds_check {
|
||||||
n := 0
|
n := 0
|
||||||
@@ -2967,7 +2967,7 @@ Splits a string into a slice of substrings at each run of unicode code points `c
|
|||||||
|
|
||||||
NOTE: 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
|
NOTE: 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
|
||||||
|
|
||||||
**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
|
**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 {
|
fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator) -> []string #no_bounds_check {
|
||||||
substrings := make([dynamic]string, 0, 32, allocator)
|
substrings := make([dynamic]string, 0, 32, allocator)
|
||||||
@@ -3044,7 +3044,7 @@ NOTE: Does not perform internal allocation if Length of String b in Runes is Sma
|
|||||||
- a, b: The two strings to compare
|
- a, b: The two strings to compare
|
||||||
- allocator: (default is context.allocator)
|
- allocator: (default is context.allocator)
|
||||||
|
|
||||||
**Returns** The Levenshtein edit distance between the two strings
|
**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.
|
NOTE: This implementation is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user