From e4112b8b1bf6a99d0eb5c5d512110951390f6666 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 31 Mar 2003 22:08:08 +0000 Subject: [PATCH] New function, to insert possible non-nul-terminated byte sequences into a 2003-04-01 Matthias Clasen * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to insert possible non-nul-terminated byte sequences into a string chunk. (#96279) (g_string_chunk_insert): Implement in terms of g_string_chunk_insert_len() now. --- ChangeLog | 8 ++++ ChangeLog.pre-2-10 | 8 ++++ ChangeLog.pre-2-12 | 8 ++++ ChangeLog.pre-2-4 | 8 ++++ ChangeLog.pre-2-6 | 8 ++++ ChangeLog.pre-2-8 | 8 ++++ docs/reference/ChangeLog | 4 ++ docs/reference/glib/glib-sections.txt | 1 + glib/gstring.c | 74 +++++++++++++++++++++++++---------- glib/gstring.h | 3 ++ 10 files changed, 109 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index f885529..8e84a5f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index f885529..8e84a5f 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index f885529..8e84a5f 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index f885529..8e84a5f 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index f885529..8e84a5f 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index f885529..8e84a5f 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,11 @@ +2003-04-01 Matthias Clasen + + * glib/gstring.[hc] (g_string_chunk_insert_len): New function, to + insert possible non-nul-terminated byte sequences into a string + chunk. (#96279) + (g_string_chunk_insert): Implement in terms of + g_string_chunk_insert_len() now. + 2003-03-30 Matthias Clasen * glib/gstring.c (g_string_new): Optimize the common cases diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index ce2ff54..fd5999c 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2003-04-01 Matthias Clasen + + * glib/glib-sections.txt: Add g_string_chunk_insert_len. + 2003-03-28 Matthias Clasen * gobject/tmpl/param_value_types.sgml: Additions. diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 8980fe3..c3837c9 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1554,6 +1554,7 @@ GStringChunk g_string_chunk_new g_string_chunk_insert g_string_chunk_insert_const +g_string_chunk_insert_len g_string_chunk_free diff --git a/glib/gstring.c b/glib/gstring.c index fb93591..cdbda81 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -146,29 +146,9 @@ gchar* g_string_chunk_insert (GStringChunk *chunk, const gchar *string) { - gsize len = strlen (string); - char* pos; - g_return_val_if_fail (chunk != NULL, NULL); - if ((chunk->storage_next + len + 1) > chunk->this_size) - { - gsize new_size = nearest_power (chunk->default_size, len + 1); - - chunk->storage_list = g_slist_prepend (chunk->storage_list, - g_new (char, new_size)); - - chunk->this_size = new_size; - chunk->storage_next = 0; - } - - pos = ((char *) chunk->storage_list->data) + chunk->storage_next; - - strcpy (pos, string); - - chunk->storage_next += len + 1; - - return pos; + return g_string_chunk_insert_len (chunk, string, -1); } gchar* @@ -193,6 +173,58 @@ g_string_chunk_insert_const (GStringChunk *chunk, return lookup; } +/** + * g_string_chunk_insert_len: + * @chunk: a #GStringChunk + * @string: bytes to insert + * @len: number of bytes of @string to insert, or -1 to insert a + * nul-terminated string. + * + * Adds a copy of the first @len bytes of @string to the #GStringChunk. The + * copy is nul-terminated. + * + * The characters in the string can be changed, if necessary, though you + * should not change anything after the end of the string. + * + * Return value: a pointer to the copy of @string within the #GStringChunk + * + * Since: 2.4 + **/ +gchar* +g_string_chunk_insert_len (GStringChunk *chunk, + const gchar *string, + gssize len) +{ + gchar* pos; + + g_return_val_if_fail (chunk != NULL, NULL); + + if (len < 0) + len = strlen (string); + + if ((chunk->storage_next + len + 1) > chunk->this_size) + { + gsize new_size = nearest_power (chunk->default_size, len + 1); + + chunk->storage_list = g_slist_prepend (chunk->storage_list, + g_new (gchar, new_size)); + + chunk->this_size = new_size; + chunk->storage_next = 0; + } + + pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next; + + *(pos + len) = '\0'; + + strncpy (pos, string, len); + len = strlen (pos); + + chunk->storage_next += len + 1; + + return pos; +} + /* Strings. */ static void diff --git a/glib/gstring.h b/glib/gstring.h index a9127b3..e50c642 100644 --- a/glib/gstring.h +++ b/glib/gstring.h @@ -48,6 +48,9 @@ GStringChunk* g_string_chunk_new (gsize size); void g_string_chunk_free (GStringChunk *chunk); gchar* g_string_chunk_insert (GStringChunk *chunk, const gchar *string); +gchar* g_string_chunk_insert_len (GStringChunk *chunk, + const gchar *string, + gssize len); gchar* g_string_chunk_insert_const (GStringChunk *chunk, const gchar *string); -- 2.7.4