From 521d96fbdab843ac938dc3eec877651d31c210ad Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 21 Jul 2021 16:40:36 +0800 Subject: [PATCH] Use binary search for name to function lookup. Fix name mapping table order. Rework test scripts. --- blindmaz.cmd | 1 + eval.c | 2 -- floodmaz.cmd | 5 +++++ input.c | 18 ------------------ input.h | 1 - names.c | 51 +++++++++++++++++++++++++++++++++++++-------------- names.h | 2 ++ 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/blindmaz.cmd b/blindmaz.cmd index d883e6a..ecc67cc 100644 --- a/blindmaz.cmd +++ b/blindmaz.cmd @@ -67,3 +67,4 @@ set %D 0 # looking EAST beginning-of-file set $curline 3 set $curcol 1 +unmark-buffer diff --git a/eval.c b/eval.c index 5b06a06..689a74d 100644 --- a/eval.c +++ b/eval.c @@ -35,8 +35,6 @@ #define MAXVARS 255 -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - /* Macro argument token types */ #define TKNUL 0 /* end-of-string */ diff --git a/floodmaz.cmd b/floodmaz.cmd index fb83593..5d2d20a 100644 --- a/floodmaz.cmd +++ b/floodmaz.cmd @@ -61,3 +61,8 @@ set %NC &asc "█" !endwhile set $curline 3 set $curcol 1 +select-buffer stack +unmark-buffer +select-buffer %thisbuf +unmark-buffer +delete-buffer stack diff --git a/input.c b/input.c index e972dd9..fc67ece 100644 --- a/input.c +++ b/input.c @@ -157,24 +157,6 @@ int ectoc( int c) { return c ; } -/* - * match fname to a function in the names table - * and return any match or NULL if none - * - * char *fname; name to attempt to match - */ -const name_bind *fncmatch( char *fname) { - const name_bind *ffp ; /* pointer to entry in name binding table */ - - /* scan through the table, returning any match */ - for( ffp = names ; ffp->n_func != NULL ; ffp++) - if( strcmp( fname, bind_name( ffp)) == 0) - break ; - - return ffp ; -} - - const name_bind *getfncnb( fnp_t func) { const name_bind *nptr ; /* pointer into the name binding table */ diff --git a/input.h b/input.h index d633b64..25a907f 100644 --- a/input.h +++ b/input.h @@ -29,7 +29,6 @@ int newmlargt( char **outbufref, const char *prompt, int size) ; int ectoc( int c) ; /* Look up in names to function association table */ -const name_bind *fncmatch( char *name) ; /* by name */ const name_bind *getname( void) ; /* interactively */ const name_bind *getfncnb( fnp_t func) ; /* by function */ diff --git a/names.c b/names.c index f341e38..f50fd42 100644 --- a/names.c +++ b/names.c @@ -8,6 +8,8 @@ * processing. */ +#include + #include "basic.h" #include "bind.h" #include "bindable.h" @@ -22,13 +24,14 @@ #include "random.h" #include "search.h" #include "spawn.h" +#include "util.h" #include "window.h" #include "word.h" const name_bind names[] = { {" abort-command", ctrlg} , - {" add-mode", setemode} , {" add-global-mode", setgmode} , + {" add-mode", setemode} , #if APROP {" apropos", apro} , #endif @@ -55,8 +58,8 @@ const name_bind names[] = { {" ctlx-prefix", cex} , {"!delete-blank-lines", deblank} , {" delete-buffer", killbuffer} , - {" delete-mode", delmode} , {" delete-global-mode", delgmode} , + {" delete-mode", delmode} , {"!delete-next-character", forwdel} , {"!delete-next-word", delfword} , {" delete-other-windows", onlywind} , @@ -80,14 +83,6 @@ const name_bind names[] = { {" execute-file", execfile} , {" execute-macro", ctlxe} , {" execute-macro-1", cbuf1} , - {" execute-macro-2", cbuf2} , - {" execute-macro-3", cbuf3} , - {" execute-macro-4", cbuf4} , - {" execute-macro-5", cbuf5} , - {" execute-macro-6", cbuf6} , - {" execute-macro-7", cbuf7} , - {" execute-macro-8", cbuf8} , - {" execute-macro-9", cbuf9} , {" execute-macro-10", cbuf10} , {" execute-macro-11", cbuf11} , {" execute-macro-12", cbuf12} , @@ -98,6 +93,7 @@ const name_bind names[] = { {" execute-macro-17", cbuf17} , {" execute-macro-18", cbuf18} , {" execute-macro-19", cbuf19} , + {" execute-macro-2", cbuf2} , {" execute-macro-20", cbuf20} , {" execute-macro-21", cbuf21} , {" execute-macro-22", cbuf22} , @@ -108,6 +104,7 @@ const name_bind names[] = { {" execute-macro-27", cbuf27} , {" execute-macro-28", cbuf28} , {" execute-macro-29", cbuf29} , + {" execute-macro-3", cbuf3} , {" execute-macro-30", cbuf30} , {" execute-macro-31", cbuf31} , {" execute-macro-32", cbuf32} , @@ -118,7 +115,13 @@ const name_bind names[] = { {" execute-macro-37", cbuf37} , {" execute-macro-38", cbuf38} , {" execute-macro-39", cbuf39} , + {" execute-macro-4", cbuf4} , {" execute-macro-40", cbuf40} , + {" execute-macro-5", cbuf5} , + {" execute-macro-6", cbuf6} , + {" execute-macro-7", cbuf7} , + {" execute-macro-8", cbuf8} , + {" execute-macro-9", cbuf9} , {" execute-named-command", namedcmd} , #if PROC {" execute-procedure", execproc} , @@ -137,9 +140,9 @@ const name_bind names[] = { #endif {" grow-window", enlargewind} , {"!handle-tab", insert_tab} , - {" hunt-forward", forwhunt} , - {" hunt-backward", backhunt} , {" help", help} , + {" hunt-backward", backhunt} , + {" hunt-forward", forwhunt} , {" i-shell", spawncli} , #if ISRCH {" incremental-search", fisearch} , @@ -186,9 +189,9 @@ const name_bind names[] = { {"!quote-character", quote} , {"!read-file", fileread} , {" redraw-display", reposition} , + {"!replace-string", sreplace} , {" resize-window", resize} , {" restore-window", restwnd} , - {"!replace-string", sreplace} , #if ISRCH {" reverse-incremental-search", risearch} , #endif @@ -197,8 +200,8 @@ const name_bind names[] = { #endif {"!save-file", filesave} , {" save-window", savewnd} , - {" scroll-next-up", scrnextup} , {" scroll-next-down", scrnextdw} , + {" scroll-next-up", scrnextup} , {" search-forward", forwsearch} , {" search-reverse", backsearch} , {" select-buffer", usebuffer} , @@ -232,4 +235,24 @@ const name_bind names[] = { {" ", NULL} }; + +const name_bind *fncmatch( char *name) { + int found = ARRAY_SIZE( names) - 1 ; /* index of last entry/ catch all */ + int low = 0 ; + int high = found - 1 ; + do { + int cur = (high + low) / 2 ; + int s = strcmp( name, bind_name( &names[ cur])) ; + if( s < 0) + high = cur - 1 ; + else if( s == 0) { + found = cur ; + break ; + } else + low = cur + 1 ; + } while( low <= high) ; + + return &names[ found] ; +} + /* end of names.c */ diff --git a/names.h b/names.h index 0ee79c8..ba763a4 100644 --- a/names.h +++ b/names.h @@ -15,4 +15,6 @@ typedef struct name_bind { extern const name_bind names[] ; /* name to function mapping table */ +const name_bind *fncmatch( char *name) ; /* look up by name */ + #endif