Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/StringObj.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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