diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-10-16 21:52:15 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-20 10:30:34 +1000 |
commit | c00426df647271ce753acf97103086d73b8fbb83 (patch) | |
tree | a6ddea00fe9232092607c25cbe48a3a874f1871d /jim-regexp.c | |
parent | 9fcb8099151969bc148ecd69934ee061b77eb2bd (diff) | |
download | jimtcl-c00426df647271ce753acf97103086d73b8fbb83.zip jimtcl-c00426df647271ce753acf97103086d73b8fbb83.tar.gz jimtcl-c00426df647271ce753acf97103086d73b8fbb83.tar.bz2 |
Switch regexp/regsub to use Jim_GetEnum()
Provides a better error message on wrong args and allows
abbreviations.
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim-regexp.c')
-rw-r--r-- | jim-regexp.c | 128 |
1 files changed, 76 insertions, 52 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) { |