Skip to content

Commit

Permalink
text: provide public text_iterator_init
Browse files Browse the repository at this point in the history
It can be used to initialize a (stack allocated) Iterator structure,
avoiding the copying of the return value as done by text_iterator_get
which depending on the implementation might be problematic.
  • Loading branch information
martanne committed Oct 10, 2020
1 parent fc85c33 commit 70a8f73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions text.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,10 +1403,14 @@ static bool iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) {
return text_iterator_valid(it);
}

bool text_iterator_init(const Text *txt, Iterator *it, size_t pos) {
Location loc = piece_get_extern(txt, pos);
return iterator_init(it, pos, loc.piece, loc.off);
}

Iterator text_iterator_get(const Text *txt, size_t pos) {
Iterator it;
Location loc = piece_get_extern(txt, pos);
iterator_init(&it, pos, loc.piece, loc.off);
text_iterator_init(txt, &it, pos);
return it;
}

Expand Down
1 change: 1 addition & 0 deletions text.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ char *text_bytes_alloc0(const Text*, size_t pos, size_t len);
* @{
*/
Iterator text_iterator_get(const Text*, size_t pos);
bool text_iterator_init(const Text*, Iterator*, size_t pos);
const Text *text_iterator_text(const Iterator*);
bool text_iterator_valid(const Iterator*);
bool text_iterator_has_next(const Iterator*);
Expand Down

0 comments on commit 70a8f73

Please sign in to comment.