test: update
[vis.git] / text-util.c
blob4936d9260ed577256948419fd79a8a8e85631a95
1 #include "text-util.h"
2 #include "util.h"
3 #include <wchar.h>
4 #include <errno.h>
5 #include <stdlib.h>
7 bool text_range_valid(const Filerange *r) {
8 return r->start != EPOS && r->end != EPOS && r->start <= r->end;
11 size_t text_range_size(const Filerange *r) {
12 return text_range_valid(r) ? r->end - r->start : 0;
15 Filerange text_range_empty(void) {
16 return (Filerange){ .start = EPOS, .end = EPOS };
19 Filerange text_range_union(const Filerange *r1, const Filerange *r2) {
20 if (!text_range_valid(r1))
21 return *r2;
22 if (!text_range_valid(r2))
23 return *r1;
24 return (Filerange) {
25 .start = MIN(r1->start, r2->start),
26 .end = MAX(r1->end, r2->end),
30 Filerange text_range_intersect(const Filerange *r1, const Filerange *r2) {
31 if (!text_range_overlap(r1, r2))
32 return text_range_empty();
33 return text_range_new(MAX(r1->start, r2->start), MIN(r1->end, r2->end));
36 Filerange text_range_new(size_t a, size_t b) {
37 return (Filerange) {
38 .start = MIN(a, b),
39 .end = MAX(a, b),
43 bool text_range_equal(const Filerange *r1, const Filerange *r2) {
44 if (!text_range_valid(r1) && !text_range_valid(r2))
45 return true;
46 return r1->start == r2->start && r1->end == r2->end;
49 bool text_range_overlap(const Filerange *r1, const Filerange *r2) {
50 if (!text_range_valid(r1) || !text_range_valid(r2))
51 return false;
52 return r1->start < r2->end && r2->start < r1->end;
55 bool text_range_contains(const Filerange *r, size_t pos) {
56 return text_range_valid(r) && r->start <= pos && pos <= r->end;
59 int text_char_count(const char *data, size_t len) {
60 int count = 0;
61 mbstate_t ps = { 0 };
62 while (len > 0) {
63 wchar_t wc;
64 size_t wclen = mbrtowc(&wc, data, len, &ps);
65 if (wclen == (size_t)-1 && errno == EILSEQ) {
66 count++;
67 while (!ISUTF8(*data))
68 data++, len--;
69 } else if (wclen == (size_t)-2) {
70 break;
71 } else if (wclen == 0) {
72 count++;
73 data++;
74 len--;
75 } else {
76 int width = wcwidth(wc);
77 if (width != 0)
78 count++;
79 data += wclen;
80 len -= wclen;
83 return count;
86 int text_string_width(const char *data, size_t len) {
88 int width = 0;
89 mbstate_t ps = { 0 };
90 const char *s = data;
92 while (len > 0) {
93 wchar_t wc;
94 size_t wclen = mbrtowc(&wc, s, len, &ps);
95 if (wclen == (size_t)-1 && errno == EILSEQ) {
96 /* assume a replacement symbol will be displayed */
97 width++;
98 wclen = 1;
99 } else if (wclen == (size_t)-2) {
100 /* do nothing, advance to next character */
101 wclen = 1;
102 } else if (wclen == 0) {
103 /* assume NUL byte will be displayed as ^@ */
104 width += 2;
105 wclen = 1;
106 } else if (wc == L'\t') {
107 width++;
108 wclen = 1;
109 } else {
110 int w = wcwidth(wc);
111 if (w == -1)
112 w = 2; /* assume non-printable will be displayed as ^{char} */
113 width += w;
115 len -= wclen;
116 s += wclen;
119 return width;