]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
rich-text: Add iterator API
authorRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 17:22:35 +0000 (12:22 -0500)
committerRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 23:14:42 +0000 (18:14 -0500)
This commit adds a small wrapper around ply_rich_text_get_characters
to make it easier to iterate over every character in a loop.

src/libply-splash-core/ply-rich-text.c
src/libply-splash-core/ply-rich-text.h

index 0cc87893e53144431953837ca9f5a854046f57a9..13a9791fcddf4f010b6935f6950341cd94b50e5d 100644 (file)
@@ -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;
+}
index 27692debbfa62425e99efbff0da19bd2f7944f25..6455158c74f34af4813b447a3e74d97f87751db8 100644 (file)
@@ -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