forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-util.c
122 lines (108 loc) · 2.79 KB
/
text-util.c
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
#include "text-util.h"
#include "util.h"
#include <wchar.h>
#include <errno.h>
#include <stdlib.h>
bool text_range_valid(const Filerange *r) {
return r->start != EPOS && r->end != EPOS && r->start <= r->end;
}
size_t text_range_size(const Filerange *r) {
return text_range_valid(r) ? r->end - r->start : 0;
}
Filerange text_range_empty(void) {
return (Filerange){ .start = EPOS, .end = EPOS };
}
Filerange text_range_union(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1))
return *r2;
if (!text_range_valid(r2))
return *r1;
return (Filerange) {
.start = MIN(r1->start, r2->start),
.end = MAX(r1->end, r2->end),
};
}
Filerange text_range_intersect(const Filerange *r1, const Filerange *r2) {
if (!text_range_overlap(r1, r2))
return text_range_empty();
return text_range_new(MAX(r1->start, r2->start), MIN(r1->end, r2->end));
}
Filerange text_range_new(size_t a, size_t b) {
return (Filerange) {
.start = MIN(a, b),
.end = MAX(a, b),
};
}
bool text_range_equal(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1) && !text_range_valid(r2))
return true;
return r1->start == r2->start && r1->end == r2->end;
}
bool text_range_overlap(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1) || !text_range_valid(r2))
return false;
return r1->start < r2->end && r2->start < r1->end;
}
bool text_range_contains(const Filerange *r, size_t pos) {
return text_range_valid(r) && r->start <= pos && pos <= r->end;
}
int text_char_count(const char *data, size_t len) {
int count = 0;
mbstate_t ps = { 0 };
while (len > 0) {
wchar_t wc;
size_t wclen = mbrtowc(&wc, data, len, &ps);
if (wclen == (size_t)-1 && errno == EILSEQ) {
ps = (mbstate_t){0};
count++;
while (!ISUTF8(*data))
data++, len--;
} else if (wclen == (size_t)-2) {
break;
} else if (wclen == 0) {
count++;
data++;
len--;
} else {
int width = wcwidth(wc);
if (width != 0)
count++;
data += wclen;
len -= wclen;
}
}
return count;
}
int text_string_width(const char *data, size_t len) {
int width = 0;
mbstate_t ps = { 0 };
const char *s = data;
while (len > 0) {
wchar_t wc;
size_t wclen = mbrtowc(&wc, s, len, &ps);
if (wclen == (size_t)-1 && errno == EILSEQ) {
ps = (mbstate_t){0};
/* assume a replacement symbol will be displayed */
width++;
wclen = 1;
} else if (wclen == (size_t)-2) {
/* do nothing, advance to next character */
wclen = 1;
} else if (wclen == 0) {
/* assume NUL byte will be displayed as ^@ */
width += 2;
wclen = 1;
} else if (wc == L'\t') {
width++;
wclen = 1;
} else {
int w = wcwidth(wc);
if (w == -1)
w = 2; /* assume non-printable will be displayed as ^{char} */
width += w;
}
len -= wclen;
s += wclen;
}
return width;
}