Unref callback->cb_data if it was still set when the source is freed.
[platform/upstream/glib.git] / gstring.c
index 299b242..ff6fc7c 100644 (file)
--- a/gstring.c
+++ b/gstring.c
@@ -2,23 +2,23 @@
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
 /*
- * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
+ * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
@@ -67,7 +67,7 @@ static GMemChunk *string_mem_chunk = NULL;
 /* Hash Functions.
  */
 
-gint
+gboolean
 g_str_equal (gconstpointer v1,
             gconstpointer v2)
 {
@@ -243,7 +243,7 @@ g_string_new (const gchar *init)
 {
   GString *string;
 
-  string = g_string_sized_new (2);
+  string = g_string_sized_new (init ? strlen (init) + 2 : 2);
 
   if (init)
     g_string_append (string, init);
@@ -251,18 +251,88 @@ g_string_new (const gchar *init)
   return string;
 }
 
-void
+GString*
+g_string_new_len (const gchar *init,
+                  gint         len)
+{
+  GString *string;
+
+  if (len < 0)
+    return g_string_new (init);
+  else
+    {
+      string = g_string_sized_new (len);
+      
+      if (init)
+        g_string_append_len (string, init, len);
+      
+      return string;
+    }
+}
+
+gchar*
 g_string_free (GString *string,
               gboolean free_segment)
 {
-  g_return_if_fail (string != NULL);
+  gchar *segment;
+
+  g_return_val_if_fail (string != NULL, NULL);
 
   if (free_segment)
-    g_free (string->str);
+    {
+      g_free (string->str);
+      segment = NULL;
+    }
+  else
+    segment = string->str;
 
   G_LOCK (string_mem_chunk);
   g_mem_chunk_free (string_mem_chunk, string);
   G_UNLOCK (string_mem_chunk);
+
+  return segment;
+}
+
+gboolean
+g_string_equal (const GString *v,
+                const GString *v2)
+{
+  gchar *p, *q;
+  GRealString *string1 = (GRealString *) v;
+  GRealString *string2 = (GRealString *) v2;
+  gint i = string1->len;
+
+  if (i != string2->len)
+    return FALSE;
+
+  p = string1->str;
+  q = string2->str;
+  while (i)
+    {
+      if (*p != *q)
+       return FALSE;
+      p++;
+      q++;
+      i--;
+    }
+  return TRUE;
+}
+
+/* 31 bit hash function */
+guint
+g_string_hash (const GString *str)
+{
+  const gchar *p = str->str;
+  gint n = str->len;
+  guint h = 0;
+
+  while (n--)
+    {
+      h = (h << 5) - h + *p;
+      p++;
+    }
+
+  return h;
 }
 
 GString*
@@ -320,7 +390,7 @@ g_string_insert_len (GString     *fstring,
     g_memmove (string->str + pos + len, string->str + pos, string->len - pos);
   
   /* insert the new string */
-  strncpy (string->str + pos, val, len);
+  g_memmove (string->str + pos, val, len);
 
   string->len += len;
 
@@ -457,15 +527,17 @@ g_string_down (GString *fstring)
 {
   GRealString *string = (GRealString *) fstring;
   guchar *s;
+  gint n = string->len;
 
   g_return_val_if_fail (string != NULL, NULL);
 
-  s = string->str;
+  s = (guchar *) string->str;
 
-  while (*s)
+  while (n)
     {
       *s = tolower (*s);
       s++;
+      n--;
     }
 
   return fstring;
@@ -476,15 +548,17 @@ g_string_up (GString *fstring)
 {
   GRealString *string = (GRealString *) fstring;
   guchar *s;
+  gint n = string->len;
 
   g_return_val_if_fail (string != NULL, NULL);
 
-  s = string->str;
+  s = (guchar *) string->str;
 
-  while (*s)
+  while (n)
     {
       *s = toupper (*s);
       s++;
+      n--;
     }
 
   return fstring;