1 | '\" |
---|
2 | '\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
---|
3 | '\" |
---|
4 | '\" See the file "license.terms" for information on usage and redistribution |
---|
5 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
6 | '\" |
---|
7 | '\" RCS: @(#) $Id: StringObj.3,v 1.26 2007/12/13 15:22:32 dgp Exp $ |
---|
8 | '\" |
---|
9 | .so man.macros |
---|
10 | .TH Tcl_StringObj 3 8.1 Tcl "Tcl Library Procedures" |
---|
11 | .BS |
---|
12 | .SH NAME |
---|
13 | Tcl_NewStringObj, Tcl_NewUnicodeObj, Tcl_SetStringObj, Tcl_SetUnicodeObj, Tcl_GetStringFromObj, Tcl_GetString, Tcl_GetUnicodeFromObj, Tcl_GetUnicode, Tcl_GetUniChar, Tcl_GetCharLength, Tcl_GetRange, Tcl_AppendToObj, Tcl_AppendUnicodeToObj, Tcl_AppendObjToObj, Tcl_AppendStringsToObj, Tcl_AppendStringsToObjVA, Tcl_AppendLimitedToObj, Tcl_Format, Tcl_AppendFormatToObj, Tcl_ObjPrintf, Tcl_AppendPrintfToObj, Tcl_SetObjLength, Tcl_AttemptSetObjLength, Tcl_ConcatObj \- manipulate Tcl objects as strings |
---|
14 | .SH SYNOPSIS |
---|
15 | .nf |
---|
16 | \fB#include <tcl.h>\fR |
---|
17 | .sp |
---|
18 | Tcl_Obj * |
---|
19 | \fBTcl_NewStringObj\fR(\fIbytes, length\fR) |
---|
20 | .sp |
---|
21 | Tcl_Obj * |
---|
22 | \fBTcl_NewUnicodeObj\fR(\fIunicode, numChars\fR) |
---|
23 | .sp |
---|
24 | void |
---|
25 | \fBTcl_SetStringObj\fR(\fIobjPtr, bytes, length\fR) |
---|
26 | .sp |
---|
27 | void |
---|
28 | \fBTcl_SetUnicodeObj\fR(\fIobjPtr, unicode, numChars\fR) |
---|
29 | .sp |
---|
30 | char * |
---|
31 | \fBTcl_GetStringFromObj\fR(\fIobjPtr, lengthPtr\fR) |
---|
32 | .sp |
---|
33 | char * |
---|
34 | \fBTcl_GetString\fR(\fIobjPtr\fR) |
---|
35 | .sp |
---|
36 | Tcl_UniChar * |
---|
37 | \fBTcl_GetUnicodeFromObj\fR(\fIobjPtr, lengthPtr\fR) |
---|
38 | .sp |
---|
39 | Tcl_UniChar * |
---|
40 | \fBTcl_GetUnicode\fR(\fIobjPtr\fR) |
---|
41 | .sp |
---|
42 | Tcl_UniChar |
---|
43 | \fBTcl_GetUniChar\fR(\fIobjPtr, index\fR) |
---|
44 | .sp |
---|
45 | int |
---|
46 | \fBTcl_GetCharLength\fR(\fIobjPtr\fR) |
---|
47 | .sp |
---|
48 | Tcl_Obj * |
---|
49 | \fBTcl_GetRange\fR(\fIobjPtr, first, last\fR) |
---|
50 | .sp |
---|
51 | void |
---|
52 | \fBTcl_AppendToObj\fR(\fIobjPtr, bytes, length\fR) |
---|
53 | .sp |
---|
54 | void |
---|
55 | \fBTcl_AppendUnicodeToObj\fR(\fIobjPtr, unicode, numChars\fR) |
---|
56 | .sp |
---|
57 | void |
---|
58 | \fBTcl_AppendObjToObj\fR(\fIobjPtr, appendObjPtr\fR) |
---|
59 | .sp |
---|
60 | void |
---|
61 | \fBTcl_AppendStringsToObj\fR(\fIobjPtr, string, string, ... \fB(char *) NULL\fR) |
---|
62 | .sp |
---|
63 | void |
---|
64 | \fBTcl_AppendStringsToObjVA\fR(\fIobjPtr, argList\fR) |
---|
65 | .VS 8.5 |
---|
66 | .sp |
---|
67 | void |
---|
68 | \fBTcl_AppendLimitedToObj\fR(\fIobjPtr, bytes, length, limit, ellipsis\fR) |
---|
69 | .sp |
---|
70 | Tcl_Obj * |
---|
71 | \fBTcl_Format\fR(\fIinterp, format, objc, objv\fR) |
---|
72 | .sp |
---|
73 | int |
---|
74 | \fBTcl_AppendFormatToObj\fR(\fIinterp, objPtr, format, objc, objv\fR) |
---|
75 | .sp |
---|
76 | Tcl_Obj * |
---|
77 | \fBTcl_ObjPrintf\fR(\fIformat, ...\fR) |
---|
78 | .sp |
---|
79 | int |
---|
80 | \fBTcl_AppendPrintfToObj\fR(\fIobjPtr, format, ...\fR) |
---|
81 | .VE 8.5 |
---|
82 | .sp |
---|
83 | void |
---|
84 | \fBTcl_SetObjLength\fR(\fIobjPtr, newLength\fR) |
---|
85 | .sp |
---|
86 | int |
---|
87 | \fBTcl_AttemptSetObjLength\fR(\fIobjPtr, newLength\fR) |
---|
88 | .sp |
---|
89 | Tcl_Obj * |
---|
90 | \fBTcl_ConcatObj\fR(\fIobjc, objv\fR) |
---|
91 | .SH ARGUMENTS |
---|
92 | .AS "const Tcl_UniChar" *appendObjPtr in/out |
---|
93 | .AP "const char" *bytes in |
---|
94 | Points to the first byte of an array of UTF-8-encoded bytes |
---|
95 | used to set or append to a string object. |
---|
96 | This byte array may contain embedded null characters |
---|
97 | unless \fInumChars\fR is negative. (Applications needing null bytes |
---|
98 | should represent them as the two-byte sequence \fI\e700\e600\fR, use |
---|
99 | \fBTcl_ExternalToUtf\fR to convert, or \fBTcl_NewByteArrayObj\fR if |
---|
100 | the string is a collection of uninterpreted bytes.) |
---|
101 | .AP int length in |
---|
102 | The number of bytes to copy from \fIbytes\fR when |
---|
103 | initializing, setting, or appending to a string object. |
---|
104 | If negative, all bytes up to the first null are used. |
---|
105 | .AP "const Tcl_UniChar" *unicode in |
---|
106 | Points to the first byte of an array of Unicode characters |
---|
107 | used to set or append to a string object. |
---|
108 | This byte array may contain embedded null characters |
---|
109 | unless \fInumChars\fR is negative. |
---|
110 | .AP int numChars in |
---|
111 | The number of Unicode characters to copy from \fIunicode\fR when |
---|
112 | initializing, setting, or appending to a string object. |
---|
113 | If negative, all characters up to the first null character are used. |
---|
114 | .AP int index in |
---|
115 | The index of the Unicode character to return. |
---|
116 | .AP int first in |
---|
117 | The index of the first Unicode character in the Unicode range to be |
---|
118 | returned as a new object. |
---|
119 | .AP int last in |
---|
120 | The index of the last Unicode character in the Unicode range to be |
---|
121 | returned as a new object. |
---|
122 | .AP Tcl_Obj *objPtr in/out |
---|
123 | Points to an object to manipulate. |
---|
124 | .AP Tcl_Obj *appendObjPtr in |
---|
125 | The object to append to \fIobjPtr\fR in \fBTcl_AppendObjToObj\fR. |
---|
126 | .AP int *lengthPtr out |
---|
127 | If non-NULL, the location where \fBTcl_GetStringFromObj\fR will store |
---|
128 | the length of an object's string representation. |
---|
129 | .AP "const char" *string in |
---|
130 | Null-terminated string value to append to \fIobjPtr\fR. |
---|
131 | .AP va_list argList in |
---|
132 | An argument list which must have been initialised using |
---|
133 | \fBva_start\fR, and cleared using \fBva_end\fR. |
---|
134 | .AP int limit in |
---|
135 | Maximum number of bytes to be appended. |
---|
136 | .AP "const char" *ellipsis in |
---|
137 | Suffix to append when the limit leads to string truncation. |
---|
138 | If NULL is passed then the suffix "..." is used. |
---|
139 | .AP "const char" *format in |
---|
140 | Format control string including % conversion specifiers. |
---|
141 | .AP int objc in |
---|
142 | The number of elements to format or concatenate. |
---|
143 | .AP Tcl_Obj *objv[] in |
---|
144 | The array of objects to format or concatenate. |
---|
145 | .AP int newLength in |
---|
146 | New length for the string value of \fIobjPtr\fR, not including the |
---|
147 | final null character. |
---|
148 | .BE |
---|
149 | |
---|
150 | .SH DESCRIPTION |
---|
151 | .PP |
---|
152 | The procedures described in this manual entry allow Tcl objects to |
---|
153 | be manipulated as string values. They use the internal representation |
---|
154 | of the object to store additional information to make the string |
---|
155 | manipulations more efficient. In particular, they make a series of |
---|
156 | append operations efficient by allocating extra storage space for the |
---|
157 | string so that it does not have to be copied for each append. |
---|
158 | Also, indexing and length computations are optimized because the |
---|
159 | Unicode string representation is calculated and cached as needed. |
---|
160 | When using the \fBTcl_Append*\fR family of functions where the |
---|
161 | interpreter's result is the object being appended to, it is important |
---|
162 | to call Tcl_ResetResult first to ensure you are not unintentionally |
---|
163 | appending to existing data in the result object. |
---|
164 | .PP |
---|
165 | \fBTcl_NewStringObj\fR and \fBTcl_SetStringObj\fR create a new object |
---|
166 | or modify an existing object to hold a copy of the string given by |
---|
167 | \fIbytes\fR and \fIlength\fR. \fBTcl_NewUnicodeObj\fR and |
---|
168 | \fBTcl_SetUnicodeObj\fR create a new object or modify an existing |
---|
169 | object to hold a copy of the Unicode string given by \fIunicode\fR and |
---|
170 | \fInumChars\fR. \fBTcl_NewStringObj\fR and \fBTcl_NewUnicodeObj\fR |
---|
171 | return a pointer to a newly created object with reference count zero. |
---|
172 | All four procedures set the object to hold a copy of the specified |
---|
173 | string. \fBTcl_SetStringObj\fR and \fBTcl_SetUnicodeObj\fR free any |
---|
174 | old string representation as well as any old internal representation |
---|
175 | of the object. |
---|
176 | .PP |
---|
177 | \fBTcl_GetStringFromObj\fR and \fBTcl_GetString\fR return an object's |
---|
178 | string representation. This is given by the returned byte pointer and |
---|
179 | (for \fBTcl_GetStringFromObj\fR) length, which is stored in |
---|
180 | \fIlengthPtr\fR if it is non-NULL. If the object's UTF string |
---|
181 | representation is invalid (its byte pointer is NULL), the string |
---|
182 | representation is regenerated from the object's internal |
---|
183 | representation. The storage referenced by the returned byte pointer |
---|
184 | is owned by the object manager. It is passed back as a writable |
---|
185 | pointer so that extension author creating their own \fBTcl_ObjType\fR |
---|
186 | will be able to modify the string representation within the |
---|
187 | \fBTcl_UpdateStringProc\fR of their \fBTcl_ObjType\fR. Except for that |
---|
188 | limited purpose, the pointer returned by \fBTcl_GetStringFromObj\fR |
---|
189 | or \fBTcl_GetString\fR should be treated as read-only. It is |
---|
190 | recommended that this pointer be assigned to a (const char *) variable. |
---|
191 | Even in the limited situations where writing to this pointer is |
---|
192 | acceptable, one should take care to respect the copy-on-write |
---|
193 | semantics required by \fBTcl_Obj\fR's, with appropriate calls |
---|
194 | to \fBTcl_IsShared\fR and \fBTcl_DuplicateObj\fR prior to any |
---|
195 | in-place modification of the string representation. |
---|
196 | The procedure \fBTcl_GetString\fR is used in the common case |
---|
197 | where the caller does not need the length of the string |
---|
198 | representation. |
---|
199 | .PP |
---|
200 | \fBTcl_GetUnicodeFromObj\fR and \fBTcl_GetUnicode\fR return an object's |
---|
201 | value as a Unicode string. This is given by the returned pointer and |
---|
202 | (for \fBTcl_GetUnicodeFromObj\fR) length, which is stored in |
---|
203 | \fIlengthPtr\fR if it is non-NULL. The storage referenced by the returned |
---|
204 | byte pointer is owned by the object manager and should not be modified by |
---|
205 | the caller. The procedure \fBTcl_GetUnicode\fR is used in the common case |
---|
206 | where the caller does not need the length of the unicode string |
---|
207 | representation. |
---|
208 | .PP |
---|
209 | \fBTcl_GetUniChar\fR returns the \fIindex\fR'th character in the |
---|
210 | object's Unicode representation. |
---|
211 | .PP |
---|
212 | \fBTcl_GetRange\fR returns a newly created object comprised of the |
---|
213 | characters between \fIfirst\fR and \fIlast\fR (inclusive) in the |
---|
214 | object's Unicode representation. If the object's Unicode |
---|
215 | representation is invalid, the Unicode representation is regenerated |
---|
216 | from the object's string representation. |
---|
217 | .PP |
---|
218 | \fBTcl_GetCharLength\fR returns the number of characters (as opposed |
---|
219 | to bytes) in the string object. |
---|
220 | .PP |
---|
221 | \fBTcl_AppendToObj\fR appends the data given by \fIbytes\fR and |
---|
222 | \fIlength\fR to the string representation of the object specified by |
---|
223 | \fIobjPtr\fR. If the object has an invalid string representation, |
---|
224 | then an attempt is made to convert \fIbytes\fR is to the Unicode |
---|
225 | format. If the conversion is successful, then the converted form of |
---|
226 | \fIbytes\fR is appended to the object's Unicode representation. |
---|
227 | Otherwise, the object's Unicode representation is invalidated and |
---|
228 | converted to the UTF format, and \fIbytes\fR is appended to the |
---|
229 | object's new string representation. |
---|
230 | .PP |
---|
231 | \fBTcl_AppendUnicodeToObj\fR appends the Unicode string given by |
---|
232 | \fIunicode\fR and \fInumChars\fR to the object specified by |
---|
233 | \fIobjPtr\fR. If the object has an invalid Unicode representation, |
---|
234 | then \fIunicode\fR is converted to the UTF format and appended to the |
---|
235 | object's string representation. Appends are optimized to handle |
---|
236 | repeated appends relatively efficiently (it overallocates the string |
---|
237 | or Unicode space to avoid repeated reallocations and copies of |
---|
238 | object's string value). |
---|
239 | .PP |
---|
240 | \fBTcl_AppendObjToObj\fR is similar to \fBTcl_AppendToObj\fR, but it |
---|
241 | appends the string or Unicode value (whichever exists and is best |
---|
242 | suited to be appended to \fIobjPtr\fR) of \fIappendObjPtr\fR to |
---|
243 | \fIobjPtr\fR. |
---|
244 | .PP |
---|
245 | \fBTcl_AppendStringsToObj\fR is similar to \fBTcl_AppendToObj\fR |
---|
246 | except that it can be passed more than one value to append and |
---|
247 | each value must be a null-terminated string (i.e. none of the |
---|
248 | values may contain internal null characters). Any number of |
---|
249 | \fIstring\fR arguments may be provided, but the last argument |
---|
250 | must be a NULL pointer to indicate the end of the list. |
---|
251 | .PP |
---|
252 | \fBTcl_AppendStringsToObjVA\fR is the same as \fBTcl_AppendStringsToObj\fR |
---|
253 | except that instead of taking a variable number of arguments it takes an |
---|
254 | argument list. |
---|
255 | .PP |
---|
256 | .VS 8.5 |
---|
257 | \fBTcl_AppendLimitedToObj\fR is similar to \fBTcl_AppendToObj\fR |
---|
258 | except that it imposes a limit on how many bytes are appended. |
---|
259 | This can be handy when the string to be appended might be |
---|
260 | very large, but the value being constructed should not be allowed to grow |
---|
261 | without bound. A common usage is when constructing an error message, where the |
---|
262 | end result should be kept short enough to be read. |
---|
263 | Bytes from \fIbytes\fR are appended to \fIobjPtr\fR, but no more |
---|
264 | than \fIlimit\fR bytes total are to be appended. If the limit prevents |
---|
265 | all \fIlength\fR bytes that are available from being appended, then the |
---|
266 | appending is done so that the last bytes appended are from the |
---|
267 | string \fIellipsis\fR. This allows for an indication of the truncation |
---|
268 | to be left in the string. |
---|
269 | When \fIlength\fR is \fB-1\fR, all bytes up to the first zero byte are appended, |
---|
270 | subject to the limit. When \fIellipsis\fR is NULL, the default |
---|
271 | string \fB...\fR is used. When \fIellipsis\fR is non-NULL, it must point |
---|
272 | to a zero-byte-terminated string in Tcl's internal UTF encoding. |
---|
273 | The number of bytes appended can be less than the lesser |
---|
274 | of \fIlength\fR and \fIlimit\fR when appending fewer |
---|
275 | bytes is necessary to append only whole multi-byte characters. |
---|
276 | .PP |
---|
277 | \fBTcl_Format\fR is the C-level interface to the engine of the \fBformat\fR |
---|
278 | command. The actual command procedure for \fBformat\fR is little more |
---|
279 | than |
---|
280 | .CS |
---|
281 | Tcl_Format(interp, Tcl_GetString(objv[1]), objc-2, objv+2); |
---|
282 | .CE |
---|
283 | The \fIobjc\fR Tcl_Obj values in \fIobjv\fR are formatted into a string |
---|
284 | according to the conversion specification in \fIformat\fR argument, following |
---|
285 | the documentation for the \fBformat\fR command. The resulting formatted |
---|
286 | string is converted to a new Tcl_Obj with refcount of zero and returned. |
---|
287 | If some error happens during production of the formatted string, NULL is |
---|
288 | returned, and an error message is recorded in \fIinterp\fR, if \fIinterp\fR |
---|
289 | is non-NULL. |
---|
290 | .PP |
---|
291 | \fBTcl_AppendFormatToObj\fR is an appending alternative form |
---|
292 | of \fBTcl_Format\fR with functionality equivalent to |
---|
293 | .CS |
---|
294 | Tcl_Obj *newPtr = Tcl_Format(interp, format, objc, objv); |
---|
295 | if (newPtr == NULL) return TCL_ERROR; |
---|
296 | Tcl_AppendObjToObj(objPtr, newPtr); |
---|
297 | return TCL_OK; |
---|
298 | .CE |
---|
299 | but with greater convenience and efficiency when the appending |
---|
300 | functionality is needed. |
---|
301 | .PP |
---|
302 | \fBTcl_ObjPrintf\fR serves as a replacement for the common sequence |
---|
303 | .CS |
---|
304 | char buf[SOME_SUITABLE_LENGTH]; |
---|
305 | sprintf(buf, format, ...); |
---|
306 | Tcl_NewStringObj(buf, -1); |
---|
307 | .CE |
---|
308 | but with greater convenience and no need to |
---|
309 | determine \fBSOME_SUITABLE_LENGTH\fR. The formatting is done with the same |
---|
310 | core formatting engine used by \fBTcl_Format\fR. This means the set of |
---|
311 | supported conversion specifiers is that of the \fBformat\fR command and |
---|
312 | not that of the \fBsprintf\fR routine where the two sets differ. When a |
---|
313 | conversion specifier passed to \fBTcl_ObjPrintf\fR includes a precision, |
---|
314 | the value is taken as a number of bytes, as \fBsprintf\fR does, and not |
---|
315 | as a number of characters, as \fBformat\fR does. This is done on the |
---|
316 | assumption that C code is more likely to know how many bytes it is |
---|
317 | passing around than the number of encoded characters those bytes happen |
---|
318 | to represent. The variable number of arguments passed in should be of |
---|
319 | the types that would be suitable for passing to \fBsprintf\fR. Note in |
---|
320 | this example usage, \fIx\fR is of type \fBlong\fR. |
---|
321 | .CS |
---|
322 | long x = 5; |
---|
323 | Tcl_Obj *objPtr = Tcl_ObjPrintf("Value is %d", x); |
---|
324 | .CE |
---|
325 | If the value of \fIformat\fR contains internal inconsistencies or invalid |
---|
326 | specifier formats, the formatted string result produced by |
---|
327 | \fBTcl_ObjPrintf\fR will be an error message describing the error. |
---|
328 | .PP |
---|
329 | \fBTcl_AppendPrintfToObj\fR is an appending alternative form |
---|
330 | of \fBTcl_ObjPrintf\fR with functionality equivalent to |
---|
331 | .CS |
---|
332 | Tcl_AppendObjToObj(objPtr, Tcl_ObjPrintf(format, ...)); |
---|
333 | .CE |
---|
334 | but with greater convenience and efficiency when the appending |
---|
335 | functionality is needed. |
---|
336 | .VE 8.5 |
---|
337 | .PP |
---|
338 | The \fBTcl_SetObjLength\fR procedure changes the length of the |
---|
339 | string value of its \fIobjPtr\fR argument. If the \fInewLength\fR |
---|
340 | argument is greater than the space allocated for the object's |
---|
341 | string, then the string space is reallocated and the old value |
---|
342 | is copied to the new space; the bytes between the old length of |
---|
343 | the string and the new length may have arbitrary values. |
---|
344 | If the \fInewLength\fR argument is less than the current length |
---|
345 | of the object's string, with \fIobjPtr->length\fR is reduced without |
---|
346 | reallocating the string space; the original allocated size for the |
---|
347 | string is recorded in the object, so that the string length can be |
---|
348 | enlarged in a subsequent call to \fBTcl_SetObjLength\fR without |
---|
349 | reallocating storage. In all cases \fBTcl_SetObjLength\fR leaves |
---|
350 | a null character at \fIobjPtr->bytes[newLength]\fR. |
---|
351 | .PP |
---|
352 | \fBTcl_AttemptSetObjLength\fR is identical in function to |
---|
353 | \fBTcl_SetObjLength\fR except that if sufficient memory to satisfy the |
---|
354 | request cannot be allocated, it does not cause the Tcl interpreter to |
---|
355 | \fBpanic\fR. Thus, if \fInewLength\fR is greater than the space |
---|
356 | allocated for the object's string, and there is not enough memory |
---|
357 | available to satisfy the request, \fBTcl_AttemptSetObjLength\fR will take |
---|
358 | no action and return 0 to indicate failure. If there is enough memory |
---|
359 | to satisfy the request, \fBTcl_AttemptSetObjLength\fR behaves just like |
---|
360 | \fBTcl_SetObjLength\fR and returns 1 to indicate success. |
---|
361 | .PP |
---|
362 | The \fBTcl_ConcatObj\fR function returns a new string object whose |
---|
363 | value is the space-separated concatenation of the string |
---|
364 | representations of all of the objects in the \fIobjv\fR |
---|
365 | array. \fBTcl_ConcatObj\fR eliminates leading and trailing white space |
---|
366 | as it copies the string representations of the \fIobjv\fR array to the |
---|
367 | result. If an element of the \fIobjv\fR array consists of nothing but |
---|
368 | white space, then that object is ignored entirely. This white-space |
---|
369 | removal was added to make the output of the \fBconcat\fR command |
---|
370 | cleaner-looking. \fBTcl_ConcatObj\fR returns a pointer to a |
---|
371 | newly-created object whose ref count is zero. |
---|
372 | |
---|
373 | .SH "SEE ALSO" |
---|
374 | Tcl_NewObj, Tcl_IncrRefCount, Tcl_DecrRefCount, format, sprintf |
---|
375 | |
---|
376 | .SH KEYWORDS |
---|
377 | append, internal representation, object, object type, string object, |
---|
378 | string type, string representation, concat, concatenate, unicode |
---|