summaryrefslogtreecommitdiff
path: root/txt.c
diff options
context:
space:
mode:
Diffstat (limited to 'txt.c')
-rw-r--r--txt.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/txt.c b/txt.c
index 8fcafe9..9046b79 100644
--- a/txt.c
+++ b/txt.c
@@ -198,7 +198,7 @@ void txt_free(Txt *t) {
u8 txt_byte(Txt *t, TxtLoc l) {
TxtPiece *p = &t->ptbl.v[l.p];
TxtBuf *b = &t->buf[p->buf];
- if (l.p < t->ptbl.n && l.i == p->n) p++, l.i = 0;
+ //if (l.p < t->ptbl.n && l.i == p->n) p++, l.i = 0;
if (p->ofs + l.i >= b->n) return 0;
return b->s[p->ofs + l.i];
}
@@ -206,7 +206,10 @@ u8 txt_byte(Txt *t, TxtLoc l) {
u32 txt_chr(Txt *t, TxtLoc l) {
TxtPiece *p = &t->ptbl.v[l.p];
TxtBuf *b = &t->buf[p->buf];
- if (l.p < t->ptbl.n && l.i == p->n) p++, l.i = 0;
+ /*
+ * is this ever executed?
+ * if (l.p < t->ptbl.n && l.i == p->n) p++, l.i = 0;
+ */
return utf8_decode_at(b->s, p->ofs + l.i, b->n);
}
@@ -232,12 +235,14 @@ u32 txt_ofs(Txt *b, TxtLoc l) {
TxtLoc txt_next(Txt *b, TxtLoc l) {
TxtPiece *p = &b->ptbl.v[l.p];
- if (l.i+1 < p->n) return (TxtLoc) { l.p, l.i + 1 };
- if (l.p+1 < b->ptbl.n) {
- return (TxtLoc) { l.p + 1, l.i };
+ if (l.p + 1 < b->ptbl.n) {
+ if (l.i + 1 < p->n) l.i++;
+ else l.p++, l.i = 0;
} else {
- return (TxtLoc) { l.p, p->n };
+ l.i++;
+ if (l.i > p->n) l.i = p->n;
}
+ return l;
}
TxtLoc txt_prev(Txt *b, TxtLoc l) {
@@ -258,3 +263,19 @@ int txt_at_start(Txt *b, TxtLoc l) {
int txt_at_end(Txt *b, TxtLoc l) {
return l.p + 1 == b->ptbl.n && l.i == b->ptbl.v[l.p].n;
}
+
+int txt_before(TxtLoc a, TxtLoc b) {
+ return a.p < b.p || (a.p == b.p && a.i < b.i);
+}
+
+int txt_after(TxtLoc a, TxtLoc b) {
+ return a.p > b.p || (a.p == b.p && a.i > b.i);
+}
+
+u32 txt_chr_next(Txt *t, TxtLoc *l) {
+ if (txt_at_end(t, *l)) return 0;
+ u32 c = txt_chr(t, *l);
+ u32 n = UTF8_CP_LEN(c);
+ for (u32 i = 0; i < n; i++) *l = txt_next(t, *l);
+ return c;
+}