dbus-string-util.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-string-util.c Would be in dbus-string.c, but not used in libdbus
00003  * 
00004  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-internals.h"
00025 #include "dbus-string.h"
00026 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
00027 #include "dbus-string-private.h"
00028 
00043 dbus_bool_t
00044 _dbus_string_ends_with_c_str (const DBusString *a,
00045                               const char       *c_str)
00046 {
00047   const unsigned char *ap;
00048   const unsigned char *bp;
00049   const unsigned char *a_end;
00050   unsigned long c_str_len;
00051   const DBusRealString *real_a = (const DBusRealString*) a;
00052   DBUS_GENERIC_STRING_PREAMBLE (real_a);
00053   _dbus_assert (c_str != NULL);
00054   
00055   c_str_len = strlen (c_str);
00056   if (((unsigned long)real_a->len) < c_str_len)
00057     return FALSE;
00058   
00059   ap = real_a->str + (real_a->len - c_str_len);
00060   bp = (const unsigned char*) c_str;
00061   a_end = real_a->str + real_a->len;
00062   while (ap != a_end)
00063     {
00064       if (*ap != *bp)
00065         return FALSE;
00066       
00067       ++ap;
00068       ++bp;
00069     }
00070 
00071   _dbus_assert (*ap == '\0');
00072   _dbus_assert (*bp == '\0');
00073   
00074   return TRUE;
00075 }
00076 
00087 dbus_bool_t
00088 _dbus_string_find_byte_backward (const DBusString  *str,
00089                                  int                start,
00090                                  unsigned char      byte,
00091                                  int               *found)
00092 {
00093   int i;
00094   DBUS_CONST_STRING_PREAMBLE (str);
00095   _dbus_assert (start <= real->len);
00096   _dbus_assert (start >= 0);
00097   _dbus_assert (found != NULL);
00098 
00099   i = start - 1;
00100   while (i >= 0)
00101     {
00102       if (real->str[i] == byte)
00103         break;
00104       
00105       --i;
00106     }
00107 
00108   if (found)
00109     *found = i;
00110 
00111   return i >= 0;
00112 }
00113 
00116 #ifdef DBUS_BUILD_TESTS
00117 #include "dbus-test.h"
00118 #include <stdio.h>
00119 
00120 static void
00121 test_max_len (DBusString *str,
00122               int         max_len)
00123 {
00124   if (max_len > 0)
00125     {
00126       if (!_dbus_string_set_length (str, max_len - 1))
00127         _dbus_assert_not_reached ("setting len to one less than max should have worked");
00128     }
00129 
00130   if (!_dbus_string_set_length (str, max_len))
00131     _dbus_assert_not_reached ("setting len to max len should have worked");
00132 
00133   if (_dbus_string_set_length (str, max_len + 1))
00134     _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
00135 
00136   if (!_dbus_string_set_length (str, 0))
00137     _dbus_assert_not_reached ("setting len to zero should have worked");
00138 }
00139 
00140 static void
00141 test_hex_roundtrip (const unsigned char *data,
00142                     int                  len)
00143 {
00144   DBusString orig;
00145   DBusString encoded;
00146   DBusString decoded;
00147   int end;
00148 
00149   if (len < 0)
00150     len = strlen (data);
00151   
00152   if (!_dbus_string_init (&orig))
00153     _dbus_assert_not_reached ("could not init string");
00154 
00155   if (!_dbus_string_init (&encoded))
00156     _dbus_assert_not_reached ("could not init string");
00157   
00158   if (!_dbus_string_init (&decoded))
00159     _dbus_assert_not_reached ("could not init string");
00160 
00161   if (!_dbus_string_append_len (&orig, data, len))
00162     _dbus_assert_not_reached ("couldn't append orig data");
00163 
00164   if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
00165     _dbus_assert_not_reached ("could not encode");
00166 
00167   if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
00168     _dbus_assert_not_reached ("could not decode");
00169     
00170   _dbus_assert (_dbus_string_get_length (&encoded) == end);
00171 
00172   if (!_dbus_string_equal (&orig, &decoded))
00173     {
00174       const char *s;
00175       
00176       printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
00177               _dbus_string_get_length (&orig),
00178               _dbus_string_get_length (&encoded),
00179               _dbus_string_get_length (&decoded));
00180       printf ("Original: %s\n", data);
00181       s = _dbus_string_get_const_data (&decoded);
00182       printf ("Decoded: %s\n", s);
00183       _dbus_assert_not_reached ("original string not the same as string decoded from hex");
00184     }
00185   
00186   _dbus_string_free (&orig);
00187   _dbus_string_free (&encoded);
00188   _dbus_string_free (&decoded);  
00189 }
00190 
00191 typedef void (* TestRoundtripFunc) (const unsigned char *data,
00192                                     int                  len);
00193 static void
00194 test_roundtrips (TestRoundtripFunc func)
00195 {
00196   (* func) ("Hello this is a string\n", -1);
00197   (* func) ("Hello this is a string\n1", -1);
00198   (* func) ("Hello this is a string\n12", -1);
00199   (* func) ("Hello this is a string\n123", -1);
00200   (* func) ("Hello this is a string\n1234", -1);
00201   (* func) ("Hello this is a string\n12345", -1);
00202   (* func) ("", 0);
00203   (* func) ("1", 1);
00204   (* func) ("12", 2);
00205   (* func) ("123", 3);
00206   (* func) ("1234", 4);
00207   (* func) ("12345", 5);
00208   (* func) ("", 1);
00209   (* func) ("1", 2);
00210   (* func) ("12", 3);
00211   (* func) ("123", 4);
00212   (* func) ("1234", 5);
00213   (* func) ("12345", 6);
00214   {
00215     unsigned char buf[512];
00216     int i;
00217     
00218     i = 0;
00219     while (i < _DBUS_N_ELEMENTS (buf))
00220       {
00221         buf[i] = i;
00222         ++i;
00223       }
00224     i = 0;
00225     while (i < _DBUS_N_ELEMENTS (buf))
00226       {
00227         (* func) (buf, i);
00228         ++i;
00229       }
00230   }
00231 }
00232 
00233 #ifdef DBUS_BUILD_TESTS
00234 /* The max length thing is sort of a historical artifact
00235  * from a feature that turned out to be dumb; perhaps
00236  * we should purge it entirely. The problem with
00237  * the feature is that it looks like memory allocation
00238  * failure, but is not a transient or resolvable failure.
00239  */
00240 static void
00241 set_max_length (DBusString *str,
00242                 int         max_length)
00243 {
00244   DBusRealString *real;
00245   
00246   real = (DBusRealString*) str;
00247 
00248   real->max_length = max_length;
00249 }
00250 #endif /* DBUS_BUILD_TESTS */
00251 
00262 dbus_bool_t
00263 _dbus_string_test (void)
00264 {
00265   DBusString str;
00266   DBusString other;
00267   int i, end;
00268   long v;
00269   double d;
00270   int lens[] = { 0, 1, 2, 3, 4, 5, 10, 16, 17, 18, 25, 31, 32, 33, 34, 35, 63, 64, 65, 66, 67, 68, 69, 70, 71, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136 };
00271   char *s;
00272   dbus_unichar_t ch;
00273   
00274   i = 0;
00275   while (i < _DBUS_N_ELEMENTS (lens))
00276     {
00277       if (!_dbus_string_init (&str))
00278         _dbus_assert_not_reached ("failed to init string");
00279 
00280       set_max_length (&str, lens[i]);
00281       
00282       test_max_len (&str, lens[i]);
00283       _dbus_string_free (&str);
00284 
00285       ++i;
00286     }
00287 
00288   /* Test shortening and setting length */
00289   i = 0;
00290   while (i < _DBUS_N_ELEMENTS (lens))
00291     {
00292       int j;
00293       
00294       if (!_dbus_string_init (&str))
00295         _dbus_assert_not_reached ("failed to init string");
00296 
00297       set_max_length (&str, lens[i]);
00298       
00299       if (!_dbus_string_set_length (&str, lens[i]))
00300         _dbus_assert_not_reached ("failed to set string length");
00301 
00302       j = lens[i];
00303       while (j > 0)
00304         {
00305           _dbus_assert (_dbus_string_get_length (&str) == j);
00306           if (j > 0)
00307             {
00308               _dbus_string_shorten (&str, 1);
00309               _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
00310             }
00311           --j;
00312         }
00313       
00314       _dbus_string_free (&str);
00315 
00316       ++i;
00317     }
00318 
00319   /* Test equality */
00320   if (!_dbus_string_init (&str))
00321     _dbus_assert_not_reached ("oom");
00322 
00323   if (!_dbus_string_append (&str, "Hello World"))
00324     _dbus_assert_not_reached ("oom");
00325 
00326   _dbus_string_init_const (&other, "H");
00327   _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
00328   _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
00329   _dbus_string_init_const (&other, "Hello");
00330   _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
00331   _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
00332   _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
00333   _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
00334   _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
00335   _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
00336 
00337   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
00338   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
00339   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
00340   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
00341   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
00342   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
00343 
00344   
00345   _dbus_string_init_const (&other, "World");
00346   _dbus_assert (_dbus_string_equal_substring (&str, 6,  5, &other, 0));
00347   _dbus_assert (_dbus_string_equal_substring (&str, 7,  4, &other, 1));
00348   _dbus_assert (_dbus_string_equal_substring (&str, 8,  3, &other, 2));
00349   _dbus_assert (_dbus_string_equal_substring (&str, 9,  2, &other, 3));
00350   _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
00351   _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
00352 
00353   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
00354   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
00355   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
00356   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
00357   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
00358   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
00359   
00360   _dbus_string_free (&str);
00361   
00362   /* Test appending data */
00363   if (!_dbus_string_init (&str))
00364     _dbus_assert_not_reached ("failed to init string");
00365 
00366   i = 0;
00367   while (i < 10)
00368     {
00369       if (!_dbus_string_append (&str, "a"))
00370         _dbus_assert_not_reached ("failed to append string to string\n");
00371 
00372       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
00373 
00374       if (!_dbus_string_append_byte (&str, 'b'))
00375         _dbus_assert_not_reached ("failed to append byte to string\n");
00376 
00377       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
00378                     
00379       ++i;
00380     }
00381 
00382   _dbus_string_free (&str);
00383 
00384   /* Check steal_data */
00385   
00386   if (!_dbus_string_init (&str))
00387     _dbus_assert_not_reached ("failed to init string");
00388 
00389   if (!_dbus_string_append (&str, "Hello World"))
00390     _dbus_assert_not_reached ("could not append to string");
00391 
00392   i = _dbus_string_get_length (&str);
00393   
00394   if (!_dbus_string_steal_data (&str, &s))
00395     _dbus_assert_not_reached ("failed to steal data");
00396 
00397   _dbus_assert (_dbus_string_get_length (&str) == 0);
00398   _dbus_assert (((int)strlen (s)) == i);
00399 
00400   dbus_free (s);
00401 
00402   /* Check move */
00403   
00404   if (!_dbus_string_append (&str, "Hello World"))
00405     _dbus_assert_not_reached ("could not append to string");
00406 
00407   i = _dbus_string_get_length (&str);
00408 
00409   if (!_dbus_string_init (&other))
00410     _dbus_assert_not_reached ("could not init string");
00411   
00412   if (!_dbus_string_move (&str, 0, &other, 0))
00413     _dbus_assert_not_reached ("could not move");
00414 
00415   _dbus_assert (_dbus_string_get_length (&str) == 0);
00416   _dbus_assert (_dbus_string_get_length (&other) == i);
00417 
00418   if (!_dbus_string_append (&str, "Hello World"))
00419     _dbus_assert_not_reached ("could not append to string");
00420   
00421   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
00422     _dbus_assert_not_reached ("could not move");
00423 
00424   _dbus_assert (_dbus_string_get_length (&str) == 0);
00425   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00426 
00427     if (!_dbus_string_append (&str, "Hello World"))
00428     _dbus_assert_not_reached ("could not append to string");
00429   
00430   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00431     _dbus_assert_not_reached ("could not move");
00432 
00433   _dbus_assert (_dbus_string_get_length (&str) == 0);
00434   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00435   
00436   _dbus_string_free (&other);
00437 
00438   /* Check copy */
00439   
00440   if (!_dbus_string_append (&str, "Hello World"))
00441     _dbus_assert_not_reached ("could not append to string");
00442 
00443   i = _dbus_string_get_length (&str);
00444   
00445   if (!_dbus_string_init (&other))
00446     _dbus_assert_not_reached ("could not init string");
00447   
00448   if (!_dbus_string_copy (&str, 0, &other, 0))
00449     _dbus_assert_not_reached ("could not copy");
00450 
00451   _dbus_assert (_dbus_string_get_length (&str) == i);
00452   _dbus_assert (_dbus_string_get_length (&other) == i);
00453 
00454   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
00455     _dbus_assert_not_reached ("could not copy");
00456 
00457   _dbus_assert (_dbus_string_get_length (&str) == i);
00458   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00459   _dbus_assert (_dbus_string_equal_c_str (&other,
00460                                           "Hello WorldHello World"));
00461 
00462   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00463     _dbus_assert_not_reached ("could not copy");
00464 
00465   _dbus_assert (_dbus_string_get_length (&str) == i);
00466   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00467   _dbus_assert (_dbus_string_equal_c_str (&other,
00468                                           "Hello WorldHello WorldHello World"));
00469   
00470   _dbus_string_free (&str);
00471   _dbus_string_free (&other);
00472 
00473   /* Check replace */
00474 
00475   if (!_dbus_string_init (&str))
00476     _dbus_assert_not_reached ("failed to init string");
00477   
00478   if (!_dbus_string_append (&str, "Hello World"))
00479     _dbus_assert_not_reached ("could not append to string");
00480 
00481   i = _dbus_string_get_length (&str);
00482   
00483   if (!_dbus_string_init (&other))
00484     _dbus_assert_not_reached ("could not init string");
00485   
00486   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00487                                  &other, 0, _dbus_string_get_length (&other)))
00488     _dbus_assert_not_reached ("could not replace");
00489 
00490   _dbus_assert (_dbus_string_get_length (&str) == i);
00491   _dbus_assert (_dbus_string_get_length (&other) == i);
00492   _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
00493   
00494   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00495                                  &other, 5, 1))
00496     _dbus_assert_not_reached ("could not replace center space");
00497 
00498   _dbus_assert (_dbus_string_get_length (&str) == i);
00499   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00500   _dbus_assert (_dbus_string_equal_c_str (&other,
00501                                           "HelloHello WorldWorld"));
00502 
00503   
00504   if (!_dbus_string_replace_len (&str, 1, 1,
00505                                  &other,
00506                                  _dbus_string_get_length (&other) - 1,
00507                                  1))
00508     _dbus_assert_not_reached ("could not replace end character");
00509   
00510   _dbus_assert (_dbus_string_get_length (&str) == i);
00511   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00512   _dbus_assert (_dbus_string_equal_c_str (&other,
00513                                           "HelloHello WorldWorle"));
00514   
00515   _dbus_string_free (&str);
00516   _dbus_string_free (&other);
00517   
00518   /* Check append/get unichar */
00519   
00520   if (!_dbus_string_init (&str))
00521     _dbus_assert_not_reached ("failed to init string");
00522 
00523   ch = 0;
00524   if (!_dbus_string_append_unichar (&str, 0xfffc))
00525     _dbus_assert_not_reached ("failed to append unichar");
00526 
00527   _dbus_string_get_unichar (&str, 0, &ch, &i);
00528 
00529   _dbus_assert (ch == 0xfffc);
00530   _dbus_assert (i == _dbus_string_get_length (&str));
00531 
00532   _dbus_string_free (&str);
00533 
00534   /* Check insert/set/get byte */
00535   
00536   if (!_dbus_string_init (&str))
00537     _dbus_assert_not_reached ("failed to init string");
00538 
00539   if (!_dbus_string_append (&str, "Hello"))
00540     _dbus_assert_not_reached ("failed to append Hello");
00541 
00542   _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
00543   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
00544   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
00545   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
00546   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
00547 
00548   _dbus_string_set_byte (&str, 1, 'q');
00549   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
00550 
00551   if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
00552     _dbus_assert_not_reached ("can't insert byte");
00553 
00554   if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
00555     _dbus_assert_not_reached ("can't insert byte");
00556 
00557   if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
00558     _dbus_assert_not_reached ("can't insert byte");
00559   
00560   _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
00561   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
00562   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
00563   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
00564   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
00565   _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
00566   _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
00567   _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
00568   _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
00569   _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
00570   _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
00571 
00572   _dbus_string_free (&str);
00573   
00574   /* Check append/parse int/double */
00575   
00576   if (!_dbus_string_init (&str))
00577     _dbus_assert_not_reached ("failed to init string");
00578 
00579   if (!_dbus_string_append_int (&str, 27))
00580     _dbus_assert_not_reached ("failed to append int");
00581 
00582   i = _dbus_string_get_length (&str);
00583 
00584   if (!_dbus_string_parse_int (&str, 0, &v, &end))
00585     _dbus_assert_not_reached ("failed to parse int");
00586 
00587   _dbus_assert (v == 27);
00588   _dbus_assert (end == i);
00589 
00590   _dbus_string_free (&str);
00591   
00592   if (!_dbus_string_init (&str))
00593     _dbus_assert_not_reached ("failed to init string");
00594   
00595   if (!_dbus_string_append_double (&str, 50.3))
00596     _dbus_assert_not_reached ("failed to append float");
00597 
00598   i = _dbus_string_get_length (&str);
00599 
00600   if (!_dbus_string_parse_double (&str, 0, &d, &end))
00601     _dbus_assert_not_reached ("failed to parse float");
00602 
00603   _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
00604   _dbus_assert (end == i);
00605 
00606   _dbus_string_free (&str);
00607 
00608   /* Test find */
00609   if (!_dbus_string_init (&str))
00610     _dbus_assert_not_reached ("failed to init string");
00611 
00612   if (!_dbus_string_append (&str, "Hello"))
00613     _dbus_assert_not_reached ("couldn't append to string");
00614   
00615   if (!_dbus_string_find (&str, 0, "He", &i))
00616     _dbus_assert_not_reached ("didn't find 'He'");
00617   _dbus_assert (i == 0);
00618 
00619   if (!_dbus_string_find (&str, 0, "Hello", &i))
00620     _dbus_assert_not_reached ("didn't find 'Hello'");
00621   _dbus_assert (i == 0);
00622   
00623   if (!_dbus_string_find (&str, 0, "ello", &i))
00624     _dbus_assert_not_reached ("didn't find 'ello'");
00625   _dbus_assert (i == 1);
00626 
00627   if (!_dbus_string_find (&str, 0, "lo", &i))
00628     _dbus_assert_not_reached ("didn't find 'lo'");
00629   _dbus_assert (i == 3);
00630 
00631   if (!_dbus_string_find (&str, 2, "lo", &i))
00632     _dbus_assert_not_reached ("didn't find 'lo'");
00633   _dbus_assert (i == 3);
00634 
00635   if (_dbus_string_find (&str, 4, "lo", &i))
00636     _dbus_assert_not_reached ("did find 'lo'");
00637   
00638   if (!_dbus_string_find (&str, 0, "l", &i))
00639     _dbus_assert_not_reached ("didn't find 'l'");
00640   _dbus_assert (i == 2);
00641 
00642   if (!_dbus_string_find (&str, 0, "H", &i))
00643     _dbus_assert_not_reached ("didn't find 'H'");
00644   _dbus_assert (i == 0);
00645 
00646   if (!_dbus_string_find (&str, 0, "", &i))
00647     _dbus_assert_not_reached ("didn't find ''");
00648   _dbus_assert (i == 0);
00649   
00650   if (_dbus_string_find (&str, 0, "Hello!", NULL))
00651     _dbus_assert_not_reached ("Did find 'Hello!'");
00652 
00653   if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
00654     _dbus_assert_not_reached ("Did find 'Oh, Hello'");
00655   
00656   if (_dbus_string_find (&str, 0, "ill", NULL))
00657     _dbus_assert_not_reached ("Did find 'ill'");
00658 
00659   if (_dbus_string_find (&str, 0, "q", NULL))
00660     _dbus_assert_not_reached ("Did find 'q'");
00661 
00662   if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
00663     _dbus_assert_not_reached ("Didn't find 'He'");
00664 
00665   if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
00666     _dbus_assert_not_reached ("Did find 'Hello'");
00667 
00668   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
00669     _dbus_assert_not_reached ("Did not find 'H'");
00670   _dbus_assert (i == 0);
00671 
00672   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
00673     _dbus_assert_not_reached ("Did not find 'o'");
00674   _dbus_assert (i == _dbus_string_get_length (&str) - 1);
00675 
00676   if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
00677     _dbus_assert_not_reached ("Did find 'o'");
00678   _dbus_assert (i == -1);
00679 
00680   if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
00681     _dbus_assert_not_reached ("Did find 'e'");
00682   _dbus_assert (i == -1);
00683 
00684   if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
00685     _dbus_assert_not_reached ("Didn't find 'e'");
00686   _dbus_assert (i == 1);
00687   
00688   _dbus_string_free (&str);
00689 
00690   /* Hex encoding */
00691   _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
00692   if (!_dbus_string_init (&other))
00693     _dbus_assert_not_reached ("could not init string");
00694 
00695   if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
00696     _dbus_assert_not_reached ("deccoded bogus hex string with no error");
00697 
00698   _dbus_assert (end == 8);
00699 
00700   _dbus_string_free (&other);
00701 
00702   test_roundtrips (test_hex_roundtrip);
00703   
00704   _dbus_string_free (&str);
00705   
00706   return TRUE;
00707 }
00708 
00709 #endif /* DBUS_BUILD_TESTS */

Generated on Fri Sep 21 18:12:13 2007 for D-Bus by  doxygen 1.5.1