diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-04-27 14:37:15 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-04-27 14:37:15 +0200 |
| commit | c4e0d1efa1ec655bae9134b95a0fcd060cc7bbea (patch) | |
| tree | c29bd0b78138e8d67aebe34ac689d13e32d9d15f /core/bytes/bytes.odin | |
| parent | 6e61abc7d06f22129f93110a9f652c3eec21f0c6 (diff) | |
| parent | 9349dfba8fec53f52f77a0c8928e115ec93ff447 (diff) | |
Merge branch 'master' into xml
Diffstat (limited to 'core/bytes/bytes.odin')
| -rw-r--r-- | core/bytes/bytes.odin | 51 |
1 files changed, 13 insertions, 38 deletions
diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin index cbc1e2506..09a3ed259 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)] } @@ -219,61 +218,37 @@ 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 { +_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):] 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 = len(res) != 0 + 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 } 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) + 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)) } -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 { @@ -1143,7 +1118,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[:] |