Gstring
In addition to using gchar * for string processing, g l I B also defines a new data type: g s t r I n g. It is similar to the string type of standard C, but g s t r I n gcan automatically increase. Its string data ends with n u L. These features can prevent Program Buffer overflow. This is a very important feature. The following is the definition of g s t r I n g:
Struct gstring
{
Gchar * STR;/* points to the st' RSI ncgurrent/0-terminated value .*/
Gint Len;/* Current length */
};
Use the following function to create a new g s t r I n g variable:
Gstring * g_string_new (gchar * init );
This function creates a g s t r I n g, copies the string value I n I t to g s t r I n g, and returns a pointer to it. If the I n I t parameter is n u L, create an empty g s t r I n g.
Void g_string_free (gstring * string, Gint free_segment );
This function releases the memory occupied by s t r I n g. The f r e _ s e g m e n t parameter is a Boolean variable. If the f r e _ s e g m e n t parameter is t r u e, it also releases the character data.
Gstring * g_string_assign (gstring * lval, const gchar * rval );
This function copies characters from R v a l to l v a l and destroys the original content of l v a l. Note that if necessary, l v a l will be extended to hold the content of the string. This is the same as the standard string replication function s t r c p y. The meanings of the following functions are obvious. The function ending with _ c accepts a character rather than a string.
Take the s t r I N G string and generate a substring with the length of l e n:
Gstring * g_string_truncate (gstring * string, Gint Len );
Append the string v a l to s t r I n g and return a new string:
Gstring * g_string_append (gstring * string, gchar * val );
Append character C to s t r I n g and return a new string:
Gstring * g_string_append_c (gstring * string, gchar C );
Insert the string v a l before s t r I n g to generate a new string:
Gstring * g_string_prepend (gstring * string, gchar * val );
Insert character C to s t r I n g to generate a new string:
Gstring * g_string_prepend_c (gstring * string, gchar C );
Write a formatted string to s t r I n g, similar to the standard s p r I n t f function:
Void g_string_sprintf (gstring * string, gchar * FMT ,...);
Append a formatted string to the end of s t r I n g, which is slightly different from the previous function:
Void g_string_sprintfa (gstring * string, gchar * FMT ,...);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.