Skip to content

Commit 4c32dab

Browse files
author
jan.nijtmans
committed
More refactoring
1 parent 19b438a commit 4c32dab

File tree

1 file changed

+51
-64
lines changed

1 file changed

+51
-64
lines changed

generic/tclObj.c

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,6 @@ Tcl_GetBoolFromObj(
20062006
char *charPtr) /* Place to store resulting boolean. */
20072007
{
20082008
int result;
2009-
Tcl_Size length;
20102009

20112010
if ((flags & TCL_NULL_OK) && (objPtr == NULL || Tcl_GetString(objPtr)[0] == '\0')) {
20122011
result = -1;
@@ -2062,22 +2061,6 @@ Tcl_GetBoolFromObj(
20622061
}
20632062
return TCL_OK;
20642063
}
2065-
/* Handle dict separately, because it doesn't have a lengthProc */
2066-
if (TclHasInternalRep(objPtr, &tclDictType)) {
2067-
Tcl_DictObjSize(NULL, objPtr, &length);
2068-
if (length > 0) {
2069-
listRep:
2070-
if (interp) {
2071-
Tcl_SetObjResult(interp, Tcl_ObjPrintf("expected boolean value%s but got a list",
2072-
(flags & TCL_NULL_OK) ? " or \"\"" : ""));
2073-
}
2074-
return TCL_ERROR;
2075-
}
2076-
}
2077-
Tcl_ObjTypeLengthProc *lengthProc = TclObjTypeHasProc(objPtr, lengthProc);
2078-
if (lengthProc && lengthProc(objPtr) > 1) {
2079-
goto listRep;
2080-
}
20812064
} while ((ParseBoolean(objPtr) == TCL_OK) || (TCL_OK ==
20822065
TclParseNumber(interp, objPtr, (flags & TCL_NULL_OK)
20832066
? "boolean value or \"\"" : "boolean value", NULL,-1,NULL,0)));
@@ -2119,42 +2102,28 @@ TclSetBooleanFromAny(
21192102
Tcl_Interp *interp, /* Used for error reporting if not NULL. */
21202103
Tcl_Obj *objPtr) /* The object to convert. */
21212104
{
2122-
/*
2123-
* For some "pure" numeric Tcl_ObjTypes (no string rep), we can determine
2124-
* whether a boolean conversion is possible without generating the string
2125-
* rep.
2126-
*/
2127-
2128-
if (objPtr->bytes == NULL) {
2129-
if (TclHasInternalRep(objPtr, &tclIntType)) {
2130-
if ((Tcl_WideUInt)objPtr->internalRep.wideValue < 2) {
2131-
return TCL_OK;
2132-
}
2133-
goto badBoolean;
2134-
}
2135-
2136-
if (TclHasInternalRep(objPtr, &tclBignumType)) {
2137-
goto badBoolean;
2138-
}
2139-
2140-
if (TclHasInternalRep(objPtr, &tclDoubleType)) {
2141-
goto badBoolean;
2142-
}
2143-
}
2144-
21452105
if (ParseBoolean(objPtr) == TCL_OK) {
21462106
return TCL_OK;
21472107
}
21482108

2149-
badBoolean:
21502109
if (interp != NULL) {
2151-
Tcl_Size length;
2152-
const char *str = Tcl_GetStringFromObj(objPtr, &length);
21532110
Tcl_Obj *msg;
21542111

2155-
TclNewLiteralStringObj(msg, "expected boolean value but got \"");
2156-
Tcl_AppendLimitedToObj(msg, str, length, 50, "");
2157-
Tcl_AppendToObj(msg, "\"", -1);
2112+
TclNewLiteralStringObj(msg, "expected boolean value but got ");
2113+
2114+
Tcl_Size argc;
2115+
const char **argv = NULL;
2116+
if (!objPtr->bytes || ((TclMaxListLength(objPtr->bytes, TCL_INDEX_NONE, NULL) > 1)
2117+
&& Tcl_SplitList(NULL, objPtr->bytes, &argc, &argv) == TCL_OK)) {
2118+
if (argv) {
2119+
Tcl_Free(argv);
2120+
}
2121+
Tcl_AppendToObj(msg, "a list", -1);
2122+
} else {
2123+
Tcl_AppendToObj(msg, "\"", -1);
2124+
Tcl_AppendLimitedToObj(msg, objPtr->bytes, objPtr->length, 50, "");
2125+
Tcl_AppendToObj(msg, "\"", -1);
2126+
}
21582127
Tcl_SetObjResult(interp, msg);
21592128
Tcl_SetErrorCode(interp, "TCL", "VALUE", "BOOLEAN", (char *)NULL);
21602129
}
@@ -2168,7 +2137,42 @@ ParseBoolean(
21682137
int newBool;
21692138
char lowerCase[6];
21702139
Tcl_Size i, length;
2171-
const char *str = Tcl_GetStringFromObj(objPtr, &length);
2140+
const char *str;
2141+
2142+
/*
2143+
* For some "pure" numeric Tcl_ObjTypes (no string rep), we can determine
2144+
* whether a boolean conversion is possible without generating the string
2145+
* rep.
2146+
*/
2147+
2148+
if (objPtr->bytes == NULL) {
2149+
if (TclHasInternalRep(objPtr, &tclIntType)) {
2150+
if ((Tcl_WideUInt)objPtr->internalRep.wideValue < 2) {
2151+
return TCL_OK;
2152+
}
2153+
return TCL_ERROR;
2154+
}
2155+
2156+
if (TclHasInternalRep(objPtr, &tclBignumType)) {
2157+
return TCL_ERROR;
2158+
}
2159+
2160+
if (TclHasInternalRep(objPtr, &tclDoubleType)) {
2161+
return TCL_ERROR;
2162+
}
2163+
/* Handle dict separately, because it doesn't have a lengthProc */
2164+
if (TclHasInternalRep(objPtr, &tclDictType)) {
2165+
Tcl_DictObjSize(NULL, objPtr, &length);
2166+
if (length > 0) {
2167+
return TCL_ERROR;
2168+
}
2169+
}
2170+
Tcl_ObjTypeLengthProc *lengthProc = TclObjTypeHasProc(objPtr, lengthProc);
2171+
if (lengthProc && lengthProc(objPtr) != 1) {
2172+
return TCL_ERROR;
2173+
}
2174+
}
2175+
str = Tcl_GetStringFromObj(objPtr, &length);
21722176

21732177
if ((length < 1) || (length > 5)) {
21742178
/*
@@ -2438,7 +2442,6 @@ Tcl_GetDoubleFromObj(
24382442
Tcl_Obj *objPtr, /* The object from which to get a double. */
24392443
double *dblPtr) /* Place to store resulting double. */
24402444
{
2441-
Tcl_Size length;
24422445
do {
24432446
if (TclHasInternalRep(objPtr, &tclDoubleType)) {
24442447
if (isnan(objPtr->internalRep.doubleValue)) {
@@ -2464,22 +2467,6 @@ Tcl_GetDoubleFromObj(
24642467
*dblPtr = TclBignumToDouble(&big);
24652468
return TCL_OK;
24662469
}
2467-
/* Handle dict separately, because it doesn't have a lengthProc */
2468-
if (TclHasInternalRep(objPtr, &tclDictType)) {
2469-
Tcl_DictObjSize(NULL, objPtr, &length);
2470-
if (length > 0) {
2471-
listRep:
2472-
if (interp) {
2473-
Tcl_SetObjResult(interp,
2474-
Tcl_NewStringObj("expected floating-point number but got a list", TCL_INDEX_NONE));
2475-
}
2476-
return TCL_ERROR;
2477-
}
2478-
}
2479-
Tcl_ObjTypeLengthProc *lengthProc = TclObjTypeHasProc(objPtr, lengthProc);
2480-
if (lengthProc && lengthProc(objPtr) > 1) {
2481-
goto listRep;
2482-
}
24832470
} while (SetDoubleFromAny(interp, objPtr) == TCL_OK);
24842471
return TCL_ERROR;
24852472
}

0 commit comments

Comments
 (0)