docs
[platform/upstream/glib.git] / docs / reference / glib / tmpl / strings.sgml
1 <!-- ##### SECTION Title ##### -->
2 Strings
3
4 <!-- ##### SECTION Short_Description ##### -->
5 text buffers which grow automatically as text is added.
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 A #GString is similar to a standard C string, except that it grows automatically
10 as text is appended or inserted. Also, it stores the length of the string, so
11 can be used for binary data with embedded nul bytes.
12 </para>
13
14 <!-- ##### SECTION See_Also ##### -->
15 <para>
16
17 </para>
18
19 <!-- ##### STRUCT GString ##### -->
20 <para>
21 The #GString struct contains the public fields of a #GString.
22 The <structfield>str</structfield> field points to the character data.
23 It may move as text is added.
24 The <structfield>len</structfield> field contains the length of the string,
25 not including the terminating null character.
26 </para>
27 <para>
28 The str field is zero-terminated and so can be used as an ordinary C
29 string. But it may be moved when text is appended or inserted into the
30 string.
31 </para>
32
33 @str: 
34 @len: 
35
36 <!-- ##### FUNCTION g_string_new ##### -->
37 <para>
38 Creates a new #GString, initialized with the given string.
39 </para>
40
41 @init: the initial text to copy into the string.
42 @Returns: the new #GString.
43
44
45 <!-- ##### FUNCTION g_string_new_len ##### -->
46 <para>
47 Creates a new #GString with @len bytes of the @init buffer.  Because a length is
48 provided, @init need not be nul-terminated, and can contain embedded nul bytes.
49 </para>
50
51 @init: initial contents of string
52 @len: length of @init to use
53 @Returns: a new #GString
54
55
56 <!-- ##### FUNCTION g_string_sized_new ##### -->
57 <para>
58 Creates a new GString, with enough space for @dfl_size characters.
59 This is useful if you are going to add a lot of text to the string and
60 don't want it to be reallocated too often.
61 </para>
62
63 @dfl_size: the default size of the space allocated to hold the string.
64 @Returns: the new #GString.
65
66
67 <!-- ##### FUNCTION g_string_assign ##### -->
68 <para>
69 Copies the characters from one #GString into another, destroying any previous
70 contents. It is rather like the standard strcpy() function, except that
71 you do not have to worry about having enough space to copy the string.
72 </para>
73
74 @string: the destination #GString. Its current contents are destroyed.
75 @rval: the source #GString.
76 @Returns: the destination #GString.
77
78
79 <!-- ##### MACRO g_string_sprintf ##### -->
80 <para>
81 Writes a formatted string into a #GString.
82 This is similar to the standard <function>sprintf()</function> function,
83 except that the GString buffer automatically expands to contain the results.
84 The previous contents of the GString are destroyed.
85 </para>
86
87 <!-- # Unused Parameters # -->
88 @string: a #GString.
89 @format: the string format. See the <function>sprintf()</function>
90 documentation.
91 @Varargs: the parameters to insert into the format string.
92
93
94 <!-- ##### MACRO g_string_sprintfa ##### -->
95 <para>
96 Appends a formatted string onto the end of a #GString.
97 This function is is similar to g_string_sprintf() except that
98 the text is appended to the GString.
99 </para>
100
101 <!-- # Unused Parameters # -->
102 @string: a #GString.
103 @format: the string format. See the <function>sprintf()</function>
104 documentation.
105 @Varargs: the parameters to insert into the format string.
106
107
108 <!-- ##### FUNCTION g_string_append ##### -->
109 <para>
110 Adds a string onto the end of a #GString, expanding it if necessary.
111 </para>
112
113 @string: a #GString.
114 @val: the string to append onto the end of the #GString.
115 @Returns: the #GString.
116
117
118 <!-- ##### FUNCTION g_string_append_c ##### -->
119 <para>
120 Adds a character onto the end of a #GString, expanding it if necessary.
121 </para>
122
123 @string: a #GString.
124 @c: the character to append onto the end of the #GString.
125 @Returns: the #GString.
126
127
128 <!-- ##### FUNCTION g_string_append_len ##### -->
129 <para>
130 Appends @len bytes of @val to @string. Because @len is provided, 
131 @val may contain embedded nuls and need not be nul-terminated.
132 </para>
133
134 @string: a #GString
135 @val: bytes to append
136 @len: number of bytes of @val to use
137 @Returns: the #GString
138
139
140 <!-- ##### FUNCTION g_string_prepend ##### -->
141 <para>
142 Adds a string on to the start of a #GString, expanding it if necessary.
143 </para>
144
145 @string: a #GString.
146 @val: the string to prepend on the start of the #GString.
147 @Returns: the #GString.
148
149
150 <!-- ##### FUNCTION g_string_prepend_c ##### -->
151 <para>
152 Adds a character onto the start of a #GString, expanding it if necessary.
153 </para>
154
155 @string: a #GString.
156 @c: the character to prepend on the start of the #GString.
157 @Returns: the #GString.
158
159
160 <!-- ##### FUNCTION g_string_prepend_len ##### -->
161 <para>
162 Prepends @len bytes of @val to @string. Because @len is provided, 
163 @val may contain embedded nuls and need not be nul-terminated.
164 </para>
165
166 @string: a #GString
167 @val: bytes to prepend
168 @len: number of bytes in @val to prepend
169 @Returns: the #GString passed in
170
171
172 <!-- ##### FUNCTION g_string_insert ##### -->
173 <para>
174 Inserts a copy of a string into a #GString, expanding it if necessary.
175 </para>
176
177 @string: a #GString.
178 @pos: the position to insert the copy of the string.
179 @val: the string to insert.
180 @Returns: the #GString.
181
182
183 <!-- ##### FUNCTION g_string_insert_c ##### -->
184 <para>
185 Inserts a character into a #GString, expanding it if necessary.
186 </para>
187
188 @string: a #GString.
189 @pos: the position to insert the character.
190 @c: the character to insert.
191 @Returns: the #GString.
192
193
194 <!-- ##### FUNCTION g_string_insert_len ##### -->
195 <para>
196 Inserts @len bytes of @val into @string at @pos.  Because @len is provided, @val
197  may contain embedded nuls and need not be nul-terminated. If @pos is -1, bytes are inserted at the end of the string.
198 </para>
199
200 @string: a #GString
201 @pos: position in @string where insertion should happen, or -1 for at the end
202 @val: bytes to insert
203 @len: number of bytes of @val to insert
204 @Returns: the #GString
205
206
207 <!-- ##### FUNCTION g_string_erase ##### -->
208 <para>
209 Removes @len characters from a #GString, starting at position @pos.
210 The rest of the #GString is shifted down to fill the gap.
211 </para>
212
213 @string: a #GString.
214 @pos: the position of the characters to remove.
215 @len: the number of characters to remove.
216 @Returns: the #GString.
217
218
219 <!-- ##### FUNCTION g_string_truncate ##### -->
220 <para>
221 Cuts off the end of the GString, leaving the first @len characters.
222 </para>
223
224 @string: a #GString.
225 @len: the new size of the #GString.
226 @Returns: the #GString.
227
228
229 <!-- ##### FUNCTION g_string_free ##### -->
230 <para>
231 Frees the memory allocated for the #GString.
232 If free_segment is TRUE it also frees the character data.
233 </para>
234
235 @string: a #GString.
236 @free_segment: if TRUE the actual character data is freed as well.
237 @Returns: 
238
239
240 <!-- ##### FUNCTION g_string_up ##### -->
241 <para>
242 Converts a #GString to upper case.
243 </para>
244
245 @string: a #GString.
246 @Returns: the #GString.
247
248
249 <!-- ##### FUNCTION g_string_down ##### -->
250 <para>
251 Converts a #GString to lower case.
252 </para>
253
254 @string: a #GString.
255 @Returns: the #GString.
256
257
258 <!-- ##### FUNCTION g_string_hash ##### -->
259 <para>
260 Creates a hash code for @str; for use with #GHashTable.
261 </para>
262
263 @str: a string to hash
264 @Returns: hash code for @str
265
266
267 <!-- ##### FUNCTION g_string_equal ##### -->
268 <para>
269 Compares two strings for equality, returning %TRUE if they are equal. 
270 For use with #GHashTable.
271 </para>
272
273 @v: a #GString
274 @v2: another #GString
275 @Returns: %TRUE if they strings are the same length and contain the same bytes
276
277