Struct
GLibString
Description [src]
struct GString {
gchar* str;
gsize len;
gsize allocated_len;
}
A GString
is an object that handles the memory management of a C string.
The emphasis of GString
is on text, typically UTF-8. Crucially, the “str” member
of a GString
is guaranteed to have a trailing nul character, and it is therefore
always safe to call functions such as strchr()
or strdup()
on it.
However, a GString
can also hold arbitrary binary data, because it has a “len” member,
which includes any possible embedded nul characters in the data. Conceptually then,
GString
is like a GByteArray
with the addition of many convenience methods for
text, and a guaranteed nul terminator.
Structure members
str
Points to the character data. It may move as text is added. The
str
field is null-terminated and so can be used as an ordinary C string.len
Contains the length of the string, not including the terminating nul byte.
allocated_len
The number of bytes that can be stored in the string before it needs to be reallocated. May be larger than
len
.
Constructors
g_string_new_len
Creates a new GString
with len
bytes of the init
buffer.
Because a length is provided, init
need not be nul-terminated,
and can contain embedded nul bytes.
g_string_sized_new
Creates a new GString
, with enough space for dfl_size
bytes. This is useful if you are going to add a lot of
text to the string and don’t want it to be reallocated
too often.
Instance methods
g_string_append_printf
Appends a formatted string onto the end of a GString
.
This function is similar to g_string_printf()
except
that the text is appended to the GString
.
g_string_append_uri_escaped
Appends unescaped
to string
, escaping any characters that
are reserved in URIs using URI-style escape sequences.
since: 2.16
g_string_append_vprintf
Appends a formatted string onto the end of a GString
.
This function is similar to g_string_append_printf()
except that the arguments to the format string are passed
as a va_list.
since: 2.14
g_string_assign
Copies the bytes from a string into a GString
,
destroying any previous contents. It is rather like
the standard strcpy()
function, except that you do not
have to worry about having enough space to copy the string.
g_string_equal
Compares two strings for equality, returning TRUE
if they are equal.
For use with GHashTable
.
g_string_erase
Removes len
bytes from a GString
, starting at position pos
.
The rest of the GString
is shifted down to fill the gap.
g_string_free
Frees the memory allocated for the GString
.
If free_segment
is TRUE
it also frees the character data. If
it’s FALSE
, the caller gains ownership of the buffer and must
free it after use with g_free().
g_string_free_to_bytes
Transfers ownership of the contents of string
to a newly allocated
GBytes
. The GString
structure itself is deallocated, and it is
therefore invalid to use string
after invoking this function.
since: 2.34
g_string_insert_unichar
Converts a Unicode character into UTF-8, and insert it into the string at the given position.
g_string_overwrite_len
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.
since: 2.14
g_string_printf
Writes a formatted string into a GString
.
This is similar to the standard sprintf()
function,
except that the GString
buffer automatically expands
to contain the results. The previous contents of the
GString
are destroyed.
g_string_replace
Replaces the string find
with the string replace
in a GString
up to
limit
times. If the number of instances of find
in the GString
is
less than limit
, all instances are replaced. If limit
is 0
,
all instances of find
are replaced.
since: 2.68
g_string_set_size
Sets the length of a GString
. If the length is less than
the current length, the string will be truncated. If the
length is greater than the current length, the contents
of the newly added area are undefined. (However, as
always, string->str[string->len] will be a nul byte.).
g_string_vprintf
Writes a formatted string into a GString
.
This function is similar to g_string_printf()
except that
the arguments to the format string are passed as a va_list.
since: 2.14