diff options
author | pooryorick <com.digitalsmarties@pooryorick.com> | 2016-07-10 23:07:47 (GMT) |
---|---|---|
committer | pooryorick <com.digitalsmarties@pooryorick.com> | 2016-07-10 23:07:47 (GMT) |
commit | b72f659998fee56defeaa1dae2d98dafab30a574 (patch) | |
tree | 1d858b72944dfa9d5ec698e95ddb33e92e23b6ca /generic/tclStringObj.c | |
parent | 2de34a7674bffea69b889e1326b18e442d8e8589 (diff) | |
download | tcl-b72f659998fee56defeaa1dae2d98dafab30a574.zip tcl-b72f659998fee56defeaa1dae2d98dafab30a574.tar.gz tcl-b72f659998fee56defeaa1dae2d98dafab30a574.tar.bz2 |
Avoid generating string representation for comparisons against empty string.
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r-- | generic/tclStringObj.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c index e3cede6..de3a82e 100644 --- a/generic/tclStringObj.c +++ b/generic/tclStringObj.c @@ -421,6 +421,7 @@ Tcl_GetCharLength( return length; } + /* * OK, need to work with the object as a string. */ @@ -439,6 +440,53 @@ Tcl_GetCharLength( } return numChars; } + + + +/* + *---------------------------------------------------------------------- + * + * TclIsEmpty -- + * + * Determine whether the string value of an object is or would be the + * empty string, without generating a string representation. + * + * Results: + * Returns 1 if empty, 0 if not, and -1 if unknown. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ +int +TclIsEmpty ( + Tcl_Obj *objPtr +) { + int length = -1; + + if (objPtr->bytes == tclEmptyStringRep) { + return 1; + } + + if (TclIsPureList(objPtr)) { + Tcl_ListObjLength(NULL, objPtr, &length); + return length == 0; + } + + if (TclIsPureDict(objPtr)) { + Tcl_DictObjSize(NULL, objPtr, &length); + return length == 0; + } + + if (objPtr->bytes == NULL) { + return -1; + } + if (objPtr->bytes[0] == '\0') { + return 1; + } + return 0; +} /* *---------------------------------------------------------------------- |