Jim Tcl
Check-in [cdedd1ab2a]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:jim.c: minor code cleanups in [switch]

Signed-off-by: Steve Bennett <steveb@workware.net.au>

Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: cdedd1ab2ae8897d985e8da5367fcbbba51dfa35
User & Date: steveb@workware.net.au 2017-09-21 06:30:49
Context
2017-09-21
06:30
tcl::prefix: respect -message when no valid options

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: a181b7fe80 user: steveb@workware.net.au tags: trunk

06:30
jim.c: minor code cleanups in [switch]

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: cdedd1ab2a user: steveb@workware.net.au tags: trunk

2017-09-16
05:38
perf: cache the result of successful subcmd lookup

Speeds up jim-subcmd commands such as array, clock, file, aio

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: c8ee350881 user: steveb@workware.net.au tags: trunk

Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to jim.c.

 12085  12085       if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) {
 12086  12086           eq = -rc;
 12087  12087       }
 12088  12088   
 12089  12089       return eq;
 12090  12090   }
 12091  12091   
 12092         -enum
 12093         -{ SWITCH_EXACT, SWITCH_GLOB, SWITCH_RE, SWITCH_CMD };
 12094         -
 12095  12092   /* [switch] */
 12096  12093   static int Jim_SwitchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 12097  12094   {
        12095  +    enum { SWITCH_EXACT, SWITCH_GLOB, SWITCH_RE, SWITCH_CMD };
 12098  12096       int matchOpt = SWITCH_EXACT, opt = 1, patCount, i;
 12099         -    Jim_Obj *command = 0, *const *caseList = 0, *strObj;
 12100         -    Jim_Obj *script = 0;
        12097  +    Jim_Obj *command = NULL, *scriptObj = NULL, *strObj;
        12098  +    Jim_Obj **caseList;
 12101  12099   
 12102  12100       if (argc < 3) {
 12103  12101         wrongnumargs:
 12104  12102           Jim_WrongNumArgs(interp, 1, argv, "?options? string "
 12105  12103               "pattern body ... ?default body?   or   " "{pattern body ?pattern body ...?}");
 12106  12104           return JIM_ERR;
 12107  12105       }
................................................................................
 12134  12132           }
 12135  12133           if ((argc - opt) < 2)
 12136  12134               goto wrongnumargs;
 12137  12135       }
 12138  12136       strObj = argv[opt++];
 12139  12137       patCount = argc - opt;
 12140  12138       if (patCount == 1) {
 12141         -        Jim_Obj **vector;
 12142         -
 12143         -        JimListGetElements(interp, argv[opt], &patCount, &vector);
 12144         -        caseList = vector;
        12139  +        JimListGetElements(interp, argv[opt], &patCount, &caseList);
 12145  12140       }
 12146  12141       else
 12147         -        caseList = &argv[opt];
        12142  +        caseList = (Jim_Obj **)&argv[opt];
 12148  12143       if (patCount == 0 || patCount % 2 != 0)
 12149  12144           goto wrongnumargs;
 12150         -    for (i = 0; script == 0 && i < patCount; i += 2) {
        12145  +    for (i = 0; scriptObj == NULL && i < patCount; i += 2) {
 12151  12146           Jim_Obj *patObj = caseList[i];
 12152  12147   
 12153  12148           if (!Jim_CompareStringImmediate(interp, patObj, "default")
 12154  12149               || i < (patCount - 2)) {
 12155  12150               switch (matchOpt) {
 12156  12151                   case SWITCH_EXACT:
 12157  12152                       if (Jim_StringEqObj(strObj, patObj))
 12158         -                        script = caseList[i + 1];
        12153  +                        scriptObj = caseList[i + 1];
 12159  12154                       break;
 12160  12155                   case SWITCH_GLOB:
 12161  12156                       if (Jim_StringMatchObj(interp, patObj, strObj, 0))
 12162         -                        script = caseList[i + 1];
        12157  +                        scriptObj = caseList[i + 1];
 12163  12158                       break;
 12164  12159                   case SWITCH_RE:
 12165  12160                       command = Jim_NewStringObj(interp, "regexp", -1);
 12166  12161                       /* Fall thru intentionally */
 12167  12162                   case SWITCH_CMD:{
 12168  12163                           int rc = Jim_CommandMatchObj(interp, command, patObj, strObj, 0);
 12169  12164   
 12170  12165                           /* After the execution of a command we need to
 12171  12166                            * make sure to reconvert the object into a list
 12172  12167                            * again. Only for the single-list style [switch]. */
 12173  12168                           if (argc - opt == 1) {
 12174         -                            Jim_Obj **vector;
 12175         -
 12176         -                            JimListGetElements(interp, argv[opt], &patCount, &vector);
 12177         -                            caseList = vector;
        12169  +                            JimListGetElements(interp, argv[opt], &patCount, &caseList);
 12178  12170                           }
 12179  12171                           /* command is here already decref'd */
 12180  12172                           if (rc < 0) {
 12181  12173                               return -rc;
 12182  12174                           }
 12183  12175                           if (rc)
 12184         -                            script = caseList[i + 1];
        12176  +                            scriptObj = caseList[i + 1];
 12185  12177                           break;
 12186  12178                       }
 12187  12179               }
 12188  12180           }
 12189  12181           else {
 12190         -            script = caseList[i + 1];
        12182  +            scriptObj = caseList[i + 1];
 12191  12183           }
 12192  12184       }
 12193         -    for (; i < patCount && Jim_CompareStringImmediate(interp, script, "-"); i += 2)
 12194         -        script = caseList[i + 1];
 12195         -    if (script && Jim_CompareStringImmediate(interp, script, "-")) {
        12185  +    for (; i < patCount && Jim_CompareStringImmediate(interp, scriptObj, "-"); i += 2)
        12186  +        scriptObj = caseList[i + 1];
        12187  +    if (scriptObj && Jim_CompareStringImmediate(interp, scriptObj, "-")) {
 12196  12188           Jim_SetResultFormatted(interp, "no body specified for pattern \"%#s\"", caseList[i - 2]);
 12197  12189           return JIM_ERR;
 12198  12190       }
 12199  12191       Jim_SetEmptyResult(interp);
 12200         -    if (script) {
 12201         -        return Jim_EvalObj(interp, script);
        12192  +    if (scriptObj) {
        12193  +        return Jim_EvalObj(interp, scriptObj);
 12202  12194       }
 12203  12195       return JIM_OK;
 12204  12196   }
 12205  12197   
 12206  12198   /* [list] */
 12207  12199   static int Jim_ListCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 12208  12200   {