aboutsummaryrefslogtreecommitdiff
path: root/core/strings/strings.odin
diff options
context:
space:
mode:
Diffstat (limited to 'core/strings/strings.odin')
-rw-r--r--core/strings/strings.odin75
1 files changed, 41 insertions, 34 deletions
diff --git a/core/strings/strings.odin b/core/strings/strings.odin
index cda12f9dc..75c7154af 100644
--- a/core/strings/strings.odin
+++ b/core/strings/strings.odin
@@ -5,7 +5,6 @@ import "base:intrinsics"
import "base:runtime"
import "core:bytes"
import "core:io"
-import "core:mem"
import "core:unicode"
import "core:unicode/utf8"
@@ -23,7 +22,7 @@ Returns:
- res: The cloned string
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
c := make([]byte, len(s), allocator, loc) or_return
copy(c, s)
return string(c), nil
@@ -43,7 +42,7 @@ Returns:
- res: A cloned cstring with an appended null-byte
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: cstring, err: mem.Allocator_Error) #optional_allocator_error {
+clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: cstring, err: runtime.Allocator_Error) #optional_allocator_error {
c := make([]byte, len(s)+1, allocator, loc) or_return
copy(c, s)
c[len(s)] = 0
@@ -63,7 +62,7 @@ Returns:
- res: A string created from the byte pointer and length
*/
string_from_ptr :: proc(ptr: ^byte, len: int) -> (res: string) {
- return transmute(string)mem.Raw_String{ptr, len}
+ return transmute(string)runtime.Raw_String{ptr, len}
}
/*
@@ -97,7 +96,7 @@ Returns:
- res: The converted cstring
*/
unsafe_string_to_cstring :: proc(str: string) -> (res: cstring) {
- d := transmute(mem.Raw_String)str
+ d := transmute(runtime.Raw_String)str
return cstring(d.data)
}
@@ -153,7 +152,7 @@ Returns:
- res: The cloned string from the byte array with a null-byte
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone_from_bytes :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+clone_from_bytes :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
c := make([]byte, len(s)+1, allocator, loc) or_return
copy(c, s)
c[len(s)] = 0
@@ -174,7 +173,7 @@ Returns:
- res: The cloned string from the cstring
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone_from_cstring :: proc(s: cstring, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+clone_from_cstring :: proc(s: cstring, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
return clone(string(s), allocator, loc)
}
@@ -195,7 +194,7 @@ Returns:
- res: The cloned string from the byte pointer and length
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone_from_ptr :: proc(ptr: ^byte, len: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+clone_from_ptr :: proc(ptr: ^byte, len: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
s := string_from_ptr(ptr, len)
return clone(s, allocator, loc)
}
@@ -225,7 +224,7 @@ Returns:
- res: The cloned string from the null-terminated cstring and byte length
- err: An optional allocator error if one occured, `nil` otherwise
*/
-clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+clone_from_cstring_bounded :: proc(ptr: cstring, len: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
s := string_from_ptr((^u8)(ptr), len)
s = truncate_to_byte(s, 0)
return clone(s, allocator, loc)
@@ -242,8 +241,16 @@ Inputs:
Returns:
- result: `-1` if `lhs` comes first, `1` if `rhs` comes first, or `0` if they are equal
*/
-compare :: proc "contextless" (lhs, rhs: string) -> (result: int) {
- return mem.compare(transmute([]byte)lhs, transmute([]byte)rhs)
+compare :: proc "contextless" (lhs, rhs: string) -> (res: int) {
+ a := transmute([]byte)lhs
+ b := transmute([]byte)rhs
+ res = runtime.memory_compare(raw_data(a), raw_data(b), min(len(a), len(b)))
+ if res == 0 && len(a) != len(b) {
+ return len(a) <= len(b) ? -1 : +1
+ } else if len(a) == 0 && len(b) == 0 {
+ return 0
+ }
+ return res
}
/*
@@ -634,7 +641,7 @@ Output:
a...b...c
*/
-join :: proc(a: []string, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+join :: proc(a: []string, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
if len(a) == 0 {
return "", nil
}
@@ -681,7 +688,7 @@ Output:
abc
*/
-concatenate :: proc(a: []string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+concatenate :: proc(a: []string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
if len(a) == 0 {
return "", nil
}
@@ -788,7 +795,7 @@ Output:
example
*/
-cut_clone :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+cut_clone :: proc(s: string, rune_offset := int(0), rune_length := int(0), allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
res = cut(s, rune_offset, rune_length)
return clone(res, allocator, loc)
}
@@ -815,7 +822,7 @@ Returns:
- err: An optional allocator error if one occured, `nil` otherwise
*/
@private
-_split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) {
+_split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) {
s, n := s_, n_
if n == 0 {
@@ -895,7 +902,7 @@ Output:
["aaa", "bbb", "ccc", "ddd", "eee"]
*/
-split :: proc(s, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split :: proc(s, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
return _split(s, sep, 0, -1, allocator, loc)
}
@@ -933,7 +940,7 @@ Output:
["aaa", "bbb", "ccc.ddd.eee"]
*/
-split_n :: proc(s, sep: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_n :: proc(s, sep: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
return _split(s, sep, 0, n, allocator, loc)
}
@@ -970,7 +977,7 @@ Output:
["aaa.", "bbb.", "ccc.", "ddd.", "eee"]
*/
-split_after :: proc(s, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_after :: proc(s, sep: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
return _split(s, sep, len(sep), -1, allocator, loc)
}
@@ -1008,7 +1015,7 @@ Output:
["aaa.", "bbb.", "ccc.ddd.eee"]
*/
-split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
return _split(s, sep, len(sep), n, allocator, loc)
}
@@ -1224,7 +1231,7 @@ Output:
["a", "b", "c", "d", "e"]
*/
-split_lines :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_lines :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
sep :: "\n"
lines := _split(s, sep, 0, -1, allocator, loc) or_return
for &line in lines {
@@ -1266,7 +1273,7 @@ Output:
["a", "b", "c\nd\ne"]
*/
-split_lines_n :: proc(s: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_lines_n :: proc(s: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
sep :: "\n"
lines := _split(s, sep, 0, n, allocator, loc) or_return
for &line in lines {
@@ -1307,7 +1314,7 @@ Output:
["a\n", "b\n", "c\n", "d\n", "e"]
*/
-split_lines_after :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_lines_after :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
sep :: "\n"
lines := _split(s, sep, len(sep), -1, allocator, loc) or_return
for &line in lines {
@@ -1350,7 +1357,7 @@ Output:
["a\n", "b\n", "c\nd\ne"]
*/
-split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error {
+split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error {
sep :: "\n"
lines := _split(s, sep, len(sep), n, allocator, loc) or_return
for &line in lines {
@@ -2008,7 +2015,7 @@ Output:
abcabc
*/
-repeat :: proc(s: string, count: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+repeat :: proc(s: string, count: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
if count < 0 {
panic("strings: negative repeat count")
} else if count > 0 && (len(s)*count)/count != len(s) {
@@ -2711,7 +2718,7 @@ Output:
["testing", "this", "out", "nice", "done", "last"]
*/
-split_multi :: proc(s: string, substrs: []string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error #no_bounds_check {
+split_multi :: proc(s: string, substrs: []string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error #no_bounds_check {
if s == "" || len(substrs) <= 0 {
return nil, nil
}
@@ -2842,7 +2849,7 @@ Output:
Hello?
*/
-scrub :: proc(s: string, replacement: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+scrub :: proc(s: string, replacement: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
str := s
b: Builder
builder_init(&b, 0, len(s), allocator, loc) or_return
@@ -2904,7 +2911,7 @@ Output:
abcxyz zyxcba
*/
-reverse :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+reverse :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
str := s
n := len(str)
buf := make([]byte, n, allocator, loc) or_return
@@ -2951,7 +2958,7 @@ Output:
abc1 abc2 abc3
*/
-expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
if tab_size <= 0 {
panic("tab size must be positive")
}
@@ -3063,7 +3070,7 @@ Returns:
- res: A new string centered within a field of the specified length
- err: An optional allocator error if one occured, `nil` otherwise
*/
-centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
n := rune_count(str)
if n >= length || pad == "" {
return clone(str, allocator, loc)
@@ -3100,7 +3107,7 @@ Returns:
- res: A new string left-justified within a field of the specified length
- err: An optional allocator error if one occured, `nil` otherwise
*/
-left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
n := rune_count(str)
if n >= length || pad == "" {
return clone(str, allocator, loc)
@@ -3136,7 +3143,7 @@ Returns:
- res: A new string right-justified within a field of the specified length
- err: An optional allocator error if one occured, `nil` otherwise
*/
-right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: mem.Allocator_Error) #optional_allocator_error {
+right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator, loc := #caller_location) -> (res: string, err: runtime.Allocator_Error) #optional_allocator_error {
n := rune_count(str)
if n >= length || pad == "" {
return clone(str, allocator, loc)
@@ -3197,7 +3204,7 @@ Returns:
- res: A slice of substrings of the input string, or an empty slice if the input string only contains white space
- err: An optional allocator error if one occured, `nil` otherwise
*/
-fields :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error #no_bounds_check {
+fields :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error #no_bounds_check {
n := 0
was_space := 1
set_bits := u8(0)
@@ -3263,7 +3270,7 @@ Returns:
- res: A slice of substrings of the input string, or an empty slice if all code points in the input string satisfy the predicate or if the input string is empty
- err: An optional allocator error if one occured, `nil` otherwise
*/
-fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: mem.Allocator_Error) #optional_allocator_error #no_bounds_check {
+fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator, loc := #caller_location) -> (res: []string, err: runtime.Allocator_Error) #optional_allocator_error #no_bounds_check {
substrings := make([dynamic]string, 0, 32, allocator, loc) or_return
start, end := -1, -1
@@ -3347,7 +3354,7 @@ Returns:
NOTE: This implementation is a single-row-version of the Wagner–Fischer algorithm, based on C code by Martin Ettl.
*/
-levenshtein_distance :: proc(a, b: string, allocator := context.allocator, loc := #caller_location) -> (res: int, err: mem.Allocator_Error) #optional_allocator_error {
+levenshtein_distance :: proc(a, b: string, allocator := context.allocator, loc := #caller_location) -> (res: int, err: runtime.Allocator_Error) #optional_allocator_error {
LEVENSHTEIN_DEFAULT_COSTS: []int : {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,