vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit b54d51bdf204d43d51b3d652272a5b6aba0f4803 parent 3ff6935f6c7c2fde4eb4c61bcfe89a6a19e93508 Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 14 Jun 2017 14:03:22 +0200 text-util: add text_range_intersect utility function Diffstat:
| M | text-util.c | | | 6 | ++++++ |
| M | text-util.h | | | 2 | ++ |
2 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/text-util.c b/text-util.c @@ -27,6 +27,12 @@ Filerange text_range_union(const Filerange *r1, const Filerange *r2) { }; } +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), diff --git a/text-util.h b/text-util.h @@ -13,6 +13,8 @@ size_t text_range_size(const Filerange*); Filerange text_range_empty(void); /* merge two ranges into a new one which contains both of them */ Filerange text_range_union(const Filerange*, const Filerange*); +/* get intersection of two ranges */ +Filerange text_range_intersect(const Filerange*, const Filerange*); /* create new range [min(a,b), max(a,b)] */ Filerange text_range_new(size_t a, size_t b); /* test whether two ranges are equal */