From a61e253248562dee493bf3cbe2547a6d4751f889 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 29 May 2018 17:17:14 -0400 Subject: Add CharIter in edi --- bin/edi.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/edi.c b/bin/edi.c index 326d4d1a..8818d3cb 100644 --- a/bin/edi.c +++ b/bin/edi.c @@ -20,6 +20,7 @@ #include #include #include +#include static struct Span { size_t at; @@ -138,6 +139,42 @@ static struct Table *tableDelete(struct Table *prev, struct Span span) { return next; } +static struct CharIter { + struct TableIter in; + struct Seg seg; + struct Span span; + wchar_t ch; + int len; +} CharIter(struct Table *table, size_t at) { + struct TableIter in; + for (in = TableIter(table); in.len; tableNext(&in)) { + if (at >= in.span.at && at < in.span.to) break; + } + struct CharIter it = { + in, + segTail(*in.seg, at - in.span.at), + Span(at, in.span.to), + 0, + 0, + }; + it.len = mbtowc(&it.ch, it.seg.ptr, it.seg.len); + if (it.len < 0) err(EX_DATAERR, "mbtowc"); + return it; +} + +static void charNext(struct CharIter *it) { + it->seg.ptr += it->len; + it->seg.len -= it->len; + it->span.at += it->len; + if (!it->seg.len && it->in.len) { + tableNext(&it->in); + it->seg = *it->in.seg; + it->span = it->in.span; + } + it->len = mbtowc(&it->ch, it->seg.ptr, it->seg.len); + if (it->len < 0) err(EX_DATAERR, "mbtowc"); +} + int main() { struct Table *table = Table(0); table = tableInsert(table, 0, Seg("Hello, world!\n", 14)); @@ -146,7 +183,7 @@ int main() { table = tableDelete(table, Span(3, 6)); table = tableInsert(table, 3, Seg(" do", 3)); - for (size_t i = 0; i < table->len; ++i) { - printf("%.*s", (int)table->seg[i].len, table->seg[i].ptr); + for (struct CharIter it = CharIter(table, 0); it.seg.len; charNext(&it)) { + printf("%C", it.ch); } } -- cgit 1.4.1