Class GString
- All Implemented Interfaces:
Proxy
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newGString, initialized with the given string.GString(MemorySegment address) Create a GString proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionAdds a string onto the end of aGString, expanding it if necessary.appendC(byte c) Adds a byte onto the end of aGString, expanding it if necessary.Appendslenbytes ofvaltostring.voidappendPrintf(String format, Object... varargs) Appends a formatted string onto the end of aGString.appendUnichar(int wc) Converts a Unicode character into UTF-8, and appends it to the string.appendUriEscaped(String unescaped, String reservedCharsAllowed, boolean allowUtf8) Appendsunescapedtostring,escaping any characters that are reserved in URIs using URI-style escape sequences.Converts all uppercase ASCII letters to lowercase ASCII letters.asciiUp()Converts all lowercase ASCII letters to uppercase ASCII letters.Copies the bytes from a string into aGString, destroying any previous contents.copy()Copies theGLib.Stringinstance and its contents.down()Deprecated.This function uses the locale-specific tolower() function, which is almost never the right thing.booleanCompares two strings for equality, returningtrueif they are equal.erase(long pos, long len) Removeslenbytes from aGString, starting at positionpos.The rest of theGStringis shifted down to fill the gap.@Nullable Stringfree(boolean freeSegment) Frees the memory allocated for theGString.Frees the memory allocated for theGString.byte[]Transfers ownership of the contents of this String to a newly allocatedGBytes.static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the GString class.inthash()Creates a hash code forstr;for use withGHashTable.Inserts a copy of a string into aGString, expanding it if necessary.insertC(long pos, byte c) Inserts a byte into aGString, expanding it if necessary.Insertslenbytes ofvalinto this String atpos.insertUnichar(long pos, int wc) Converts a Unicode character into UTF-8, and insert it into the string at the given position.static StringCreates a newGStringwithlenbytes of theinitbuffer.Overwrites part of a string, lengthening it if necessary.overwriteLen(long pos, String val, long len) Overwrites part of a string, lengthening it if necessary.Adds a string on to the start of aGString, expanding it if necessary.prependC(byte c) Adds a byte onto the start of aGString, expanding it if necessary.prependLen(String val, long len) Prependslenbytes ofvaltostring.prependUnichar(int wc) Converts a Unicode character into UTF-8, and prepends it to the string.voidWrites a formatted string into aGString.longRead the value of the fieldallocated_len.longreadLen()Read the value of the fieldlen.readStr()Read the value of the fieldstr.intReplaces the stringfindwith the stringreplacein aGStringup tolimittimes.setSize(long len) Sets the length of aGString.static StringsizedNew(long dflSize) Creates a newGString, with enough space fordflSizebytes.static StringCreates a newGString, initialized with the given string.truncate(long len) Cuts off the end of the GString, leaving the firstlenbytes.up()Deprecated.This function uses the locale-specific toupper() function, which is almost never the right thing.voidwriteAllocatedLen(long allocatedLen) Write a value in the fieldallocated_len.voidwriteLen(long len) Write a value in the fieldlen.voidWrite a value in the fieldstr.Methods inherited from class ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
GString
Create a GString proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
GString
Creates a newGString, initialized with the given string.- Parameters:
init- the initial text to copy into the string, ornullto start with an empty string
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readStr
-
writeStr
-
readLen
public long readLen()Read the value of the fieldlen.- Returns:
- The value of the field
len
-
writeLen
public void writeLen(long len) Write a value in the fieldlen.- Parameters:
len- The new value for the fieldlen
-
readAllocatedLen
public long readAllocatedLen()Read the value of the fieldallocated_len.- Returns:
- The value of the field
allocated_len
-
writeAllocatedLen
public void writeAllocatedLen(long allocatedLen) Write a value in the fieldallocated_len.- Parameters:
allocatedLen- The new value for the fieldallocated_len
-
len
Creates a new
GStringwithlenbytes 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 stringlen- length ofinitto use- Returns:
- a new
GString
-
take
Creates a new
GString, initialized with the given string.After this call,
initbelongs to theGStringand may no longer be modified by the caller. The memory ofinithas to be dynamically allocated and will eventually be freed with g_free().- Parameters:
init- initial text used as the string. Ownership of the string is transferred to theGString. Passingnullcreates an empty string.- Returns:
- the new
GString - Since:
- 2.78
-
sizedNew
Creates a newGString, with enough space fordflSizebytes. 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:
dflSize- the default size of the space allocated to hold the string- Returns:
- the new
GString
-
append
-
appendC
Adds a byte onto the end of aGString, expanding it if necessary.- Parameters:
c- the byte to append onto the end of this String- Returns:
- this String
-
appendLen
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 makes g_string_append_len() equivalent to g_string_append().- Parameters:
val- bytes to appendlen- number of bytes ofvalto use, or -1 for all ofval- Returns:
- this String
-
appendPrintf
Appends a formatted string onto the end of aGString. This function is similar to g_string_printf() except that the text is appended to theGString.- Parameters:
format- the string format. See the printf() documentationvarargs- the parameters to insert into the format string
-
appendUnichar
Converts a Unicode character into UTF-8, and appends it to the string.- Parameters:
wc- a Unicode character- Returns:
- this String
-
appendUriEscaped
Appendsunescapedtostring,escaping any characters that are reserved in URIs using URI-style escape sequences.- Parameters:
unescaped- a stringreservedCharsAllowed- a string of reserved characters allowed to be used, ornullallowUtf8- settrueif the escaped string may include UTF8 characters- Returns:
- this String
- Since:
- 2.16
-
asciiDown
Converts all uppercase ASCII letters to lowercase ASCII letters.- Returns:
- passed-in this String pointer, with all the uppercase characters converted to lowercase in place, with semantics that exactly match g_ascii_tolower().
-
asciiUp
Converts all lowercase ASCII letters to uppercase ASCII letters.- Returns:
- passed-in this String pointer, with all the lowercase characters converted to uppercase in place, with semantics that exactly match g_ascii_toupper().
-
assign
Copies the bytes from a string into aGString, 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 this String- Returns:
- this String
-
copy
Copies the
GLib.Stringinstance and its contents.This will preserve the allocation length of the
GLib.Stringin the copy.- Returns:
- a copy of this String
- Since:
- 2.86
-
down
Deprecated.This function uses the locale-specific tolower() function, which is almost never the right thing. Use g_string_ascii_down() or g_utf8_strdown() instead.Converts aGStringto lowercase.- Returns:
- the
GString
-
equal
Compares two strings for equality, returningtrueif they are equal. For use withGHashTable.- Parameters:
v2- anotherGString- Returns:
trueif the strings are the same length and contain the same bytes
-
erase
Removeslenbytes from aGString, starting at positionpos.The rest of theGStringis shifted down to fill the gap.- Parameters:
pos- the position of the content to removelen- the number of bytes to remove, or -1 to remove all following bytes- Returns:
- this String
-
free
Frees the memory allocated for the
GString. IffreeSegmentistrueit also frees the character data. If it'sfalse, the caller gains ownership of the buffer and must free it after use with g_free().Instead of passing
falseto this function, consider using g_string_free_and_steal().- Parameters:
freeSegment- iftrue, the actual character data is freed as well- Returns:
- the character data of this String
(i.e.
nulliffreeSegmentistrue)
-
freeAndSteal
Frees the memory allocated for the
GString.The caller gains ownership of the buffer and must free it after use with g_free().
- Returns:
- the character data of this String
- Since:
- 2.76
-
freeToBytes
public byte[] freeToBytes()Transfers ownership of the contents of this String to a newly allocated
GBytes. TheGStringstructure itself is deallocated, and it is therefore invalid to use this String after invoking this function.Note that while
GStringensures that its buffer always has a trailing nul character (not reflected in its "len"), the returnedGBytesdoes not include this extra nul; i.e. it has length exactly equal to the "len" member.- Returns:
- A newly allocated
GBytescontaining contents ofstring;this String itself is freed - Since:
- 2.34
-
hash
public int hash()Creates a hash code forstr;for use withGHashTable.- Returns:
- hash code for this String
-
insert
-
insertC
Inserts a byte into aGString, expanding it if necessary.- Parameters:
pos- the position to insert the bytec- the byte to insert- Returns:
- this String
-
insertLen
Inserts
lenbytes ofvalinto this String atpos.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 this String where insertion should happen, or -1 for at the endval- bytes to insertlen- number of bytes ofvalto insert, or -1 for all ofval- Returns:
- this String
-
insertUnichar
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 stringwc- a Unicode character- Returns:
- this String
-
overwrite
-
overwriteLen
Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.- Parameters:
pos- the position at which to start overwritingval- the string that will overwrite the this String starting atposlen- the number of bytes to write fromval- Returns:
- this String
- Since:
- 2.14
-
prepend
-
prependC
Adds a byte onto the start of aGString, expanding it if necessary.- Parameters:
c- the byte to prepend on the start of theGString- Returns:
- this String
-
prependLen
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 makes g_string_prepend_len() equivalent to g_string_prepend().- Parameters:
val- bytes to prependlen- number of bytes invalto prepend, or -1 for all ofval- Returns:
- this String
-
prependUnichar
Converts a Unicode character into UTF-8, and prepends it to the string.- Parameters:
wc- a Unicode character- Returns:
- this String
-
printf
Writes a formatted string into aGString. This is similar to the standard sprintf() function, except that theGStringbuffer automatically expands to contain the results. The previous contents of theGStringare destroyed.- Parameters:
format- the string format. See the printf() documentationvarargs- the parameters to insert into the format string
-
replace
Replaces the string
findwith the stringreplacein aGStringup tolimittimes. If the number of instances offindin theGStringis 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.If
limitis zero and more thanG_MAXUINTinstances offindare in the input string, they will all be replaced, but the return value will be capped atG_MAXUINT.- Parameters:
find- the string to find in this Stringreplace- the string to insert in place offindlimit- the maximum instances offindto replace withreplace,or0for no limit- Returns:
- the number of find and replace operations performed,
up to
G_MAXUINT - Since:
- 2.68
-
setSize
Sets the length of aGString. 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- Returns:
- this String
-
truncate
Cuts off the end of the GString, leaving the firstlenbytes.- Parameters:
len- the new size of this String- Returns:
- this String
-
up
Deprecated.This function uses the locale-specific toupper() function, which is almost never the right thing. Use g_string_ascii_up() or g_utf8_strup() instead.Converts aGStringto uppercase.- Returns:
- this String
-