From 85f8c8df91022fae3fef996cd45b9e631e6b2a66 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 11 Dec 2021 12:04:34 +0000 Subject: Fix `fields_proc` in `strings` and `bytes` --- core/bytes/bytes.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/bytes') diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index cbc1e2506..1e83b93c8 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -1143,7 +1143,7 @@ fields_proc :: proc(s: []byte, f: proc(rune) -> bool, allocator := context.alloc } if start >= 0 { - append(&subslices, s[start : end]) + append(&subslices, s[start : len(s)]) } return subslices[:] -- cgit v1.2.3 From a032a2ef322de150587117396eaf6e5ae7a11768 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 1 Jan 2022 15:33:19 +0000 Subject: Remove the hidden NUL byte past the end from `bytes.clone` --- core/bytes/bytes.odin | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'core/bytes') diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index 1e83b93c8..1bf11e0b0 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -5,9 +5,8 @@ import "core:unicode" import "core:unicode/utf8" clone :: proc(s: []byte, allocator := context.allocator, loc := #caller_location) -> []byte { - c := make([]byte, len(s)+1, allocator, loc) + c := make([]byte, len(s), allocator, loc) copy(c, s) - c[len(s)] = 0 return c[:len(s)] } -- cgit v1.2.3 From f56114719081164fb38a700dacc298173b0f34dc Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 14 Feb 2022 10:57:29 +0000 Subject: Correct _split_iterator --- core/bytes/bytes.odin | 40 +++++++------------------------- core/strings/strings.odin | 59 ++++++++++------------------------------------- 2 files changed, 20 insertions(+), 79 deletions(-) (limited to 'core/bytes') diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index 1bf11e0b0..fb36b2d6e 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -219,39 +219,24 @@ split_after_n :: proc(s, sep: []byte, n: int, allocator := context.allocator) -> @private _split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []byte, ok: bool) { - s, n := s, n - - if n == 0 { - return - } - - if sep == nil { + if sep == "" { res = s[:] ok = true s^ = s[len(s):] return } - if n < 0 { - n = count(s^, sep) + 1 - } - - n -= 1 - - i := 0 - for ; i < n; i += 1 { - m := index(s^, sep) - if m < 0 { - break - } + m := index(s^, sep) + if m < 0 { + // not found + res = s[:] + ok = res != "" + s^ = s[len(s):] + } else { res = s[:m+sep_save] ok = true s^ = s[m+len(sep):] - return } - res = s[:] - ok = res != nil - s^ = s[len(s):] return } @@ -260,19 +245,10 @@ split_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) { return _split_iterator(s, sep, 0, -1) } -split_n_iterator :: proc(s: ^[]byte, sep: []byte, n: int) -> ([]byte, bool) { - return _split_iterator(s, sep, 0, n) -} - split_after_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) { return _split_iterator(s, sep, len(sep), -1) } -split_after_n_iterator :: proc(s: ^[]byte, sep: []byte, n: int) -> ([]byte, bool) { - return _split_iterator(s, sep, len(sep), n) -} - - index_byte :: proc(s: []byte, c: byte) -> int { for i := 0; i < len(s); i += 1 { diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 67046c669..4daa0bacd 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -298,13 +298,7 @@ split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> @private -_split_iterator :: proc(s: ^string, sep: string, sep_save, n: int) -> (res: string, ok: bool) { - s, n := s, n - - if n == 0 { - return - } - +_split_iterator :: proc(s: ^string, sep: string, sep_save: int) -> (res: string, ok: bool) { if sep == "" { res = s[:] ok = true @@ -312,44 +306,27 @@ _split_iterator :: proc(s: ^string, sep: string, sep_save, n: int) -> (res: stri return } - if n < 0 { - n = count(s^, sep) + 1 - } - - n -= 1 - - i := 0 - for ; i < n; i += 1 { - m := index(s^, sep) - if m < 0 { - break - } + m := index(s^, sep) + if m < 0 { + // not found + res = s[:] + ok = res != "" + s^ = s[len(s):] + } else { res = s[:m+sep_save] ok = true s^ = s[m+len(sep):] - return } - res = s[:] - ok = res != "" - s^ = s[len(s):] return } split_iterator :: proc(s: ^string, sep: string) -> (string, bool) { - return _split_iterator(s, sep, 0, -1) -} - -split_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) { - return _split_iterator(s, sep, 0, n) + return _split_iterator(s, sep, 0) } split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) { - return _split_iterator(s, sep, len(sep), -1) -} - -split_after_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) { - return _split_iterator(s, sep, len(sep), n) + return _split_iterator(s, sep, len(sep)) } @@ -402,25 +379,13 @@ split_lines_after_n :: proc(s: string, n: int, allocator := context.allocator) - split_lines_iterator :: proc(s: ^string) -> (line: string, ok: bool) { sep :: "\n" - line = _split_iterator(s, sep, 0, -1) or_return - return _trim_cr(line), true -} - -split_lines_n_iterator :: proc(s: ^string, n: int) -> (line: string, ok: bool) { - sep :: "\n" - line = _split_iterator(s, sep, 0, n) or_return + line = _split_iterator(s, sep, 0) or_return return _trim_cr(line), true } split_lines_after_iterator :: proc(s: ^string) -> (line: string, ok: bool) { sep :: "\n" - line = _split_iterator(s, sep, len(sep), -1) or_return - return _trim_cr(line), true -} - -split_lines_after_n_iterator :: proc(s: ^string, n: int) -> (line: string, ok: bool) { - sep :: "\n" - line = _split_iterator(s, sep, len(sep), n) or_return + line = _split_iterator(s, sep, len(sep)) or_return return _trim_cr(line), true } -- cgit v1.2.3 From 2e7157ae9cd0eb38133a750e7f4845661d8751ba Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 14 Feb 2022 11:01:34 +0000 Subject: Correct bytes._split_iterator --- core/bytes/bytes.odin | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'core/bytes') diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index fb36b2d6e..09a3ed259 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -218,8 +218,8 @@ split_after_n :: proc(s, sep: []byte, n: int, allocator := context.allocator) -> @private -_split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []byte, ok: bool) { - if sep == "" { +_split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save: int) -> (res: []byte, ok: bool) { + if len(sep) == 0 { res = s[:] ok = true s^ = s[len(s):] @@ -230,7 +230,7 @@ _split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []by if m < 0 { // not found res = s[:] - ok = res != "" + ok = len(res) != 0 s^ = s[len(s):] } else { res = s[:m+sep_save] @@ -242,11 +242,11 @@ _split_iterator :: proc(s: ^[]byte, sep: []byte, sep_save, n: int) -> (res: []by split_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) { - return _split_iterator(s, sep, 0, -1) + return _split_iterator(s, sep, 0) } split_after_iterator :: proc(s: ^[]byte, sep: []byte) -> ([]byte, bool) { - return _split_iterator(s, sep, len(sep), -1) + return _split_iterator(s, sep, len(sep)) } -- cgit v1.2.3