1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 text buffers which grow automatically as text is added.
7 <!-- ##### SECTION Long_Description ##### -->
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.
14 <!-- ##### SECTION See_Also ##### -->
19 <!-- ##### STRUCT GString ##### -->
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.
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
36 <!-- ##### FUNCTION g_string_new ##### -->
38 Creates a new #GString, initialized with the given string.
41 @init: the initial text to copy into the string.
42 @Returns: the new #GString.
45 <!-- ##### FUNCTION g_string_new_len ##### -->
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.
51 @init: initial contents of string
52 @len: length of @init to use
53 @Returns: a new #GString
56 <!-- ##### FUNCTION g_string_sized_new ##### -->
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.
63 @dfl_size: the default size of the space allocated to hold the string.
64 @Returns: the new #GString.
67 <!-- ##### FUNCTION g_string_assign ##### -->
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.
74 @string: the destination #GString. Its current contents are destroyed.
75 @rval: the source #GString.
76 @Returns: the destination #GString.
79 <!-- ##### MACRO g_string_sprintf ##### -->
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.
87 <!-- # Unused Parameters # -->
89 @format: the string format. See the <function>sprintf()</function>
91 @Varargs: the parameters to insert into the format string.
94 <!-- ##### MACRO g_string_sprintfa ##### -->
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.
101 <!-- # Unused Parameters # -->
103 @format: the string format. See the <function>sprintf()</function>
105 @Varargs: the parameters to insert into the format string.
108 <!-- ##### FUNCTION g_string_printf ##### -->
118 <!-- ##### FUNCTION g_string_printfa ##### -->
128 <!-- ##### FUNCTION g_string_append ##### -->
130 Adds a string onto the end of a #GString, expanding it if necessary.
134 @val: the string to append onto the end of the #GString.
135 @Returns: the #GString.
138 <!-- ##### FUNCTION g_string_append_c ##### -->
140 Adds a character onto the end of a #GString, expanding it if necessary.
144 @c: the character to append onto the end of the #GString.
145 @Returns: the #GString.
148 <!-- ##### FUNCTION g_string_append_len ##### -->
150 Appends @len bytes of @val to @string. Because @len is provided,
151 @val may contain embedded nuls and need not be nul-terminated.
155 @val: bytes to append
156 @len: number of bytes of @val to use
157 @Returns: the #GString
160 <!-- ##### FUNCTION g_string_prepend ##### -->
162 Adds a string on to the start of a #GString, expanding it if necessary.
166 @val: the string to prepend on the start of the #GString.
167 @Returns: the #GString.
170 <!-- ##### FUNCTION g_string_prepend_c ##### -->
172 Adds a character onto the start of a #GString, expanding it if necessary.
176 @c: the character to prepend on the start of the #GString.
177 @Returns: the #GString.
180 <!-- ##### FUNCTION g_string_prepend_len ##### -->
182 Prepends @len bytes of @val to @string. Because @len is provided,
183 @val may contain embedded nuls and need not be nul-terminated.
187 @val: bytes to prepend
188 @len: number of bytes in @val to prepend
189 @Returns: the #GString passed in
192 <!-- ##### FUNCTION g_string_insert ##### -->
194 Inserts a copy of a string into a #GString, expanding it if necessary.
198 @pos: the position to insert the copy of the string.
199 @val: the string to insert.
200 @Returns: the #GString.
203 <!-- ##### FUNCTION g_string_insert_c ##### -->
205 Inserts a character into a #GString, expanding it if necessary.
209 @pos: the position to insert the character.
210 @c: the character to insert.
211 @Returns: the #GString.
214 <!-- ##### FUNCTION g_string_insert_len ##### -->
216 Inserts @len bytes of @val into @string at @pos. Because @len is provided, @val
217 may contain embedded nuls and need not be nul-terminated. If @pos is -1, bytes are inserted at the end of the string.
221 @pos: position in @string where insertion should happen, or -1 for at the end
222 @val: bytes to insert
223 @len: number of bytes of @val to insert
224 @Returns: the #GString
227 <!-- ##### FUNCTION g_string_erase ##### -->
229 Removes @len characters from a #GString, starting at position @pos.
230 The rest of the #GString is shifted down to fill the gap.
234 @pos: the position of the characters to remove.
235 @len: the number of characters to remove, or -1 to remove all
236 following characters.
237 @Returns: the #GString.
240 <!-- ##### FUNCTION g_string_truncate ##### -->
242 Cuts off the end of the GString, leaving the first @len characters.
246 @len: the new size of the #GString.
247 @Returns: the #GString.
250 <!-- ##### FUNCTION g_string_free ##### -->
252 Frees the memory allocated for the #GString.
253 If free_segment is TRUE it also frees the character data.
257 @free_segment: if TRUE the actual character data is freed as well.
261 <!-- ##### FUNCTION g_string_up ##### -->
263 Converts a #GString to upper case.
267 @Returns: the #GString.
270 <!-- ##### FUNCTION g_string_down ##### -->
272 Converts a #GString to lower case.
276 @Returns: the #GString.
279 <!-- ##### FUNCTION g_string_hash ##### -->
281 Creates a hash code for @str; for use with #GHashTable.
284 @str: a string to hash
285 @Returns: hash code for @str
288 <!-- ##### FUNCTION g_string_equal ##### -->
290 Compares two strings for equality, returning %TRUE if they are equal.
291 For use with #GHashTable.
295 @v2: another #GString
296 @Returns: %TRUE if they strings are the same length and contain the same bytes