| 1 |
/* |
| 2 |
* tclStringRep.h -- |
| 3 |
* |
| 4 |
* This file contains the definition of the Unicode string internal |
| 5 |
* representation and macros to access it. |
| 6 |
* |
| 7 |
* A Unicode string is an internationalized string. Conceptually, a |
| 8 |
* Unicode string is an array of 16-bit quantities organized as a |
| 9 |
* sequence of properly formed UTF-8 characters. There is a one-to-one |
| 10 |
* map between Unicode and UTF characters. Because Unicode characters |
| 11 |
* have a fixed width, operations such as indexing operate on Unicode |
| 12 |
* data. The String object is optimized for the case where each UTF char |
| 13 |
* in a string is only one byte. In this case, we store the value of |
| 14 |
* numChars, but we don't store the Unicode data (unless Tcl_GetUnicode |
| 15 |
* is explicitly called). |
| 16 |
* |
| 17 |
* The String object type stores one or both formats. The default |
| 18 |
* behavior is to store UTF. Once Unicode is calculated by a function, it |
| 19 |
* is stored in the internal rep for future access (without an additional |
| 20 |
* O(n) cost). |
| 21 |
* |
| 22 |
* To allow many appends to be done to an object without constantly |
| 23 |
* reallocating the space for the string or Unicode representation, we |
| 24 |
* allocate double the space for the string or Unicode and use the |
| 25 |
* internal representation to keep track of how much space is used vs. |
| 26 |
* allocated. |
| 27 |
* |
| 28 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 29 |
* Copyright (c) 1999 by Scriptics Corporation. |
| 30 |
* |
| 31 |
* See the file "license.terms" for information on usage and redistribution of |
| 32 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 33 |
*/ |
| 34 |
|
| 35 |
/* |
| 36 |
* The following structure is the internal rep for a String object. It keeps |
| 37 |
* track of how much memory has been used and how much has been allocated for |
| 38 |
* the Unicode and UTF string to enable growing and shrinking of the UTF and |
| 39 |
* Unicode reps of the String object with fewer mallocs. To optimize string |
| 40 |
* length and indexing operations, this structure also stores the number of |
| 41 |
* characters (same of UTF and Unicode!) once that value has been computed. |
| 42 |
* |
| 43 |
* Under normal configurations, what Tcl calls "Unicode" is actually UTF-16 |
| 44 |
* restricted to the Basic Multilingual Plane (i.e. U+00000 to U+0FFFF). This |
| 45 |
* can be officially modified by altering the definition of Tcl_UniChar in |
| 46 |
* tcl.h, but do not do that unless you are sure what you're doing! |
| 47 |
*/ |
| 48 |
|
| 49 |
typedef struct String { |
| 50 |
int numChars; /* The number of chars in the string. -1 means |
| 51 |
* this value has not been calculated. >= 0 |
| 52 |
* means that there is a valid Unicode rep, or |
| 53 |
* that the number of UTF bytes == the number |
| 54 |
* of chars. */ |
| 55 |
int allocated; /* The amount of space actually allocated for |
| 56 |
* the UTF string (minus 1 byte for the |
| 57 |
* termination char). */ |
| 58 |
int maxChars; /* Max number of chars that can fit in the |
| 59 |
* space allocated for the Unicode array. */ |
| 60 |
int hasUnicode; /* Boolean determining whether the string has |
| 61 |
* a Unicode representation. */ |
| 62 |
Tcl_UniChar unicode[TCLFLEXARRAY]; /* The array of Unicode chars. The actual size |
| 63 |
* of this field depends on the 'maxChars' |
| 64 |
* field above. */ |
| 65 |
} String; |
| 66 |
|
| 67 |
#define STRING_MAXCHARS \ |
| 68 |
(int)(((size_t)UINT_MAX - TclOffset(String, unicode))/sizeof(Tcl_UniChar) - 1) |
| 69 |
#define STRING_SIZE(numChars) \ |
| 70 |
(TclOffset(String, unicode) + sizeof(Tcl_UniChar) + ((numChars) * sizeof(Tcl_UniChar))) |
| 71 |
#define stringCheckLimits(numChars) \ |
| 72 |
do { \ |
| 73 |
if ((numChars) < 0 || (numChars) > STRING_MAXCHARS) { \ |
| 74 |
Tcl_Panic("max length for a Tcl unicode value (%d chars) exceeded", \ |
| 75 |
STRING_MAXCHARS); \ |
| 76 |
} \ |
| 77 |
} while (0) |
| 78 |
#define stringAttemptAlloc(numChars) \ |
| 79 |
(String *) attemptckalloc((unsigned) STRING_SIZE(numChars)) |
| 80 |
#define stringAlloc(numChars) \ |
| 81 |
(String *) ckalloc((unsigned) STRING_SIZE(numChars)) |
| 82 |
#define stringRealloc(ptr, numChars) \ |
| 83 |
(String *) ckrealloc((ptr), (unsigned) STRING_SIZE(numChars)) |
| 84 |
#define stringAttemptRealloc(ptr, numChars) \ |
| 85 |
(String *) attemptckrealloc((ptr), (unsigned) STRING_SIZE(numChars)) |
| 86 |
#define GET_STRING(objPtr) \ |
| 87 |
((String *) (objPtr)->internalRep.twoPtrValue.ptr1) |
| 88 |
#define SET_STRING(objPtr, stringPtr) \ |
| 89 |
((objPtr)->internalRep.twoPtrValue.ptr1 = (void *) (stringPtr)) |
| 90 |
|
| 91 |
/* |
| 92 |
* Local Variables: |
| 93 |
* mode: c |
| 94 |
* c-basic-offset: 4 |
| 95 |
* fill-column: 78 |
| 96 |
* End: |
| 97 |
*/ |