vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit b12254420ef83faf9d38d423bec0e165356ed6dd
parent fd44ce83969a89754fe1bac1ea866f779bdb5049
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 25 Feb 2017 19:57:02 +0100

text: add iterator accessor function which translates \r\n to \n

Diffstat:
Mtext.c | 11+++++++++++
Mtext.h | 3+++
2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/text.c b/text.c @@ -1379,6 +1379,17 @@ bool text_iterator_byte_get(Iterator *it, char *b) { return false; } +bool text_iterator_char_get(Iterator *it, char *c) { + bool ret = text_iterator_byte_get(it, c); + if (ret && *c == '\r') { + char d; + if (text_iterator_byte_next(it, &d) && d == '\n') + *c = '\n'; + return text_iterator_byte_prev(it, NULL); + } + return ret; +} + bool text_iterator_next(Iterator *it) { return text_iterator_init(it, it->pos, it->piece ? it->piece->next : NULL, 0); } diff --git a/text.h b/text.h @@ -86,6 +86,9 @@ bool text_iterator_prev(Iterator*); /* get byte at current iterator position, if this is at EOF a NUL * byte (which is not actually part of the file) is read. */ bool text_iterator_byte_get(Iterator*, char *b); +/* same as byte get, but if a sequence of '\r\n' is read at the + * iterator position, *c is set to \n instead of \r. */ +bool text_iterator_char_get(Iterator*, char *c); /* advance iterator by one byte and get byte at new position. */ bool text_iterator_byte_prev(Iterator*, char *b); /* if the new position is at EOF a NUL byte (which is not actually