From c00426df647271ce753acf97103086d73b8fbb83 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sat, 16 Oct 2010 21:52:15 +1000 Subject: Switch regexp/regsub to use Jim_GetEnum() Provides a better error message on wrong args and allows abbreviations. Signed-off-by: Steve Bennett --- jim-regexp.c | 128 ++++++++++++++++++++++++++++++++---------------------- tests/regexp.test | 14 +++--- 2 files changed, 82 insertions(+), 60 deletions(-) diff --git a/jim-regexp.c b/jim-regexp.c index f41d8aa..1ca27d7 100644 --- a/jim-regexp.c +++ b/jim-regexp.c @@ -123,6 +123,13 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) Jim_Obj *resultListObj = NULL; int regcomp_flags = 0; int eflags = 0; + int option; + enum { + OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END + }; + static const char const *options[] = { + "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL + }; if (argc < 3) { wrongNumArgs: @@ -132,41 +139,47 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) } for (i = 1; i < argc; i++) { - if (Jim_CompareStringImmediate(interp, argv[i], "-indices")) { - opt_indices = 1; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-nocase")) { - regcomp_flags |= REG_ICASE; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-line")) { - regcomp_flags |= REG_NEWLINE; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-all")) { - opt_all = 1; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-inline")) { - opt_inline = 1; + const char *opt = Jim_GetString(argv[i], NULL); + + if (*opt != '-') { + break; } - else if (Jim_CompareStringImmediate(interp, argv[i], "-start")) { - if (++i == argc) { - goto wrongNumArgs; - } - if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { - return JIM_ERR; - } + if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; } - else if (Jim_CompareStringImmediate(interp, argv[i], "--")) { + if (option == OPT_END) { i++; break; } - else { - const char *opt = Jim_GetString(argv[i], NULL); + switch (option) { + case OPT_INDICES: + opt_indices = 1; + break; - if (*opt == '-') { - /* Bad option */ - goto wrongNumArgs; - } - break; + case OPT_NOCASE: + regcomp_flags |= REG_ICASE; + break; + + case OPT_LINE: + regcomp_flags |= REG_NEWLINE; + break; + + case OPT_ALL: + opt_all = 1; + break; + + case OPT_INLINE: + opt_inline = 1; + break; + + case OPT_START: + if (++i == argc) { + goto wrongNumArgs; + } + if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { + return JIM_ERR; + } + break; } } if (argc - i < 2) { @@ -339,6 +352,13 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) const char *replace_str; int replace_len; const char *pattern; + int option; + enum { + OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_END + }; + static const char const *options[] = { + "-nocase", "-line", "-all", "-start", "--", NULL + }; if (argc < 4) { wrongNumArgs: @@ -348,35 +368,39 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) } for (i = 1; i < argc; i++) { - if (Jim_CompareStringImmediate(interp, argv[i], "-nocase")) { - regcomp_flags |= REG_ICASE; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-line")) { - regcomp_flags |= REG_NEWLINE; - } - else if (Jim_CompareStringImmediate(interp, argv[i], "-all")) { - opt_all = 1; + const char *opt = Jim_GetString(argv[i], NULL); + + if (*opt != '-') { + break; } - else if (Jim_CompareStringImmediate(interp, argv[i], "-start")) { - if (++i == argc) { - goto wrongNumArgs; - } - if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { - return JIM_ERR; - } + if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; } - else if (Jim_CompareStringImmediate(interp, argv[i], "--")) { + if (option == OPT_END) { i++; break; } - else { - const char *opt = Jim_GetString(argv[i], NULL); + switch (option) { + case OPT_NOCASE: + regcomp_flags |= REG_ICASE; + break; - if (*opt == '-') { - /* Bad option */ - goto wrongNumArgs; - } - break; + case OPT_LINE: + regcomp_flags |= REG_NEWLINE; + break; + + case OPT_ALL: + opt_all = 1; + break; + + case OPT_START: + if (++i == argc) { + goto wrongNumArgs; + } + if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { + return JIM_ERR; + } + break; } } if (argc - i != 3 && argc - i != 4) { diff --git a/tests/regexp.test b/tests/regexp.test index 82a6c67..c6d9a01 100644 --- a/tests/regexp.test +++ b/tests/regexp.test @@ -197,10 +197,9 @@ test regexp-6.1 {regexp errors} { test regexp-6.2 {regexp errors} { list [catch {regexp -nocase a} msg] $msg } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}} -# XXX -#test regexp-6.3 {regexp errors} { -# list [catch {regexp -gorp a} msg] $msg -#} {1 {bad switch "-gorp": must be -all, -about, -indices, -inline, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --}} +test regexp-6.3 {regexp errors} { + list [catch {regexp -gorp a} msg] $msg +} {1 {bad switch "-gorp": must be --, -all, -indices, -inline, -line, -nocase, or -start}} test regexp-6.4 {regexp errors} { list [catch {regexp a( b} msg] [string match *parentheses* $msg] } {1 1} @@ -360,10 +359,9 @@ test regexp-11.3 {regsub errors} { test regexp-11.4 {regsub errors} { list [catch {regsub a b c d e f} msg] $msg } {1 {wrong # args: should be "regsub ?switches? exp string subSpec ?varName?"}} -# XXX -#test regexp-11.5 {regsub errors} { -# list [catch {regsub -gorp a b c} msg] $msg -#} {1 {bad switch "-gorp": must be -all, -nocase, -expanded, -line, -linestop, -lineanchor, -start, or --}} +test regexp-11.5 {regsub errors} { + list [catch {regsub -gorp a b c} msg] $msg +} {1 {bad switch "-gorp": must be --, -all, -line, -nocase, or -start}} test regexp-11.6 {regsub errors} { list [catch {regsub -nocase a( b c d} msg] [string match *parentheses* $msg] } {1 1} -- cgit v1.1