aboutsummaryrefslogtreecommitdiff
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
parent57ffc7e18a1ac0b44d14fd8bc61442b7651dcd02 (diff)
downloadvis-b1302b29d9158bb62707203ea54fa5b13904ac15.tar.gz
vis-b1302b29d9158bb62707203ea54fa5b13904ac15.tar.xz
text: move utility functions to separate file
-rw-r--r--editor.c1
-rw-r--r--text-objects.c1
-rw-r--r--text-util.c38
-rw-r--r--text-util.h21
-rw-r--r--text.c37
-rw-r--r--text.h14
-rw-r--r--view.c1
-rw-r--r--vis.c1
8 files changed, 64 insertions, 50 deletions
diff --git a/editor.c b/editor.c
index a47c141..c94e9be 100644
--- a/editor.c
+++ b/editor.c
@@ -6,6 +6,7 @@
#include "editor.h"
#include "util.h"
#include "text-motions.h"
+#include "text-util.h"
static void file_free(Editor *ed, File *file);
static File *file_new(Editor *ed, const char *filename);
diff --git a/text-objects.c b/text-objects.c
index b06d680..d33132c 100644
--- a/text-objects.c
+++ b/text-objects.c
@@ -17,6 +17,7 @@
#include <ctype.h>
#include "text-motions.h"
#include "text-objects.h"
+#include "text-util.h"
#include "util.h"
#define isboundry is_word_boundry
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;
+}
diff --git a/text-util.h b/text-util.h
new file mode 100644
index 0000000..d81c645
--- /dev/null
+++ b/text-util.h
@@ -0,0 +1,21 @@
+#ifndef TEXT_UTIL_H
+#define TEXT_UTIL_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include "text.h"
+
+/* test whether the given range is valid (start <= end) */
+bool text_range_valid(Filerange*);
+/* get the size of the range (end-start) or zero if invalid */
+size_t text_range_size(Filerange*);
+/* create an empty / invalid range of size zero */
+Filerange text_range_empty(void);
+/* merge two ranges into a new one which contains both of them */
+Filerange text_range_union(Filerange*, Filerange*);
+/* create new range [min(a,b), max(a,b)] */
+Filerange text_range_new(size_t a, size_t b);
+/* test whether two ranges overlap */
+bool text_range_overlap(Filerange*, Filerange*);
+
+#endif \ No newline at end of file
diff --git a/text.c b/text.c
index 9a1bd42..26b851d 100644
--- a/text.c
+++ b/text.c
@@ -31,6 +31,7 @@
#endif
#include "text.h"
+#include "text-util.h"
#include "util.h"
/* Allocate buffers holding the actual file content in junks of size: */
@@ -1474,39 +1475,3 @@ size_t text_history_get(Text *txt, size_t index) {
}
return EPOS;
}
-
-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;
-}
diff --git a/text.h b/text.h
index 65ac053..217c801 100644
--- a/text.h
+++ b/text.h
@@ -15,20 +15,6 @@ typedef struct {
size_t start, end; /* range in bytes from start of the file */
} Filerange;
-
-/* test whether the given range is valid (start <= end) */
-bool text_range_valid(Filerange*);
-/* get the size of the range (end-start) or zero if invalid */
-size_t text_range_size(Filerange*);
-/* create an empty / invalid range of size zero */
-Filerange text_range_empty(void);
-/* merge two ranges into a new one which contains both of them */
-Filerange text_range_union(Filerange*, Filerange*);
-/* create new range [min(a,b), max(a,b)] */
-Filerange text_range_new(size_t a, size_t b);
-/* test whether two ranges overlap */
-bool text_range_overlap(Filerange*, Filerange*);
-
typedef struct Text Text;
typedef struct Piece Piece;
diff --git a/view.c b/view.c
index f30af09..875635f 100644
--- a/view.c
+++ b/view.c
@@ -24,6 +24,7 @@
#include "syntax.h"
#include "text.h"
#include "text-motions.h"
+#include "text-util.h"
#include "util.h"
struct Selection {
diff --git a/vis.c b/vis.c
index 76e806d..216ee2f 100644
--- a/vis.c
+++ b/vis.c
@@ -35,6 +35,7 @@
#include "ui-curses.h"
#include "editor.h"
+#include "text-util.h"
#include "text-motions.h"
#include "text-objects.h"
#include "util.h"