aboutsummaryrefslogtreecommitdiff
path: root/text-util.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-07-27 11:56:20 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-07-28 13:21:49 +0200
commitb1302b29d9158bb62707203ea54fa5b13904ac15 (patch)
tree7816891578b604b66cdf485cf8d88f8f8c015eee /text-util.c
parent57ffc7e18a1ac0b44d14fd8bc61442b7651dcd02 (diff)
downloadvis-b1302b29d9158bb62707203ea54fa5b13904ac15.tar.gz
vis-b1302b29d9158bb62707203ea54fa5b13904ac15.tar.xz
text: move utility functions to separate file
Diffstat (limited to 'text-util.c')
-rw-r--r--text-util.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/text-util.c b/text-util.c
new file mode 100644
index 0000000..4139454
--- /dev/null
+++ b/text-util.c
@@ -0,0 +1,38 @@
+#include "text-util.h"
+#include "util.h"
+
+bool text_range_valid(Filerange *r) {
+ return r->start != EPOS && r->end != EPOS && r->start <= r->end;
+}
+
+size_t text_range_size(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(Filerange *r1, 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_new(size_t a, size_t b) {
+ return (Filerange) {
+ .start = MIN(a, b),
+ .end = MAX(a, b),
+ };
+}
+
+bool text_range_overlap(Filerange *r1, Filerange *r2) {
+ if (!text_range_valid(r1) || !text_range_valid(r2))
+ return false;
+ return r1->start <= r2->end && r2->start <= r1->end;
+}