summaryrefslogtreecommitdiff
path: root/txt.h
diff options
context:
space:
mode:
Diffstat (limited to 'txt.h')
-rw-r--r--txt.h51
1 files changed, 44 insertions, 7 deletions
diff --git a/txt.h b/txt.h
index a6e92eb..09d1c61 100644
--- a/txt.h
+++ b/txt.h
@@ -85,13 +85,6 @@ int txt_after(TxtLoc a, TxtLoc b);
TxtLoc txt_start(Txt *t);
TxtLoc txt_end(Txt *t);
-TxtLoc bnext(TxtLoc l);
-TxtLoc bprev(TxtLoc l);
-TxtLoc cnext(TxtLoc l);
-TxtLoc cprev(TxtLoc l);
-int at_start(TxtLoc l);
-int at_end(TxtLoc l);
-
TxtLoc next_newline(TxtLoc l);
TxtLoc prev_newline(TxtLoc l);
TxtLoc start_of_line(TxtLoc l);
@@ -109,4 +102,48 @@ u32 txt_chr(TxtLoc l);
u8 txt_byte(TxtLoc l);
u32 txt_chr_next(TxtLoc *l);
+/* inline funcs */
+
+static inline int at_start(TxtLoc l) {
+ return l.p == 0 && l.i == 0;
+}
+
+static inline int at_end(TxtLoc l) {
+ return l.p + 1 == l.t->ptbl.n && l.i == l.t->ptbl.v[l.p].n;
+}
+
+static inline TxtLoc bnext(TxtLoc l) {
+ TxtPiece *p = &l.t->ptbl.v[l.p];
+ if (l.p + 1 < l.t->ptbl.n) {
+ if (l.i + 1 < p->n) l.i++;
+ else l.p++, l.i = 0;
+ } else {
+ l.i++;
+ if (l.i > p->n) l.i = p->n;
+ }
+ return l;
+}
+
+static inline TxtLoc bprev(TxtLoc l) {
+ if (l.i > 0) {
+ return (TxtLoc) { l.t, l.p, l.i - 1 };
+ } else if (l.p > 0 && l.t->ptbl.v[l.p - 1].n > 0) {
+ return (TxtLoc) { l.t, l.p - 1, l.t->ptbl.v[l.p - 1].n - 1 };
+ } else {
+ return (TxtLoc) { l.t, 0, 0 };
+ }
+}
+
+static inline TxtLoc cnext(TxtLoc l) {
+ l = bnext(l);
+ while ((txt_byte(l) & 0xc0) == 0x80) l = bnext(l);
+ return l;
+}
+
+static inline TxtLoc cprev(TxtLoc l) {
+ l = bprev(l);
+ while ((txt_byte(l) & 0xc0) == 0x80) l = bprev(l);
+ return l;
+}
+
#endif