summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2018-03-06 20:09:00 (GMT)
committerdgp <dgp@users.sourceforge.net>2018-03-06 20:09:00 (GMT)
commit847a048c18e68af9dd1140e12e922544b4fb25c6 (patch)
tree915aa577410bcb5a35adbc8c08776ca89888aa70
parentda8fbb89c41229c79b8f373f955ce1fb59cb4233 (diff)
downloadtcl-847a048c18e68af9dd1140e12e922544b4fb25c6.zip
tcl-847a048c18e68af9dd1140e12e922544b4fb25c6.tar.gz
tcl-847a048c18e68af9dd1140e12e922544b4fb25c6.tar.bz2
New internal routine TclGetEndOffsetFromObj.
-rw-r--r--generic/tclInt.h2
-rw-r--r--generic/tclUtil.c46
2 files changed, 41 insertions, 7 deletions
diff --git a/generic/tclInt.h b/generic/tclInt.h
index 567a28c..1b1b078 100644
--- a/generic/tclInt.h
+++ b/generic/tclInt.h
@@ -2978,6 +2978,8 @@ MODULE_SCOPE int TclGetChannelFromObj(Tcl_Interp *interp,
MODULE_SCOPE CmdFrame * TclGetCmdFrameForProcedure(Proc *procPtr);
MODULE_SCOPE int TclGetCompletionCodeFromObj(Tcl_Interp *interp,
Tcl_Obj *value, int *code);
+MODULE_SCOPE int TclGetEndOffsetFromObj(Tcl_Interp *interp,
+ Tcl_Obj *objPtr, int endValue, int *indexPtr);
MODULE_SCOPE int TclGetNumberFromObj(Tcl_Interp *interp,
Tcl_Obj *objPtr, ClientData *clientDataPtr,
int *typePtr);
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index 3424295..beeaae1 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -3575,13 +3575,7 @@ TclGetIntForIndex(
return TCL_OK;
}
- if (SetEndOffsetFromAny(NULL, objPtr) == TCL_OK) {
- /*
- * If the object is already an offset from the end of the list, or can
- * be converted to one, use it.
- */
-
- *indexPtr = endValue + objPtr->internalRep.longValue;
+ if (TclGetEndOffsetFromObj(NULL, objPtr, endValue, indexPtr) == TCL_OK) {
return TCL_OK;
}
@@ -3684,6 +3678,42 @@ UpdateStringOfEndOffset(
/*
*----------------------------------------------------------------------
*
+ * TclGetEndOffsetFromObj --
+ *
+ * Look for a string of the form "end[+-]offset" and convert it to an
+ * internal representation holding the offset.
+ *
+ * Results:
+ * Tcl return code.
+ *
+ * Side effects:
+ * May store a Tcl_ObjType.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclGetEndOffsetFromObj(
+ Tcl_Interp *interp, /* For error reporting, may be NULL. */
+ Tcl_Obj *objPtr, /* Pointer to the object to parse */
+ int endValue, /* The value to be stored at "indexPtr" if
+ * "objPtr" holds "end". */
+ int *indexPtr) /* Location filled in with an integer
+ * representing an index. */
+{
+ if (SetEndOffsetFromAny(interp, objPtr) != TCL_OK) {
+ return TCL_ERROR;
+ }
+
+ /* TODO: Handle overflow cases sensibly */
+ *indexPtr = endValue + (int)objPtr->internalRep.longValue;
+ return TCL_OK;
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
* SetEndOffsetFromAny --
*
* Look for a string of the form "end[+-]offset" and convert it to an
@@ -3750,6 +3780,8 @@ SetEndOffsetFromAny(
return TCL_ERROR;
}
if (bytes[3] == '-') {
+
+ /* TODO: Review overflow concerns here! */
offset = -offset;
}
} else {