From 76fe5830bed534d446962fa983e56b65e21265f7 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 24 Apr 2024 13:35:12 -0700 Subject: [PATCH] Abstracted reg lookup --- src/modal.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/modal.c b/src/modal.c index de10d82..571d8ec 100644 --- a/src/modal.c +++ b/src/modal.c @@ -166,6 +166,16 @@ parse_frag(char *s) return s; } +static int +find_register(Rule *r, char reg) +{ + int i; + for(i = 0; i < (int)r->ptr; i++) + if(r->key[i] == reg) + return i; + return -1; +} + static void compile_rule(Rule *r) { @@ -175,12 +185,7 @@ compile_rule(Rule *r) s = r->a; while((c = *s++)) { /* left */ if(c == '?') { - int reg = -1; - for(i = 0; i < (int)r->ptr; i++) - if(r->key[i] == *s) { - reg = i; - break; - } + int reg = find_register(r, *s); if(reg < 0 && *s != '(') r->key[r->ptr] = *s, reg = r->ptr++; *s = '0' + reg; @@ -189,11 +194,9 @@ compile_rule(Rule *r) s = r->b; while((c = *s++)) { /* right */ if(c == '?') { - for(i = 0; i < (int)r->ptr; i++) - if(r->key[i] == *s) { - *s = '0' + i; - break; - } + int reg = find_register(r, *s); + if(reg >= 0) + *s = '0' + reg; } } }