1.26 <GString>

Structure: <GString>

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.

Fields

str

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.

len

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

allocated-len

the number of bytes that can be stored in the string before it needs to be reallocated. May be larger than len.

Procedures

Method: string:append self val ⇒ %return
Method: append self val ⇒ %return

Adds a string onto the end of a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

val

Passed: a string.

the string to append onto the end of self

%return

Returned: a <GString>.

self

Method: string:append-c self c ⇒ %return
Method: append-c self c ⇒ %return

Adds a byte onto the end of a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

c

Passed: an 8-bit integer, or a character.

the byte to append onto the end of self

%return

Returned: a <GString>.

self

Method: string:append-len self val len ⇒ %return
Method: append-len self val len ⇒ %return

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 string:append-len equivalent to string:append.

self

Passed: a <GString>.

a <GString>

val

Passed: a string.

bytes to append

len

Passed: a 64-bit integer.

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

%return

Returned: a <GString>.

self

Method: string:append-unichar self wc ⇒ %return
Method: append-unichar self wc ⇒ %return

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

self

Passed: a <GString>.

a <GString>

wc

Passed: a character.

a Unicode character

%return

Returned: a <GString>.

self

Method: string:append-uri-escaped self unescaped reserved-chars-allowed allow-utf8 ⇒ %return
Method: append-uri-escaped self unescaped reserved-chars-allowed allow-utf8 ⇒ %return

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

self

Passed: a <GString>.

a <GString>

unescaped

Passed: a string.

a string

reserved-chars-allowed

Passed: a string.

a string of reserved characters allowed to be used, or #f

allow-utf8

Passed: a boolean.

set #t if the escaped string may include UTF8 characters

%return

Returned: a <GString>.

self

Method: string:ascii-down self ⇒ %return
Method: ascii-down self ⇒ %return

Converts all uppercase ASCII letters to lowercase ASCII letters.

self

Passed: a <GString>.

a GString

%return

Returned: a <GString>.

passed-in self pointer, with all the uppercase characters converted to lowercase in place, with semantics that exactly match ascii-tolower.

Method: string:ascii-up self ⇒ %return
Method: ascii-up self ⇒ %return

Converts all lowercase ASCII letters to uppercase ASCII letters.

self

Passed: a <GString>.

a GString

%return

Returned: a <GString>.

passed-in self pointer, with all the lowercase characters converted to uppercase in place, with semantics that exactly match ascii-toupper.

Method: string:assign self rval ⇒ %return
Method: assign self rval ⇒ %return

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

self

Passed: a <GString>.

the destination <GString>. Its current contents are destroyed.

rval

Passed: a string.

the string to copy into self

%return

Returned: a <GString>.

self

Method: string:copy self ⇒ %return
Method: copy self ⇒ %return

Copies the <GString> instance and its contents.

This will preserve the allocation length of the <GString> in the copy.

self

Passed: a <GString>.

a string

%return

Returned: a <GString>.

a copy of self

Method: string:down self ⇒ %return
Method: down self ⇒ %return

Converts a <GString> to lowercase.

self

Passed: a <GString>.

a <GString>

%return

Returned: a <GString>.

the <GString>

Method: string:equal? self v2 ⇒ %return
Method: equal? self v2 ⇒ %return

Compares two strings for equality, returning #t if they are equal. For use with <GHashTable>.

self

Passed: a <GString>.

a <GString>

v2

Passed: a <GString>.

another <GString>

%return

Returned: a boolean.

#t if the strings are the same length and contain the same bytes

Method: string:erase self pos len ⇒ %return
Method: erase self pos len ⇒ %return

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

self

Passed: a <GString>.

a <GString>

pos

Passed: a 64-bit integer.

the position of the content to remove

len

Passed: a 64-bit integer.

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

%return

Returned: a <GString>.

self

Method: string:free self free-segment ⇒ %return
Method: free self free-segment ⇒ %return

Frees the memory allocated for the <GString>. If free-segment is #t it also frees the character data. If it’s #f, the caller gains ownership of the buffer and must free it after use with byte-array:free.

Instead of passing #f to this function, consider using string:free-and-steal.

self

Passed: a <GString>.

a <GString>

free-segment

Passed: a boolean.

if #t, the actual character data is freed as well

%return

Returned: a string, or #f.

