mirror of
https://gitlab.freedesktop.org/plymouth/plymouth.git
synced 2026-05-07 21:18:05 +02:00
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.
This commit is contained in:
parent
9637be7d88
commit
8d9e3c0350
2 changed files with 46 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue