Add new function _dbus_string_split_on_byte
authorRay Strode <rstrode@redhat.com>
Fri, 11 Jul 2008 15:32:30 +0000 (11:32 -0400)
committerRay Strode <rstrode@redhat.com>
Sat, 12 Jul 2008 03:58:59 +0000 (23:58 -0400)
It allows you to turn a string like KEY=VALUE
into two strings key and value.

dbus/dbus-string-util.c
dbus/dbus-string.c
dbus/dbus-string.h

index 492c528..aed9487 100644 (file)
@@ -846,6 +846,31 @@ _dbus_string_test (void)
 
     _dbus_string_free (&str);
   }
+
+  {
+    const char two_strings[] = "one\ttwo";
+
+    if (!_dbus_string_init (&str))
+      _dbus_assert_not_reached ("no memory");
+
+    if (!_dbus_string_init (&other))
+      _dbus_assert_not_reached ("no memory");
+
+    if (!_dbus_string_append (&str, two_strings))
+      _dbus_assert_not_reached ("no memory");
+
+    if (!_dbus_string_split_on_byte (&str, '\t', &other))
+      _dbus_assert_not_reached ("no memory or delimiter not found");
+
+    if (strcmp (_dbus_string_get_data (&str), "one") != 0)
+      _dbus_assert_not_reached ("left side after split on tab is wrong");
+
+    if (strcmp (_dbus_string_get_data (&other), "two") != 0)
+      _dbus_assert_not_reached ("right side after split on tab is wrong");
+
+    _dbus_string_free (&str);
+    _dbus_string_free (&other);
+  }
   
   return TRUE;
 }
index cb108a8..6b9b2bf 100644 (file)
@@ -1677,6 +1677,48 @@ _dbus_string_replace_len (const DBusString *source,
   return TRUE;
 }
 
+/**
+ * Looks for the first occurance of a byte, deletes that byte,
+ * and moves everything after the byte to the beginning of a
+ * separate string.  Both strings must be initialized, valid
+ * strings.
+ *
+ * @param source the source string
+ * @param byte the byte to remove and split the string at
+ * @param tail the split off string
+ * @returns #FALSE if not enough memory or if byte could not be found
+ *
+ */
+dbus_bool_t
+_dbus_string_split_on_byte (DBusString        *source,
+                            unsigned char      byte,
+                            DBusString        *tail)
+{
+  int byte_position;
+  char byte_string[2] = "";
+  int head_length;
+  int tail_length;
+
+  byte_string[0] = (char) byte;
+
+  if (!_dbus_string_find (source, 0, byte_string, &byte_position))
+    return FALSE;
+
+  head_length = byte_position;
+  tail_length = _dbus_string_get_length (source) - head_length - 1;
+
+  if (!_dbus_string_move_len (source, byte_position + 1, tail_length,
+                              tail, 0))
+    return FALSE;
+
+  /* remove the trailing delimiter byte from the head now.
+   */
+  if (!_dbus_string_set_length (source, head_length))
+    return FALSE;
+
+  return TRUE;
+}
+
 /* Unicode macros and utf8_validate() from GLib Owen Taylor, Havoc
  * Pennington, and Tom Tromey are the authors and authorized relicense.
  */
index d88d67e..374f0a8 100644 (file)
@@ -201,6 +201,9 @@ dbus_bool_t   _dbus_string_replace_len           (const DBusString  *source,
                                                   DBusString        *dest,
                                                   int                replace_at,
                                                   int                replace_len);
+dbus_bool_t   _dbus_string_split_on_byte         (DBusString        *source,
+                                                  unsigned char      byte,
+                                                  DBusString        *tail);
 void          _dbus_string_get_unichar           (const DBusString  *str,
                                                   int                start,
                                                   dbus_unichar_t    *ch_return,