Skip to content

Commit

Permalink
text-util: constify text_range_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Mar 30, 2016
1 parent 1cd15a9 commit eb7187a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions text-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <wchar.h>
#include <errno.h>

bool text_range_valid(Filerange *r) {
bool text_range_valid(const Filerange *r) {
return r->start != EPOS && r->end != EPOS && r->start <= r->end;
}

size_t text_range_size(Filerange *r) {
size_t text_range_size(const 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) {
Filerange text_range_union(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1))
return *r2;
if (!text_range_valid(r2))
Expand All @@ -33,19 +33,19 @@ Filerange text_range_new(size_t a, size_t b) {
};
}

bool text_range_equal(Filerange *r1, Filerange *r2) {
bool text_range_equal(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1) && !text_range_valid(r2))
return true;
return r1->start == r2->start && r1->end == r2->end;
}

bool text_range_overlap(Filerange *r1, Filerange *r2) {
bool text_range_overlap(const Filerange *r1, const Filerange *r2) {
if (!text_range_valid(r1) || !text_range_valid(r2))
return false;
return r1->start <= r2->end && r2->start <= r1->end;
}

bool text_range_contains(Filerange *r, size_t pos) {
bool text_range_contains(const Filerange *r, size_t pos) {
return text_range_valid(r) && r->start <= pos && pos <= r->end;
}

Expand Down
12 changes: 6 additions & 6 deletions text-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
#include "text.h"

/* test whether the given range is valid (start <= end) */
bool text_range_valid(Filerange*);
bool text_range_valid(const Filerange*);
/* get the size of the range (end-start) or zero if invalid */
size_t text_range_size(Filerange*);
size_t text_range_size(const 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*);
Filerange text_range_union(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 */
bool text_range_equal(Filerange*, Filerange*);
bool text_range_equal(const Filerange*, const Filerange*);
/* test whether two ranges overlap */
bool text_range_overlap(Filerange*, Filerange*);
bool text_range_overlap(const Filerange*, const Filerange*);
/* test whether a given position is within a certain range */
bool text_range_contains(Filerange*, size_t pos);
bool text_range_contains(const Filerange*, size_t pos);
/* count the number of graphemes in data */
int text_char_count(const char *data, size_t len);

Expand Down

0 comments on commit eb7187a

Please sign in to comment.