Class GString
- All Implemented Interfaces:
Proxy
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
ConstructorDescriptionGString()
CallsGString(java.lang.String)
with init =null
Creates 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.Appendslen
bytes ofval
to this String.void
appendPrintf
(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) Appendsunescaped
to this String, 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.down()
Deprecated.This function uses the locale-specific tolower() function, which is almost never the right thing.boolean
Compares two strings for equality, returningtrue
if they are equal.erase
(long pos, long len) Removeslen
bytes from aGString
, starting at positionpos
.free
(boolean freeSegment) Frees the memory allocated for theGString
.Frees the memory allocated for theGString
.Transfers ownership of the contents of this String to a newly allocatedGBytes
.static MemoryLayout
The memory layout of the native struct.static Type
getType()
Get the GType of the GString classint
hash()
Creates a hash code for this String; 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.Insertslen
bytes ofval
into 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 GString
Creates a newGString
withlen
bytes of theinit
buffer.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) Prependslen
bytes ofval
to this String.prependUnichar
(int wc) Converts a Unicode character into UTF-8, and prepends it to the string.void
Writes a formatted string into aGString
.long
Read the value of the fieldallocated_len
.long
readLen()
Read the value of the fieldlen
.readStr()
Read the value of the fieldstr
.int
Replaces the stringfind
with the stringreplace
in aGString
up tolimit
times.setSize
(long len) Sets the length of aGString
.static GString
sizedNew
(long dflSize) Creates a newGString
, with enough space fordflSize
bytes.static GString
Creates a newGString
, initialized with the given string.truncate
(long len) Cuts off the end of the GString, leaving the firstlen
bytes.up()
Deprecated.This function uses the locale-specific toupper() function, which is almost never the right thing.void
writeAllocatedLen
(long allocatedLen) Write a value in the fieldallocated_len
.void
writeLen
(long len) Write a value in the fieldlen
.void
Write a value in the fieldstr
.Methods inherited from class io.github.jwharm.javagi.base.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, ornull
to start with an empty string
-
GString
public GString()CallsGString(java.lang.String)
with init =null
-
-
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 newGString
withlen
bytes of theinit
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 leastlen
addressable bytes.- Parameters:
init
- initial contents of the stringlen
- length ofinit
to use- Returns:
- a new
GString
-
take
Creates a newGString
, initialized with the given string.After this call,
init
belongs to theGString
and may no longer be modified by the caller. The memory ofdata
has 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
. Passingnull
creates an empty string.- Returns:
- the new
GString
-
sizedNew
Creates a newGString
, with enough space fordflSize
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:
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
Appendslen
bytes ofval
to this String.If
len
is positive,val
may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure thatval
has at leastlen
addressable bytes.If
len
is negative,val
must be nul-terminated andlen
is 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 ofval
to 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
Appendsunescaped
to this String, 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, ornull
allowUtf8
- settrue
if the escaped string may include UTF8 characters- Returns:
- this String
-
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
-
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 aGString
to lowercase.- Returns:
- the
GString
-
equal
Compares two strings for equality, returningtrue
if they are equal. For use withGHashTable
.- Parameters:
v2
- anotherGString
- Returns:
true
if the strings are the same length and contain the same bytes
-
erase
Removeslen
bytes from aGString
, starting at positionpos
. The rest of theGString
is 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 theGString
. IffreeSegment
istrue
it 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
false
to 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.
null
iffreeSegment
istrue
)
-
freeAndSteal
Frees the memory allocated for theGString
.The caller gains ownership of the buffer and must free it after use with g_free().
- Returns:
- the character data of this String
-
freeToBytes
Transfers ownership of the contents of this String to a newly allocatedGBytes
. TheGString
structure itself is deallocated, and it is therefore invalid to use this String after invoking this function.Note that while
GString
ensures that its buffer always has a trailing nul character (not reflected in its "len"), the returnedGBytes
does not include this extra nul; i.e. it has length exactly equal to the "len" member.- Returns:
- A newly allocated
GBytes
containing contents of this String; this String itself is freed
-
hash
public int hash()Creates a hash code for this String; 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
Insertslen
bytes ofval
into this String atpos
.If
len
is positive,val
may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure thatval
has at leastlen
addressable bytes.If
len
is negative,val
must be nul-terminated andlen
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 this String where insertion should happen, or -1 for at the endval
- bytes to insertlen
- number of bytes ofval
to 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 atpos
len
- the number of bytes to write fromval
- Returns:
- this String
-
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
Prependslen
bytes ofval
to this String.If
len
is positive,val
may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure thatval
has at leastlen
addressable bytes.If
len
is negative,val
must be nul-terminated andlen
is 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 inval
to 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 theGString
buffer automatically expands to contain the results. The previous contents of theGString
are destroyed.- Parameters:
format
- the string format. See the printf() documentationvarargs
- the parameters to insert into the format string
-
replace
Replaces the stringfind
with the stringreplace
in aGString
up tolimit
times. If the number of instances offind
in theGString
is less thanlimit
, all instances are replaced. Iflimit
is0
, all instances offind
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.- Parameters:
find
- the string to find in this Stringreplace
- the string to insert in place offind
limit
- the maximum instances offind
to replace withreplace
, or0
for no limit- Returns:
- the number of find and replace operations performed.
-
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 firstlen
bytes.- 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 aGString
to uppercase.- Returns:
- this String
-