aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-04-05 13:00:38 +0200
committerMarc André Tanner <mat@brain-dump.org>2017-04-08 22:54:36 +0200
commitdf762bf0bbdf373a53fdfd5f90b730fe6f7850bb (patch)
treeb74a06e14f5f2ac109c9a23985968e945f656877
parent391720cb430ad74d01dacb1cd53c06d6283aba4e (diff)
downloadvis-df762bf0bbdf373a53fdfd5f90b730fe6f7850bb.tar.gz
vis-df762bf0bbdf373a53fdfd5f90b730fe6f7850bb.tar.xz
text: simplify \r\n handling
-rw-r--r--text-motions.c38
-rw-r--r--text-objects.c12
2 files changed, 23 insertions, 27 deletions
diff --git a/text-motions.c b/text-motions.c
index 210e1f4..bca923d 100644
--- a/text-motions.c
+++ b/text-motions.c
@@ -115,12 +115,10 @@ size_t text_line_find_prev(Text *txt, size_t pos, const char *s) {
size_t text_line_prev(Text *txt, size_t pos) {
char c;
Iterator it = text_iterator_get(txt, pos);
- if (!text_iterator_byte_get(&it, &c))
+ if (!text_iterator_char_get(&it, &c))
return pos;
if (c == '\n')
- text_iterator_byte_prev(&it, &c);
- if (c == '\r')
- text_iterator_byte_prev(&it, &c);
+ text_iterator_char_prev(&it, &c);
while (text_iterator_byte_get(&it, &c) && c != '\n')
text_iterator_byte_prev(&it, NULL);
if (text_iterator_byte_prev(&it, &c) && c != '\r')
@@ -131,12 +129,10 @@ size_t text_line_prev(Text *txt, size_t pos) {
size_t text_line_begin(Text *txt, size_t pos) {
char c;
Iterator it = text_iterator_get(txt, pos);
- if (!text_iterator_byte_get(&it, &c))
+ if (!text_iterator_char_get(&it, &c))
return pos;
if (c == '\n')
- text_iterator_byte_prev(&it, &c);
- if (c == '\r')
- text_iterator_byte_prev(&it, &c);
+ text_iterator_char_prev(&it, &c);
while (text_iterator_byte_get(&it, &c)) {
if (c == '\n') {
it.pos++;
@@ -159,9 +155,9 @@ size_t text_line_finish(Text *txt, size_t pos) {
char c;
size_t end = text_line_end(txt, pos);
Iterator it = text_iterator_get(txt, end);
- if (!text_iterator_char_prev(&it, &c) || c == '\n')
+ if (!text_iterator_byte_prev(&it, &c) || c == '\n')
return end;
- while (blank(c) && text_iterator_char_prev(&it, &c));
+ while (blank(c) && text_iterator_byte_prev(&it, &c));
return it.pos + (c == '\n');
}
@@ -186,7 +182,7 @@ size_t text_line_offset(Text *txt, size_t pos, size_t off) {
char c;
size_t bol = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, bol);
- while (off-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n')
+ while (off-- > 0 && text_iterator_char_get(&it, &c) && c != '\n')
text_iterator_byte_next(&it, NULL);
return it.pos;
}
@@ -195,8 +191,8 @@ size_t text_line_char_set(Text *txt, size_t pos, int count) {
char c;
size_t bol = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, bol);
- while (count-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n')
- text_iterator_char_next(&it, NULL);
+ if (text_iterator_char_get(&it, &c) && c != '\n')
+ while (count-- > 0 && text_iterator_char_next(&it, &c) && c != '\n');
return it.pos;
}
@@ -205,9 +201,9 @@ int text_line_char_get(Text *txt, size_t pos) {
int count = 0;
size_t bol = text_line_begin(txt, pos);
Iterator it = text_iterator_get(txt, bol);
- while (text_iterator_byte_get(&it, &c) && it.pos < pos && c != '\r' && c != '\n') {
- text_iterator_char_next(&it, NULL);
- count++;
+ if (text_iterator_char_get(&it, &c) && c != '\n') {
+ while (it.pos < pos && c != '\n' && text_iterator_char_next(&it, &c))
+ count++;
}
return count;
}
@@ -289,7 +285,7 @@ size_t text_line_width_set(Text *txt, size_t pos, int width) {
size_t text_line_char_next(Text *txt, size_t pos) {
char c;
Iterator it = text_iterator_get(txt, pos);
- if (!text_iterator_byte_get(&it, &c) || c == '\r' || c == '\n')
+ if (!text_iterator_char_get(&it, &c) || c == '\n')
return pos;
text_iterator_char_next(&it, NULL);
return it.pos;
@@ -473,8 +469,8 @@ size_t text_paragraph_next(Text *txt, size_t pos) {
char c;
Iterator it = text_iterator_get(txt, pos);
- while (text_iterator_byte_get(&it, &c) && (c == '\n' || c == '\r'))
- text_iterator_byte_next(&it, NULL);
+ while (text_iterator_char_get(&it, &c) && c == '\n')
+ text_iterator_char_next(&it, NULL);
return text_line_empty_next(txt, it.pos);
}
@@ -483,8 +479,8 @@ size_t text_paragraph_prev(Text *txt, size_t pos) {
Iterator it = text_iterator_get(txt, pos);
/* c == \0 catches starting the search at EOF */
- while (text_iterator_byte_get(&it, &c) && (c == '\n' || c == '\r' || c == '\0'))
- text_iterator_byte_prev(&it, NULL);
+ while (text_iterator_char_get(&it, &c) && (c == '\n' || c == '\0'))
+ text_iterator_char_prev(&it, NULL);
return text_line_empty_prev(txt, it.pos);
}
diff --git a/text-objects.c b/text-objects.c
index fee8235..3b55b2d 100644
--- a/text-objects.c
+++ b/text-objects.c
@@ -18,11 +18,11 @@ Filerange text_object_entire_inner(Text *txt, size_t pos) {
char c;
Filerange r = text_object_entire(txt, pos);
Iterator it = text_iterator_get(txt, r.start);
- while (text_iterator_byte_get(&it, &c) && (c == '\r' || c == '\n'))
- text_iterator_byte_next(&it, NULL);
+ if (text_iterator_char_get(&it, &c) && c == '\n')
+ while (text_iterator_char_next(&it, &c) && c == '\n');
r.start = it.pos;
it = text_iterator_get(txt, r.end);
- while (text_iterator_byte_prev(&it, &c) && (c == '\r' || c == '\n'));
+ while (text_iterator_char_prev(&it, &c) && c == '\n');
r.end = it.pos;
return text_range_linewise(txt, &r);
}
@@ -282,7 +282,7 @@ Filerange text_object_indentation(Text *txt, size_t pos) {
size_t start = bol;
size_t end = text_line_next(txt, bol);
size_t line_indent = sol - bol;
- bool line_empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
+ bool line_empty = text_char_get(txt, bol, &c) && c == '\n';
char *buf = text_bytes_alloc0(txt, bol, line_indent);
char *tmp = malloc(line_indent);
@@ -298,7 +298,7 @@ Filerange text_object_indentation(Text *txt, size_t pos) {
size_t indent = sol - bol;
if (indent < line_indent)
break;
- bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
+ bool empty = text_char_get(txt, bol, &c) && c == '\n';
if (line_empty && !empty)
break;
if (line_indent == 0 && empty)
@@ -315,7 +315,7 @@ Filerange text_object_indentation(Text *txt, size_t pos) {
size_t indent = sol - bol;
if (indent < line_indent)
break;
- bool empty = text_byte_get(txt, bol, &c) && (c == '\r' || c == '\n');
+ bool empty = text_char_get(txt, bol, &c) && c == '\n';
if (line_empty && !empty)
break;
if (line_indent == 0 && empty)