diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-01-24 12:44:43 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-15 11:02:44 +1000 |
commit | a230afdc68bcad14a9dfd0f9c8c6955980669cd6 (patch) | |
tree | 7f0824345c96818381a7e8c4f919d1aadce44322 /jim-array.c | |
parent | 9652302fec62f76bf894c6b9eb849bda6994c293 (diff) | |
download | jimtcl-a230afdc68bcad14a9dfd0f9c8c6955980669cd6.zip jimtcl-a230afdc68bcad14a9dfd0f9c8c6955980669cd6.tar.gz jimtcl-a230afdc68bcad14a9dfd0f9c8c6955980669cd6.tar.bz2 |
Many improvements, bug fixes
*: Allow math functions to be enabled via configure
*: Allow support for references to be removed
*: Documentation updates
*: Jim_ListLength() now returns the result directly
*: Optimise list -> dict conversion
*: Consistent capitalisation of some structures, functions
*: Add support for abbreviations to Jim_GetEnum()
*: The commands to 'info' may be abbreviated
*: Use abbreviation support in parsing options to 'subst'
*: Use Jim_GetEnum() to parse return code names
*: Optimise 'array get', 'array set' if no conversion needed
*: Import Tcl string.test
*: string compare now returns -1,0,1 like Tcl
*: Fix 'string last' with index=0
*: Add support for 'string reverse'
*: Add -nocase option to 'string equal'
*: Fix infinite loop in 'string repeat str -1'
*: Support braced patterns in glob
*: glob should not return dot files unless the pattern starts with .
*: Simplify glob.tcl by using some new features
*: When creating C extensions from Tcl, preserve newlines and invoke
with Jim_Eval_Named() to produce more meaningful error messages.
*: Also remove all comments, not just those starting in the first column
*: Add support for 'n+n' and 'n-n' in string/list indexes (Tcl 8.5)
*: Add a level to the stack trace for 'return -code error'
*: 'return -code' should also affect the return from 'source' (see Tcl docs)
*: Fix lsort -command
*: Some systems don't have INFINITY
Diffstat (limited to 'jim-array.c')
-rw-r--r-- | jim-array.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/jim-array.c b/jim-array.c index 903b157..ee61af4 100644 --- a/jim-array.c +++ b/jim-array.c @@ -66,6 +66,7 @@ static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int i; int len; + int all = 0; Jim_Obj *resultObj; Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); Jim_Obj *dictObj; @@ -75,6 +76,17 @@ static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_OK; } + if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) { + all = 1; + } + + /* If it is a dictionary or list, nothing else to do */ + if (all && (Jim_IsDict(objPtr) || Jim_IsList(objPtr))) { + /* XXX If it is a odd-length list no error will be returned */ + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + if (Jim_DictKeysVector(interp, objPtr, NULL, 0, &dictObj, JIM_ERRMSG) != JIM_OK) { return JIM_ERR; } @@ -83,7 +95,7 @@ static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return JIM_ERR; } - if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) { + if (all) { /* Return the whole array */ Jim_SetResult(interp, dictObj); } @@ -188,8 +200,7 @@ static int array_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv) /* Not found means zero length */ objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); if (objPtr) { - Jim_ListLength(interp, objPtr, &len); - len /= 2; + len = Jim_ListLength(interp, objPtr) / 2; } Jim_SetResultInt(interp, len); @@ -201,23 +212,29 @@ static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int i; int len; + int rc = JIM_OK; Jim_Obj *listObj = argv[1]; - Jim_ListLength(interp, listObj, &len); + if (Jim_GetVariable(interp, argv[0], JIM_NONE) == NULL) { + /* Doesn't exist, so just set the list directly */ + return Jim_SetVariable(interp, argv[0], listObj); + } + + len = Jim_ListLength(interp, listObj); if (len % 2) { Jim_SetResultString(interp, "list must have an even number of elements", -1); return JIM_ERR; } - for (i = 0; i < len; i += 2) { + for (i = 0; i < len && rc == JIM_OK; i += 2) { Jim_Obj *nameObj; Jim_Obj *valueObj; Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE); Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE); - Jim_SetDictKeysVector(interp, argv[0], &nameObj, 1, valueObj); + rc = Jim_SetDictKeysVector(interp, argv[0], &nameObj, 1, valueObj); } - return JIM_OK; + return rc; } static const jim_subcmd_type command_table[] = { |