Line data Source code
1 : // Internal Includes 2 : #include "kirke/split_iterator.h" 3 : #include "kirke/string.h" 4 : 5 3 : void split_iterator__initialize( SplitIterator* iterator, const String* string_, const String* delimiter ){ 6 3 : iterator->string = string_; 7 3 : iterator->delimiter = delimiter; 8 3 : iterator->position = 0; 9 3 : } 10 : 11 5 : bool split_iterator__next( SplitIterator* iterator, String* out_token ){ 12 5 : if( iterator->position >= iterator->string->length ){ 13 1 : return false; 14 : } 15 : 16 : /* 17 : * The do {} while( out_token->length == 0 ); prevents returning 0-length tokens, in case 18 : * one delimiter directly follows another. 19 : */ 20 : do{ 21 4 : split_iterator__rest( iterator, out_token ); 22 : 23 4 : if( string__index_of( out_token, iterator->delimiter, &out_token->length ) ){ 24 3 : iterator->position += out_token->length + iterator->delimiter->length; 25 : } 26 : else{ 27 1 : iterator->position += out_token->length; 28 : } 29 4 : } while( out_token->length == 0 ); 30 : 31 : 32 4 : return true; 33 : } 34 : 35 6 : void split_iterator__rest( SplitIterator* iterator, String* ref_rest ){ 36 6 : *ref_rest = (String){ 37 6 : .data = iterator->string->data + iterator->position, 38 6 : .length = iterator->string->length - iterator->position, 39 6 : .capacity = iterator->string->length - iterator->position, 40 : .element_size = sizeof( char ) 41 : }; 42 6 : }