From: Ray Strode Date: Sat, 2 Dec 2023 17:22:35 +0000 (-0500) Subject: rich-text: Add iterator API X-Git-Tag: 23.51.283~17^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d9e3c0350163fca8b0c7745ce5084f41c71e84e;p=thirdparty%2Fplymouth.git rich-text: Add iterator API This commit adds a small wrapper around ply_rich_text_get_characters to make it easier to iterate over every character in a loop. --- diff --git a/src/libply-splash-core/ply-rich-text.c b/src/libply-splash-core/ply-rich-text.c index 0cc87893..13a9791f 100644 --- a/src/libply-splash-core/ply-rich-text.c +++ b/src/libply-splash-core/ply-rich-text.c @@ -219,3 +219,36 @@ ply_rich_text_set_character (ply_rich_text_t *rich_text, character->length = length; character->style = style; } + +void +ply_rich_text_iterator_init (ply_rich_text_iterator_t *iterator, + ply_rich_text_t *rich_text, + ply_rich_text_span_t *span) +{ + iterator->rich_text = rich_text; + iterator->span = *span; + iterator->current_offset = span->offset; +} + +bool +ply_rich_text_iterator_next (ply_rich_text_iterator_t *iterator, + ply_rich_text_character_t **character) +{ + ply_rich_text_t *rich_text = iterator->rich_text; + ply_rich_text_span_t *span = &iterator->span; + ply_rich_text_character_t **characters = ply_rich_text_get_characters (rich_text); + + if (iterator->current_offset >= span->offset + span->range) { + return false; + } + + if (characters[iterator->current_offset] == NULL) { + return false; + } + + *character = characters[iterator->current_offset]; + + iterator->current_offset++; + + return true; +} diff --git a/src/libply-splash-core/ply-rich-text.h b/src/libply-splash-core/ply-rich-text.h index 27692deb..6455158c 100644 --- a/src/libply-splash-core/ply-rich-text.h +++ b/src/libply-splash-core/ply-rich-text.h @@ -52,6 +52,13 @@ typedef struct ssize_t range; } ply_rich_text_span_t; +typedef struct +{ + ply_rich_text_t *rich_text; + ply_rich_text_span_t span; + ssize_t current_offset; +} ply_rich_text_iterator_t; + #ifndef PLY_HIDE_FUNCTION_DECLARATIONS ply_rich_text_t *ply_rich_text_new (void); void ply_rich_text_take_reference (ply_rich_text_t *rich_text); @@ -77,5 +84,11 @@ void ply_rich_text_free (ply_rich_text_t *rich_text); ply_rich_text_character_t *ply_rich_text_character_new (void); void ply_rich_text_character_free (ply_rich_text_character_t *character); +void ply_rich_text_iterator_init (ply_rich_text_iterator_t *iterator, + ply_rich_text_t *rich_text, + ply_rich_text_span_t *span); +bool ply_rich_text_iterator_next (ply_rich_text_iterator_t *iterator, + ply_rich_text_character_t **character); + #endif //PLY_HIDE_FUNCTION_DECLARATIONS #endif //PLY_RICH_TEXT_H