1Tcl_DString(3) Tcl Library Procedures Tcl_DString(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_DStringInit, Tcl_DStringAppend, Tcl_DStringAppendElement,
9 Tcl_DStringStartSublist, Tcl_DStringEndSublist, Tcl_DStringLength,
10 Tcl_DStringValue, Tcl_DStringSetLength, Tcl_DStringTrunc,
11 Tcl_DStringFree, Tcl_DStringResult, Tcl_DStringGetResult - manipulate
12 dynamic strings
13
15 #include <tcl.h>
16
17 Tcl_DStringInit(dsPtr)
18
19 char *
20 Tcl_DStringAppend(dsPtr, string, length)
21
22 char *
23 Tcl_DStringAppendElement(dsPtr, string)
24
25 Tcl_DStringStartSublist(dsPtr)
26
27 Tcl_DStringEndSublist(dsPtr)
28
29 int
30 Tcl_DStringLength(dsPtr)
31
32 char *
33 Tcl_DStringValue(dsPtr)
34
35 Tcl_DStringSetLength(dsPtr, newLength)
36
37 Tcl_DStringTrunc(dsPtr, newLength)
38
39 Tcl_DStringFree(dsPtr)
40
41 Tcl_DStringResult(interp, dsPtr)
42
43 Tcl_DStringGetResult(interp, dsPtr)
44
46 Tcl_DString *dsPtr (in/out) Pointer to structure that is used
47 to manage a dynamic string.
48
49 CONST char *string (in) Pointer to characters to add to
50 dynamic string.
51
52 int length (in) Number of characters from string to
53 add to dynamic string. If -1, add
54 all characters up to null terminat‐
55 ing character.
56
57 int newLength (in) New length for dynamic string, not
58 including null terminating charac‐
59 ter.
60
61 Tcl_Interp *interp (in/out) Interpreter whose result is to be
62 set from or moved to the dynamic
63 string.
64_________________________________________________________________
65
66
68 Dynamic strings provide a mechanism for building up arbitrarily long
69 strings by gradually appending information. If the dynamic string is
70 short then there will be no memory allocation overhead; as the string
71 gets larger, additional space will be allocated as needed.
72
73 Tcl_DStringInit initializes a dynamic string to zero length. The
74 Tcl_DString structure must have been allocated by the caller. No
75 assumptions are made about the current state of the structure; anything
76 already in it is discarded. If the structure has been used previously,
77 Tcl_DStringFree should be called first to free up any memory allocated
78 for the old string.
79
80 Tcl_DStringAppend adds new information to a dynamic string, allocating
81 more memory for the string if needed. If length is less than zero then
82 everything in string is appended to the dynamic string; otherwise
83 length specifies the number of bytes to append. Tcl_DStringAppend
84 returns a pointer to the characters of the new string. The string can
85 also be retrieved from the string field of the Tcl_DString structure.
86
87 Tcl_DStringAppendElement is similar to Tcl_DStringAppend except that it
88 doesn't take a length argument (it appends all of string) and it con‐
89 verts the string to a proper list element before appending.
90 Tcl_DStringAppendElement adds a separator space before the new list
91 element unless the new list element is the first in a list or sub-list
92 (i.e. either the current string is empty, or it contains the single
93 character ``{'', or the last two characters of the current string are
94 `` {''). Tcl_DStringAppendElement returns a pointer to the characters
95 of the new string.
96
97 Tcl_DStringStartSublist and Tcl_DStringEndSublist can be used to create
98 nested lists. To append a list element that is itself a sublist, first
99 call Tcl_DStringStartSublist, then call Tcl_DStringAppendElement for
100 each of the elements in the sublist, then call Tcl_DStringEndSublist to
101 end the sublist. Tcl_DStringStartSublist appends a space character if
102 needed, followed by an open brace; Tcl_DStringEndSublist appends a
103 close brace. Lists can be nested to any depth.
104
105 Tcl_DStringLength is a macro that returns the current length of a
106 dynamic string (not including the terminating null character).
107 Tcl_DStringValue is a macro that returns a pointer to the current con‐
108 tents of a dynamic string.
109
110 Tcl_DStringSetLength changes the length of a dynamic string. If
111 newLength is less than the string's current length, then the string is
112 truncated. If newLength is greater than the string's current length,
113 then the string will become longer and new space will be allocated for
114 the string if needed. However, Tcl_DStringSetLength will not initial‐
115 ize the new space except to provide a terminating null character; it
116 is up to the caller to fill in the new space. Tcl_DStringSetLength
117 does not free up the string's storage space even if the string is trun‐
118 cated to zero length, so Tcl_DStringFree will still need to be called.
119
120 Tcl_DStringTrunc changes the length of a dynamic string. This proce‐
121 dure is now deprecated. Tcl_DStringSetLength should be used instead.
122
123 Tcl_DStringFree should be called when you're finished using the string.
124 It frees up any memory that was allocated for the string and reinitial‐
125 izes the string's value to an empty string.
126
127 Tcl_DStringResult sets the result of interp to the value of the dynamic
128 string given by dsPtr. It does this by moving a pointer from dsPtr to
129 the interpreter's result. This saves the cost of allocating new memory
130 and copying the string. Tcl_DStringResult also reinitializes the
131 dynamic string to an empty string.
132
133 Tcl_DStringGetResult does the opposite of Tcl_DStringResult. It sets
134 the value of dsPtr to the result of interp and it clears interp's
135 result. If possible it does this by moving a pointer rather than by
136 copying the string.
137
138
140 append, dynamic string, free, result
141
142
143
144Tcl 7.4 Tcl_DString(3)