Class (GI Struct)

GLib-2.0GLibString

A GLib.String is an object that handles the memory management of a C string.

The emphasis of GLib.String is on text, typically UTF-8. Crucially, the "str" member of a GLib.String 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 GLib.String can also hold arbitrary binary data, because it has a "len" member, which includes any possible embedded nul characters in the data. Conceptually then, GLib.String is like a GLib.ByteArray with the addition of many convenience methods for text, and a guaranteed nul terminator.

Index

Constructors

  • Parameters

    • Optionalproperties: Partial<{ allocated_len: number; len: number; str: string }>

    Returns GLib.String

Properties

allocated_len: number
len: number
str: string
$gtype: GType<GLib.String>

Methods

  • 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 g_string_append_len() equivalent to g_string_append().

    Parameters

    • val: string

      bytes to append

    • len: number

      number of bytes of val to use, or -1 for all of val

    Returns GLib.String

    string

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

    Parameters

    • unescaped: string

      a string

    • reserved_chars_allowed: string

      a string of reserved characters allowed to be used, or null

    • allow_utf8: boolean

      set true if the escaped string may include UTF8 characters

    Returns GLib.String

    string

  • Converts all uppercase ASCII letters to lowercase ASCII letters.

    Returns GLib.String

    passed-in string pointer, with all the uppercase characters converted to lowercase in place, with semantics that exactly match g_ascii_tolower().

  • Converts all lowercase ASCII letters to uppercase ASCII letters.

    Returns GLib.String

    passed-in string pointer, with all the lowercase characters converted to uppercase in place, with semantics that exactly match g_ascii_toupper().

  • 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.

    Parameters

    • rval: string

      the string to copy into string

    Returns GLib.String

    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.

    Parameters

    • pos: number

      the position of the content to remove

    • len: number

      the number of bytes to remove, or -1 to remove all following bytes

    Returns GLib.String

    string

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

    Instead of passing false to this function, consider using g_string_free_and_steal().

    Parameters

    • free_segment: boolean

      if true, the actual character data is freed as well

    Returns string

    the character data of string (i.e. null if free_segment is true)

  • Frees the memory allocated for the GLib.String.

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

    Returns string

    the character data of string

  • Transfers ownership of the contents of string to a newly allocated GLib.Bytes. The GLib.String structure itself is deallocated, and it is therefore invalid to use string 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.

    Returns GLib.Bytes

    A newly allocated GLib.Bytes containing contents of string; string itself is freed

  • 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: number

      position in string where insertion should happen, or -1 for at the end

    • val: string

      bytes to insert

    • len: number

      number of bytes of val to insert, or -1 for all of val

    Returns GLib.String

    string

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

    Parameters

    • pos: number

      the position at which to insert character, or -1 to append at the end of the string

    • wc: string

      a Unicode character

    Returns GLib.String

    string

  • Overwrites part of a string, lengthening it if necessary.

    Parameters

    • pos: number

      the position at which to start overwriting

    • val: string

      the string that will overwrite the string starting at pos

    Returns GLib.String

    string

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

    Parameters

    • pos: number

      the position at which to start overwriting

    • val: string

      the string that will overwrite the string starting at pos

    • len: number

      the number of bytes to write from val

    Returns GLib.String

    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 g_string_prepend_len() equivalent to g_string_prepend().

    Parameters

    • val: string

      bytes to prepend

    • len: number

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

    Returns GLib.String

    string

  • 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.

    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: string

      the string to find in string

    • replace: string

      the string to insert in place of find

    • limit: number

      the maximum instances of find to replace with replace, or 0 for no limit

    Returns number

    the number of find and replace operations performed, up to G_MAXUINT

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

    Parameters

    • len: number

      the new length

    Returns GLib.String

    string