From d33350e3a5280cee5af74ed71f776f783a49e362 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 22 Apr 2021 15:49:10 +0100 Subject: [PATCH] Add truncate_to_byte and truncate_to_rune for packages strings and bytes --- core/bytes/bytes.odin | 15 +++++++++++++++ core/strings/strings.odin | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index c951e19a0..639e36721 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -16,6 +16,21 @@ ptr_from_slice :: proc(str: []byte) -> ^byte { return d.data; } +truncate_to_byte :: proc(str: []byte, b: byte) -> []byte { + n := index_byte(str, b); + if n < 0 { + n = len(str); + } + return str[:n]; +} +truncate_to_rune :: proc(str: []byte, r: rune) -> []byte { + n := index_rune(str, r); + if n < 0 { + n = len(str); + } + return str[:n]; +} + // Compares two strings, returning a value representing which one comes first lexiographically. // -1 for `a`; 1 for `b`, or 0 if they are equal. compare :: proc(lhs, rhs: []byte) -> int { diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 382d0ca25..2aa2ac71d 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -33,6 +33,21 @@ unsafe_string_to_cstring :: proc(str: string) -> cstring { return cstring(d.data); } +truncate_to_byte :: proc(str: string, b: byte) -> string { + n := index_byte(str, b); + if n < 0 { + n = len(str); + } + return str[:n]; +} +truncate_to_rune :: proc(str: string, r: rune) -> string { + n := index_rune(str, r); + if n < 0 { + n = len(str); + } + return str[:n]; +} + // Compares two strings, returning a value representing which one comes first lexiographically. // -1 for `a`; 1 for `b`, or 0 if they are equal. compare :: proc(lhs, rhs: string) -> int {