From 2123804abef4cc95cf587327380abbfbcc9c6fe0 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 12 Mar 2022 19:56:56 -0500 Subject: Use a bool for vi.mode enums use 4 bytes even if they only have 2 values and we have 256 of these structs... --- edit.c | 28 +++++++++++++++++----------- edit.h | 7 ++----- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/edit.c b/edit.c index 370b9ee..67c35b9 100644 --- a/edit.c +++ b/edit.c @@ -197,6 +197,11 @@ int editInsert(struct Edit *e, wchar_t ch) { return 0; } +enum { + Insert = false, + Command = true, +}; + enum { Esc = L'\33', Erase = L'\177', @@ -206,7 +211,7 @@ enum { }; static void viEscape(struct Edit *e) { - e->vi.mode = EditViCommand; + e->vi.mode = Command; e->vi.count = 0; e->vi.verb = '\0'; } @@ -254,17 +259,18 @@ static int viCommand(struct Edit *e, wchar_t ch) { switch (ch) { break; case Esc: viEscape(e); break; case L'0': e->pos = 0; viEscape(e); - break; case L'R': e->vi.verb = 'R'; e->vi.mode = EditViInsert; - break; case L'i': e->vi.verb = 'i'; e->vi.mode = EditViInsert; - break; case L'r': e->vi.verb = 'r'; e->vi.mode = EditViInsert; + break; case L'R': e->vi.verb = 'R'; e->vi.mode = Insert; + break; case L'i': e->vi.verb = 'i'; e->vi.mode = Insert; + break; case L'r': e->vi.verb = 'r'; e->vi.mode = Insert; } return 0; } int editVi(struct Edit *e, wchar_t ch) { - switch (e->vi.mode) { - break; case EditViInsert: return viInsert(e, ch); - break; case EditViCommand: return viCommand(e, ch); + if (e->vi.mode == Command) { + return viCommand(e, ch); + } else { + return viInsert(e, ch); } } @@ -274,7 +280,7 @@ int editVi(struct Edit *e, wchar_t ch) { #include static void fix(struct Edit *e, const char *str) { - e->vi.mode = EditViInsert; + e->vi.mode = Insert; assert(0 == editFn(e, EditClear)); for (const char *ch = str; *ch; ++ch) { assert(0 == editInsert(e, (wchar_t)*ch)); @@ -376,12 +382,12 @@ int main(void) { assert(eq(&e, "foo bar\0")); fix(&e, "foo"); - assert(e.vi.mode == EditViInsert); + assert(e.vi.mode == Insert); editVi(&e, Esc); - assert(e.vi.mode == EditViCommand); + assert(e.vi.mode == Command); assert(eq(&e, "fo\0o")); editVi(&e, L'i'); - assert(e.vi.mode == EditViInsert); + assert(e.vi.mode == Insert); editVi(&e, L'b'); assert(eq(&e, "fob\0o")); editVi(&e, Esc); diff --git a/edit.h b/edit.h index 18e9cd2..d94a028 100644 --- a/edit.h +++ b/edit.h @@ -35,13 +35,10 @@ struct Edit { size_t cap; struct Edit *cut; struct { - enum EditViMode { - EditViInsert, - EditViCommand, - } mode; - unsigned count; + bool mode; char verb; bool lnext; + unsigned count; } vi; }; -- cgit 1.4.1