String#
- class String(*args, **kwargs)#
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#
- class String
- classmethod new(init: str | None = None) String#
Creates a new
String, initialized with the given string.- Parameters:
init – the initial text to copy into the string, or
Noneto start with an empty string
- classmethod new_len(init: str, len: int) String#
Creates a new
Stringwithlenbytes of theinitbuffer. Because a length is provided,initneed 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
inithas at leastlenaddressable bytes.- Parameters:
init – initial contents of the string
len – length of
initto use
- classmethod new_take(init: str | None = None) String#
Creates a new
String, initialized with the given string.After this call,
initbelongs to theStringand may no longer be modified by the caller. The memory ofdatahas to be dynamically allocated and will eventually be freed withfree().Added in version 2.78.
- Parameters:
init – initial text used as the string. Ownership of the string is transferred to the
String. PassingNonecreates an empty string.
- classmethod sized_new(dfl_size: int) String#
Creates a new
String, with enough space fordfl_sizebytes. 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#
- class String
- append(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(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(val: str, len: int) String#
Appends
lenbytes ofvaltostring.If
lenis positive,valmay contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure thatvalhas at leastlenaddressable bytes.If
lenis negative,valmust be nul-terminated andlenis considered to request the entire string length. This makesappend_len()equivalent toappend().- Parameters:
val – bytes to append
len – number of bytes of
valto use, or -1 for all ofval
- append_unichar(wc: str) String#
Converts a Unicode character into UTF-8, and appends it to the string.
- Parameters:
wc – a Unicode character
- append_uri_escaped(unescaped: str, reserved_chars_allowed: str, allow_utf8: bool) String#
Appends
unescapedtostring, escaping any characters that are reserved in URIs using URI-style escape sequences.Added in version 2.16.
- Parameters:
unescaped – a string
reserved_chars_allowed – a string of reserved characters allowed to be used, or
Noneallow_utf8 – set
Trueif the escaped string may include UTF8 characters
- assign(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
- down() String#
Converts a
Stringto lowercase.Deprecated since version 2.2:
- This function uses the locale-specific
tolower() function, which is almost never the right thing. Use
ascii_down()orutf8_strdown()instead.
- equal(v2: String) bool#
Compares two strings for equality, returning
Trueif they are equal. For use withHashTable.- Parameters:
v2 – another
String
- erase(pos: int, len: int) String#
Removes
lenbytes from aString, starting at positionpos. The rest of theStringis 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(free_segment: bool) str | None#
Frees the memory allocated for the
String. Iffree_segmentisTrueit also frees the character data. If it’sFalse, the caller gains ownership of the buffer and must free it after use withfree().Instead of passing
Falseto this function, consider usingfree_and_steal().- Parameters:
free_segment – if
True, the actual character data is freed as well
- free_to_bytes() Bytes#
Transfers ownership of the contents of
stringto a newly allocatedBytes. TheStringstructure itself is deallocated, and it is therefore invalid to usestringafter invoking this function.Note that while
Stringensures that its buffer always has a trailing nul character (not reflected in its “len”), the returnedBytesdoes not include this extra nul; i.e. it has length exactly equal to the “len” member.Added in version 2.34.
- insert(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(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(pos: int, val: str, len: int) String#
Inserts
lenbytes ofvalintostringatpos.If
lenis positive,valmay contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure thatvalhas at leastlenaddressable bytes.If
lenis negative,valmust be nul-terminated andlenis considered to request the entire string length.If
posis -1, bytes are inserted at the end of the string.- Parameters:
pos – position in
stringwhere insertion should happen, or -1 for at the endval – bytes to insert
len – number of bytes of
valto insert, or -1 for all ofval
- insert_unichar(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(pos: int, val: str) String#
Overwrites part of a string, lengthening it if necessary.
Added in version 2.14.
- Parameters:
pos – the position at which to start overwriting
val – the string that will overwrite the
stringstarting atpos
- overwrite_len(pos: int, val: str, len: int) String#
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.
Added in version 2.14.
- Parameters:
pos – the position at which to start overwriting
val – the string that will overwrite the
stringstarting atposlen – the number of bytes to write from
val
- prepend(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(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(val: str, len: int) String#
Prepends
lenbytes ofvaltostring.If
lenis positive,valmay contain embedded nuls and need not be nul-terminated. It is the caller’s responsibility to ensure thatvalhas at leastlenaddressable bytes.If
lenis negative,valmust be nul-terminated andlenis considered to request the entire string length. This makesprepend_len()equivalent toprepend().- Parameters:
val – bytes to prepend
len – number of bytes in
valto prepend, or -1 for all ofval
- prepend_unichar(wc: str) String#
Converts a Unicode character into UTF-8, and prepends it to the string.
- Parameters:
wc – a Unicode character
- replace(find: str, replace: str, limit: int) int#
Replaces the string
findwith the stringreplacein aStringup tolimittimes. If the number of instances offindin theStringis less thanlimit, all instances are replaced. Iflimitis0, all instances offindare replaced.If
findis 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.Added in version 2.68.
- Parameters:
find – the string to find in
stringreplace – the string to insert in place of
findlimit – the maximum instances of
findto replace withreplace, or0for no limit
- set_size(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(len: int) String#
Cuts off the end of the GString, leaving the first
lenbytes.- Parameters:
len – the new size of
string
- up() String#
Converts a
Stringto uppercase.Deprecated since version 2.2:
- This function uses the locale-specific
toupper() function, which is almost never the right thing. Use
ascii_up()orutf8_strup()instead.
Fields#
- class String
- allocated_len#
- The number of bytes that can be stored in the
string before it needs to be reallocated. May be larger than
len.
- len#
- Contains the length of the string, not including the
terminating nul byte.
- str#
- Points to the character data. It may move as text is added.
The
strfield is null-terminated and so can be used as an ordinary C string.