Strings

Name

Strings -- text buffers which grow automatically as text is added.

Synopsis


#include <glib.h>


struct      GString;
GString*    g_string_new                    (const gchar *init);
GString*    g_string_sized_new              (guint dfl_size);
GString*    g_string_assign                 (GString *lval,
                                             const gchar *rval);
void        g_string_sprintf                (GString *string,
                                             const gchar *format,
                                             ...);
void        g_string_sprintfa               (GString *string,
                                             const gchar *format,
                                             ...);
GString*    g_string_append                 (GString *string,
                                             const gchar *val);
GString*    g_string_append_c               (GString *string,
                                             gchar c);
GString*    g_string_prepend                (GString *string,
                                             const gchar *val);
GString*    g_string_prepend_c              (GString *string,
                                             gchar c);
GString*    g_string_insert                 (GString *string,
                                             gint pos,
                                             const gchar *val);
GString*    g_string_insert_c               (GString *string,
                                             gint pos,
                                             gchar c);
GString*    g_string_erase                  (GString *string,
                                             gint pos,
                                             gint len);
GString*    g_string_truncate               (GString *string,
                                             gint len);
void        g_string_free                   (GString *string,
                                             gint free_segment);

GString*    g_string_up                     (GString *string);
GString*    g_string_down                   (GString *string);

Description

A GString is similar to a standard C string, except that it grows automatically as text is appended or inserted.

The space allocated for the string is always a power of two, so as the string grows it will occupy 2, 4, 8, 16, 32, 64, 128 etc. characters.

Details

struct GString

struct GString
{
  gchar *str;
  gint len;
};

The GString struct contains the public fields of a GString. The str field points to the character data. It may move as text is added. The len field contains the length of the string, not including the terminating null character.

The str field is zero-terminated and so can be used as an ordinary C string. But it may be moved when text is appended or inserted into the string.


g_string_new ()

GString*    g_string_new                    (const gchar *init);

Creates a new GString, initialized with the given string.

init :the initial text to copy into the string.
Returns :the new GString.


g_string_sized_new ()

GString*    g_string_sized_new              (guint dfl_size);

Creates a new GString, with enough space for dfl_size characters. 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 :the default size of the space allocated to hold the string.
Returns :the new GString.


g_string_assign ()

GString*    g_string_assign                 (GString *lval,
                                             const gchar *rval);

Copies the characters from one GString into another, 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.

lval :the destination GString. Its current contents are destroyed.
rval :the source GString.
Returns :the destination GString.


g_string_sprintf ()

void        g_string_sprintf                (GString *string,
                                             const gchar *format,
                                             ...);

Writes a formatted string into a GString. This is similar to the standard sprintf() function, except that the GString buffer automatically expands to contain the results. The previous contents of the GString are destroyed.

string :a GString.
format :the string format. See the sprintf() documentation.
... :the parameters to insert into the format string.


g_string_sprintfa ()

void        g_string_sprintfa               (GString *string,
                                             const gchar *format,
                                             ...);

Appends a formatted string onto the end of a GString. This function is is similar to g_string_sprintf() except that the text is appended to the GString.

string :a GString.
format :the string format. See the sprintf() documentation.
... :the parameters to insert into the format string.


g_string_append ()

GString*    g_string_append                 (GString *string,
                                             const gchar *val);

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

string :a GString.
val :the string to append onto the end of the GString.
Returns :the GString.


g_string_append_c ()

GString*    g_string_append_c               (GString *string,
                                             gchar c);

Adds a character onto the end of a GString, expanding it if necessary.

string :a GString.
c :the character to append onto the end of the GString.
Returns :the GString.


g_string_prepend ()

GString*    g_string_prepend                (GString *string,
                                             const gchar *val);

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

string :a GString.
val :the string to prepend on the start of the GString.
Returns :the GString.


g_string_prepend_c ()

GString*    g_string_prepend_c              (GString *string,
                                             gchar c);

Adds a character onto the start of a GString, expanding it if necessary.

string :a GString.
c :the character to prepend on the start of the GString.
Returns :the GString.


g_string_insert ()

GString*    g_string_insert                 (GString *string,
                                             gint pos,
                                             const gchar *val);

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

string :a GString.
pos :the position to insert the copy of the string.
val :the string to insert.
Returns :the GString.


g_string_insert_c ()

GString*    g_string_insert_c               (GString *string,
                                             gint pos,
                                             gchar c);

Inserts a character into a GString, expanding it if necessary.

string :a GString.
pos :the position to insert the character.
c :the character to insert.
Returns :the GString.


g_string_erase ()

GString*    g_string_erase                  (GString *string,
                                             gint pos,
                                             gint len);

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

string :a GString.
pos :the position of the characters to remove.
len :the number of characters to remove.
Returns :the GString.


g_string_truncate ()

GString*    g_string_truncate               (GString *string,
                                             gint len);

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

string :a GString.
len :the new size of the GString.
Returns :the GString.


g_string_free ()

void        g_string_free                   (GString *string,
                                             gint free_segment);

Frees the memory allocated for the GString. If free_segment is TRUE it also frees the character data.

string :a GString.
free_segment :if TRUE the actual character data is freed as well.


g_string_up ()

GString*    g_string_up                     (GString *string);

Converts a GString to upper case.

string :a GString.
Returns :the GString.


g_string_down ()

GString*    g_string_down                   (GString *string);

Converts a GString to lower case.

string :a GString.
Returns :the GString.