GLib.String

Fields

Name

Type

Access

Description

allocated_len

int

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

int

r/w

contains the length of the string, not including the terminating nul byte.

str

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

new (init)

class

new_len (init, len)

class

new_take (init)

class

sized_new (dfl_size)

append (val)

append_c (c)

append_len (val, len)

append_unichar (wc)

append_uri_escaped (unescaped, reserved_chars_allowed, allow_utf8)

ascii_down ()

ascii_up ()

assign (rval)

down ()

equal (v2)

erase (pos, len)

free (free_segment)

free_and_steal ()

free_to_bytes ()

hash ()

insert (pos, val)

insert_c (pos, c)

insert_len (pos, val, len)

insert_unichar (pos, wc)

overwrite (pos, val)

overwrite_len (pos, val, len)

prepend (val)

prepend_c (c)

prepend_len (val, len)

prepend_unichar (wc)

replace (find, replace, limit)

set_size (len)

truncate (len)

up ()

Details

class GLib.String

The GLib.String struct contains the public fields of a GLib.String.

classmethod new(init)[source]
Parameters:

init (str or None) – the initial text to copy into the string, or None to start with an empty string

Returns:

the new GLib.String

Return type:

GLib.String

Creates a new GLib.String, initialized with the given string.

classmethod new_len(init, len)[source]
Parameters:
  • init (str) – initial contents of the string

  • len (int) – length of init to use

Returns:

a new GLib.String

Return type:

GLib.String

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 or None) – initial text used as the string. Ownership of the string is transferred to the GLib.String. Passing None creates an empty string.

Returns:

the new GLib.String

Return type:

GLib.String

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 with GLib.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:

GLib.String

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:

GLib.String

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:

GLib.String

Adds a byte onto the end of a GLib.String, expanding it if necessary.

append_len(val, len)[source]
Parameters:
  • val (str) – bytes to append

  • len (int) – number of bytes of val to use, or -1 for all of val

Returns:

self

Return type:

GLib.String

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 to GLib.String.append().

append_unichar(wc)[source]
Parameters:

wc (str) – a Unicode character

Returns:

self

Return type:

GLib.String

Converts a Unicode character into UTF-8, and appends it to the string.

append_uri_escaped(unescaped, reserved_chars_allowed, allow_utf8)[source]
Parameters:
  • unescaped (str) – a string

  • reserved_chars_allowed (str) – a string of reserved characters allowed to be used, or None

  • allow_utf8 (bool) – set True if the escaped string may include UTF8 characters

Returns:

self

Return type:

GLib.String

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:

GLib.String

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:

GLib.String

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:

GLib.String

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:

GLib.String

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() or GLib.utf8_strdown() instead.

equal(v2)[source]
Parameters:

v2 (GLib.String) – another GLib.String

Returns:

True if the strings are the same length and contain the same bytes

Return type:

bool

Compares two strings for equality, returning True if they are equal. For use with GLib.HashTable.

erase(pos, len)[source]
Parameters:
  • pos (int) – the position of the content to remove

  • len (int) – the number of bytes to remove, or -1 to remove all following bytes

Returns:

self

Return type:

GLib.String

Removes len bytes from a GLib.String, starting at position pos. The rest of the GLib.String is shifted down to fill the gap.

free(free_segment)[source]
Parameters:

free_segment (bool) – if True, the actual character data is freed as well

Returns:

the character data of self (i.e. None if free_segment is True)

Return type:

str or None

Frees the memory allocated for the GLib.String. 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 GLib.free().

Instead of passing False to this function, consider using GLib.String.free_and_steal().

free_and_steal()[source]
Returns:

the character data of self

Return type:

str

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:

GLib.Bytes

Transfers ownership of the contents of self to a newly allocated GLib.Bytes. The GLib.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 returned GLib.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:

int

Creates a hash code for self; for use with GLib.HashTable.

insert(pos, val)[source]
Parameters:
  • pos (int) – the position to insert the copy of the string

  • val (str) – the string to insert

Returns:

self

Return type:

GLib.String

Inserts a copy of a string into a GLib.String, expanding it if necessary.

insert_c(pos, c)[source]
Parameters:
  • pos (int) – the position to insert the byte

  • c (int) – the byte to insert

Returns:

self

Return type:

GLib.String

Inserts a byte into a GLib.String, expanding it if necessary.

insert_len(pos, val, len)[source]
Parameters:
  • pos (int) – position in self where insertion should happen, or -1 for at the end

  • val (str) – bytes to insert

  • len (int) – number of bytes of val to insert, or -1 for all of val

Returns:

self

Return type:

GLib.String

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:
  • pos (int) – the position at which to insert character, or -1 to append at the end of the string

  • wc (str) – a Unicode character

Returns:

self

Return type:

GLib.String

Converts a Unicode character into UTF-8, and insert it into the string at the given position.

overwrite(pos, val)[source]
Parameters:
  • pos (int) – the position at which to start overwriting

  • val (str) – the string that will overwrite the self starting at pos

Returns:

self

Return type:

GLib.String

Overwrites part of a string, lengthening it if necessary.

New in version 2.14.

overwrite_len(pos, val, len)[source]
Parameters:
  • pos (int) – the position at which to start overwriting

  • val (str) – the string that will overwrite the self starting at pos

  • len (int) – the number of bytes to write from val

Returns:

self

Return type:

GLib.String

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:

GLib.String

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 the GLib.String

Returns:

self

Return type:

GLib.String

Adds a byte onto the start of a GLib.String, expanding it if necessary.

prepend_len(val, len)[source]
Parameters:
  • val (str) – bytes to prepend

  • len (int) – number of bytes in val to prepend, or -1 for all of val

Returns:

self

Return type:

GLib.String

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 to GLib.String.prepend().

prepend_unichar(wc)[source]
Parameters:

wc (str) – a Unicode character

Returns:

self

Return type:

GLib.String

Converts a Unicode character into UTF-8, and prepends it to the string.

replace(find, replace, limit)[source]
Parameters:
  • find (str) – the string to find in self

  • replace (str) – the string to insert in place of find

  • limit (int) – the maximum instances of find to replace with replace, or 0 for no limit

Returns:

the number of find and replace operations performed.

Return type:

int

Replaces the string find with the string replace in a GLib.String up to limit times. If the number of instances of find in the GLib.String 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.

New in version 2.68.

set_size(len)[source]
Parameters:

len (int) – the new length

Returns:

self

Return type:

GLib.String

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:

GLib.String

Cuts off the end of the GLib.String, leaving the first len bytes.

up()[source]
Returns:

self

Return type:

GLib.String

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() or GLib.utf8_strup() instead.