Debian Codegraph @

doc/StringObj.3

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