forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext-iterator.c
More file actions
169 lines (156 loc) · 3.49 KB
/
text-iterator.c
File metadata and controls
169 lines (156 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "text.h"
#include "util.h"
bool text_iterator_byte_get(const Iterator *it, char *b) {
if (text_iterator_valid(it)) {
const Text *txt = text_iterator_text(it);
if (it->start <= it->text && it->text < it->end) {
*b = *it->text;
return true;
} else if (it->pos == text_size(txt)) {
*b = '\0';
return true;
}
}
return false;
}
bool text_iterator_byte_next(Iterator *it, char *b) {
if (!text_iterator_has_next(it))
return false;
bool eof = true;
if (it->text != it->end) {
it->text++;
it->pos++;
eof = false;
} else if (!text_iterator_has_prev(it)) {
eof = false;
}
while (it->text == it->end) {
if (!text_iterator_next(it)) {
if (eof)
return false;
if (b)
*b = '\0';
return text_iterator_prev(it);
}
}
if (b)
*b = *it->text;
return true;
}
bool text_iterator_byte_prev(Iterator *it, char *b) {
if (!text_iterator_has_prev(it))
return false;
bool eof = !text_iterator_has_next(it);
while (it->text == it->start) {
if (!text_iterator_prev(it)) {
if (!eof)
return false;
if (b)
*b = '\0';
return text_iterator_next(it);
}
}
--it->text;
--it->pos;
if (b)
*b = *it->text;
return true;
}
bool text_iterator_byte_find_prev(Iterator *it, char b) {
while (it->text) {
const char *match = memory_scan_reverse(it->start, b, it->text - it->start);
if (match) {
it->pos -= it->text - match;
it->text = match;
return true;
}
text_iterator_prev(it);
}
text_iterator_next(it);
return false;
}
bool text_iterator_byte_find_next(Iterator *it, char b) {
while (it->text) {
const char *match = memchr(it->text, b, it->end - it->text);
if (match) {
it->pos += match - it->text;
it->text = match;
return true;
}
text_iterator_next(it);
}
text_iterator_prev(it);
return false;
}
bool text_iterator_codepoint_next(Iterator *it, char *c) {
char test_byte;
while (text_iterator_byte_next(it, &test_byte)) {
if (ISUTF8(test_byte)) {
if (c) *c = test_byte;
return true;
}
}
return false;
}
bool text_iterator_codepoint_prev(Iterator *it, char *c) {
char test_byte;
while (text_iterator_byte_prev(it, &test_byte)) {
if (ISUTF8(test_byte)) {
if (c) *c = test_byte;
return true;
}
}
return false;
}
bool text_iterator_char_next(Iterator *it, char *c) {
if (!text_iterator_codepoint_next(it, c))
return false;
mbstate_t ps = { 0 };
const Text *txt = text_iterator_text(it);
for (;;) {
char buf[MB_LEN_MAX];
size_t len = text_bytes_get(txt, it->pos, sizeof buf, buf);
wchar_t wc;
size_t wclen = mbrtowc(&wc, buf, len, &ps);
if (wclen == (size_t)-1 && errno == EILSEQ) {
return true;
} else if (wclen == (size_t)-2) {
return false;
} else if (wclen == 0) {
return true;
} else {
int width = wcwidth(wc);
if (width != 0)
return true;
if (!text_iterator_codepoint_next(it, c))
return false;
}
}
return true;
}
bool text_iterator_char_prev(Iterator *it, char *c) {
if (!text_iterator_codepoint_prev(it, c))
return false;
const Text *txt = text_iterator_text(it);
for (;;) {
char buf[MB_LEN_MAX];
size_t len = text_bytes_get(txt, it->pos, sizeof buf, buf);
wchar_t wc;
mbstate_t ps = { 0 };
size_t wclen = mbrtowc(&wc, buf, len, &ps);
if (wclen == (size_t)-1 && errno == EILSEQ) {
return true;
} else if (wclen == (size_t)-2) {
return false;
} else if (wclen == 0) {
return true;
} else {
int width = wcwidth(wc);
if (width != 0)
return true;
if (!text_iterator_codepoint_prev(it, c))
return false;
}
}
return true;
}