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.
strpoints 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.
lencontains the length of the string, not including the terminating nul byte.
allocated-lenthe number of bytes that can be stored in the string before it needs to be reallocated. May be larger than len.
Adds a string onto the end of a <GString>,
expanding it if necessary.
Adds a byte onto the end of a <GString>, expanding
it if necessary.
Appends len bytes of val to self.
If len is positive, val may contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure that val has at least len addressable bytes.
If len is negative, val must be nul-terminated and len
is considered to request the entire string length. This makes
string:append-len equivalent to
string:append.
Converts a Unicode character into UTF-8, and appends it to the string.
Appends unescaped to self, escaping any characters that are reserved in URIs using URI-style escape sequences.
Converts all uppercase ASCII letters to lowercase ASCII letters.
Passed: a <GString>.
a GString
Returned: a <GString>.
passed-in self pointer, with all the uppercase characters
converted to lowercase in place, with semantics that exactly match
ascii-tolower.
Converts all lowercase ASCII letters to uppercase ASCII letters.
Passed: a <GString>.
a GString
Returned: a <GString>.
passed-in self pointer, with all the lowercase characters
converted to uppercase in place, with semantics that exactly match
ascii-toupper.
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.
Copies the <GString> instance and its contents.
This will preserve the allocation length of the
<GString> in the copy.
Converts a <GString> to lowercase.
Compares two strings for equality, returning #t if they are
equal. For use with <GHashTable>.
Removes len bytes from a <GString>, starting
at position pos. The rest of the <GString>
is shifted down to fill the gap.
Frees the memory allocated for the <GString>. If
free-segment is #t it also frees the character data. If
it’s #f, the caller gains ownership of the buffer and must free
it after use with byte-array:free.
Instead of passing #f to this function, consider using
string:free-and-steal.
Frees the memory allocated for the <GString>.
The caller gains ownership of the buffer and must free it after use with
byte-array:free.
Transfers ownership of the contents of self to a newly allocated
<GBytes>. The <GString>
structure itself is deallocated, and it is therefore invalid to use
self after invoking this function.
Note that while <GString> ensures that its buffer
always has a trailing nul character (not reflected in its "len"), the
returned <GBytes> does not include this extra nul;
i.e. it has length exactly equal to the "len" member.
Creates a hash code for self; for use with
<GHashTable>.
Passed: a <GString>.
a string to hash
Returned: an unsigned 32-bit integer.
hash code for self
Inserts a copy of a string into a <GString>,
expanding it if necessary.
Inserts a byte into a <GString>, expanding it if
necessary.
Inserts len bytes of val into self at pos.
If len is positive, val may contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure that val has at least len addressable bytes.
If len is negative, val must be nul-terminated and len is considered to request the entire string length.
If pos is -1, bytes are inserted at the end of the string.
Converts a Unicode character into UTF-8, and insert it into the string at the given position.
Creates a new <GString>, initialized with the given
string.
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.
Since this function does not stop at nul bytes, it is the caller’s responsibility to ensure that init has at least len addressable bytes.
Creates a new <GString>, initialized with the given
string.
After this call, init belongs to the
<GString> and may no longer be modified by the
caller. The memory of init has to be dynamically allocated and
will eventually be freed with byte-array:free.
Overwrites part of a string, lengthening it if necessary.
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.
Adds a string on to the start of a <GString>,
expanding it if necessary.
Adds a byte onto the start of a <GString>,
expanding it if necessary.
Prepends len bytes of val to self.
If len is positive, val may contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure that val has at least len addressable bytes.
If len is negative, val must be nul-terminated and len
is considered to request the entire string length. This makes
string:prepend-len equivalent to
string:prepend.
Converts a Unicode character into UTF-8, and prepends it to the string.
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.
If find is the empty string, since versions 2.69.1 and 2.68.4 the replacement will be inserted no more than once per possible position (beginning of string, end of string and between characters). This did not work correctly in earlier versions.
If limit is zero and more than G_MAXUINT instances of
find are in the input string, they will all be replaced, but the
return value will be capped at G_MAXUINT.
Passed: a <GString>.
Passed: a string.
the string to find in self
Passed: a string.
the string to insert in place of find
Passed: an unsigned 32-bit integer.
the maximum instances of find to replace with replace, or
0 for no limit
Returned: an unsigned 32-bit integer.
the number of find and replace operations performed, up to
G_MAXUINT
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.)
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.
Cuts off the end of the GString, leaving the first len bytes.