aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jim.c48
-rw-r--r--test.tcl328
2 files changed, 367 insertions, 9 deletions
diff --git a/jim.c b/jim.c
index f6e72c6..9fd3c64 100644
--- a/jim.c
+++ b/jim.c
@@ -290,8 +290,32 @@ int Jim_StringToWide(char *str, jim_wide *widePtr, int base)
#else
*widePtr = strtol(str, &endptr, base);
#endif
- if (str[0] == '\0' || endptr[0] != '\0')
+ if (str[0] == '\0')
return JIM_ERR;
+ if (endptr[0] != '\0') {
+ while(*endptr) {
+ if (!isspace((int)*endptr))
+ return JIM_ERR;
+ endptr++;
+ }
+ }
+ return JIM_OK;
+}
+
+int Jim_StringToIndex(char *str, int *intPtr)
+{
+ char *endptr;
+
+ *intPtr = strtol(str, &endptr, 10);
+ if (str[0] == '\0')
+ return JIM_ERR;
+ if (endptr[0] != '\0') {
+ while(*endptr) {
+ if (!isspace((int)*endptr))
+ return JIM_ERR;
+ endptr++;
+ }
+ }
return JIM_OK;
}
@@ -4832,7 +4856,7 @@ void UpdateStringOfIndex(struct Jim_Obj *objPtr)
int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
{
int index, end = 0;
- char *str, *endptr;
+ char *str;
/* Get the string representation */
str = Jim_GetString(objPtr, NULL);
@@ -4845,11 +4869,10 @@ int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
str += 4;
end = 1;
}
- index = strtol(str, &endptr, 0);
- if (str[0] == '\0' || endptr[0] != '\0') {
+ if (Jim_StringToIndex(str, &index) != JIM_OK) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
- "bad index \"", str, "\": "
+ "bad index \"", Jim_GetString(objPtr, NULL), "\": "
"must be integer or end?-integer?",
NULL);
return JIM_ERR;
@@ -6003,7 +6026,7 @@ static int Jim_Unknown(Jim_Interp *interp, int argc, Jim_Obj **argv)
if (argc+1 <= JIM_EVAL_SARGV_LEN)
v = sv;
else
- v = Jim_Alloc(sizeof(Jim_Obj*)*argc+1);
+ v = Jim_Alloc(sizeof(Jim_Obj*)*(argc+1));
/* Make a copy of the arguments vector, but shifted on
* the right of one position. The command name of the
* command will be instead the first argument of the
@@ -7243,23 +7266,30 @@ int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj **argv)
int index;
if (argc < 3) {
- Jim_WrongNumArgs(interp, 1, argv, "listValue index ?...?");
+ Jim_WrongNumArgs(interp, 1, argv, "list index ?...?");
return JIM_ERR;
}
objPtr = argv[1];
+ Jim_IncrRefCount(objPtr);
for (i = 2; i < argc; i++) {
listObjPtr = objPtr;
- if (Jim_GetIndex(interp, argv[i], &index) != JIM_OK)
+ if (Jim_GetIndex(interp, argv[i], &index) != JIM_OK) {
+ Jim_DecrRefCount(interp, listObjPtr);
return JIM_ERR;
+ }
if (Jim_ListIndex(interp, listObjPtr, index, &objPtr,
JIM_NONE) != JIM_OK) {
/* Returns an empty object if the index
* is out of range. */
+ Jim_DecrRefCount(interp, listObjPtr);
Jim_SetEmptyResult(interp);
return JIM_OK;
}
+ Jim_IncrRefCount(objPtr);
+ Jim_DecrRefCount(interp, listObjPtr);
}
Jim_SetResult(interp, objPtr);
+ Jim_DecrRefCount(interp, objPtr);
return JIM_OK;
}
@@ -7269,7 +7299,7 @@ int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj **argv)
int len;
if (argc != 2) {
- Jim_WrongNumArgs(interp, 1, argv, "listValue");
+ Jim_WrongNumArgs(interp, 1, argv, "list");
return JIM_ERR;
}
Jim_ListLength(interp, argv[1], &len);
diff --git a/test.tcl b/test.tcl
index d56e8fb..cfe3118 100644
--- a/test.tcl
+++ b/test.tcl
@@ -1605,6 +1605,334 @@ test incr-2.29 {incr command (not compiled): runtime error, bad variable value}
list [catch {$z x 1} msg] $msg
} {1 {expected integer but got " - "}}
+################################################################################
+# LLENGTH
+################################################################################
+
+test llength-1.1 {length of list} {
+ llength {a b c d}
+} 4
+test llength-1.2 {length of list} {
+ llength {a b c {a b {c d}} d}
+} 5
+test llength-1.3 {length of list} {
+ llength {}
+} 0
+
+test llength-2.1 {error conditions} {
+ list [catch {llength} msg] $msg
+} {1 {wrong # args: should be "llength list"}}
+test llength-2.2 {error conditions} {
+ list [catch {llength 123 2} msg] $msg
+} {1 {wrong # args: should be "llength list"}}
+
+################################################################################
+# LINDEX
+################################################################################
+
+set lindex lindex
+set minus -
+
+# Tests of Tcl_LindexObjCmd, NOT COMPILED
+
+#test lindex-1.1 {wrong # args} {
+# list [catch {eval $lindex} result] $result
+#} "1 {wrong # args: should be \"lindex list ?index...?\"}"
+
+# Indices that are lists or convertible to lists
+
+#test lindex-2.1 {empty index list} {
+# set x {}
+# list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+#} {{a b c} {a b c}}
+
+test lindex-2.2 {singleton index list} {
+ set x { 1 }
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {b b}
+
+test lindex-2.4 {malformed index list} {
+ set x \{
+ list [catch { eval [list $lindex {a b c} $x] } result] $result
+} {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
+
+# Indices that are integers or convertible to integers
+
+test lindex-3.1 {integer -1} {
+ set x ${minus}1
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {{} {}}
+
+test lindex-3.2 {integer 0} {
+ set x [string range 00 0 0]
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {a a}
+
+test lindex-3.3 {integer 2} {
+ set x [string range 22 0 0]
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {c c}
+
+test lindex-3.4 {integer 3} {
+ set x [string range 33 0 0]
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {{} {}}
+
+test lindex-3.7 {indexes don't shimmer wide ints} {
+ set x [expr {(1<<31) - 2}]
+ list $x [lindex {1 2 3} $x] [incr x] [incr x]
+} {2147483646 {} 2147483647 2147483648}
+
+# Indices relative to end
+
+test lindex-4.1 {index = end} {
+ set x end
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {c c}
+
+test lindex-4.2 {index = end--1} {
+ set x end--1
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {{} {}}
+
+test lindex-4.3 {index = end-0} {
+ set x end-0
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {c c}
+
+test lindex-4.4 {index = end-2} {
+ set x end-2
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {a a}
+
+test lindex-4.5 {index = end-3} {
+ set x end-3
+ list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+} {{} {}}
+
+test lindex-4.8 {bad integer, not octal} {
+ set x end-0a2
+ list [catch { eval [list $lindex {a b c} $x] } result] $result
+} "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
+
+#test lindex-4.9 {incomplete end} {
+# set x en
+# list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
+#} {c c}
+
+test lindex-4.10 {incomplete end-} {
+ set x end-
+ list [catch { eval [list $lindex {a b c} $x] } result] $result
+} "1 {bad index \"end-\": must be integer or end?-integer?}"
+
+test lindex-5.1 {bad second index} {
+ list [catch { eval [list $lindex {a b c} 0 0a2] } result] $result
+} "1 {bad index \"0a2\": must be integer or end?-integer?}"
+
+test lindex-5.2 {good second index} {
+ eval [list $lindex {{a b c} {d e f} {g h i}} 1 2]
+} f
+
+test lindex-5.3 {three indices} {
+ eval [list $lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1]
+} f
+
+test lindex-7.1 {quoted elements} {
+ eval [list $lindex {a "b c" d} 1]
+} {b c}
+test lindex-7.2 {quoted elements} {
+ eval [list $lindex {"{}" b c} 0]
+} {{}}
+test lindex-7.3 {quoted elements} {
+ eval [list $lindex {ab "c d \" x" y} 1]
+} {c d " x}
+test lindex-7.4 {quoted elements} {
+ lindex {a b {c d "e} {f g"}} 2
+} {c d "e}
+
+test lindex-8.1 {data reuse} {
+ set x 0
+ eval [list $lindex $x $x]
+} {0}
+
+test lindex-8.2 {data reuse} {
+ set a 0
+ eval [list $lindex $a $a $a]
+} 0
+test lindex-8.3 {data reuse} {
+ set a 1
+ eval [list $lindex $a $a $a]
+} {}
+
+#----------------------------------------------------------------------
+
+test lindex-10.2 {singleton index list} {
+ set x { 1 }
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {b b}
+
+test lindex-10.4 {malformed index list} {
+ set x \{
+ list [catch { lindex {a b c} $x } result] $result
+} {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
+
+# Indices that are integers or convertible to integers
+
+test lindex-11.1 {integer -1} {
+ set x ${minus}1
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {{} {}}
+
+test lindex-11.2 {integer 0} {
+ set x [string range 00 0 0]
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {a a}
+
+test lindex-11.3 {integer 2} {
+ set x [string range 22 0 0]
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {c c}
+
+test lindex-11.4 {integer 3} {
+ set x [string range 33 0 0]
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {{} {}}
+
+# Indices relative to end
+test lindex-12.1 {index = end} {
+ set x end
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {c c}
+
+test lindex-12.2 {index = end--1} {
+ set x end--1
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {{} {}}
+
+test lindex-12.3 {index = end-0} {
+ set x end-0
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {c c}
+
+test lindex-12.4 {index = end-2} {
+ set x end-2
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {a a}
+
+test lindex-12.5 {index = end-3} {
+ set x end-3
+ catch {
+ list [lindex {a b c} $x] [lindex {a b c} $x]
+ } result
+ set result
+} {{} {}}
+
+test lindex-12.8 {bad integer, not octal} {
+ set x end-0a2
+ list [catch { lindex {a b c} $x } result] $result
+} "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
+
+test lindex-12.10 {incomplete end-} {
+ set x end-
+ list [catch { lindex {a b c} $x } result] $result
+} "1 {bad index \"end-\": must be integer or end?-integer?}"
+
+test lindex-13.1 {bad second index} {
+ list [catch { lindex {a b c} 0 0a2 } result] $result
+} "1 {bad index \"0a2\": must be integer or end?-integer?}"
+
+test lindex-13.2 {good second index} {
+ catch {
+ lindex {{a b c} {d e f} {g h i}} 1 2
+ } result
+ set result
+} f
+
+test lindex-13.3 {three indices} {
+ catch {
+ lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1
+ } result
+ set result
+} f
+
+test lindex-15.1 {quoted elements} {
+ catch {
+ lindex {a "b c" d} 1
+ } result
+ set result
+} {b c}
+test lindex-15.2 {quoted elements} {
+ catch {
+ lindex {"{}" b c} 0
+ } result
+ set result
+} {{}}
+test lindex-15.3 {quoted elements} {
+ catch {
+ lindex {ab "c d \" x" y} 1
+ } result
+ set result
+} {c d " x}
+test lindex-15.4 {quoted elements} {
+ catch {
+ lindex {a b {c d "e} {f g"}} 2
+ } result
+ set result
+} {c d "e}
+
+test lindex-16.1 {data reuse} {
+ set x 0
+ catch {
+ lindex $x $x
+ } result
+ set result
+} {0}
+
+test lindex-16.2 {data reuse} {
+ set a 0
+ catch {
+ lindex $a $a $a
+ } result
+ set result
+} 0
+test lindex-16.3 {data reuse} {
+ set a 1
+ catch {
+ lindex $a $a $a
+ } result
+ set result
+} {}
+
+catch { unset lindex}
+catch { unset minus }
+
################################################################################
# FINAL REPORT