aboutsummaryrefslogtreecommitdiff
path: root/core/bytes/bytes.odin
diff options
context:
space:
mode:
authorjason <jkercher@rlcsystems.com>2022-05-16 13:49:57 -0400
committerjason <jkercher@rlcsystems.com>2022-05-16 13:49:57 -0400
commitfff23e2bbbd1574debce9e0dee894f3cc84a04c4 (patch)
tree4055ea217375d34693861b39fc284e411f7c0366 /core/bytes/bytes.odin
parent97d1a6787189d7630650612f44c393f7a635019a (diff)
parent33895b6d927c70167f3bfa64c6cc1c15c4e428c5 (diff)
merge from upstream and convert to ^File types
Diffstat (limited to 'core/bytes/bytes.odin')
-rw-r--r--core/bytes/bytes.odin46
1 files changed, 45 insertions, 1 deletions
diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin
index 09a3ed259..f1737f3c5 100644
--- a/core/bytes/bytes.odin
+++ b/core/bytes/bytes.odin
@@ -10,7 +10,14 @@ clone :: proc(s: []byte, allocator := context.allocator, loc := #caller_location
return c[:len(s)]
}
-ptr_from_slice :: proc(str: []byte) -> ^byte {
+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 :: ptr_from_bytes
+ptr_from_bytes :: proc(str: []byte) -> ^byte {
d := transmute(mem.Raw_String)str
return d.data
}
@@ -134,6 +141,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 +177,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