summaryrefslogtreecommitdiffstats
path: root/generic/tclStringObj.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r--generic/tclStringObj.c145
1 files changed, 71 insertions, 74 deletions
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index 59758bb..e4ddf37 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -45,28 +45,28 @@
static void AppendPrintfToObjVA(Tcl_Obj *objPtr,
const char *format, va_list argList);
static void AppendUnicodeToUnicodeRep(Tcl_Obj *objPtr,
- const Tcl_UniChar *unicode, int appendNumChars);
+ const Tcl_UniChar *unicode, size_t appendNumChars);
static void AppendUnicodeToUtfRep(Tcl_Obj *objPtr,
- const Tcl_UniChar *unicode, int numChars);
+ const Tcl_UniChar *unicode, size_t numChars);
static void AppendUtfToUnicodeRep(Tcl_Obj *objPtr,
- const char *bytes, int numBytes);
+ const char *bytes, size_t numBytes);
static void AppendUtfToUtfRep(Tcl_Obj *objPtr,
- const char *bytes, int numBytes);
+ const char *bytes, size_t numBytes);
static void DupStringInternalRep(Tcl_Obj *objPtr,
Tcl_Obj *copyPtr);
-static int ExtendStringRepWithUnicode(Tcl_Obj *objPtr,
- const Tcl_UniChar *unicode, int numChars);
+static size_t ExtendStringRepWithUnicode(Tcl_Obj *objPtr,
+ const Tcl_UniChar *unicode, size_t numChars);
static void ExtendUnicodeRepWithString(Tcl_Obj *objPtr,
- const char *bytes, int numBytes,
- int numAppendChars);
+ const char *bytes, size_t numBytes,
+ size_t numAppendChars);
static void FillUnicodeRep(Tcl_Obj *objPtr);
static void FreeStringInternalRep(Tcl_Obj *objPtr);
-static void GrowStringBuffer(Tcl_Obj *objPtr, int needed, int flag);
-static void GrowUnicodeBuffer(Tcl_Obj *objPtr, int needed);
+static void GrowStringBuffer(Tcl_Obj *objPtr, size_t needed, int flag);
+static void GrowUnicodeBuffer(Tcl_Obj *objPtr, size_t needed);
static int SetStringFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static void SetUnicodeObj(Tcl_Obj *objPtr,
- const Tcl_UniChar *unicode, int numChars);
-static int UnicodeLength(const Tcl_UniChar *unicode);
+ const Tcl_UniChar *unicode, size_t numChars);
+static size_t UnicodeLength(const Tcl_UniChar *unicode);
static void UpdateStringOfString(Tcl_Obj *objPtr);
/*
@@ -122,7 +122,7 @@ const Tcl_ObjType tclStringType = {
static void
GrowStringBuffer(
Tcl_Obj *objPtr,
- int needed,
+ size_t needed,
int flag)
{
/*
@@ -134,14 +134,14 @@ GrowStringBuffer(
String *stringPtr = GET_STRING(objPtr);
char *ptr = NULL;
- int attempt;
+ size_t attempt;
if (objPtr->bytes == &tclEmptyString) {
objPtr->bytes = NULL;
}
if (flag == 0 || stringPtr->allocated > 0) {
attempt = 2 * needed;
- if (attempt >= 0) {
+ if (attempt <= STRING_MAXCHARS) {
ptr = attemptckrealloc(objPtr->bytes, attempt + 1);
}
if (ptr == NULL) {
@@ -150,9 +150,9 @@ GrowStringBuffer(
* overflow into invalid argument values for attempt.
*/
- unsigned int limit = INT_MAX - needed;
- unsigned int extra = needed - objPtr->length + TCL_MIN_GROWTH;
- int growth = (int) ((extra > limit) ? limit : extra);
+ size_t limit = INT_MAX - needed;
+ size_t extra = needed - objPtr->length + TCL_MIN_GROWTH;
+ size_t growth = (extra > limit) ? limit : extra;
attempt = needed + growth;
ptr = attemptckrealloc(objPtr->bytes, attempt + 1);
@@ -173,7 +173,7 @@ GrowStringBuffer(
static void
GrowUnicodeBuffer(
Tcl_Obj *objPtr,
- int needed)
+ size_t needed)
{
/*
* Pre-conditions:
@@ -183,7 +183,7 @@ GrowUnicodeBuffer(
*/
String *ptr = NULL, *stringPtr = GET_STRING(objPtr);
- int attempt;
+ size_t attempt;
if (stringPtr->maxChars > 0) {
/*
@@ -191,7 +191,7 @@ GrowUnicodeBuffer(
*/
attempt = 2 * needed;
- if (attempt >= 0 && attempt <= STRING_MAXCHARS) {
+ if (attempt <= STRING_MAXCHARS) {
ptr = stringAttemptRealloc(stringPtr, attempt);
}
if (ptr == NULL) {
@@ -200,10 +200,10 @@ GrowUnicodeBuffer(
* overflow into invalid argument values for attempt.
*/
- unsigned int limit = STRING_MAXCHARS - needed;
- unsigned int extra = needed - stringPtr->numChars
+ size_t limit = STRING_MAXCHARS - needed;
+ size_t extra = needed - stringPtr->numChars
+ TCL_MIN_UNICHAR_GROWTH;
- int growth = (int) ((extra > limit) ? limit : extra);
+ size_t growth = (extra > limit) ? limit : extra;
attempt = needed + growth;
ptr = stringAttemptRealloc(stringPtr, attempt);
@@ -498,10 +498,10 @@ Tcl_GetUniChar(
* If numChars is unknown, compute it.
*/
- if (stringPtr->numChars == -1) {
+ if (stringPtr->numChars == (size_t)-1) {
TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);
}
- if (stringPtr->numChars == objPtr->length) {
+ if (stringPtr->numChars == (size_t)objPtr->length) {
return (Tcl_UniChar) objPtr->bytes[index];
}
FillUnicodeRep(objPtr);
@@ -632,10 +632,10 @@ Tcl_GetRange(
* If numChars is unknown, compute it.
*/
- if (stringPtr->numChars == -1) {
+ if (stringPtr->numChars == (size_t)-1) {
TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);
}
- if (stringPtr->numChars == objPtr->length) {
+ if (stringPtr->numChars == (size_t)objPtr->length) {
newObjPtr = Tcl_NewStringObj(objPtr->bytes + first, last-first+1);
/*
@@ -763,7 +763,7 @@ Tcl_SetObjLength(
/*
* Change length of an existing string rep.
*/
- if (length > stringPtr->allocated) {
+ if ((size_t)length > stringPtr->allocated) {
/*
* Need to enlarge the buffer.
*/
@@ -782,7 +782,7 @@ Tcl_SetObjLength(
* Invalidate the unicode data.
*/
- stringPtr->numChars = -1;
+ stringPtr->numChars = (size_t)-1;
stringPtr->hasUnicode = 0;
} else {
/*
@@ -790,7 +790,7 @@ Tcl_SetObjLength(
*/
stringCheckLimits(length);
- if (length > stringPtr->maxChars) {
+ if ((size_t)length > stringPtr->maxChars) {
stringPtr = stringRealloc(stringPtr, length);
SET_STRING(objPtr, stringPtr);
stringPtr->maxChars = length;
@@ -866,7 +866,7 @@ Tcl_AttemptSetObjLength(
/*
* Change length of an existing string rep.
*/
- if (length > stringPtr->allocated) {
+ if ((size_t)length > stringPtr->allocated) {
/*
* Need to enlarge the buffer.
*/
@@ -892,17 +892,17 @@ Tcl_AttemptSetObjLength(
* Invalidate the unicode data.
*/
- stringPtr->numChars = -1;
+ stringPtr->numChars = (size_t)-1;
stringPtr->hasUnicode = 0;
} else {
/*
* Changing length of pure unicode string.
*/
- if (length > STRING_MAXCHARS) {
+ if ((size_t)length > STRING_MAXCHARS) {
return 0;
}
- if (length > stringPtr->maxChars) {
+ if ((size_t)length > stringPtr->maxChars) {
stringPtr = stringAttemptRealloc(stringPtr, length);
if (stringPtr == NULL) {
return 0;
@@ -958,14 +958,14 @@ Tcl_SetUnicodeObj(
SetUnicodeObj(objPtr, unicode, numChars);
}
-static int
+static size_t
UnicodeLength(
const Tcl_UniChar *unicode)
{
- int numChars = 0;
+ size_t numChars = 0;
if (unicode) {
- while (numChars >= 0 && unicode[numChars] != 0) {
+ while (numChars != (size_t)-1 && unicode[numChars] != 0) {
numChars++;
}
}
@@ -978,12 +978,12 @@ SetUnicodeObj(
Tcl_Obj *objPtr, /* The object to set the string of. */
const Tcl_UniChar *unicode, /* The unicode string used to initialize the
* object. */
- int numChars) /* Number of characters in the unicode
+ size_t numChars) /* Number of characters in the unicode
* string. */
{
String *stringPtr;
- if (numChars < 0) {
+ if (numChars == (size_t)-1) {
numChars = UnicodeLength(unicode);
}
@@ -1071,7 +1071,7 @@ Tcl_AppendLimitedToObj(
SetStringFromAny(NULL, objPtr);
stringPtr = GET_STRING(objPtr);
- if (stringPtr->hasUnicode && stringPtr->numChars > 0) {
+ if (stringPtr->hasUnicode && (stringPtr->numChars+1) > 1) {
AppendUtfToUnicodeRep(objPtr, bytes, toCopy);
} else {
AppendUtfToUtfRep(objPtr, bytes, toCopy);
@@ -1082,7 +1082,7 @@ Tcl_AppendLimitedToObj(
}
stringPtr = GET_STRING(objPtr);
- if (stringPtr->hasUnicode && stringPtr->numChars > 0) {
+ if (stringPtr->hasUnicode && (stringPtr->numChars+1) > 1) {
AppendUtfToUnicodeRep(objPtr, ellipsis, strlen(ellipsis));
} else {
AppendUtfToUtfRep(objPtr, ellipsis, strlen(ellipsis));
@@ -1194,7 +1194,8 @@ Tcl_AppendObjToObj(
Tcl_Obj *appendObjPtr) /* Object to append. */
{
String *stringPtr;
- int length, numChars, appendNumChars = -1;
+ int length, numChars;
+ size_t appendNumChars = (size_t)-1;
const char *bytes;
/*
@@ -1300,7 +1301,7 @@ Tcl_AppendObjToObj(
AppendUtfToUtfRep(objPtr, bytes, length);
- if (numChars >= 0 && appendNumChars >= 0) {
+ if (numChars >= 0 && appendNumChars != (size_t)-1) {
stringPtr->numChars = numChars + appendNumChars;
}
}
@@ -1326,12 +1327,12 @@ static void
AppendUnicodeToUnicodeRep(
Tcl_Obj *objPtr, /* Points to the object to append to. */
const Tcl_UniChar *unicode, /* String to append. */
- int appendNumChars) /* Number of chars of "unicode" to append. */
+ size_t appendNumChars) /* Number of chars of "unicode" to append. */
{
String *stringPtr;
- int numChars;
+ size_t numChars;
- if (appendNumChars < 0) {
+ if (appendNumChars == (size_t)-1) {
appendNumChars = UnicodeLength(unicode);
}
if (appendNumChars == 0) {
@@ -1353,7 +1354,7 @@ AppendUnicodeToUnicodeRep(
stringCheckLimits(numChars);
if (numChars > stringPtr->maxChars) {
- int offset = -1;
+ size_t offset = (size_t)-1;
/*
* Protect against case where unicode points into the existing
@@ -1373,7 +1374,7 @@ AppendUnicodeToUnicodeRep(
* Relocate unicode if needed; see above.
*/
- if (offset >= 0) {
+ if (offset != (size_t)-1) {
unicode = stringPtr->unicode + offset;
}
}
@@ -1415,13 +1416,13 @@ static void
AppendUnicodeToUtfRep(
Tcl_Obj *objPtr, /* Points to the object to append to. */
const Tcl_UniChar *unicode, /* String to convert to UTF. */
- int numChars) /* Number of chars of "unicode" to convert. */
+ size_t numChars) /* Number of chars of "unicode" to convert. */
{
String *stringPtr = GET_STRING(objPtr);
numChars = ExtendStringRepWithUnicode(objPtr, unicode, numChars);
- if (stringPtr->numChars != -1) {
+ if (stringPtr->numChars != (size_t)-1) {
stringPtr->numChars += numChars;
}
}
@@ -1448,7 +1449,7 @@ static void
AppendUtfToUnicodeRep(
Tcl_Obj *objPtr, /* Points to the object to append to. */
const char *bytes, /* String to convert to Unicode. */
- int numBytes) /* Number of bytes of "bytes" to convert. */
+ size_t numBytes) /* Number of bytes of "bytes" to convert. */
{
String *stringPtr;
@@ -1484,10 +1485,10 @@ static void
AppendUtfToUtfRep(
Tcl_Obj *objPtr, /* Points to the object to append to. */
const char *bytes, /* String to append. */
- int numBytes) /* Number of bytes of "bytes" to append. */
+ size_t numBytes) /* Number of bytes of "bytes" to append. */
{
String *stringPtr;
- int newLength, oldLength;
+ size_t newLength, oldLength;
if (numBytes == 0) {
return;
@@ -1503,13 +1504,13 @@ AppendUtfToUtfRep(
}
oldLength = objPtr->length;
newLength = numBytes + oldLength;
- if (newLength < 0) {
+ if ((int)newLength < 0) {
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
}
stringPtr = GET_STRING(objPtr);
if (newLength > stringPtr->allocated) {
- int offset = -1;
+ size_t offset = (size_t)-1;
/*
* Protect against case where unicode points into the existing
@@ -1533,7 +1534,7 @@ AppendUtfToUtfRep(
* Relocate bytes if needed; see above.
*/
- if (offset >= 0) {
+ if (offset != (size_t)-1) {
bytes = objPtr->bytes + offset;
}
}
@@ -1542,7 +1543,7 @@ AppendUtfToUtfRep(
* Invalidate the unicode data.
*/
- stringPtr->numChars = -1;
+ stringPtr->numChars = (size_t)-1;
stringPtr->hasUnicode = 0;
if (bytes) {
@@ -2042,10 +2043,6 @@ Tcl_AppendFormatToObj(
Tcl_AppendToObj(segment, "0b", 2);
segmentLimit -= 2;
break;
- case 'd':
- Tcl_AppendToObj(segment, "0d", 2);
- segmentLimit -= 2;
- break;
}
}
@@ -3554,17 +3551,17 @@ static void
ExtendUnicodeRepWithString(
Tcl_Obj *objPtr,
const char *bytes,
- int numBytes,
- int numAppendChars)
+ size_t numBytes,
+ size_t numAppendChars)
{
String *stringPtr = GET_STRING(objPtr);
- int needed, numOrigChars = 0;
+ size_t needed, numOrigChars = 0;
Tcl_UniChar *dst;
if (stringPtr->hasUnicode) {
numOrigChars = stringPtr->numChars;
}
- if (numAppendChars == -1) {
+ if (numAppendChars == (size_t)-1) {
TclNumUtfChars(numAppendChars, bytes, numBytes);
}
needed = numOrigChars + numAppendChars;
@@ -3615,7 +3612,7 @@ DupStringInternalRep(
String *srcStringPtr = GET_STRING(srcPtr);
String *copyStringPtr = NULL;
- if (srcStringPtr->numChars == -1) {
+ if (srcStringPtr->numChars == (size_t)-1) {
/*
* The String struct in the source value holds zero useful data. Don't
* bother copying it. Don't even bother allocating space in which to
@@ -3699,7 +3696,7 @@ SetStringFromAny(
* already in place at objPtr->bytes.
*/
- stringPtr->numChars = -1;
+ stringPtr->numChars = (size_t)-1;
stringPtr->allocated = objPtr->length;
stringPtr->maxChars = 0;
stringPtr->hasUnicode = 0;
@@ -3751,21 +3748,21 @@ UpdateStringOfString(
}
}
-static int
+static size_t
ExtendStringRepWithUnicode(
Tcl_Obj *objPtr,
const Tcl_UniChar *unicode,
- int numChars)
+ size_t numChars)
{
/*
* Pre-condition: this is the "string" Tcl_ObjType.
*/
- int i, origLength, size = 0;
+ size_t i, origLength, size = 0;
char *dst;
String *stringPtr = GET_STRING(objPtr);
- if (numChars < 0) {
+ if (numChars == (size_t)-1) {
numChars = UnicodeLength(unicode);
}
@@ -3787,10 +3784,10 @@ ExtendStringRepWithUnicode(
goto copyBytes;
}
- for (i = 0; i < numChars && size >= 0; i++) {
+ for (i = 0; i < numChars; i++) {
size += TclUtfCount(unicode[i]);
}
- if (size < 0) {
+ if ((int)size < 0) {
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
}