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

  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_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)
  set_size (len)
  truncate (len)
  up ()

Details

class GLib.String

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

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().

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.

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.