Skip to content

Instantly share code, notes, and snippets.

@oskarnp
Last active December 5, 2024 15:35
Show Gist options
  • Save oskarnp/1dea3f3ea038db04344404ba09c77c79 to your computer and use it in GitHub Desktop.
Save oskarnp/1dea3f3ea038db04344404ba09c77c79 to your computer and use it in GitHub Desktop.
import "core:fmt"
import "core:strings"
main :: proc() {
// Demo: Split `text` on any character in delimiter string.
{
text := "\nThis is a long text\nwith line breaks.";
lines_itr := split_iterator(text, "\n");
for line in split_any_next(&lines_itr) {
fmt.println(string(line));
}
}
fmt.println("---");
// Demo: Split `text` by substring (match on all characters in delimiter)
{
text := "This ++ string ++ was ++ separated ++ by ++ double ++ plus ++ signs ++ for ++ some ++ reason.";
words_itr := split_iterator(text, " ++ ");
for word in split_all_next(&words_itr) {
fmt.printf("%v ", string(word));
}
fmt.printf("\n");
}
}
Split_Iterator :: struct {
index: int,
buffer: []u8,
delims: []u8,
};
split_iterator_slice :: proc(buffer, delims: []u8) -> Split_Iterator {
return Split_Iterator{index = 0, buffer = buffer, delims = delims};
}
split_iterator_string :: proc(buffer, delims: string) -> Split_Iterator {
return Split_Iterator{
index = 0,
buffer = transmute([]u8) buffer,
delims = transmute([]u8) delims
};
}
split_iterator :: proc{split_iterator_string, split_iterator_slice};
split_any_next :: proc(it: ^Split_Iterator) -> (val: []u8, idx: int, cond: bool) {
if cond = it.index < len(it.buffer); cond {
idx = it.index;
start := string(it.buffer[it.index:]);
delims := string(it.delims);
delim_idx := strings.index_any(start, delims);
if delim_idx != -1 {
val = transmute([]u8) start[:delim_idx];
it.index += delim_idx + 1;
} else {
val = transmute([]u8) start[:];
it.index = len(it.buffer);
}
}
return;
}
split_all_next :: proc(it: ^Split_Iterator) -> (val: []u8, idx: int, cond: bool) {
if cond = it.index < len(it.buffer); cond {
idx = it.index;
start := string(it.buffer[it.index:]);
substr := string(it.delims);
delim_idx := strings.index(start, substr);
if delim_idx != -1 {
val = transmute([]u8) start[:delim_idx];
it.index += delim_idx + len(substr);
} else {
val = transmute([]u8) start[:];
it.index = len(it.buffer);
}
}
return;
}
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
@oskarnp
Copy link
Author

oskarnp commented Feb 17, 2020

I think idx is supposed to return the index of the split, not the index within the original string. So known bug if that's the case.

@Tetralux
Copy link

idx is supposed to be the counter variable, not an index into the string/bytes.
The first loop should be idx == 0, the second idx == 1, the third idx == 2, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment