Skip to content

GLib.String

record (struct)

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.

Constructors

new

@classmethod
def new(cls, init: str | None = ...) -> String

Creates a new String, initialized with the given string.

Parameters:

  • init — the initial text to copy into the string, or None to start with an empty string

new_len

@classmethod
def new_len(cls, init: str, len: int) -> String

Creates a new 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.

Parameters:

  • init — initial contents of the string
  • len — length of init to use

new_take

@classmethod
def new_take(cls, init: str | None = ...) -> String

Creates a new String, initialized with the given string.

After this call, init belongs to the String and may no longer be modified by the caller. The memory of init has to be dynamically allocated and will eventually be freed with free.

Parameters:

  • init — initial text used as the string. Ownership of the string is transferred to the String. Passing None creates an empty string.

sized_new

@classmethod
def sized_new(cls, dfl_size: int) -> String

Creates a new 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.

Parameters:

  • dfl_size — the default size of the space allocated to hold the string

Methods

append

def append(self, val: str) -> String

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

Parameters:

  • val — the string to append onto the end of string

append_c

def append_c(self, c: int) -> String

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

Parameters:

  • c — the byte to append onto the end of string

append_len

def append_len(self, val: str, len: int) -> String

Appends len bytes of val to string.

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.

Parameters:

  • val — bytes to append
  • len — number of bytes of val to use, or -1 for all of val

append_unichar

def append_unichar(self, wc: str) -> String

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

Parameters:

  • wc — a Unicode character

append_uri_escaped

def append_uri_escaped(self, unescaped: str, reserved_chars_allowed: str, allow_utf8: bool) -> String

Appends unescaped to string, escaping any characters that are reserved in URIs using URI-style escape sequences.

Parameters:

  • unescaped — a string
  • reserved_chars_allowed — a string of reserved characters allowed to be used, or None
  • allow_utf8 — set True if the escaped string may include UTF8 characters

ascii_down

def ascii_down(self) -> String

Converts all uppercase ASCII letters to lowercase ASCII letters.

ascii_up

def ascii_up(self) -> String

Converts all lowercase ASCII letters to uppercase ASCII letters.

assign

def assign(self, rval: str) -> String

Copies the bytes from a string into a 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.

Parameters:

  • rval — the string to copy into string

copy

def copy(self) -> String

Copies the String instance and its contents.

This will preserve the allocation length of the String in the copy.

down

def down(self) -> String

:::warning Deprecated since 2.2 This API is deprecated. :::

Converts a String to lowercase.

equal

def equal(self, v2: String) -> bool

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

Parameters:

erase

def erase(self, pos: int, len: int) -> String

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

Parameters:

  • pos — the position of the content to remove
  • len — the number of bytes to remove, or -1 to remove all following bytes

free

def free(self, free_segment: bool) -> str | None

Frees the memory allocated for the 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 free.

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

Parameters:

  • free_segment — if True, the actual character data is freed as well

free_and_steal

def free_and_steal(self) -> str

Frees the memory allocated for the String.

The caller gains ownership of the buffer and must free it after use with free.

free_to_bytes

def free_to_bytes(self) -> Bytes

Transfers ownership of the contents of string to a newly allocated Bytes. The String structure itself is deallocated, and it is therefore invalid to use string after invoking this function.

Note that while String ensures that its buffer always has a trailing nul character (not reflected in its "len"), the returned Bytes does not include this extra nul; i.e. it has length exactly equal to the "len" member.

hash

def hash(self) -> int

Creates a hash code for str; for use with HashTable.

insert

def insert(self, pos: int, val: str) -> String

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

Parameters:

  • pos — the position to insert the copy of the string
  • val — the string to insert

insert_c

def insert_c(self, pos: int, c: int) -> String

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

Parameters:

  • pos — the position to insert the byte
  • c — the byte to insert

insert_len

def insert_len(self, pos: int, val: str, len: int) -> String

Inserts len bytes of val into string 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.

Parameters:

  • pos — position in string where insertion should happen, or -1 for at the end
  • val — bytes to insert
  • len — number of bytes of val to insert, or -1 for all of val

insert_unichar

def insert_unichar(self, pos: int, wc: str) -> String

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

Parameters:

  • pos — the position at which to insert character, or -1 to append at the end of the string
  • wc — a Unicode character

overwrite

def overwrite(self, pos: int, val: str) -> String

Overwrites part of a string, lengthening it if necessary.

Parameters:

  • pos — the position at which to start overwriting
  • val — the string that will overwrite the string starting at pos

overwrite_len

def overwrite_len(self, pos: int, val: str, len: int) -> String

Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.

Parameters:

  • pos — the position at which to start overwriting
  • val — the string that will overwrite the string starting at pos
  • len — the number of bytes to write from val

prepend

def prepend(self, val: str) -> String

Adds a string on to the start of a String, expanding it if necessary.

Parameters:

  • val — the string to prepend on the start of string

prepend_c

def prepend_c(self, c: int) -> String

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

Parameters:

  • c — the byte to prepend on the start of the String

prepend_len

def prepend_len(self, val: str, len: int) -> String

Prepends len bytes of val to string.

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.

Parameters:

  • val — bytes to prepend
  • len — number of bytes in val to prepend, or -1 for all of val

prepend_unichar

def prepend_unichar(self, wc: str) -> String

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

Parameters:

  • wc — a Unicode character

replace

def replace(self, find: str, replace: str, limit: int) -> int

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

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.

Parameters:

  • find — the string to find in string
  • replace — the string to insert in place of find
  • limit — the maximum instances of find to replace with replace, or 0 for no limit

set_size

def set_size(self, len: int) -> String

Sets the length of a 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.)

Parameters:

  • len — the new length

truncate

def truncate(self, len: int) -> String

Cuts off the end of the GString, leaving the first len bytes.

Parameters:

  • len — the new size of string

up

def up(self) -> String

:::warning Deprecated since 2.2 This API is deprecated. :::

Converts a String to uppercase.

Properties

str

str: str  # read/write

len

len: int  # read/write

allocated_len

allocated_len: int  # read/write