From ccb38c3dc684ece829c7ca1066867cc5212533c3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 May 2022 12:54:14 +0100 Subject: Add _safe versions --- core/bytes/bytes.odin | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'core/bytes') 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 -- cgit v1.2.3 From 33895b6d927c70167f3bfa64c6cc1c15c4e428c5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 May 2022 01:43:43 +0100 Subject: Convert all uses of `*_from_slice` to `*_from_bytes` where appropriate --- core/bytes/bytes.odin | 3 ++- core/fmt/fmt.odin | 6 +++--- core/log/file_console_logger.odin | 2 +- core/os/os2/file.odin | 21 ++++++++++++--------- core/strings/builder.odin | 5 +++-- core/sys/darwin/xnu_system_call_wrappers.odin | 2 +- core/sys/unix/syscalls_linux.odin | 2 +- core/sys/windows/bcrypt.odin | 2 +- core/text/i18n/gettext.odin | 6 +++--- core/text/i18n/qt_linguist.odin | 6 +++--- vendor/microui/microui.odin | 2 +- 11 files changed, 31 insertions(+), 26 deletions(-) (limited to 'core/bytes') diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index 66fd20829..f1737f3c5 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -16,7 +16,8 @@ clone_safe :: proc(s: []byte, allocator := context.allocator, loc := #caller_loc return c[:len(s)], nil } -ptr_from_slice :: proc(str: []byte) -> ^byte { +ptr_from_slice :: ptr_from_bytes +ptr_from_bytes :: proc(str: []byte) -> ^byte { d := transmute(mem.Raw_String)str return d.data } diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index d006d0ef8..4f78cfcce 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -119,17 +119,17 @@ tprintf :: proc(fmt: string, args: ..any) -> string { // bprint procedures return a string using a buffer from an array bprint :: proc(buf: []byte, args: ..any, sep := " ") -> string { - sb := strings.builder_from_slice(buf[0:len(buf)]) + sb := strings.builder_from_bytes(buf[0:len(buf)]) return sbprint(buf=&sb, args=args, sep=sep) } // bprintln procedures return a string using a buffer from an array bprintln :: proc(buf: []byte, args: ..any, sep := " ") -> string { - sb := strings.builder_from_slice(buf[0:len(buf)]) + sb := strings.builder_from_bytes(buf[0:len(buf)]) return sbprintln(buf=&sb, args=args, sep=sep) } // bprintf procedures return a string using a buffer from an array bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string { - sb := strings.builder_from_slice(buf[0:len(buf)]) + sb := strings.builder_from_bytes(buf[0:len(buf)]) return sbprintf(&sb, fmt, ..args) } diff --git a/core/log/file_console_logger.odin b/core/log/file_console_logger.odin index cc019617f..7f0d3b07a 100644 --- a/core/log/file_console_logger.odin +++ b/core/log/file_console_logger.odin @@ -67,7 +67,7 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string h = data.file_handle } backing: [1024]byte //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths. - buf := strings.builder_from_slice(backing[:]) + buf := strings.builder_from_bytes(backing[:]) do_level_header(options, level, &buf) diff --git a/core/os/os2/file.odin b/core/os/os2/file.odin index 4b271b9ea..eb6d9e366 100644 --- a/core/os/os2/file.odin +++ b/core/os/os2/file.odin @@ -34,17 +34,20 @@ File_Flag :: enum { Trunc, Sparse, Close_On_Exec, + + Unbuffered_IO, } -O_RDONLY :: File_Flags{.Read} -O_WRONLY :: File_Flags{.Write} -O_RDWR :: File_Flags{.Read, .Write} -O_APPEND :: File_Flags{.Append} -O_CREATE :: File_Flags{.Create} -O_EXCL :: File_Flags{.Excl} -O_SYNC :: File_Flags{.Sync} -O_TRUNC :: File_Flags{.Trunc} -O_SPARSE :: File_Flags{.Sparse} +O_RDONLY :: File_Flags{.Read} +O_WRONLY :: File_Flags{.Write} +O_RDWR :: File_Flags{.Read, .Write} +O_APPEND :: File_Flags{.Append} +O_CREATE :: File_Flags{.Create} +O_EXCL :: File_Flags{.Excl} +O_SYNC :: File_Flags{.Sync} +O_TRUNC :: File_Flags{.Trunc} +O_SPARSE :: File_Flags{.Sparse} +O_CLOEXEC :: File_Flags{.Close_On_Exec} diff --git a/core/strings/builder.odin b/core/strings/builder.odin index d6065cf70..d51e21827 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -124,11 +124,11 @@ reset_builder :: proc(b: ^Builder) { used in `fmt.bprint*` bytes: [8]byte // <-- gets filled - builder := strings.builder_from_slice(bytes[:]) + builder := strings.builder_from_bytes(bytes[:]) strings.write_byte(&builder, 'a') -> "a" strings.write_byte(&builder, 'b') -> "ab" */ -builder_from_slice :: proc(backing: []byte) -> Builder { +builder_from_bytes :: proc(backing: []byte) -> Builder { s := transmute(mem.Raw_Slice)backing d := mem.Raw_Dynamic_Array{ data = s.data, @@ -140,6 +140,7 @@ builder_from_slice :: proc(backing: []byte) -> Builder { buf = transmute([dynamic]byte)d, } } +builder_from_slice :: builder_from_bytes // cast the builder byte buffer to a string and return it to_string :: proc(b: Builder) -> string { diff --git a/core/sys/darwin/xnu_system_call_wrappers.odin b/core/sys/darwin/xnu_system_call_wrappers.odin index 4e4227f1f..685f75ffa 100644 --- a/core/sys/darwin/xnu_system_call_wrappers.odin +++ b/core/sys/darwin/xnu_system_call_wrappers.odin @@ -402,7 +402,7 @@ syscall_openat :: #force_inline proc(fd: int, path: cstring, oflag: u32, mode: u return cast(c.int)intrinsics.syscall(unix_offset_syscall(.openat), uintptr(fd), transmute(uintptr)path, uintptr(oflag), uintptr(mode)) } -syscall_getentropy :: #force_inline proc(buf: ^u8, buflen: u64) -> c.int { +syscall_getentropy :: #force_inline proc(buf: [^]u8, buflen: u64) -> c.int { return cast(c.int)intrinsics.syscall(unix_offset_syscall(.getentropy), uintptr(buf), uintptr(buflen)) } diff --git a/core/sys/unix/syscalls_linux.odin b/core/sys/unix/syscalls_linux.odin index f50ae825b..7300193df 100644 --- a/core/sys/unix/syscalls_linux.odin +++ b/core/sys/unix/syscalls_linux.odin @@ -1522,6 +1522,6 @@ sys_gettid :: proc "contextless" () -> int { return cast(int)intrinsics.syscall(SYS_gettid) } -sys_getrandom :: proc "contextless" (buf: ^byte, buflen: int, flags: uint) -> int { +sys_getrandom :: proc "contextless" (buf: [^]byte, buflen: int, flags: uint) -> int { return cast(int)intrinsics.syscall(SYS_getrandom, buf, cast(uintptr)(buflen), cast(uintptr)(flags)) } diff --git a/core/sys/windows/bcrypt.odin b/core/sys/windows/bcrypt.odin index ed28d5b7f..52eb4b1b6 100644 --- a/core/sys/windows/bcrypt.odin +++ b/core/sys/windows/bcrypt.odin @@ -7,5 +7,5 @@ BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD : 0x00000002 @(default_calling_convention="stdcall") foreign bcrypt { - BCryptGenRandom :: proc(hAlgorithm: LPVOID, pBuffer: ^u8, cbBuffer: ULONG, dwFlags: ULONG) -> LONG --- + BCryptGenRandom :: proc(hAlgorithm: LPVOID, pBuffer: [^]u8, cbBuffer: ULONG, dwFlags: ULONG) -> LONG --- } diff --git a/core/text/i18n/gettext.odin b/core/text/i18n/gettext.odin index eed73855b..d99ec1c9b 100644 --- a/core/text/i18n/gettext.odin +++ b/core/text/i18n/gettext.odin @@ -18,7 +18,7 @@ import "core:os" import "core:strings" import "core:bytes" -parse_mo_from_slice :: proc(data: []u8, options := DEFAULT_PARSE_OPTIONS, pluralizer: proc(int) -> int = nil, allocator := context.allocator) -> (translation: ^Translation, err: Error) { +parse_mo_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTIONS, pluralizer: proc(int) -> int = nil, allocator := context.allocator) -> (translation: ^Translation, err: Error) { context.allocator = allocator /* An MO file should have at least a 4-byte magic, 2 x 2 byte version info, @@ -126,10 +126,10 @@ parse_mo_file :: proc(filename: string, options := DEFAULT_PARSE_OPTIONS, plural if !data_ok { return {}, .File_Error } - return parse_mo_from_slice(data, options, pluralizer, allocator) + return parse_mo_from_bytes(data, options, pluralizer, allocator) } -parse_mo :: proc { parse_mo_file, parse_mo_from_slice } +parse_mo :: proc { parse_mo_file, parse_mo_from_bytes } /* Helpers. diff --git a/core/text/i18n/qt_linguist.odin b/core/text/i18n/qt_linguist.odin index 15a88a42f..036a89eeb 100644 --- a/core/text/i18n/qt_linguist.odin +++ b/core/text/i18n/qt_linguist.odin @@ -27,7 +27,7 @@ TS_XML_Options := xml.Options{ expected_doctype = "TS", } -parse_qt_linguist_from_slice :: proc(data: []u8, options := DEFAULT_PARSE_OPTIONS, pluralizer: proc(int) -> int = nil, allocator := context.allocator) -> (translation: ^Translation, err: Error) { +parse_qt_linguist_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTIONS, pluralizer: proc(int) -> int = nil, allocator := context.allocator) -> (translation: ^Translation, err: Error) { context.allocator = allocator ts, xml_err := xml.parse(data, TS_XML_Options) @@ -150,7 +150,7 @@ parse_qt_linguist_file :: proc(filename: string, options := DEFAULT_PARSE_OPTION if !data_ok { return {}, .File_Error } - return parse_qt_linguist_from_slice(data, options, pluralizer, allocator) + return parse_qt_linguist_from_bytes(data, options, pluralizer, allocator) } -parse_qt :: proc { parse_qt_linguist_file, parse_qt_linguist_from_slice } \ No newline at end of file +parse_qt :: proc { parse_qt_linguist_file, parse_qt_linguist_from_bytes } \ No newline at end of file diff --git a/vendor/microui/microui.odin b/vendor/microui/microui.odin index 947f59f40..09a6b8430 100644 --- a/vendor/microui/microui.odin +++ b/vendor/microui/microui.odin @@ -309,7 +309,7 @@ init :: proc(ctx: ^Context) { ctx.draw_frame = default_draw_frame ctx._style = default_style ctx.style = &ctx._style - ctx.text_input = strings.builder_from_slice(ctx._text_store[:]) + ctx.text_input = strings.builder_from_bytes(ctx._text_store[:]) } begin :: proc(ctx: ^Context) { -- cgit v1.2.3