From b1302b29d9158bb62707203ea54fa5b13904ac15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 27 Jul 2015 11:56:20 +0200 Subject: text: move utility functions to separate file --- editor.c | 1 + text-objects.c | 1 + text-util.c | 38 ++++++++++++++++++++++++++++++++++++++ text-util.h | 21 +++++++++++++++++++++ text.c | 37 +------------------------------------ text.h | 14 -------------- view.c | 1 + vis.c | 1 + 8 files changed, 64 insertions(+), 50 deletions(-) create mode 100644 text-util.c create mode 100644 text-util.h 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 #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 +#include +#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" -- cgit v1.2.3