New function, to insert possible non-nul-terminated byte sequences into a
authorMatthias Clasen <maclas@gmx.de>
Mon, 31 Mar 2003 22:08:08 +0000 (22:08 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Mon, 31 Mar 2003 22:08:08 +0000 (22:08 +0000)
2003-04-01  Matthias Clasen  <maclas@gmx.de>

* 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
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
docs/reference/ChangeLog
docs/reference/glib/glib-sections.txt
glib/gstring.c
glib/gstring.h

index f885529..8e84a5f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index f885529..8e84a5f 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index f885529..8e84a5f 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index f885529..8e84a5f 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index f885529..8e84a5f 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index f885529..8e84a5f 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * 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  <maclas@gmx.de>
 
        * glib/gstring.c (g_string_new): Optimize the common cases 
index ce2ff54..fd5999c 100644 (file)
@@ -1,3 +1,7 @@
+2003-04-01  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/glib-sections.txt: Add g_string_chunk_insert_len.
+
 2003-03-28  Matthias Clasen  <maclas@gmx.de>
 
        * gobject/tmpl/param_value_types.sgml: Additions.
index 8980fe3..c3837c9 100644 (file)
@@ -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
 
 </SECTION>
index fb93591..cdbda81 100644 (file)
@@ -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
index a9127b3..e50c642 100644 (file)
@@ -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);