From 5616ff9a405a335103a3fed705e47d864fb05776 Mon Sep 17 00:00:00 2001 From: WalterPlinge <22519813+WalterPlinge@users.noreply.github.com> Date: Fri, 25 Mar 2022 20:03:39 +0000 Subject: [PATCH 1/2] Add fields_iterator proc Adds a `fields_iterator` proc to `core:strings` --- core/strings/strings.odin | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/core/strings/strings.odin b/core/strings/strings.odin index fe25ad3bf..21e33f22b 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -1365,3 +1365,35 @@ fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.alloc return substrings[:] } + + +// fields_iterator returns the first run of characters in s that does not contain white space, defined by unicode.is_space +// s will then start from any space after the substring, or be a nil string if the substring was the remaining characters +fields_iterator :: proc(s: ^string) -> (field: string, ok: bool) { + start, end := -1, -1 + for r, offset in s { + end = offset + if unicode.is_space(r) { + if start >= 0 { + field = s[start : end] + ok = true + s^ = s[end:] + return + } + } else { + if start < 0 { + start = end + } + } + } + + // if either of these are true, the string did not contain any characters + if end < 0 || start < 0 { + return "", false + } + + field = s[:len(s)] + ok = true + s^ = s[len(s):] + return +} From 9080fa4a9d432b172abbad83b870876631c8eb44 Mon Sep 17 00:00:00 2001 From: WalterPlinge <22519813+WalterPlinge@users.noreply.github.com> Date: Fri, 25 Mar 2022 20:51:04 +0000 Subject: [PATCH 2/2] Update fields_iterator comment Added ticks for identifiers --- core/strings/strings.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 21e33f22b..53d7ea647 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -1367,8 +1367,8 @@ fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.alloc } -// fields_iterator returns the first run of characters in s that does not contain white space, defined by unicode.is_space -// s will then start from any space after the substring, or be a nil string if the substring was the remaining characters +// `fields_iterator` returns the first run of characters in `s` that does not contain white space, defined by `unicode.is_space` +// `s` will then start from any space after the substring, or be an empty string if the substring was the remaining characters fields_iterator :: proc(s: ^string) -> (field: string, ok: bool) { start, end := -1, -1 for r, offset in s {