GLib.String¶
Fields¶
Name |
Type |
Access |
Description |
---|---|---|---|
allocated_len |
r/w |
the number of bytes that can be stored in the string before it needs to be reallocated. May be larger than len. |
|
len |
r/w |
contains the length of the string, not including the terminating nul byte. |
|
str |
r/w |
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. |
Methods¶
class |
|
class |
|
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Details¶
- class GLib.String¶
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 aGString
is guaranteed to have a trailing nul character, and it is therefore always safe to call functions such asstrchr()
orstrdup()
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 aGByteArray
with the addition of many convenience methods for text, and a guaranteed nul terminator.- classmethod new(init)[source]¶
- Parameters:
init (
str
orNone
) – the initial text to copy into the string, orNone
to start with an empty string- Returns:
the new
GLib.String
- Return type:
Creates a new
GLib.String
, initialized with the given string.
- classmethod new_len(init, len)[source]¶
- Parameters:
- Returns:
a new
GLib.String
- Return type:
Creates a new
GLib.String
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.
- classmethod new_take(init)[source]¶
- Parameters:
init (
str
orNone
) – initial text used as the string. Ownership of the string is transferred to theGLib.String
. PassingNone
creates an empty string.- Returns:
the new
GLib.String
- Return type:
Creates a new
GLib.String
, initialized with the given string.After this call, init belongs to the
GLib.String
and may no longer be modified by the caller. The memory of data has to be dynamically allocated and will eventually be freed withGLib.free
().New in version 2.78.
- classmethod sized_new(dfl_size)[source]¶
- Parameters:
dfl_size (
int
) – the default size of the space allocated to hold the string- Returns:
the new
GLib.String
- Return type:
Creates a new
GLib.String
, 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.
- append(val)[source]¶
- Parameters:
val (
str
) – the string to append onto the end of self- Returns:
self
- Return type:
Adds a string onto the end of a
GLib.String
, expanding it if necessary.
- append_c(c)[source]¶
- Parameters:
c (
int
) – the byte to append onto the end of self- Returns:
self
- Return type:
Adds a byte onto the end of a
GLib.String
, expanding it if necessary.
- append_len(val, len)[source]¶
- Parameters:
- Returns:
self
- Return type:
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
GLib.String.append_len
() equivalent toGLib.String.append
().
- append_unichar(wc)[source]¶
- Parameters:
wc (
str
) – a Unicode character- Returns:
self
- Return type:
Converts a Unicode character into UTF-8, and appends it to the string.
- append_uri_escaped(unescaped, reserved_chars_allowed, allow_utf8)[source]¶
- Parameters:
- Returns:
self
- Return type:
Appends unescaped to self, escaping any characters that are reserved in URIs using URI-style escape sequences.
New in version 2.16.
- ascii_down()[source]¶
- Returns:
passed-in self pointer, with all the uppercase characters converted to lowercase in place, with semantics that exactly match
GLib.ascii_tolower
().- Return type:
Converts all uppercase ASCII letters to lowercase ASCII letters.
- ascii_up()[source]¶
- Returns:
passed-in self pointer, with all the lowercase characters converted to uppercase in place, with semantics that exactly match
GLib.ascii_toupper
().- Return type:
Converts all lowercase ASCII letters to uppercase ASCII letters.
- assign(rval)[source]¶
- Parameters:
rval (
str
) – the string to copy into self- Returns:
self
- Return type:
Copies the bytes from a string into a
GLib.String
, 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.
- down()[source]¶
- Returns:
the
GLib.String
- Return type:
Converts a
GLib.String
to lowercase.Deprecated since version 2.2: This function uses the locale-specific tolower() function, which is almost never the right thing. Use
GLib.String.ascii_down
() orGLib.utf8_strdown
() instead.
- equal(v2)[source]¶
- Parameters:
v2 (
GLib.String
) – anotherGLib.String
- Returns:
True
if the strings are the same length and contain the same bytes- Return type:
Compares two strings for equality, returning
True
if they are equal. For use withGLib.HashTable
.
- erase(pos, len)[source]¶
- Parameters:
- Returns:
self
- Return type:
Removes len bytes from a
GLib.String
, starting at position pos. The rest of theGLib.String
is shifted down to fill the gap.
- free(free_segment)[source]¶
- Parameters:
free_segment (
bool
) – ifTrue
, the actual character data is freed as well- Returns:
the character data of self (i.e.
None
if free_segment isTrue
)- Return type:
Frees the memory allocated for the
GLib.String
. If free_segment isTrue
it also frees the character data. If it’sFalse
, the caller gains ownership of the buffer and must free it after use withGLib.free
().Instead of passing
False
to this function, consider usingGLib.String.free_and_steal
().
- free_and_steal()[source]¶
- Returns:
the character data of self
- Return type:
Frees the memory allocated for the
GLib.String
.The caller gains ownership of the buffer and must free it after use with
GLib.free
().New in version 2.76.
- free_to_bytes()[source]¶
- Returns:
A newly allocated
GLib.Bytes
containing contents of self; self itself is freed- Return type:
Transfers ownership of the contents of self to a newly allocated
GLib.Bytes
. TheGLib.String
structure itself is deallocated, and it is therefore invalid to use self after invoking this function.Note that while
GLib.String
ensures that its buffer always has a trailing nul character (not reflected in its “len”), the returnedGLib.Bytes
does not include this extra nul; i.e. it has length exactly equal to the “len” member.New in version 2.34.
- hash()[source]¶
- Returns:
hash code for self
- Return type:
Creates a hash code for self; for use with
GLib.HashTable
.
- insert(pos, val)[source]¶
- Parameters:
- Returns:
self
- Return type:
Inserts a copy of a string into a
GLib.String
, expanding it if necessary.
- insert_c(pos, c)[source]¶
- Parameters:
- Returns:
self
- Return type:
Inserts a byte into a
GLib.String
, expanding it if necessary.
- insert_len(pos, val, len)[source]¶
- Parameters:
- Returns:
self
- Return type:
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.
- insert_unichar(pos, wc)[source]¶
- Parameters:
- Returns:
self
- Return type:
Converts a Unicode character into UTF-8, and insert it into the string at the given position.
- overwrite(pos, val)[source]¶
- Parameters:
- Returns:
self
- Return type:
Overwrites part of a string, lengthening it if necessary.
New in version 2.14.
- overwrite_len(pos, val, len)[source]¶
- Parameters:
- Returns:
self
- Return type:
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.
New in version 2.14.
- prepend(val)[source]¶
- Parameters:
val (
str
) – the string to prepend on the start of self- Returns:
self
- Return type:
Adds a string on to the start of a
GLib.String
, expanding it if necessary.
- prepend_c(c)[source]¶
- Parameters:
c (
int
) – the byte to prepend on the start of theGLib.String
- Returns:
self
- Return type:
Adds a byte onto the start of a
GLib.String
, expanding it if necessary.
- prepend_len(val, len)[source]¶
- Parameters:
- Returns:
self
- Return type:
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
GLib.String.prepend_len
() equivalent toGLib.String.prepend
().
- prepend_unichar(wc)[source]¶
- Parameters:
wc (
str
) – a Unicode character- Returns:
self
- Return type:
Converts a Unicode character into UTF-8, and prepends it to the string.
- replace(find, replace, limit)[source]¶
- Parameters:
- Returns:
the number of find and replace operations performed.
- Return type:
Replaces the string find with the string replace in a
GLib.String
up to limit times. If the number of instances of find in theGLib.String
is less than limit, all instances are replaced. If limit is0
, 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.
New in version 2.68.
- set_size(len)[source]¶
- Parameters:
len (
int
) – the new length- Returns:
self
- Return type:
Sets the length of a
GLib.String
. 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.)
- truncate(len)[source]¶
- Parameters:
len (
int
) – the new size of self- Returns:
self
- Return type:
Cuts off the end of the
GLib.String
, leaving the first len bytes.
- up()[source]¶
- Returns:
self
- Return type:
Converts a
GLib.String
to uppercase.Deprecated since version 2.2: This function uses the locale-specific toupper() function, which is almost never the right thing. Use
GLib.String.ascii_up
() orGLib.utf8_strup
() instead.