| 1 | /* |
| 2 | * tclStringObj.c -- |
| 3 | * |
| 4 | * This file contains procedures that implement string operations on Tcl |
| 5 | * objects. Some string operations work with UTF strings and others |
| 6 | * require Unicode format. Functions that require knowledge of the width |
| 7 | * of each character, such as indexing, operate on Unicode data. |
| 8 | * |
| 9 | * A Unicode string is an internationalized string. Conceptually, a |
| 10 | * Unicode string is an array of 16-bit quantities organized as a sequence |
| 11 | * of properly formed UTF-8 characters. There is a one-to-one map between |
| 12 | * Unicode and UTF characters. Because Unicode characters have a fixed |
| 13 | * width, operations such as indexing operate on Unicode data. The String |
| 14 | * object is optimized for the case where each UTF char in a string is |
| 15 | * only one byte. In this case, we store the value of numChars, but we |
| 16 | * don't store the Unicode data (unless Tcl_GetUnicode is explicitly |
| 17 | * called). |
| 18 | * |
| 19 | * The String object type stores one or both formats. The default |
| 20 | * behavior is to store UTF. Once Unicode is calculated by a function, it |
| 21 | * is stored in the internal rep for future access (without an additional |
| 22 | * O(n) cost). |
| 23 | * |
| 24 | * To allow many appends to be done to an object without constantly |
| 25 | * reallocating the space for the string or Unicode representation, we |
| 26 | * allocate double the space for the string or Unicode and use the |
| 27 | * internal representation to keep track of how much space is used |
| 28 | * vs. allocated. |
| 29 | * |
| 30 | * Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 31 | * Copyright (c) 1999 by Scriptics Corporation. |
| 32 | * |
| 33 | * See the file "license.terms" for information on usage and redistribution |
| 34 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 35 | * |
| 36 | * RCS: @(#) $Id: tclStringObj.c,v 1.32 2003/02/19 16:43:28 das Exp $ */ |
| 37 | |
| 38 | #include "tclInt.h" |
| 39 | |
| 40 | /* |
| 41 | * Prototypes for procedures defined later in this file: |
| 42 | */ |
| 43 | |
| 44 | static void AppendUnicodeToUnicodeRep _ANSI_ARGS_(( |
| 45 | Tcl_Obj *objPtr, CONST Tcl_UniChar *unicode, |
| 46 | int appendNumChars)); |
| 47 | static void AppendUnicodeToUtfRep _ANSI_ARGS_(( |
| 48 | Tcl_Obj *objPtr, CONST Tcl_UniChar *unicode, |
| 49 | int numChars)); |
| 50 | static void AppendUtfToUnicodeRep _ANSI_ARGS_((Tcl_Obj *objPtr, |
| 51 | CONST char *bytes, int numBytes)); |
| 52 | static void AppendUtfToUtfRep _ANSI_ARGS_((Tcl_Obj *objPtr, |
| 53 | CONST char *bytes, int numBytes)); |
| 54 | |
| 55 | static void FillUnicodeRep _ANSI_ARGS_((Tcl_Obj *objPtr)); |
| 56 | |
| 57 | static void FreeStringInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr)); |
| 58 | static void DupStringInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr, |
| 59 | Tcl_Obj *copyPtr)); |
| 60 | static int SetStringFromAny _ANSI_ARGS_((Tcl_Interp *interp, |
| 61 | Tcl_Obj *objPtr)); |
| 62 | static void UpdateStringOfString _ANSI_ARGS_((Tcl_Obj *objPtr)); |
| 63 | |
| 64 | /* |
| 65 | * The structure below defines the string Tcl object type by means of |
| 66 | * procedures that can be invoked by generic object code. |
| 67 | */ |
| 68 | |
| 69 | Tcl_ObjType tclStringType = { |
| 70 | "string", /* name */ |
| 71 | FreeStringInternalRep, /* freeIntRepPro */ |
| 72 | DupStringInternalRep, /* dupIntRepProc */ |
| 73 | UpdateStringOfString, /* updateStringProc */ |
| 74 | SetStringFromAny /* setFromAnyProc */ |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * The following structure is the internal rep for a String object. |
| 79 | * It keeps track of how much memory has been used and how much has been |
| 80 | * allocated for the Unicode and UTF string to enable growing and |
| 81 | * shrinking of the UTF and Unicode reps of the String object with fewer |
| 82 | * mallocs. To optimize string length and indexing operations, this |
| 83 | * structure also stores the number of characters (same of UTF and Unicode!) |
| 84 | * once that value has been computed. |
| 85 | */ |
| 86 | |
| 87 | typedef struct String { |
| 88 | int numChars; /* The number of chars in the string. |
| 89 | * -1 means this value has not been |
| 90 | * calculated. >= 0 means that there is a |
| 91 | * valid Unicode rep, or that the number |
| 92 | * of UTF bytes == the number of chars. */ |
| 93 | size_t allocated; /* The amount of space actually allocated |
| 94 | * for the UTF string (minus 1 byte for |
| 95 | * the termination char). */ |
| 96 | size_t uallocated; /* The amount of space actually allocated |
| 97 | * for the Unicode string (minus 2 bytes for |
| 98 | * the termination char). */ |
| 99 | int hasUnicode; /* Boolean determining whether the string |
| 100 | * has a Unicode representation. */ |
| 101 | Tcl_UniChar unicode[2]; /* The array of Unicode chars. The actual |
| 102 | * size of this field depends on the |
| 103 | * 'uallocated' field above. */ |
| 104 | } String; |
| 105 | |
| 106 | #define STRING_UALLOC(numChars) \ |
| 107 | (numChars * sizeof(Tcl_UniChar)) |
| 108 | #define STRING_SIZE(ualloc) \ |
| 109 | ((unsigned) (sizeof(String) - sizeof(Tcl_UniChar) + ualloc)) |
| 110 | #define GET_STRING(objPtr) \ |
| 111 | ((String *) (objPtr)->internalRep.otherValuePtr) |
| 112 | #define SET_STRING(objPtr, stringPtr) \ |
| 113 | (objPtr)->internalRep.otherValuePtr = (VOID *) (stringPtr) |
| 114 | |
| 115 | /* |
| 116 | * TCL STRING GROWTH ALGORITHM |
| 117 | * |
| 118 | * When growing strings (during an append, for example), the following growth |
| 119 | * algorithm is used: |
| 120 | * |
| 121 | * Attempt to allocate 2 * (originalLength + appendLength) |
| 122 | * On failure: |
| 123 | * attempt to allocate originalLength + 2*appendLength + |
| 124 | * TCL_GROWTH_MIN_ALLOC |
| 125 | * |
| 126 | * This algorithm allows very good performance, as it rapidly increases the |
| 127 | * memory allocated for a given string, which minimizes the number of |
| 128 | * reallocations that must be performed. However, using only the doubling |
| 129 | * algorithm can lead to a significant waste of memory. In particular, it |
| 130 | * may fail even when there is sufficient memory available to complete the |
| 131 | * append request (but there is not 2 * totalLength memory available). So when |
| 132 | * the doubling fails (because there is not enough memory available), the |
| 133 | * algorithm requests a smaller amount of memory, which is still enough to |
| 134 | * cover the request, but which hopefully will be less than the total available |
| 135 | * memory. |
| 136 | * |
| 137 | * The addition of TCL_GROWTH_MIN_ALLOC allows for efficient handling |
| 138 | * of very small appends. Without this extra slush factor, a sequence |
| 139 | * of several small appends would cause several memory allocations. |
| 140 | * As long as TCL_GROWTH_MIN_ALLOC is a reasonable size, we can |
| 141 | * avoid that behavior. |
| 142 | * |
| 143 | * The growth algorithm can be tuned by adjusting the following parameters: |
| 144 | * |
| 145 | * TCL_GROWTH_MIN_ALLOC Additional space, in bytes, to allocate when |
| 146 | * the double allocation has failed. |
| 147 | * Default is 1024 (1 kilobyte). |
| 148 | */ |
| 149 | #ifndef TCL_GROWTH_MIN_ALLOC |
| 150 | #define TCL_GROWTH_MIN_ALLOC 1024 |
| 151 | #endif |
| 152 | |
| 153 | |
| 154 | /* |
| 155 | *---------------------------------------------------------------------- |
| 156 | * |
| 157 | * Tcl_NewStringObj -- |
| 158 | * |
| 159 | * This procedure is normally called when not debugging: i.e., when |
| 160 | * TCL_MEM_DEBUG is not defined. It creates a new string object and |
| 161 | * initializes it from the byte pointer and length arguments. |
| 162 | * |
| 163 | * When TCL_MEM_DEBUG is defined, this procedure just returns the |
| 164 | * result of calling the debugging version Tcl_DbNewStringObj. |
| 165 | * |
| 166 | * Results: |
| 167 | * A newly created string object is returned that has ref count zero. |
| 168 | * |
| 169 | * Side effects: |
| 170 | * The new object's internal string representation will be set to a |
| 171 | * copy of the length bytes starting at "bytes". If "length" is |
| 172 | * negative, use bytes up to the first NULL byte; i.e., assume "bytes" |
| 173 | * points to a C-style NULL-terminated string. The object's type is set |
| 174 | * to NULL. An extra NULL is added to the end of the new object's byte |
| 175 | * array. |
| 176 | * |
| 177 | *---------------------------------------------------------------------- |
| 178 | */ |
| 179 | |
| 180 | #ifdef TCL_MEM_DEBUG |
| 181 | #undef Tcl_NewStringObj |
| 182 | |
| 183 | Tcl_Obj * |
| 184 | Tcl_NewStringObj(bytes, length) |
| 185 | CONST char *bytes; /* Points to the first of the length bytes |
| 186 | * used to initialize the new object. */ |
| 187 | int length; /* The number of bytes to copy from "bytes" |
| 188 | * when initializing the new object. If |
| 189 | * negative, use bytes up to the first |
| 190 | * NULL byte. */ |
| 191 | { |
| 192 | return Tcl_DbNewStringObj(bytes, length, "unknown", 0); |
| 193 | } |
| 194 | |
| 195 | #else /* if not TCL_MEM_DEBUG */ |
| 196 | |
| 197 | Tcl_Obj * |
| 198 | Tcl_NewStringObj(bytes, length) |
| 199 | CONST char *bytes; /* Points to the first of the length bytes |
| 200 | * used to initialize the new object. */ |
| 201 | int length; /* The number of bytes to copy from "bytes" |
| 202 | * when initializing the new object. If |
| 203 | * negative, use bytes up to the first |
| 204 | * NULL byte. */ |
| 205 | { |
| 206 | register Tcl_Obj *objPtr; |
| 207 | |
| 208 | if (length < 0) { |
| 209 | length = (bytes? strlen(bytes) : 0); |
| 210 | } |
| 211 | TclNewObj(objPtr); |
| 212 | TclInitStringRep(objPtr, bytes, length); |
| 213 | return objPtr; |
| 214 | } |
| 215 | #endif /* TCL_MEM_DEBUG */ |
| 216 | |
| 217 | /* |
| 218 | *---------------------------------------------------------------------- |
| 219 | * |
| 220 | * Tcl_DbNewStringObj -- |
| 221 | * |
| 222 | * This procedure is normally called when debugging: i.e., when |
| 223 | * TCL_MEM_DEBUG is defined. It creates new string objects. It is the |
| 224 | * same as the Tcl_NewStringObj procedure above except that it calls |
| 225 | * Tcl_DbCkalloc directly with the file name and line number from its |
| 226 | * caller. This simplifies debugging since then the [memory active] |
| 227 | * command will report the correct file name and line number when |
| 228 | * reporting objects that haven't been freed. |
| 229 | * |
| 230 | * When TCL_MEM_DEBUG is not defined, this procedure just returns the |
| 231 | * result of calling Tcl_NewStringObj. |
| 232 | * |
| 233 | * Results: |
| 234 | * A newly created string object is returned that has ref count zero. |
| 235 | * |
| 236 | * Side effects: |
| 237 | * The new object's internal string representation will be set to a |
| 238 | * copy of the length bytes starting at "bytes". If "length" is |
| 239 | * negative, use bytes up to the first NULL byte; i.e., assume "bytes" |
| 240 | * points to a C-style NULL-terminated string. The object's type is set |
| 241 | * to NULL. An extra NULL is added to the end of the new object's byte |
| 242 | * array. |
| 243 | * |
| 244 | *---------------------------------------------------------------------- |
| 245 | */ |
| 246 | |
| 247 | #ifdef TCL_MEM_DEBUG |
| 248 | |
| 249 | Tcl_Obj * |
| 250 | Tcl_DbNewStringObj(bytes, length, file, line) |
| 251 | CONST char *bytes; /* Points to the first of the length bytes |
| 252 | * used to initialize the new object. */ |
| 253 | int length; /* The number of bytes to copy from "bytes" |
| 254 | * when initializing the new object. If |
| 255 | * negative, use bytes up to the first |
| 256 | * NULL byte. */ |
| 257 | CONST char *file; /* The name of the source file calling this |
| 258 | * procedure; used for debugging. */ |
| 259 | int line; /* Line number in the source file; used |
| 260 | * for debugging. */ |
| 261 | { |
| 262 | register Tcl_Obj *objPtr; |
| 263 | |
| 264 | if (length < 0) { |
| 265 | length = (bytes? strlen(bytes) : 0); |
| 266 | } |
| 267 | TclDbNewObj(objPtr, file, line); |
| 268 | TclInitStringRep(objPtr, bytes, length); |
| 269 | return objPtr; |
| 270 | } |
| 271 | |
| 272 | #else /* if not TCL_MEM_DEBUG */ |
| 273 | |
| 274 | Tcl_Obj * |
| 275 | Tcl_DbNewStringObj(bytes, length, file, line) |
| 276 | CONST char *bytes; /* Points to the first of the length bytes |
| 277 | * used to initialize the new object. */ |
| 278 | register int length; /* The number of bytes to copy from "bytes" |
| 279 | * when initializing the new object. If |
| 280 | * negative, use bytes up to the first |
| 281 | * NULL byte. */ |
| 282 | CONST char *file; /* The name of the source file calling this |
| 283 | * procedure; used for debugging. */ |
| 284 | int line; /* Line number in the source file; used |
| 285 | * for debugging. */ |
| 286 | { |
| 287 | return Tcl_NewStringObj(bytes, length); |
| 288 | } |
| 289 | #endif /* TCL_MEM_DEBUG */ |
| 290 | |
| 291 | /* |
| 292 | *--------------------------------------------------------------------------- |
| 293 | * |
| 294 | * Tcl_NewUnicodeObj -- |
| 295 | * |
| 296 | * This procedure is creates a new String object and initializes |
| 297 | * it from the given Unicode String. If the Utf String is the same size |
| 298 | * as the Unicode string, don't duplicate the data. |
| 299 | * |
| 300 | * Results: |
| 301 | * The newly created object is returned. This object will have no |
| 302 | * initial string representation. The returned object has a ref count |
| 303 | * of 0. |
| 304 | * |
| 305 | * Side effects: |
| 306 | * Memory allocated for new object and copy of Unicode argument. |
| 307 | * |
| 308 | *--------------------------------------------------------------------------- |
| 309 | */ |
| 310 | |
| 311 | Tcl_Obj * |
| 312 | Tcl_NewUnicodeObj(unicode, numChars) |
| 313 | CONST Tcl_UniChar *unicode; /* The unicode string used to initialize |
| 314 | * the new object. */ |
| 315 | int numChars; /* Number of characters in the unicode |
| 316 | * string. */ |
| 317 | { |
| 318 | Tcl_Obj *objPtr; |
| 319 | String *stringPtr; |
| 320 | size_t uallocated; |
| 321 | |
| 322 | if (numChars < 0) { |
| 323 | numChars = 0; |
| 324 | if (unicode) { |
| 325 | while (unicode[numChars] != 0) { numChars++; } |
| 326 | } |
| 327 | } |
| 328 | uallocated = STRING_UALLOC(numChars); |
| 329 | |
| 330 | /* |
| 331 | * Create a new obj with an invalid string rep. |
| 332 | */ |
| 333 | |
| 334 | TclNewObj(objPtr); |
| 335 | Tcl_InvalidateStringRep(objPtr); |
| 336 | objPtr->typePtr = &tclStringType; |
| 337 | |
| 338 | stringPtr = (String *) ckalloc(STRING_SIZE(uallocated)); |
| 339 | stringPtr->numChars = numChars; |
| 340 | stringPtr->uallocated = uallocated; |
| 341 | stringPtr->hasUnicode = (numChars > 0); |
| 342 | stringPtr->allocated = 0; |
| 343 | memcpy((VOID *) stringPtr->unicode, (VOID *) unicode, uallocated); |
| 344 | stringPtr->unicode[numChars] = 0; |
| 345 | SET_STRING(objPtr, stringPtr); |
| 346 | return objPtr; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | *---------------------------------------------------------------------- |
| 351 | * |
| 352 | * Tcl_GetCharLength -- |
| 353 | * |
| 354 | * Get the length of the Unicode string from the Tcl object. |
| 355 | * |
| 356 | * Results: |
| 357 | * Pointer to unicode string representing the unicode object. |
| 358 | * |
| 359 | * Side effects: |
| 360 | * Frees old internal rep. Allocates memory for new "String" |
| 361 | * internal rep. |
| 362 | * |
| 363 | *---------------------------------------------------------------------- |
| 364 | */ |
| 365 | |
| 366 | int |
| 367 | Tcl_GetCharLength(objPtr) |
| 368 | Tcl_Obj *objPtr; /* The String object to get the num chars of. */ |
| 369 | { |
| 370 | String *stringPtr; |
| 371 | |
| 372 | SetStringFromAny(NULL, objPtr); |
| 373 | stringPtr = GET_STRING(objPtr); |
| 374 | |
| 375 | /* |
| 376 | * If numChars is unknown, then calculate the number of characaters |
| 377 | * while populating the Unicode string. |
| 378 | */ |
| 379 | |
| 380 | if (stringPtr->numChars == -1) { |
| 381 | register int i = objPtr->length; |
| 382 | register unsigned char *str = (unsigned char *) objPtr->bytes; |
| 383 | |
| 384 | /* |
| 385 | * This is a speed sensitive function, so run specially over the |
| 386 | * string to count continuous ascii characters before resorting |
| 387 | * to the Tcl_NumUtfChars call. This is a long form of: |
| 388 | stringPtr->numChars = Tcl_NumUtfChars(objPtr->bytes, objPtr->length); |
| 389 | */ |
| 390 | |
| 391 | while (i && (*str < 0xC0)) { i--; str++; } |
| 392 | stringPtr->numChars = objPtr->length - i; |
| 393 | if (i) { |
| 394 | stringPtr->numChars += Tcl_NumUtfChars(objPtr->bytes |
| 395 | + (objPtr->length - i), i); |
| 396 | } |
| 397 | |
| 398 | if (stringPtr->numChars == objPtr->length) { |
| 399 | |
| 400 | /* |
| 401 | * Since we've just calculated the number of chars, and all |
| 402 | * UTF chars are 1-byte long, we don't need to store the |
| 403 | * unicode string. |
| 404 | */ |
| 405 | |
| 406 | stringPtr->hasUnicode = 0; |
| 407 | |
| 408 | } else { |
| 409 | |
| 410 | /* |
| 411 | * Since we've just calucalated the number of chars, and not |
| 412 | * all UTF chars are 1-byte long, go ahead and populate the |
| 413 | * unicode string. |
| 414 | */ |
| 415 | |
| 416 | FillUnicodeRep(objPtr); |
| 417 | |
| 418 | /* |
| 419 | * We need to fetch the pointer again because we have just |
| 420 | * reallocated the structure to make room for the Unicode data. |
| 421 | */ |
| 422 | |
| 423 | stringPtr = GET_STRING(objPtr); |
| 424 | } |
| 425 | } |
| 426 | return stringPtr->numChars; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | *---------------------------------------------------------------------- |
| 431 | * |
| 432 | * Tcl_GetUniChar -- |
| 433 | * |
| 434 | * Get the index'th Unicode character from the String object. The |
| 435 | * index is assumed to be in the appropriate range. |
| 436 | * |
| 437 | * Results: |
| 438 | * Returns the index'th Unicode character in the Object. |
| 439 | * |
| 440 | * Side effects: |
| 441 | * Fills unichar with the index'th Unicode character. |
| 442 | * |
| 443 | *---------------------------------------------------------------------- |
| 444 | */ |
| 445 | |
| 446 | Tcl_UniChar |
| 447 | Tcl_GetUniChar(objPtr, index) |
| 448 | Tcl_Obj *objPtr; /* The object to get the Unicode charater from. */ |
| 449 | int index; /* Get the index'th Unicode character. */ |
| 450 | { |
| 451 | Tcl_UniChar unichar; |
| 452 | String *stringPtr; |
| 453 | |
| 454 | SetStringFromAny(NULL, objPtr); |
| 455 | stringPtr = GET_STRING(objPtr); |
| 456 | |
| 457 | if (stringPtr->numChars == -1) { |
| 458 | |
| 459 | /* |
| 460 | * We haven't yet calculated the length, so we don't have the |
| 461 | * Unicode str. We need to know the number of chars before we |
| 462 | * can do indexing. |
| 463 | */ |
| 464 | |
| 465 | Tcl_GetCharLength(objPtr); |
| 466 | |
| 467 | /* |
| 468 | * We need to fetch the pointer again because we may have just |
| 469 | * reallocated the structure. |
| 470 | */ |
| 471 | |
| 472 | stringPtr = GET_STRING(objPtr); |
| 473 | } |
| 474 | if (stringPtr->hasUnicode == 0) { |
| 475 | |
| 476 | /* |
| 477 | * All of the characters in the Utf string are 1 byte chars, |
| 478 | * so we don't store the unicode char. We get the Utf string |
| 479 | * and convert the index'th byte to a Unicode character. |
| 480 | */ |
| 481 | |
| 482 | unichar = (Tcl_UniChar) objPtr->bytes[index]; |
| 483 | } else { |
| 484 | unichar = stringPtr->unicode[index]; |
| 485 | } |
| 486 | return unichar; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | *---------------------------------------------------------------------- |
| 491 | * |
| 492 | * Tcl_GetUnicode -- |
| 493 | * |
| 494 | * Get the Unicode form of the String object. If |
| 495 | * the object is not already a String object, it will be converted |
| 496 | * to one. If the String object does not have a Unicode rep, then |
| 497 | * one is create from the UTF string format. |
| 498 | * |
| 499 | * Results: |
| 500 | * Returns a pointer to the object's internal Unicode string. |
| 501 | * |
| 502 | * Side effects: |
| 503 | * Converts the object to have the String internal rep. |
| 504 | * |
| 505 | *---------------------------------------------------------------------- |
| 506 | */ |
| 507 | |
| 508 | Tcl_UniChar * |
| 509 | Tcl_GetUnicode(objPtr) |
| 510 | Tcl_Obj *objPtr; /* The object to find the unicode string for. */ |
| 511 | { |
| 512 | String *stringPtr; |
| 513 | |
| 514 | SetStringFromAny(NULL, objPtr); |
| 515 | stringPtr = GET_STRING(objPtr); |
| 516 | |
| 517 | if ((stringPtr->numChars == -1) || (stringPtr->hasUnicode == 0)) { |
| 518 | |
| 519 | /* |
| 520 | * We haven't yet calculated the length, or all of the characters |
| 521 | * in the Utf string are 1 byte chars (so we didn't store the |
| 522 | * unicode str). Since this function must return a unicode string, |
| 523 | * and one has not yet been stored, force the Unicode to be |
| 524 | * calculated and stored now. |
| 525 | */ |
| 526 | |
| 527 | FillUnicodeRep(objPtr); |
| 528 | |
| 529 | /* |
| 530 | * We need to fetch the pointer again because we have just |
| 531 | * reallocated the structure to make room for the Unicode data. |
| 532 | */ |
| 533 | |
| 534 | stringPtr = GET_STRING(objPtr); |
| 535 | } |
| 536 | return stringPtr->unicode; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | *---------------------------------------------------------------------- |
| 541 | * |
| 542 | * Tcl_GetUnicodeFromObj -- |
| 543 | * |
| 544 | * Get the Unicode form of the String object with length. If |
| 545 | * the object is not already a String object, it will be converted |
| 546 | * to one. If the String object does not have a Unicode rep, then |
| 547 | * one is create from the UTF string format. |
| 548 | * |
| 549 | * Results: |
| 550 | * Returns a pointer to the object's internal Unicode string. |
| 551 | * |
| 552 | * Side effects: |
| 553 | * Converts the object to have the String internal rep. |
| 554 | * |
| 555 | *---------------------------------------------------------------------- |
| 556 | */ |
| 557 | |
| 558 | Tcl_UniChar * |
| 559 | Tcl_GetUnicodeFromObj(objPtr, lengthPtr) |
| 560 | Tcl_Obj *objPtr; /* The object to find the unicode string for. */ |
| 561 | int *lengthPtr; /* If non-NULL, the location where the |
| 562 | * string rep's unichar length should be |
| 563 | * stored. If NULL, no length is stored. */ |
| 564 | { |
| 565 | String *stringPtr; |
| 566 | |
| 567 | SetStringFromAny(NULL, objPtr); |
| 568 | stringPtr = GET_STRING(objPtr); |
| 569 | |
| 570 | if ((stringPtr->numChars == -1) || (stringPtr->hasUnicode == 0)) { |
| 571 | |
| 572 | /* |
| 573 | * We haven't yet calculated the length, or all of the characters |
| 574 | * in the Utf string are 1 byte chars (so we didn't store the |
| 575 | * unicode str). Since this function must return a unicode string, |
| 576 | * and one has not yet been stored, force the Unicode to be |
| 577 | * calculated and stored now. |
| 578 | */ |
| 579 | |
| 580 | FillUnicodeRep(objPtr); |
| 581 | |
| 582 | /* |
| 583 | * We need to fetch the pointer again because we have just |
| 584 | * reallocated the structure to make room for the Unicode data. |
| 585 | */ |
| 586 | |
| 587 | stringPtr = GET_STRING(objPtr); |
| 588 | } |
| 589 | |
| 590 | if (lengthPtr != NULL) { |
| 591 | *lengthPtr = stringPtr->numChars; |
| 592 | } |
| 593 | return stringPtr->unicode; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | *---------------------------------------------------------------------- |
| 598 | * |
| 599 | * Tcl_GetRange -- |
| 600 | * |
| 601 | * Create a Tcl Object that contains the chars between first and last |
| 602 | * of the object indicated by "objPtr". If the object is not already |
| 603 | * a String object, convert it to one. The first and last indices |
| 604 | * are assumed to be in the appropriate range. |
| 605 | * |
| 606 | * Results: |
| 607 | * Returns a new Tcl Object of the String type. |
| 608 | * |
| 609 | * Side effects: |
| 610 | * Changes the internal rep of "objPtr" to the String type. |
| 611 | * |
| 612 | *---------------------------------------------------------------------- |
| 613 | */ |
| 614 | |
| 615 | Tcl_Obj * |
| 616 | Tcl_GetRange(objPtr, first, last) |
| 617 | Tcl_Obj *objPtr; /* The Tcl object to find the range of. */ |
| 618 | int first; /* First index of the range. */ |
| 619 | int last; /* Last index of the range. */ |
| 620 | { |
| 621 | Tcl_Obj *newObjPtr; /* The Tcl object to find the range of. */ |
| 622 | String *stringPtr; |
| 623 | |
| 624 | SetStringFromAny(NULL, objPtr); |
| 625 | stringPtr = GET_STRING(objPtr); |
| 626 | |
| 627 | if (stringPtr->numChars == -1) { |
| 628 | |
| 629 | /* |
| 630 | * We haven't yet calculated the length, so we don't have the |
| 631 | * Unicode str. We need to know the number of chars before we |
| 632 | * can do indexing. |
| 633 | */ |
| 634 | |
| 635 | Tcl_GetCharLength(objPtr); |
| 636 | |
| 637 | /* |
| 638 | * We need to fetch the pointer again because we may have just |
| 639 | * reallocated the structure. |
| 640 | */ |
| 641 | |
| 642 | stringPtr = GET_STRING(objPtr); |
| 643 | } |
| 644 | |
| 645 | if (stringPtr->numChars == objPtr->length) { |
| 646 | char *str = Tcl_GetString(objPtr); |
| 647 | |
| 648 | /* |
| 649 | * All of the characters in the Utf string are 1 byte chars, |
| 650 | * so we don't store the unicode char. Create a new string |
| 651 | * object containing the specified range of chars. |
| 652 | */ |
| 653 | |
| 654 | newObjPtr = Tcl_NewStringObj(&str[first], last-first+1); |
| 655 | |
| 656 | /* |
| 657 | * Since we know the new string only has 1-byte chars, we |
| 658 | * can set it's numChars field. |
| 659 | */ |
| 660 | |
| 661 | SetStringFromAny(NULL, newObjPtr); |
| 662 | stringPtr = GET_STRING(newObjPtr); |
| 663 | stringPtr->numChars = last-first+1; |
| 664 | } else { |
| 665 | newObjPtr = Tcl_NewUnicodeObj(stringPtr->unicode + first, |
| 666 | last-first+1); |
| 667 | } |
| 668 | return newObjPtr; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | *---------------------------------------------------------------------- |
| 673 | * |
| 674 | * Tcl_SetStringObj -- |
| 675 | * |
| 676 | * Modify an object to hold a string that is a copy of the bytes |
| 677 | * indicated by the byte pointer and length arguments. |
| 678 | * |
| 679 | * Results: |
| 680 | * None. |
| 681 | * |
| 682 | * Side effects: |
| 683 | * The object's string representation will be set to a copy of |
| 684 | * the "length" bytes starting at "bytes". If "length" is negative, use |
| 685 | * bytes up to the first NULL byte; i.e., assume "bytes" points to a |
| 686 | * C-style NULL-terminated string. The object's old string and internal |
| 687 | * representations are freed and the object's type is set NULL. |
| 688 | * |
| 689 | *---------------------------------------------------------------------- |
| 690 | */ |
| 691 | |
| 692 | void |
| 693 | Tcl_SetStringObj(objPtr, bytes, length) |
| 694 | register Tcl_Obj *objPtr; /* Object whose internal rep to init. */ |
| 695 | CONST char *bytes; /* Points to the first of the length bytes |
| 696 | * used to initialize the object. */ |
| 697 | register int length; /* The number of bytes to copy from "bytes" |
| 698 | * when initializing the object. If |
| 699 | * negative, use bytes up to the first |
| 700 | * NULL byte.*/ |
| 701 | { |
| 702 | register Tcl_ObjType *oldTypePtr = objPtr->typePtr; |
| 703 | |
| 704 | /* |
| 705 | * Free any old string rep, then set the string rep to a copy of |
| 706 | * the length bytes starting at "bytes". |
| 707 | */ |
| 708 | |
| 709 | if (Tcl_IsShared(objPtr)) { |
| 710 | panic("Tcl_SetStringObj called with shared object"); |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Set the type to NULL and free any internal rep for the old type. |
| 715 | */ |
| 716 | |
| 717 | if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) { |
| 718 | oldTypePtr->freeIntRepProc(objPtr); |
| 719 | } |
| 720 | objPtr->typePtr = NULL; |
| 721 | |
| 722 | Tcl_InvalidateStringRep(objPtr); |
| 723 | if (length < 0) { |
| 724 | length = (bytes? strlen(bytes) : 0); |
| 725 | } |
| 726 | TclInitStringRep(objPtr, bytes, length); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | *---------------------------------------------------------------------- |
| 731 | * |
| 732 | * Tcl_SetObjLength -- |
| 733 | * |
| 734 | * This procedure changes the length of the string representation |
| 735 | * of an object. |
| 736 | * |
| 737 | * Results: |
| 738 | * None. |
| 739 | * |
| 740 | * Side effects: |
| 741 | * If the size of objPtr's string representation is greater than |
| 742 | * length, then it is reduced to length and a new terminating null |
| 743 | * byte is stored in the strength. If the length of the string |
| 744 | * representation is greater than length, the storage space is |
| 745 | * reallocated to the given length; a null byte is stored at the |
| 746 | * end, but other bytes past the end of the original string |
| 747 | * representation are undefined. The object's internal |
| 748 | * representation is changed to "expendable string". |
| 749 | * |
| 750 | *---------------------------------------------------------------------- |
| 751 | */ |
| 752 | |
| 753 | void |
| 754 | Tcl_SetObjLength(objPtr, length) |
| 755 | register Tcl_Obj *objPtr; /* Pointer to object. This object must |
| 756 | * not currently be shared. */ |
| 757 | register int length; /* Number of bytes desired for string |
| 758 | * representation of object, not including |
| 759 | * terminating null byte. */ |
| 760 | { |
| 761 | String *stringPtr; |
| 762 | |
| 763 | if (Tcl_IsShared(objPtr)) { |
| 764 | panic("Tcl_SetObjLength called with shared object"); |
| 765 | } |
| 766 | SetStringFromAny(NULL, objPtr); |
| 767 | |
| 768 | stringPtr = GET_STRING(objPtr); |
| 769 | |
| 770 | /* Check that we're not extending a pure unicode string */ |
| 771 | |
| 772 | if (length > (int) stringPtr->allocated && |
| 773 | (objPtr->bytes != NULL || stringPtr->hasUnicode == 0)) { |
| 774 | char *new; |
| 775 | |
| 776 | /* |
| 777 | * Not enough space in current string. Reallocate the string |
| 778 | * space and free the old string. |
| 779 | */ |
| 780 | if (objPtr->bytes != tclEmptyStringRep && objPtr->bytes != NULL) { |
| 781 | new = (char *) ckrealloc((char *)objPtr->bytes, |
| 782 | (unsigned)(length+1)); |
| 783 | } else { |
| 784 | new = (char *) ckalloc((unsigned) (length+1)); |
| 785 | if (objPtr->bytes != NULL && objPtr->length != 0) { |
| 786 | memcpy((VOID *) new, (VOID *) objPtr->bytes, |
| 787 | (size_t) objPtr->length); |
| 788 | Tcl_InvalidateStringRep(objPtr); |
| 789 | } |
| 790 | } |
| 791 | objPtr->bytes = new; |
| 792 | stringPtr->allocated = length; |
| 793 | /* Invalidate the unicode data. */ |
| 794 | stringPtr->hasUnicode = 0; |
| 795 | } |
| 796 | |
| 797 | if (objPtr->bytes != NULL) { |
| 798 | objPtr->length = length; |
| 799 | if (objPtr->bytes != tclEmptyStringRep) { |
| 800 | /* Ensure the string is NULL-terminated */ |
| 801 | objPtr->bytes[length] = 0; |
| 802 | } |
| 803 | /* Invalidate the unicode data. */ |
| 804 | stringPtr->numChars = -1; |
| 805 | stringPtr->hasUnicode = 0; |
| 806 | } else { |
| 807 | /* Changing length of pure unicode string */ |
| 808 | size_t uallocated = STRING_UALLOC(length); |
| 809 | if (uallocated > stringPtr->uallocated) { |
| 810 | stringPtr = (String *) ckrealloc((char*) stringPtr, |
| 811 | STRING_SIZE(uallocated)); |
| 812 | SET_STRING(objPtr, stringPtr); |
| 813 | stringPtr->uallocated = uallocated; |
| 814 | } |
| 815 | stringPtr->numChars = length; |
| 816 | stringPtr->hasUnicode = (length > 0); |
| 817 | /* Ensure the string is NULL-terminated */ |
| 818 | stringPtr->unicode[length] = 0; |
| 819 | stringPtr->allocated = 0; |
| 820 | objPtr->length = 0; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | *---------------------------------------------------------------------- |
| 826 | * |
| 827 | * Tcl_AttemptSetObjLength -- |
| 828 | * |
| 829 | * This procedure changes the length of the string representation |
| 830 | * of an object. It uses the attempt* (non-panic'ing) memory allocators. |
| 831 | * |
| 832 | * Results: |
| 833 | * 1 if the requested memory was allocated, 0 otherwise. |
| 834 | * |
| 835 | * Side effects: |
| 836 | * If the size of objPtr's string representation is greater than |
| 837 | * length, then it is reduced to length and a new terminating null |
| 838 | * byte is stored in the strength. If the length of the string |
| 839 | * representation is greater than length, the storage space is |
| 840 | * reallocated to the given length; a null byte is stored at the |
| 841 | * end, but other bytes past the end of the original string |
| 842 | * representation are undefined. The object's internal |
| 843 | * representation is changed to "expendable string". |
| 844 | * |
| 845 | *---------------------------------------------------------------------- |
| 846 | */ |
| 847 | |
| 848 | int |
| 849 | Tcl_AttemptSetObjLength(objPtr, length) |
| 850 | register Tcl_Obj *objPtr; /* Pointer to object. This object must |
| 851 | * not currently be shared. */ |
| 852 | register int length; /* Number of bytes desired for string |
| 853 | * representation of object, not including |
| 854 | * terminating null byte. */ |
| 855 | { |
| 856 | String *stringPtr; |
| 857 | |
| 858 | if (Tcl_IsShared(objPtr)) { |
| 859 | panic("Tcl_AttemptSetObjLength called with shared object"); |
| 860 | } |
| 861 | SetStringFromAny(NULL, objPtr); |
| 862 | |
| 863 | stringPtr = GET_STRING(objPtr); |
| 864 | |
| 865 | /* Check that we're not extending a pure unicode string */ |
| 866 | |
| 867 | if (length > (int) stringPtr->allocated && |
| 868 | (objPtr->bytes != NULL || stringPtr->hasUnicode == 0)) { |
| 869 | char *new; |
| 870 | |
| 871 | /* |
| 872 | * Not enough space in current string. Reallocate the string |
| 873 | * space and free the old string. |
| 874 | */ |
| 875 | if (objPtr->bytes != tclEmptyStringRep && objPtr->bytes != NULL) { |
| 876 | new = (char *) attemptckrealloc((char *)objPtr->bytes, |
| 877 | (unsigned)(length+1)); |
| 878 | if (new == NULL) { |
| 879 | return 0; |
| 880 | } |
| 881 | } else { |
| 882 | new = (char *) attemptckalloc((unsigned) (length+1)); |
| 883 | if (new == NULL) { |
| 884 | return 0; |
| 885 | } |
| 886 | if (objPtr->bytes != NULL && objPtr->length != 0) { |
| 887 | memcpy((VOID *) new, (VOID *) objPtr->bytes, |
| 888 | (size_t) objPtr->length); |
| 889 | Tcl_InvalidateStringRep(objPtr); |
| 890 | } |
| 891 | } |
| 892 | objPtr->bytes = new; |
| 893 | stringPtr->allocated = length; |
| 894 | /* Invalidate the unicode data. */ |
| 895 | stringPtr->hasUnicode = 0; |
| 896 | } |
| 897 | |
| 898 | if (objPtr->bytes != NULL) { |
| 899 | objPtr->length = length; |
| 900 | if (objPtr->bytes != tclEmptyStringRep) { |
| 901 | /* Ensure the string is NULL-terminated */ |
| 902 | objPtr->bytes[length] = 0; |
| 903 | } |
| 904 | /* Invalidate the unicode data. */ |
| 905 | stringPtr->numChars = -1; |
| 906 | stringPtr->hasUnicode = 0; |
| 907 | } else { |
| 908 | /* Changing length of pure unicode string */ |
| 909 | size_t uallocated = STRING_UALLOC(length); |
| 910 | if (uallocated > stringPtr->uallocated) { |
| 911 | stringPtr = (String *) attemptckrealloc((char*) stringPtr, |
| 912 | STRING_SIZE(uallocated)); |
| 913 | if (stringPtr == NULL) { |
| 914 | return 0; |
| 915 | } |
| 916 | SET_STRING(objPtr, stringPtr); |
| 917 | stringPtr->uallocated = uallocated; |
| 918 | } |
| 919 | stringPtr->numChars = length; |
| 920 | stringPtr->hasUnicode = (length > 0); |
| 921 | /* Ensure the string is NULL-terminated */ |
| 922 | stringPtr->unicode[length] = 0; |
| 923 | stringPtr->allocated = 0; |
| 924 | objPtr->length = 0; |
| 925 | } |
| 926 | return 1; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | *--------------------------------------------------------------------------- |
| 931 | * |
| 932 | * TclSetUnicodeObj -- |
| 933 | * |
| 934 | * Modify an object to hold the Unicode string indicated by "unicode". |
| 935 | * |
| 936 | * Results: |
| 937 | * None. |
| 938 | * |
| 939 | * Side effects: |
| 940 | * Memory allocated for new "String" internal rep. |
| 941 | * |
| 942 | *--------------------------------------------------------------------------- |
| 943 | */ |
| 944 | |
| 945 | void |
| 946 | Tcl_SetUnicodeObj(objPtr, unicode, numChars) |
| 947 | Tcl_Obj *objPtr; /* The object to set the string of. */ |
| 948 | CONST Tcl_UniChar *unicode; /* The unicode string used to initialize |
| 949 | * the object. */ |
| 950 | int numChars; /* Number of characters in the unicode |
| 951 | * string. */ |
| 952 | { |
| 953 | Tcl_ObjType *typePtr; |
| 954 | String *stringPtr; |
| 955 | size_t uallocated; |
| 956 | |
| 957 | if (numChars < 0) { |
| 958 | numChars = 0; |
| 959 | if (unicode) { |
| 960 | while (unicode[numChars] != 0) { numChars++; } |
| 961 | } |
| 962 | } |
| 963 | uallocated = STRING_UALLOC(numChars); |
| 964 | |
| 965 | /* |
| 966 | * Free the internal rep if one exists, and invalidate the string rep. |
| 967 | */ |
| 968 | |
| 969 | typePtr = objPtr->typePtr; |
| 970 | if ((typePtr != NULL) && (typePtr->freeIntRepProc) != NULL) { |
| 971 | (*typePtr->freeIntRepProc)(objPtr); |
| 972 | } |
| 973 | objPtr->typePtr = &tclStringType; |
| 974 | |
| 975 | /* |
| 976 | * Allocate enough space for the String structure + Unicode string. |
| 977 | */ |
| 978 | |
| 979 | stringPtr = (String *) ckalloc(STRING_SIZE(uallocated)); |
| 980 | stringPtr->numChars = numChars; |
| 981 | stringPtr->uallocated = uallocated; |
| 982 | stringPtr->hasUnicode = (numChars > 0); |
| 983 | stringPtr->allocated = 0; |
| 984 | memcpy((VOID *) stringPtr->unicode, (VOID *) unicode, uallocated); |
| 985 | stringPtr->unicode[numChars] = 0; |
| 986 | SET_STRING(objPtr, stringPtr); |
| 987 | Tcl_InvalidateStringRep(objPtr); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | *---------------------------------------------------------------------- |
| 993 | * |
| 994 | * Tcl_AppendToObj -- |
| 995 | * |
| 996 | * This procedure appends a sequence of bytes to an object. |
| 997 | * |
| 998 | * Results: |
| 999 | * None. |
| 1000 | * |
| 1001 | * Side effects: |
| 1002 | * The bytes at *bytes are appended to the string representation |
| 1003 | * of objPtr. |
| 1004 | * |
| 1005 | *---------------------------------------------------------------------- |
| 1006 | */ |
| 1007 | |
| 1008 | void |
| 1009 | Tcl_AppendToObj(objPtr, bytes, length) |
| 1010 | register Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1011 | CONST char *bytes; /* Points to the bytes to append to the |
| 1012 | * object. */ |
| 1013 | register int length; /* The number of bytes to append from |
| 1014 | * "bytes". If < 0, then append all bytes |
| 1015 | * up to NULL byte. */ |
| 1016 | { |
| 1017 | String *stringPtr; |
| 1018 | |
| 1019 | if (Tcl_IsShared(objPtr)) { |
| 1020 | panic("Tcl_AppendToObj called with shared object"); |
| 1021 | } |
| 1022 | |
| 1023 | SetStringFromAny(NULL, objPtr); |
| 1024 | |
| 1025 | if (length < 0) { |
| 1026 | length = (bytes ? strlen(bytes) : 0); |
| 1027 | } |
| 1028 | if (length == 0) { |
| 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * If objPtr has a valid Unicode rep, then append the Unicode |
| 1034 | * conversion of "bytes" to the objPtr's Unicode rep, otherwise |
| 1035 | * append "bytes" to objPtr's string rep. |
| 1036 | */ |
| 1037 | |
| 1038 | stringPtr = GET_STRING(objPtr); |
| 1039 | if (stringPtr->hasUnicode != 0) { |
| 1040 | AppendUtfToUnicodeRep(objPtr, bytes, length); |
| 1041 | |
| 1042 | stringPtr = GET_STRING(objPtr); |
| 1043 | } else { |
| 1044 | AppendUtfToUtfRep(objPtr, bytes, length); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | *---------------------------------------------------------------------- |
| 1050 | * |
| 1051 | * Tcl_AppendUnicodeToObj -- |
| 1052 | * |
| 1053 | * This procedure appends a Unicode string to an object in the |
| 1054 | * most efficient manner possible. Length must be >= 0. |
| 1055 | * |
| 1056 | * Results: |
| 1057 | * None. |
| 1058 | * |
| 1059 | * Side effects: |
| 1060 | * Invalidates the string rep and creates a new Unicode string. |
| 1061 | * |
| 1062 | *---------------------------------------------------------------------- |
| 1063 | */ |
| 1064 | |
| 1065 | void |
| 1066 | Tcl_AppendUnicodeToObj(objPtr, unicode, length) |
| 1067 | register Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1068 | CONST Tcl_UniChar *unicode; /* The unicode string to append to the |
| 1069 | * object. */ |
| 1070 | int length; /* Number of chars in "unicode". */ |
| 1071 | { |
| 1072 | String *stringPtr; |
| 1073 | |
| 1074 | if (Tcl_IsShared(objPtr)) { |
| 1075 | panic("Tcl_AppendUnicodeToObj called with shared object"); |
| 1076 | } |
| 1077 | |
| 1078 | if (length == 0) { |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | SetStringFromAny(NULL, objPtr); |
| 1083 | stringPtr = GET_STRING(objPtr); |
| 1084 | |
| 1085 | /* |
| 1086 | * If objPtr has a valid Unicode rep, then append the "unicode" |
| 1087 | * to the objPtr's Unicode rep, otherwise the UTF conversion of |
| 1088 | * "unicode" to objPtr's string rep. |
| 1089 | */ |
| 1090 | |
| 1091 | if (stringPtr->hasUnicode != 0) { |
| 1092 | AppendUnicodeToUnicodeRep(objPtr, unicode, length); |
| 1093 | } else { |
| 1094 | AppendUnicodeToUtfRep(objPtr, unicode, length); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | *---------------------------------------------------------------------- |
| 1100 | * |
| 1101 | * Tcl_AppendObjToObj -- |
| 1102 | * |
| 1103 | * This procedure appends the string rep of one object to another. |
| 1104 | * "objPtr" cannot be a shared object. |
| 1105 | * |
| 1106 | * Results: |
| 1107 | * None. |
| 1108 | * |
| 1109 | * Side effects: |
| 1110 | * The string rep of appendObjPtr is appended to the string |
| 1111 | * representation of objPtr. |
| 1112 | * |
| 1113 | *---------------------------------------------------------------------- |
| 1114 | */ |
| 1115 | |
| 1116 | void |
| 1117 | Tcl_AppendObjToObj(objPtr, appendObjPtr) |
| 1118 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1119 | Tcl_Obj *appendObjPtr; /* Object to append. */ |
| 1120 | { |
| 1121 | String *stringPtr; |
| 1122 | int length, numChars, allOneByteChars; |
| 1123 | char *bytes; |
| 1124 | |
| 1125 | SetStringFromAny(NULL, objPtr); |
| 1126 | |
| 1127 | /* |
| 1128 | * If objPtr has a valid Unicode rep, then get a Unicode string |
| 1129 | * from appendObjPtr and append it. |
| 1130 | */ |
| 1131 | |
| 1132 | stringPtr = GET_STRING(objPtr); |
| 1133 | if (stringPtr->hasUnicode != 0) { |
| 1134 | |
| 1135 | /* |
| 1136 | * If appendObjPtr is not of the "String" type, don't convert it. |
| 1137 | */ |
| 1138 | |
| 1139 | if (appendObjPtr->typePtr == &tclStringType) { |
| 1140 | stringPtr = GET_STRING(appendObjPtr); |
| 1141 | if ((stringPtr->numChars == -1) |
| 1142 | || (stringPtr->hasUnicode == 0)) { |
| 1143 | |
| 1144 | /* |
| 1145 | * If appendObjPtr is a string obj with no valid Unicode |
| 1146 | * rep, then fill its unicode rep. |
| 1147 | */ |
| 1148 | |
| 1149 | FillUnicodeRep(appendObjPtr); |
| 1150 | stringPtr = GET_STRING(appendObjPtr); |
| 1151 | } |
| 1152 | AppendUnicodeToUnicodeRep(objPtr, stringPtr->unicode, |
| 1153 | stringPtr->numChars); |
| 1154 | } else { |
| 1155 | bytes = Tcl_GetStringFromObj(appendObjPtr, &length); |
| 1156 | AppendUtfToUnicodeRep(objPtr, bytes, length); |
| 1157 | } |
| 1158 | return; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * Append to objPtr's UTF string rep. If we know the number of |
| 1163 | * characters in both objects before appending, then set the combined |
| 1164 | * number of characters in the final (appended-to) object. |
| 1165 | */ |
| 1166 | |
| 1167 | bytes = Tcl_GetStringFromObj(appendObjPtr, &length); |
| 1168 | |
| 1169 | allOneByteChars = 0; |
| 1170 | numChars = stringPtr->numChars; |
| 1171 | if ((numChars >= 0) && (appendObjPtr->typePtr == &tclStringType)) { |
| 1172 | stringPtr = GET_STRING(appendObjPtr); |
| 1173 | if ((stringPtr->numChars >= 0) && (stringPtr->numChars == length)) { |
| 1174 | numChars += stringPtr->numChars; |
| 1175 | allOneByteChars = 1; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | AppendUtfToUtfRep(objPtr, bytes, length); |
| 1180 | |
| 1181 | if (allOneByteChars) { |
| 1182 | stringPtr = GET_STRING(objPtr); |
| 1183 | stringPtr->numChars = numChars; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | *---------------------------------------------------------------------- |
| 1189 | * |
| 1190 | * AppendUnicodeToUnicodeRep -- |
| 1191 | * |
| 1192 | * This procedure appends the contents of "unicode" to the Unicode |
| 1193 | * rep of "objPtr". objPtr must already have a valid Unicode rep. |
| 1194 | * |
| 1195 | * Results: |
| 1196 | * None. |
| 1197 | * |
| 1198 | * Side effects: |
| 1199 | * objPtr's internal rep is reallocated. |
| 1200 | * |
| 1201 | *---------------------------------------------------------------------- |
| 1202 | */ |
| 1203 | |
| 1204 | static void |
| 1205 | AppendUnicodeToUnicodeRep(objPtr, unicode, appendNumChars) |
| 1206 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1207 | CONST Tcl_UniChar *unicode; /* String to append. */ |
| 1208 | int appendNumChars; /* Number of chars of "unicode" to append. */ |
| 1209 | { |
| 1210 | String *stringPtr, *tmpString; |
| 1211 | size_t numChars; |
| 1212 | |
| 1213 | if (appendNumChars < 0) { |
| 1214 | appendNumChars = 0; |
| 1215 | if (unicode) { |
| 1216 | while (unicode[appendNumChars] != 0) { appendNumChars++; } |
| 1217 | } |
| 1218 | } |
| 1219 | if (appendNumChars == 0) { |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| 1223 | SetStringFromAny(NULL, objPtr); |
| 1224 | stringPtr = GET_STRING(objPtr); |
| 1225 | |
| 1226 | /* |
| 1227 | * If not enough space has been allocated for the unicode rep, |
| 1228 | * reallocate the internal rep object with additional space. First |
| 1229 | * try to double the required allocation; if that fails, try a more |
| 1230 | * modest increase. See the "TCL STRING GROWTH ALGORITHM" comment at |
| 1231 | * the top of this file for an explanation of this growth algorithm. |
| 1232 | */ |
| 1233 | |
| 1234 | numChars = stringPtr->numChars + appendNumChars; |
| 1235 | |
| 1236 | if (STRING_UALLOC(numChars) >= stringPtr->uallocated) { |
| 1237 | stringPtr->uallocated = STRING_UALLOC(2 * numChars); |
| 1238 | tmpString = (String *) attemptckrealloc((char *)stringPtr, |
| 1239 | STRING_SIZE(stringPtr->uallocated)); |
| 1240 | if (tmpString == NULL) { |
| 1241 | stringPtr->uallocated = |
| 1242 | STRING_UALLOC(numChars + appendNumChars) |
| 1243 | + TCL_GROWTH_MIN_ALLOC; |
| 1244 | tmpString = (String *) ckrealloc((char *)stringPtr, |
| 1245 | STRING_SIZE(stringPtr->uallocated)); |
| 1246 | } |
| 1247 | stringPtr = tmpString; |
| 1248 | SET_STRING(objPtr, stringPtr); |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Copy the new string onto the end of the old string, then add the |
| 1253 | * trailing null. |
| 1254 | */ |
| 1255 | |
| 1256 | memcpy((VOID*) (stringPtr->unicode + stringPtr->numChars), unicode, |
| 1257 | appendNumChars * sizeof(Tcl_UniChar)); |
| 1258 | stringPtr->unicode[numChars] = 0; |
| 1259 | stringPtr->numChars = numChars; |
| 1260 | |
| 1261 | Tcl_InvalidateStringRep(objPtr); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | *---------------------------------------------------------------------- |
| 1266 | * |
| 1267 | * AppendUnicodeToUtfRep -- |
| 1268 | * |
| 1269 | * This procedure converts the contents of "unicode" to UTF and |
| 1270 | * appends the UTF to the string rep of "objPtr". |
| 1271 | * |
| 1272 | * Results: |
| 1273 | * None. |
| 1274 | * |
| 1275 | * Side effects: |
| 1276 | * objPtr's internal rep is reallocated. |
| 1277 | * |
| 1278 | *---------------------------------------------------------------------- |
| 1279 | */ |
| 1280 | |
| 1281 | static void |
| 1282 | AppendUnicodeToUtfRep(objPtr, unicode, numChars) |
| 1283 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1284 | CONST Tcl_UniChar *unicode; /* String to convert to UTF. */ |
| 1285 | int numChars; /* Number of chars of "unicode" to convert. */ |
| 1286 | { |
| 1287 | Tcl_DString dsPtr; |
| 1288 | CONST char *bytes; |
| 1289 | |
| 1290 | if (numChars < 0) { |
| 1291 | numChars = 0; |
| 1292 | if (unicode) { |
| 1293 | while (unicode[numChars] != 0) { numChars++; } |
| 1294 | } |
| 1295 | } |
| 1296 | if (numChars == 0) { |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | Tcl_DStringInit(&dsPtr); |
| 1301 | bytes = Tcl_UniCharToUtfDString(unicode, numChars, &dsPtr); |
| 1302 | AppendUtfToUtfRep(objPtr, bytes, Tcl_DStringLength(&dsPtr)); |
| 1303 | Tcl_DStringFree(&dsPtr); |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | *---------------------------------------------------------------------- |
| 1308 | * |
| 1309 | * AppendUtfToUnicodeRep -- |
| 1310 | * |
| 1311 | * This procedure converts the contents of "bytes" to Unicode and |
| 1312 | * appends the Unicode to the Unicode rep of "objPtr". objPtr must |
| 1313 | * already have a valid Unicode rep. |
| 1314 | * |
| 1315 | * Results: |
| 1316 | * None. |
| 1317 | * |
| 1318 | * Side effects: |
| 1319 | * objPtr's internal rep is reallocated. |
| 1320 | * |
| 1321 | *---------------------------------------------------------------------- |
| 1322 | */ |
| 1323 | |
| 1324 | static void |
| 1325 | AppendUtfToUnicodeRep(objPtr, bytes, numBytes) |
| 1326 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1327 | CONST char *bytes; /* String to convert to Unicode. */ |
| 1328 | int numBytes; /* Number of bytes of "bytes" to convert. */ |
| 1329 | { |
| 1330 | Tcl_DString dsPtr; |
| 1331 | int numChars; |
| 1332 | Tcl_UniChar *unicode; |
| 1333 | |
| 1334 | if (numBytes < 0) { |
| 1335 | numBytes = (bytes ? strlen(bytes) : 0); |
| 1336 | } |
| 1337 | if (numBytes == 0) { |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | Tcl_DStringInit(&dsPtr); |
| 1342 | numChars = Tcl_NumUtfChars(bytes, numBytes); |
| 1343 | unicode = (Tcl_UniChar *)Tcl_UtfToUniCharDString(bytes, numBytes, &dsPtr); |
| 1344 | AppendUnicodeToUnicodeRep(objPtr, unicode, numChars); |
| 1345 | Tcl_DStringFree(&dsPtr); |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | *---------------------------------------------------------------------- |
| 1350 | * |
| 1351 | * AppendUtfToUtfRep -- |
| 1352 | * |
| 1353 | * This procedure appends "numBytes" bytes of "bytes" to the UTF string |
| 1354 | * rep of "objPtr". objPtr must already have a valid String rep. |
| 1355 | * |
| 1356 | * Results: |
| 1357 | * None. |
| 1358 | * |
| 1359 | * Side effects: |
| 1360 | * objPtr's internal rep is reallocated. |
| 1361 | * |
| 1362 | *---------------------------------------------------------------------- |
| 1363 | */ |
| 1364 | |
| 1365 | static void |
| 1366 | AppendUtfToUtfRep(objPtr, bytes, numBytes) |
| 1367 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1368 | CONST char *bytes; /* String to append. */ |
| 1369 | int numBytes; /* Number of bytes of "bytes" to append. */ |
| 1370 | { |
| 1371 | String *stringPtr; |
| 1372 | int newLength, oldLength; |
| 1373 | |
| 1374 | if (numBytes < 0) { |
| 1375 | numBytes = (bytes ? strlen(bytes) : 0); |
| 1376 | } |
| 1377 | if (numBytes == 0) { |
| 1378 | return; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * Copy the new string onto the end of the old string, then add the |
| 1383 | * trailing null. |
| 1384 | */ |
| 1385 | |
| 1386 | oldLength = objPtr->length; |
| 1387 | newLength = numBytes + oldLength; |
| 1388 | |
| 1389 | stringPtr = GET_STRING(objPtr); |
| 1390 | if (newLength > (int) stringPtr->allocated) { |
| 1391 | |
| 1392 | /* |
| 1393 | * There isn't currently enough space in the string representation |
| 1394 | * so allocate additional space. First, try to double the length |
| 1395 | * required. If that fails, try a more modest allocation. See the |
| 1396 | * "TCL STRING GROWTH ALGORITHM" comment at the top of this file for an |
| 1397 | * explanation of this growth algorithm. |
| 1398 | */ |
| 1399 | |
| 1400 | if (Tcl_AttemptSetObjLength(objPtr, 2 * newLength) == 0) { |
| 1401 | Tcl_SetObjLength(objPtr, |
| 1402 | newLength + numBytes + TCL_GROWTH_MIN_ALLOC); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Invalidate the unicode data. |
| 1408 | */ |
| 1409 | |
| 1410 | stringPtr->numChars = -1; |
| 1411 | stringPtr->hasUnicode = 0; |
| 1412 | |
| 1413 | memcpy((VOID *) (objPtr->bytes + oldLength), (VOID *) bytes, |
| 1414 | (size_t) numBytes); |
| 1415 | objPtr->bytes[newLength] = 0; |
| 1416 | objPtr->length = newLength; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | *---------------------------------------------------------------------- |
| 1421 | * |
| 1422 | * Tcl_AppendStringsToObjVA -- |
| 1423 | * |
| 1424 | * This procedure appends one or more null-terminated strings |
| 1425 | * to an object. |
| 1426 | * |
| 1427 | * Results: |
| 1428 | * None. |
| 1429 | * |
| 1430 | * Side effects: |
| 1431 | * The contents of all the string arguments are appended to the |
| 1432 | * string representation of objPtr. |
| 1433 | * |
| 1434 | *---------------------------------------------------------------------- |
| 1435 | */ |
| 1436 | |
| 1437 | void |
| 1438 | Tcl_AppendStringsToObjVA (objPtr, argList) |
| 1439 | Tcl_Obj *objPtr; /* Points to the object to append to. */ |
| 1440 | va_list argList; /* Variable argument list. */ |
| 1441 | { |
| 1442 | #define STATIC_LIST_SIZE 16 |
| 1443 | String *stringPtr; |
| 1444 | int newLength, oldLength, attemptLength; |
| 1445 | register char *string, *dst; |
| 1446 | char *static_list[STATIC_LIST_SIZE]; |
| 1447 | char **args = static_list; |
| 1448 | int nargs_space = STATIC_LIST_SIZE; |
| 1449 | int nargs, i; |
| 1450 | |
| 1451 | if (Tcl_IsShared(objPtr)) { |
| 1452 | panic("Tcl_AppendStringsToObj called with shared object"); |
| 1453 | } |
| 1454 | |
| 1455 | SetStringFromAny(NULL, objPtr); |
| 1456 | |
| 1457 | /* |
| 1458 | * Figure out how much space is needed for all the strings, and |
| 1459 | * expand the string representation if it isn't big enough. If no |
| 1460 | * bytes would be appended, just return. Note that on some platforms |
| 1461 | * (notably OS/390) the argList is an array so we need to use memcpy. |
| 1462 | */ |
| 1463 | |
| 1464 | nargs = 0; |
| 1465 | newLength = 0; |
| 1466 | oldLength = objPtr->length; |
| 1467 | while (1) { |
| 1468 | string = va_arg(argList, char *); |
| 1469 | if (string == NULL) { |
| 1470 | break; |
| 1471 | } |
| 1472 | if (nargs >= nargs_space) { |
| 1473 | /* |
| 1474 | * Expand the args buffer |
| 1475 | */ |
| 1476 | nargs_space += STATIC_LIST_SIZE; |
| 1477 | if (args == static_list) { |
| 1478 | args = (void *)ckalloc(nargs_space * sizeof(char *)); |
| 1479 | for (i = 0; i < nargs; ++i) { |
| 1480 | args[i] = static_list[i]; |
| 1481 | } |
| 1482 | } else { |
| 1483 | args = (void *)ckrealloc((void *)args, |
| 1484 | nargs_space * sizeof(char *)); |
| 1485 | } |
| 1486 | } |
| 1487 | newLength += strlen(string); |
| 1488 | args[nargs++] = string; |
| 1489 | } |
| 1490 | if (newLength == 0) { |
| 1491 | goto done; |
| 1492 | } |
| 1493 | |
| 1494 | stringPtr = GET_STRING(objPtr); |
| 1495 | if (oldLength + newLength > (int) stringPtr->allocated) { |
| 1496 | |
| 1497 | /* |
| 1498 | * There isn't currently enough space in the string |
| 1499 | * representation, so allocate additional space. If the current |
| 1500 | * string representation isn't empty (i.e. it looks like we're |
| 1501 | * doing a series of appends) then try to allocate extra space to |
| 1502 | * accomodate future growth: first try to double the required memory; |
| 1503 | * if that fails, try a more modest allocation. See the "TCL STRING |
| 1504 | * GROWTH ALGORITHM" comment at the top of this file for an explanation |
| 1505 | * of this growth algorithm. Otherwise, if the current string |
| 1506 | * representation is empty, exactly enough memory is allocated. |
| 1507 | */ |
| 1508 | |
| 1509 | if (oldLength == 0) { |
| 1510 | Tcl_SetObjLength(objPtr, newLength); |
| 1511 | } else { |
| 1512 | attemptLength = 2 * (oldLength + newLength); |
| 1513 | if (Tcl_AttemptSetObjLength(objPtr, attemptLength) == 0) { |
| 1514 | attemptLength = oldLength + (2 * newLength) + |
| 1515 | TCL_GROWTH_MIN_ALLOC; |
| 1516 | Tcl_SetObjLength(objPtr, attemptLength); |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * Make a second pass through the arguments, appending all the |
| 1523 | * strings to the object. |
| 1524 | */ |
| 1525 | |
| 1526 | dst = objPtr->bytes + oldLength; |
| 1527 | for (i = 0; i < nargs; ++i) { |
| 1528 | string = args[i]; |
| 1529 | if (string == NULL) { |
| 1530 | break; |
| 1531 | } |
| 1532 | while (*string != 0) { |
| 1533 | *dst = *string; |
| 1534 | dst++; |
| 1535 | string++; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Add a null byte to terminate the string. However, be careful: |
| 1541 | * it's possible that the object is totally empty (if it was empty |
| 1542 | * originally and there was nothing to append). In this case dst is |
| 1543 | * NULL; just leave everything alone. |
| 1544 | */ |
| 1545 | |
| 1546 | if (dst != NULL) { |
| 1547 | *dst = 0; |
| 1548 | } |
| 1549 | objPtr->length = oldLength + newLength; |
| 1550 | |
| 1551 | done: |
| 1552 | /* |
| 1553 | * If we had to allocate a buffer from the heap, |
| 1554 | * free it now. |
| 1555 | */ |
| 1556 | |
| 1557 | if (args != static_list) { |
| 1558 | ckfree((void *)args); |
| 1559 | } |
| 1560 | #undef STATIC_LIST_SIZE |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | *---------------------------------------------------------------------- |
| 1565 | * |
| 1566 | * Tcl_AppendStringsToObj -- |
| 1567 | * |
| 1568 | * This procedure appends one or more null-terminated strings |
| 1569 | * to an object. |
| 1570 | * |
| 1571 | * Results: |
| 1572 | * None. |
| 1573 | * |
| 1574 | * Side effects: |
| 1575 | * The contents of all the string arguments are appended to the |
| 1576 | * string representation of objPtr. |
| 1577 | * |
| 1578 | *---------------------------------------------------------------------- |
| 1579 | */ |
| 1580 | |
| 1581 | void |
| 1582 | Tcl_AppendStringsToObj TCL_VARARGS_DEF(Tcl_Obj *,arg1) |
| 1583 | { |
| 1584 | register Tcl_Obj *objPtr; |
| 1585 | va_list argList; |
| 1586 | |
| 1587 | objPtr = TCL_VARARGS_START(Tcl_Obj *,arg1,argList); |
| 1588 | Tcl_AppendStringsToObjVA(objPtr, argList); |
| 1589 | va_end(argList); |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | *--------------------------------------------------------------------------- |
| 1594 | * |
| 1595 | * FillUnicodeRep -- |
| 1596 | * |
| 1597 | * Populate the Unicode internal rep with the Unicode form of its string |
| 1598 | * rep. The object must alread have a "String" internal rep. |
| 1599 | * |
| 1600 | * Results: |
| 1601 | * None. |
| 1602 | * |
| 1603 | * Side effects: |
| 1604 | * Reallocates the String internal rep. |
| 1605 | * |
| 1606 | *--------------------------------------------------------------------------- |
| 1607 | */ |
| 1608 | |
| 1609 | static void |
| 1610 | FillUnicodeRep(objPtr) |
| 1611 | Tcl_Obj *objPtr; /* The object in which to fill the unicode rep. */ |
| 1612 | { |
| 1613 | String *stringPtr; |
| 1614 | size_t uallocated; |
| 1615 | char *src, *srcEnd; |
| 1616 | Tcl_UniChar *dst; |
| 1617 | src = objPtr->bytes; |
| 1618 | |
| 1619 | stringPtr = GET_STRING(objPtr); |
| 1620 | if (stringPtr->numChars == -1) { |
| 1621 | stringPtr->numChars = Tcl_NumUtfChars(src, objPtr->length); |
| 1622 | } |
| 1623 | stringPtr->hasUnicode = (stringPtr->numChars > 0); |
| 1624 | |
| 1625 | uallocated = STRING_UALLOC(stringPtr->numChars); |
| 1626 | if (uallocated > stringPtr->uallocated) { |
| 1627 | |
| 1628 | /* |
| 1629 | * If not enough space has been allocated for the unicode rep, |
| 1630 | * reallocate the internal rep object. |
| 1631 | */ |
| 1632 | |
| 1633 | /* |
| 1634 | * There isn't currently enough space in the Unicode |
| 1635 | * representation so allocate additional space. If the current |
| 1636 | * Unicode representation isn't empty (i.e. it looks like we've |
| 1637 | * done some appends) then overallocate the space so |
| 1638 | * that we won't have to do as much reallocation in the future. |
| 1639 | */ |
| 1640 | |
| 1641 | if (stringPtr->uallocated > 0) { |
| 1642 | uallocated *= 2; |
| 1643 | } |
| 1644 | stringPtr = (String *) ckrealloc((char*) stringPtr, |
| 1645 | STRING_SIZE(uallocated)); |
| 1646 | stringPtr->uallocated = uallocated; |
| 1647 | } |
| 1648 | |
| 1649 | /* |
| 1650 | * Convert src to Unicode and store the coverted data in "unicode". |
| 1651 | */ |
| 1652 | |
| 1653 | srcEnd = src + objPtr->length; |
| 1654 | for (dst = stringPtr->unicode; src < srcEnd; dst++) { |
| 1655 | src += TclUtfToUniChar(src, dst); |
| 1656 | } |
| 1657 | *dst = 0; |
| 1658 | |
| 1659 | SET_STRING(objPtr, stringPtr); |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | *---------------------------------------------------------------------- |
| 1664 | * |
| 1665 | * DupStringInternalRep -- |
| 1666 | * |
| 1667 | * Initialize the internal representation of a new Tcl_Obj to a |
| 1668 | * copy of the internal representation of an existing string object. |
| 1669 | * |
| 1670 | * Results: |
| 1671 | * None. |
| 1672 | * |
| 1673 | * Side effects: |
| 1674 | * copyPtr's internal rep is set to a copy of srcPtr's internal |
| 1675 | * representation. |
| 1676 | * |
| 1677 | *---------------------------------------------------------------------- |
| 1678 | */ |
| 1679 | |
| 1680 | static void |
| 1681 | DupStringInternalRep(srcPtr, copyPtr) |
| 1682 | register Tcl_Obj *srcPtr; /* Object with internal rep to copy. Must |
| 1683 | * have an internal rep of type "String". */ |
| 1684 | register Tcl_Obj *copyPtr; /* Object with internal rep to set. Must |
| 1685 | * not currently have an internal rep.*/ |
| 1686 | { |
| 1687 | String *srcStringPtr = GET_STRING(srcPtr); |
| 1688 | String *copyStringPtr = NULL; |
| 1689 | |
| 1690 | /* |
| 1691 | * If the src obj is a string of 1-byte Utf chars, then copy the |
| 1692 | * string rep of the source object and create an "empty" Unicode |
| 1693 | * internal rep for the new object. Otherwise, copy Unicode |
| 1694 | * internal rep, and invalidate the string rep of the new object. |
| 1695 | */ |
| 1696 | |
| 1697 | if (srcStringPtr->hasUnicode == 0) { |
| 1698 | copyStringPtr = (String *) ckalloc(STRING_SIZE(STRING_UALLOC(0))); |
| 1699 | copyStringPtr->uallocated = STRING_UALLOC(0); |
| 1700 | } else { |
| 1701 | copyStringPtr = (String *) ckalloc( |
| 1702 | STRING_SIZE(srcStringPtr->uallocated)); |
| 1703 | copyStringPtr->uallocated = srcStringPtr->uallocated; |
| 1704 | |
| 1705 | memcpy((VOID *) copyStringPtr->unicode, |
| 1706 | (VOID *) srcStringPtr->unicode, |
| 1707 | (size_t) srcStringPtr->numChars * sizeof(Tcl_UniChar)); |
| 1708 | copyStringPtr->unicode[srcStringPtr->numChars] = 0; |
| 1709 | } |
| 1710 | copyStringPtr->numChars = srcStringPtr->numChars; |
| 1711 | copyStringPtr->hasUnicode = srcStringPtr->hasUnicode; |
| 1712 | copyStringPtr->allocated = srcStringPtr->allocated; |
| 1713 | |
| 1714 | /* |
| 1715 | * Tricky point: the string value was copied by generic object |
| 1716 | * management code, so it doesn't contain any extra bytes that |
| 1717 | * might exist in the source object. |
| 1718 | */ |
| 1719 | |
| 1720 | copyStringPtr->allocated = copyPtr->length; |
| 1721 | |
| 1722 | SET_STRING(copyPtr, copyStringPtr); |
| 1723 | copyPtr->typePtr = &tclStringType; |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | *---------------------------------------------------------------------- |
| 1728 | * |
| 1729 | * SetStringFromAny -- |
| 1730 | * |
| 1731 | * Create an internal representation of type "String" for an object. |
| 1732 | * |
| 1733 | * Results: |
| 1734 | * This operation always succeeds and returns TCL_OK. |
| 1735 | * |
| 1736 | * Side effects: |
| 1737 | * Any old internal reputation for objPtr is freed and the |
| 1738 | * internal representation is set to "String". |
| 1739 | * |
| 1740 | *---------------------------------------------------------------------- |
| 1741 | */ |
| 1742 | |
| 1743 | static int |
| 1744 | SetStringFromAny(interp, objPtr) |
| 1745 | Tcl_Interp *interp; /* Used for error reporting if not NULL. */ |
| 1746 | register Tcl_Obj *objPtr; /* The object to convert. */ |
| 1747 | { |
| 1748 | /* |
| 1749 | * The Unicode object is optimized for the case where each UTF char |
| 1750 | * in a string is only one byte. In this case, we store the value of |
| 1751 | * numChars, but we don't copy the bytes to the unicodeObj->unicode. |
| 1752 | */ |
| 1753 | |
| 1754 | if (objPtr->typePtr != &tclStringType) { |
| 1755 | String *stringPtr; |
| 1756 | |
| 1757 | if (objPtr->typePtr != NULL) { |
| 1758 | if (objPtr->bytes == NULL) { |
| 1759 | objPtr->typePtr->updateStringProc(objPtr); |
| 1760 | } |
| 1761 | if ((objPtr->typePtr->freeIntRepProc) != NULL) { |
| 1762 | (*objPtr->typePtr->freeIntRepProc)(objPtr); |
| 1763 | } |
| 1764 | } |
| 1765 | objPtr->typePtr = &tclStringType; |
| 1766 | |
| 1767 | /* |
| 1768 | * Allocate enough space for the basic String structure. |
| 1769 | */ |
| 1770 | |
| 1771 | stringPtr = (String *) ckalloc(STRING_SIZE(STRING_UALLOC(0))); |
| 1772 | stringPtr->numChars = -1; |
| 1773 | stringPtr->uallocated = STRING_UALLOC(0); |
| 1774 | stringPtr->hasUnicode = 0; |
| 1775 | |
| 1776 | if (objPtr->bytes != NULL) { |
| 1777 | stringPtr->allocated = objPtr->length; |
| 1778 | objPtr->bytes[objPtr->length] = 0; |
| 1779 | } else { |
| 1780 | objPtr->length = 0; |
| 1781 | } |
| 1782 | SET_STRING(objPtr, stringPtr); |
| 1783 | } |
| 1784 | return TCL_OK; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | *---------------------------------------------------------------------- |
| 1789 | * |
| 1790 | * UpdateStringOfString -- |
| 1791 | * |
| 1792 | * Update the string representation for an object whose internal |
| 1793 | * representation is "String". |
| 1794 | * |
| 1795 | * Results: |
| 1796 | * None. |
| 1797 | * |
| 1798 | * Side effects: |
| 1799 | * The object's string may be set by converting its Unicode |
| 1800 | * represention to UTF format. |
| 1801 | * |
| 1802 | *---------------------------------------------------------------------- |
| 1803 | */ |
| 1804 | |
| 1805 | static void |
| 1806 | UpdateStringOfString(objPtr) |
| 1807 | Tcl_Obj *objPtr; /* Object with string rep to update. */ |
| 1808 | { |
| 1809 | int i, size; |
| 1810 | Tcl_UniChar *unicode; |
| 1811 | char dummy[TCL_UTF_MAX]; |
| 1812 | char *dst; |
| 1813 | String *stringPtr; |
| 1814 | |
| 1815 | stringPtr = GET_STRING(objPtr); |
| 1816 | if ((objPtr->bytes == NULL) || (stringPtr->allocated == 0)) { |
| 1817 | |
| 1818 | if (stringPtr->numChars <= 0) { |
| 1819 | |
| 1820 | /* |
| 1821 | * If there is no Unicode rep, or the string has 0 chars, |
| 1822 | * then set the string rep to an empty string. |
| 1823 | */ |
| 1824 | |
| 1825 | objPtr->bytes = tclEmptyStringRep; |
| 1826 | objPtr->length = 0; |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| 1830 | unicode = stringPtr->unicode; |
| 1831 | |
| 1832 | /* |
| 1833 | * Translate the Unicode string to UTF. "size" will hold the |
| 1834 | * amount of space the UTF string needs. |
| 1835 | */ |
| 1836 | |
| 1837 | size = 0; |
| 1838 | for (i = 0; i < stringPtr->numChars; i++) { |
| 1839 | size += Tcl_UniCharToUtf((int) unicode[i], dummy); |
| 1840 | } |
| 1841 | |
| 1842 | dst = (char *) ckalloc((unsigned) (size + 1)); |
| 1843 | objPtr->bytes = dst; |
| 1844 | objPtr->length = size; |
| 1845 | stringPtr->allocated = size; |
| 1846 | |
| 1847 | for (i = 0; i < stringPtr->numChars; i++) { |
| 1848 | dst += Tcl_UniCharToUtf(unicode[i], dst); |
| 1849 | } |
| 1850 | *dst = '\0'; |
| 1851 | } |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | *---------------------------------------------------------------------- |
| 1857 | * |
| 1858 | * FreeStringInternalRep -- |
| 1859 | * |
| 1860 | * Deallocate the storage associated with a String data object's |
| 1861 | * internal representation. |
| 1862 | * |
| 1863 | * Results: |
| 1864 | * None. |
| 1865 | * |
| 1866 | * Side effects: |
| 1867 | * Frees memory. |
| 1868 | * |
| 1869 | *---------------------------------------------------------------------- |
| 1870 | */ |
| 1871 | |
| 1872 | static void |
| 1873 | FreeStringInternalRep(objPtr) |
| 1874 | Tcl_Obj *objPtr; /* Object with internal rep to free. */ |
| 1875 | { |
| 1876 | ckfree((char *) GET_STRING(objPtr)); |
| 1877 | } |