diff options
author | dgp <dgp@users.sourceforge.net> | 2009-02-18 20:10:30 (GMT) |
---|---|---|
committer | dgp <dgp@users.sourceforge.net> | 2009-02-18 20:10:30 (GMT) |
commit | 26a75c8619e638a8cb57c46899fa77eb6fa1c8d8 (patch) | |
tree | d91d69270182bfa80aeecdc71636998a4c321761 /generic | |
parent | d6e613c86fae19a602868da81cf6077c49e5c214 (diff) | |
download | tcl-26a75c8619e638a8cb57c46899fa77eb6fa1c8d8.zip tcl-26a75c8619e638a8cb57c46899fa77eb6fa1c8d8.tar.gz tcl-26a75c8619e638a8cb57c46899fa77eb6fa1c8d8.tar.bz2 |
* generic/tclStringObj.c: Simplify the logic of the
Tcl_*SetObjLength() routines.
Diffstat (limited to 'generic')
-rw-r--r-- | generic/tclStringObj.c | 97 |
1 files changed, 31 insertions, 66 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c index d97c1e1..2925a80 100644 --- a/generic/tclStringObj.c +++ b/generic/tclStringObj.c @@ -33,7 +33,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclStringObj.c,v 1.117 2009/02/18 19:47:06 dgp Exp $ */ + * RCS: @(#) $Id: tclStringObj.c,v 1.118 2009/02/18 20:10:34 dgp Exp $ */ #include "tclInt.h" #include "tommath.h" @@ -807,42 +807,24 @@ Tcl_SetObjLength( SetStringFromAny(NULL, objPtr); stringPtr = GET_STRING(objPtr); - /* - * Check that we're not extending a pure unicode string. - */ - - if (length > stringPtr->allocated && - (objPtr->bytes != NULL || stringPtr->hasUnicode == 0)) { - /* - * Not enough space in current string. Reallocate the string space and - * free the old string. - */ - - if (objPtr->bytes != tclEmptyStringRep) { - objPtr->bytes = ckrealloc(objPtr->bytes, (unsigned) length+1); - } else { - objPtr->bytes = ckalloc((unsigned) length+1); - } - stringPtr->allocated = length; - + if (objPtr->bytes != NULL) { /* - * Invalidate the unicode data. + * Change length of an existing string rep. */ - - stringPtr->hasUnicode = 0; - } - - if (objPtr->bytes != NULL) { - objPtr->length = length; - if (objPtr->bytes != tclEmptyStringRep) { + if (length > stringPtr->allocated) { /* - * Ensure the string is NUL-terminated. + * Need to enlarge the buffer. */ - - objPtr->bytes[length] = 0; + if (objPtr->bytes == tclEmptyStringRep) { + objPtr->bytes = ckalloc((unsigned) length+1); + } else { + objPtr->bytes = ckrealloc(objPtr->bytes, (unsigned) length+1); + } + stringPtr->allocated = length; } - /* Note: here we can get an empty string != tclEmptyStringRep */ + objPtr->length = length; + objPtr->bytes[length] = 0; /* * Invalidate the unicode data. @@ -855,7 +837,6 @@ Tcl_SetObjLength( * Changing length of pure unicode string. */ - stringCheckLimits(length); if (length > stringPtr->maxChars) { stringPtr = stringRealloc(stringPtr, length); @@ -930,47 +911,31 @@ Tcl_AttemptSetObjLength( SetStringFromAny(NULL, objPtr); stringPtr = GET_STRING(objPtr); - /* - * Check that we're not extending a pure unicode string. - */ - - if (length > stringPtr->allocated && - (objPtr->bytes != NULL || stringPtr->hasUnicode == 0)) { - char *newBytes; - - /* - * Not enough space in current string. Reallocate the string space and - * free the old string. - */ - - if (objPtr->bytes != tclEmptyStringRep) { - newBytes = attemptckrealloc(objPtr->bytes, (unsigned) length+1); - } else { - newBytes = attemptckalloc((unsigned) length+1); - } - if (newBytes == NULL) { - return 0; - } - objPtr->bytes = newBytes; - stringPtr->allocated = length; - + if (objPtr->bytes != NULL) { /* - * Invalidate the unicode data. + * Change length of an existing string rep. */ - - stringPtr->hasUnicode = 0; - } - - if (objPtr->bytes != NULL) { - objPtr->length = length; - if (objPtr->bytes != tclEmptyStringRep) { + if (length > stringPtr->allocated) { /* - * Ensure the string is NULL-terminated. + * Need to enlarge the buffer. */ + char *newBytes; - objPtr->bytes[length] = 0; + if (objPtr->bytes == tclEmptyStringRep) { + newBytes = attemptckalloc((unsigned) length+1); + } else { + newBytes = attemptckrealloc(objPtr->bytes, (unsigned) length+1); + } + if (newBytes == NULL) { + return 0; + } + objPtr->bytes = newBytes; + stringPtr->allocated = length; } + objPtr->length = length; + objPtr->bytes[length] = 0; + /* * Invalidate the unicode data. */ |