From e1fb4842bf05c8c5fc66d00b61cb50acbcb783e5 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 23 Nov 2018 15:27:26 -0500 Subject: Replace tableUpdate with tableReplace --- bin/edi/edi.h | 3 +-- bin/edi/table.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 14 deletions(-) (limited to 'bin') diff --git a/bin/edi/edi.h b/bin/edi/edi.h index c7345e37..ed03bf21 100644 --- a/bin/edi/edi.h +++ b/bin/edi/edi.h @@ -51,15 +51,14 @@ void bufferTruncate(struct Buffer *buf, size_t len); static const struct Table { size_t cap, len; - size_t ins; struct Slice *slices; } TableEmpty; struct Table tableAlloc(size_t cap); void tableFree(struct Table *table); void tablePush(struct Table *table, struct Slice slice); +void tableReplace(struct Table *table, struct Slice old, struct Slice new); struct Table tableInsert(const struct Table *prev, size_t at, struct Slice ins); struct Table tableDelete(const struct Table *prev, struct Span del); -void tableUpdate(struct Table *table, struct Slice ins); struct Iter { const struct Table *table; diff --git a/bin/edi/table.c b/bin/edi/table.c index 7905d7d7..5f7e963e 100644 --- a/bin/edi/table.c +++ b/bin/edi/table.c @@ -43,20 +43,29 @@ void tablePush(struct Table *table, struct Slice slice) { table->slices[table->len++] = slice; } +void tableReplace(struct Table *table, struct Slice old, struct Slice new) { + for (size_t i = 0; i < table->len; ++i) { + if (table->slices[i].ptr != old.ptr) continue; + if (table->slices[i].len != old.len) continue; + table->slices[i] = new; + break; + } +} + struct Table tableInsert(const struct Table *prev, size_t at, struct Slice ins) { struct Table next = tableAlloc(prev->len + 2); struct Span span = { 0, 0 }; for (size_t i = 0; i < prev->len; ++i) { span = spanNext(span, prev->slices[i].len); if (span.at == at) { - next.slices[next.ins = next.len++] = ins; + next.slices[next.len++] = ins; next.slices[next.len++] = prev->slices[i]; } else if (span.at < at && span.to > at) { next.slices[next.len++] = (struct Slice) { prev->slices[i].ptr, at - span.at, }; - next.slices[next.ins = next.len++] = ins; + next.slices[next.len++] = ins; next.slices[next.len++] = (struct Slice) { &prev->slices[i].ptr[at - span.at], prev->slices[i].len - (at - span.at), @@ -66,15 +75,11 @@ struct Table tableInsert(const struct Table *prev, size_t at, struct Slice ins) } } if (span.to == at) { - next.slices[next.ins = next.len++] = ins; + next.slices[next.len++] = ins; } return next; } -void tableUpdate(struct Table *table, struct Slice ins) { - if (table->ins < table->len) table->slices[table->ins] = ins; -} - struct Table tableDelete(const struct Table *prev, struct Span del) { struct Table next = tableAlloc(prev->len + 1); struct Span span = { 0, 0 }; @@ -105,7 +110,6 @@ struct Table tableDelete(const struct Table *prev, struct Span del) { next.slices[next.len++] = prev->slices[i]; } } - next.ins = next.len; return next; } @@ -142,10 +146,6 @@ int main() { assert(eq(L"ABCD", &abcd)); assert(eq(L"ADBC", &adbc)); - assert(L'D' == dabc.slices[dabc.ins].ptr[0]); - assert(L'D' == abcd.slices[abcd.ins].ptr[0]); - assert(L'D' == adbc.slices[adbc.ins].ptr[0]); - tableFree(&dabc); tableFree(&abcd); tableFree(&adbc); -- cgit 1.4.1 mit/bin/hi.c?id=25e13814fe04576ed949d2e8f085ac23d908bd29&follow=1'>Match whitespace between * [] {}June McEnroe 2019-02-18Fix function-like #define regexJune McEnroe A define like #define FOO (1) is not function-like. 2019-02-18Match Tag in RustJune McEnroe 2019-02-18Match sh functions as TagJune McEnroe 2019-02-18Match Sh and Ss as Tag in mdocJune McEnroe 2019-02-18Match statics and typedefs as TagJune McEnroe 2019-02-18Clean up htmlHeaderJune McEnroe 2019-02-18Remove hi line numberingJune McEnroe Tags are much better for referring to specific parts of a file and line numbering is better done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe