aboutsummaryrefslogtreecommitdiff
path: root/core/bytes/bytes.odin
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-05-12 12:54:14 +0100
committergingerBill <bill@gingerbill.org>2022-05-12 12:54:14 +0100
commitccb38c3dc684ece829c7ca1066867cc5212533c3 (patch)
tree48a2cf15e14f4e18bb6cf7f5b7ca28402b79e94c /core/bytes/bytes.odin
parentcc81057d21f1dbe9a377637b1458dfc5a16f5fd0 (diff)
Add _safe versions
Diffstat (limited to 'core/bytes/bytes.odin')
-rw-r--r--core/bytes/bytes.odin43
1 files changed, 43 insertions, 0 deletions
diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin
index 09a3ed259..66fd20829 100644
--- a/core/bytes/bytes.odin
+++ b/core/bytes/bytes.odin
@@ -10,6 +10,12 @@ clone :: proc(s: []byte, allocator := context.allocator, loc := #caller_location
return c[:len(s)]
}
+clone_safe :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: mem.Allocator_Error) {
+ c := make([]byte, len(s), allocator, loc) or_return
+ copy(c, s)
+ return c[:len(s)], nil
+}
+
ptr_from_slice :: proc(str: []byte) -> ^byte {
d := transmute(mem.Raw_String)str
return d.data
@@ -134,6 +140,25 @@ join :: proc(a: [][]byte, sep: []byte, allocator := context.allocator) -> []byte
return b
}
+join_safe :: proc(a: [][]byte, sep: []byte, allocator := context.allocator) -> (data: []byte, err: mem.Allocator_Error) {
+ if len(a) == 0 {
+ return nil, nil
+ }
+
+ n := len(sep) * (len(a) - 1)
+ for s in a {
+ n += len(s)
+ }
+
+ b := make([]byte, n, allocator) or_return
+ i := copy(b, a[0])
+ for s in a[1:] {
+ i += copy(b[i:], sep)
+ i += copy(b[i:], s)
+ }
+ return b, nil
+}
+
concatenate :: proc(a: [][]byte, allocator := context.allocator) -> []byte {
if len(a) == 0 {
return nil
@@ -151,6 +176,24 @@ concatenate :: proc(a: [][]byte, allocator := context.allocator) -> []byte {
return b
}
+concatenate_safe :: proc(a: [][]byte, allocator := context.allocator) -> (data: []byte, err: mem.Allocator_Error) {
+ if len(a) == 0 {
+ return nil, nil
+ }
+
+ n := 0
+ for s in a {
+ n += len(s)
+ }
+ b := make([]byte, n, allocator) or_return
+ i := 0
+ for s in a {
+ i += copy(b[i:], s)
+ }
+ return b, nil
+}
+
+
@private
_split :: proc(s, sep: []byte, sep_save, n: int, allocator := context.allocator) -> [][]byte {
s, n := s, n