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(-) 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 earch' size='10' name='q' value=''/>
Commit message (Expand)Author
2021-01-12Remove hacky tagging from hilexJune McEnroe
2021-01-12Add htagml -iJune McEnroe
2021-01-12Render tag index in HTMLJune McEnroe
2021-01-12Add htagml -xJune McEnroe
2021-01-12Prevent matching the same tag twiceJune McEnroe
2021-01-12Process htagml file line by lineJune McEnroe
2021-01-12Split fields by tab onlyJune McEnroe
2021-01-12List both Makefile and html.sh under README.7June McEnroe
2021-01-12Add htagml exampleJune McEnroe
2021-01-12Use mandoc and htagml for bin htmlJune McEnroe
2021-01-12Add htagmlJune McEnroe
2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe
2021-01-11Publish "Using vi"June McEnroe
2021-01-11Enable diff.colorMovedJune McEnroe
2021-01-10Set less search case-insensitiveJune McEnroe
2021-01-10Set EXINITJune McEnroe
2021-01-09Add c -t flag to print expression typeJune McEnroe
2021-01-05Update taglineJune McEnroe