2004-12-31 Havoc Pennington <hp@redhat.com>
authorHavoc Pennington <hp@redhat.com>
Fri, 31 Dec 2004 21:01:56 +0000 (21:01 +0000)
committerHavoc Pennington <hp@redhat.com>
Fri, 31 Dec 2004 21:01:56 +0000 (21:01 +0000)
* dbus/dbus-string.c (_dbus_string_equal_substrings): new function
I keep wishing I had

ChangeLog
dbus/dbus-string.c
dbus/dbus-string.h

index be93fc5..82dcc33 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2004-12-31  Havoc Pennington  <hp@redhat.com>
+
+       * dbus/dbus-string.c (_dbus_string_equal_substrings): new function
+       I keep wishing I had
+
 2004-12-30  John (J5) Palmieri  <johnp@redhat.com>
 
        * python/dbus.py: s/ACTIVATION_REPLY_ACTIVE/ACTIVATION_REPLY_ACTIVATED
index b4e626b..65797b1 100644 (file)
@@ -2125,6 +2125,7 @@ _dbus_string_equal (const DBusString *a,
 
 /**
  * Tests two DBusString for equality up to the given length.
+ * The strings may be shorter than the given length.
  *
  * @todo write a unit test
  *
@@ -2132,7 +2133,7 @@ _dbus_string_equal (const DBusString *a,
  *
  * @param a first string
  * @param b second string
- * @param len the lengh
+ * @param len the maximum length to look at
  * @returns #TRUE if equal for the given number of bytes
  */
 dbus_bool_t
@@ -2168,6 +2169,63 @@ _dbus_string_equal_len (const DBusString *a,
 }
 
 /**
+ * Tests two sub-parts of two DBusString for equality.  The specified
+ * range of the first string must exist; the specified start position
+ * of the second string must exist.
+ *
+ * @todo write a unit test
+ *
+ * @todo memcmp is probably faster
+ *
+ * @param a first string
+ * @param a_start where to start substring in first string
+ * @param a_len length of substring in first string
+ * @param b second string
+ * @param b_start where to start substring in second string
+ * @returns #TRUE if the two substrings are equal
+ */
+dbus_bool_t
+_dbus_string_equal_substring (const DBusString  *a,
+                              int                a_start,
+                              int                a_len,
+                              const DBusString  *b,
+                              int                b_start)
+{
+  const unsigned char *ap;
+  const unsigned char *bp;
+  const unsigned char *a_end;
+  const DBusRealString *real_a = (const DBusRealString*) a;
+  const DBusRealString *real_b = (const DBusRealString*) b;
+  DBUS_GENERIC_STRING_PREAMBLE (real_a);
+  DBUS_GENERIC_STRING_PREAMBLE (real_b);
+  _dbus_assert (a_start >= 0);
+  _dbus_assert (a_len >= 0);
+  _dbus_assert (a_start <= real_a->len);
+  _dbus_assert (a_len <= real_a->len - a_start);
+  _dbus_assert (b_start >= 0);
+  _dbus_assert (b_start <= real_b->len);
+  
+  if (a_len > real_b->len - b_start)
+    return FALSE;
+
+  ap = real_a->str + a_start;
+  bp = real_b->str + b_start;
+  a_end = ap + a_len;
+  while (ap != a_end)
+    {
+      if (*ap != *bp)
+        return FALSE;
+      
+      ++ap;
+      ++bp;
+    }
+
+  _dbus_assert (bp <= (real_b->str + real_b->len));
+  
+  return TRUE;
+}
+
+/**
  * Checks whether a string is equal to a C string.
  *
  * @param a the string
index 2b8b09f..96f3d56 100644 (file)
@@ -230,6 +230,11 @@ dbus_bool_t   _dbus_string_equal_c_str           (const DBusString  *a,
 dbus_bool_t   _dbus_string_equal_len             (const DBusString  *a,
                                                   const DBusString  *b,
                                                   int                len);
+dbus_bool_t   _dbus_string_equal_substring       (const DBusString  *a,
+                                                  int                a_start,
+                                                  int                a_len,
+                                                  const DBusString  *b,
+                                                  int                b_start);
 dbus_bool_t   _dbus_string_starts_with_c_str     (const DBusString  *a,
                                                   const char        *c_str);
 dbus_bool_t   _dbus_string_ends_with_c_str       (const DBusString  *a,