the character data of self (i.e. #f if free-segment is #t)

Method: string:free-and-steal self ⇒ %return
Method: free-and-steal self ⇒ %return

Frees the memory allocated for the <GString>.

The caller gains ownership of the buffer and must free it after use with byte-array:free.

self

Passed: a <GString>.

a <GString>

%return

Returned: a string.

the character data of self

Method: string:free-to-bytes self ⇒ %return
Method: free-to-bytes self ⇒ %return

Transfers ownership of the contents of self to a newly allocated <GBytes>. The <GString> structure itself is deallocated, and it is therefore invalid to use self after invoking this function.

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

self

Passed: a <GString>.

a <GString>

%return

Returned: a <GBytes>.

A newly allocated <GBytes> containing contents of self; self itself is freed

Method: string:hash self ⇒ %return
Method: hash self ⇒ %return

Creates a hash code for self; for use with <GHashTable>.

self

Passed: a <GString>.

a string to hash

%return

Returned: an unsigned 32-bit integer.

hash code for self

Method: string:insert self pos val ⇒ %return
Method: insert self pos val ⇒ %return

Inserts a copy of a string into a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

pos

Passed: a 64-bit integer.

the position to insert the copy of the string

val

Passed: a string.

the string to insert

%return

Returned: a <GString>.

self

Method: string:insert-c self pos c ⇒ %return
Method: insert-c self pos c ⇒ %return

Inserts a byte into a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

pos

Passed: a 64-bit integer.

the position to insert the byte

c

Passed: an 8-bit integer, or a character.

the byte to insert

%return

Returned: a <GString>.

self

Method: string:insert-len self pos val len ⇒ %return
Method: insert-len self pos val len ⇒ %return

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.

self

Passed: a <GString>.

a <GString>

pos

Passed: a 64-bit integer.

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

val

Passed: a string.

bytes to insert

len

Passed: a 64-bit integer.

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

%return

Returned: a <GString>.

self

Method: string:insert-unichar self pos wc ⇒ %return
Method: insert-unichar self pos wc ⇒ %return

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

self

Passed: a <GString>.

a <GString>

pos

Passed: a 64-bit integer.

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

wc

Passed: a character.

a Unicode character

%return

Returned: a <GString>.

self

Procedure: string:new [init] ⇒ %return

Creates a new <GString>, initialized with the given string.

init

Passed, or left out: a string, or #f.

the initial text to copy into the string, or #f to start with an empty string

%return

Returned: a <GString>.

the new <GString>

Procedure: string:new-len init len ⇒ %return

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

init

Passed: a string.

initial contents of the string

len

Passed: a 64-bit integer.

length of init to use

%return

Returned: a <GString>.

a new <GString>

Procedure: string:new-take [init] ⇒ %return

Creates a new <GString>, initialized with the given string.

After this call, init belongs to the <GString> and may no longer be modified by the caller. The memory of init has to be dynamically allocated and will eventually be freed with byte-array:free.

init

Passed, or left out: a string, or #f.

initial text used as the string. Ownership of the string is transferred to the <GString>. Passing #f creates an empty string.

%return

Returned: a <GString>.

the new <GString>

Method: string:overwrite self pos val ⇒ %return
Method: overwrite self pos val ⇒ %return

Overwrites part of a string, lengthening it if necessary.

self

Passed: a <GString>.

a <GString>

pos

Passed: an unsigned 64-bit integer.

the position at which to start overwriting

val

Passed: a string.

the string that will overwrite the self starting at pos

%return

Returned: a <GString>.

self

Method: string:overwrite-len self pos val len ⇒ %return
Method: overwrite-len self pos val len ⇒ %return

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

self

Passed: a <GString>.

a <GString>

pos

Passed: an unsigned 64-bit integer.

the position at which to start overwriting

val

Passed: a string.

the string that will overwrite the self starting at pos

len

Passed: a 64-bit integer.

the number of bytes to write from val

%return

Returned: a <GString>.

self

Method: string:prepend self val ⇒ %return
Method: prepend self val ⇒ %return

Adds a string on to the start of a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

val

Passed: a string.

the string to prepend on the start of self

%return

Returned: a <GString>.

self

Method: string:prepend-c self c ⇒ %return
Method: prepend-c self c ⇒ %return

Adds a byte onto the start of a <GString>, expanding it if necessary.

self

Passed: a <GString>.

a <GString>

c

Passed: an 8-bit integer, or a character.

the byte to prepend on the start of the <GString>

%return

Returned: a <GString>.

self

Method: string:prepend-len self val len ⇒ %return
Method: prepend-len self val len ⇒ %return

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 string:prepend-len equivalent to string:prepend.

self

Passed: a <GString>.

a <GString>

val

Passed: a string.

bytes to prepend

len

Passed: a 64-bit integer.

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

%return

Returned: a <GString>.

self

Method: string:prepend-unichar self wc ⇒ %return
Method: prepend-unichar self wc ⇒ %return

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

self

Passed: a <GString>.

a <GString>

wc

Passed: a character.

a Unicode character

%return

Returned: a <GString>.

self

Method: string:replace self find replace limit ⇒ %return
Method: replace self find replace limit ⇒ %return

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

self

Passed: a <GString>.

a <GString>

find

Passed: a string.

the string to find in self

replace

Passed: a string.

the string to insert in place of find

limit

Passed: an unsigned 32-bit integer.

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

%return

Returned: an unsigned 32-bit integer.

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

Method: string:set-size self len ⇒ %return
Method: set-size self len ⇒ %return

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

self

Passed: a <GString>.

a <GString>

len

Passed: an unsigned 64-bit integer.

the new length

%return

Returned: a <GString>.

self

Procedure: string:sized-new dfl-size ⇒ %return

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

dfl-size

Passed: an unsigned 64-bit integer.

the default size of the space allocated to hold the string

%return

Returned: a <GString>.

the new <GString>

Method: string:truncate self len ⇒ %return
Method: truncate self len ⇒ %return

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

self

Passed: a <GString>.

a <GString>

len

Passed: an unsigned 64-bit integer.

the new size of self

%return

Returned: a <GString>.

self

Method: string:up self ⇒ %return
Method: up self ⇒ %return

Converts a <GString> to uppercase.

self

Passed: a <GString>.

a <GString>

%return

Returned: a <GString>.

self