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 
00122 void
00123 _dbus_string_skip_white (const DBusString *str,
00124                          int               start,
00125                          int              *end)
00126 {
00127   int i;
00128   DBUS_CONST_STRING_PREAMBLE (str);
00129   _dbus_assert (start <= real->len);
00130   _dbus_assert (start >= 0);
00131   
00132   i = start;
00133   while (i < real->len)
00134     {
00135       if (!(real->str[i] == ' ' ||
00136             real->str[i] == '\n' ||
00137             real->str[i] == '\r' ||
00138             real->str[i] == '\t'))
00139         break;
00140       
00141       ++i;
00142     }
00143 
00144   _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
00145                                     real->str[i] == '\t'));
00146   
00147   if (end)
00148     *end = i;
00149 }
00150 
00153 #ifdef DBUS_BUILD_TESTS
00154 #include "dbus-test.h"
00155 #include <stdio.h>
00156 
00157 static void
00158 test_max_len (DBusString *str,
00159               int         max_len)
00160 {
00161   if (max_len > 0)
00162     {
00163       if (!_dbus_string_set_length (str, max_len - 1))
00164         _dbus_assert_not_reached ("setting len to one less than max should have worked");
00165     }
00166 
00167   if (!_dbus_string_set_length (str, max_len))
00168     _dbus_assert_not_reached ("setting len to max len should have worked");
00169 
00170   if (_dbus_string_set_length (str, max_len + 1))
00171     _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
00172 
00173   if (!_dbus_string_set_length (str, 0))
00174     _dbus_assert_not_reached ("setting len to zero should have worked");
00175 }
00176 
00177 static void
00178 test_hex_roundtrip (const unsigned char *data,
00179                     int                  len)
00180 {
00181   DBusString orig;
00182   DBusString encoded;
00183   DBusString decoded;
00184   int end;
00185 
00186   if (len < 0)
00187     len = strlen (data);
00188   
00189   if (!_dbus_string_init (&orig))
00190     _dbus_assert_not_reached ("could not init string");
00191 
00192   if (!_dbus_string_init (&encoded))
00193     _dbus_assert_not_reached ("could not init string");
00194   
00195   if (!_dbus_string_init (&decoded))
00196     _dbus_assert_not_reached ("could not init string");
00197 
00198   if (!_dbus_string_append_len (&orig, data, len))
00199     _dbus_assert_not_reached ("couldn't append orig data");
00200 
00201   if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
00202     _dbus_assert_not_reached ("could not encode");
00203 
00204   if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
00205     _dbus_assert_not_reached ("could not decode");
00206     
00207   _dbus_assert (_dbus_string_get_length (&encoded) == end);
00208 
00209   if (!_dbus_string_equal (&orig, &decoded))
00210     {
00211       const char *s;
00212       
00213       printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
00214               _dbus_string_get_length (&orig),
00215               _dbus_string_get_length (&encoded),
00216               _dbus_string_get_length (&decoded));
00217       printf ("Original: %s\n", data);
00218       s = _dbus_string_get_const_data (&decoded);
00219       printf ("Decoded: %s\n", s);
00220       _dbus_assert_not_reached ("original string not the same as string decoded from hex");
00221     }
00222   
00223   _dbus_string_free (&orig);
00224   _dbus_string_free (&encoded);
00225   _dbus_string_free (&decoded);  
00226 }
00227 
00228 typedef void (* TestRoundtripFunc) (const unsigned char *data,
00229                                     int                  len);
00230 static void
00231 test_roundtrips (TestRoundtripFunc func)
00232 {
00233   (* func) ("Hello this is a string\n", -1);
00234   (* func) ("Hello this is a string\n1", -1);
00235   (* func) ("Hello this is a string\n12", -1);
00236   (* func) ("Hello this is a string\n123", -1);
00237   (* func) ("Hello this is a string\n1234", -1);
00238   (* func) ("Hello this is a string\n12345", -1);
00239   (* func) ("", 0);
00240   (* func) ("1", 1);
00241   (* func) ("12", 2);
00242   (* func) ("123", 3);
00243   (* func) ("1234", 4);
00244   (* func) ("12345", 5);
00245   (* func) ("", 1);
00246   (* func) ("1", 2);
00247   (* func) ("12", 3);
00248   (* func) ("123", 4);
00249   (* func) ("1234", 5);
00250   (* func) ("12345", 6);
00251   {
00252     unsigned char buf[512];
00253     int i;
00254     
00255     i = 0;
00256     while (i < _DBUS_N_ELEMENTS (buf))
00257       {
00258         buf[i] = i;
00259         ++i;
00260       }
00261     i = 0;
00262     while (i < _DBUS_N_ELEMENTS (buf))
00263       {
00264         (* func) (buf, i);
00265         ++i;
00266       }
00267   }
00268 }
00269 
00270 #ifdef DBUS_BUILD_TESTS
00271 /* The max length thing is sort of a historical artifact
00272  * from a feature that turned out to be dumb; perhaps
00273  * we should purge it entirely. The problem with
00274  * the feature is that it looks like memory allocation
00275  * failure, but is not a transient or resolvable failure.
00276  */
00277 static void
00278 set_max_length (DBusString *str,
00279                 int         max_length)
00280 {
00281   DBusRealString *real;
00282   
00283   real = (DBusRealString*) str;
00284 
00285   real->max_length = max_length;
00286 }
00287 #endif /* DBUS_BUILD_TESTS */
00288 
00299 dbus_bool_t
00300 _dbus_string_test (void)
00301 {
00302   DBusString str;
00303   DBusString other;
00304   int i, end;
00305   long v;
00306   double d;
00307   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 };
00308   char *s;
00309   dbus_unichar_t ch;
00310   
00311   i = 0;
00312   while (i < _DBUS_N_ELEMENTS (lens))
00313     {
00314       if (!_dbus_string_init (&str))
00315         _dbus_assert_not_reached ("failed to init string");
00316 
00317       set_max_length (&str, lens[i]);
00318       
00319       test_max_len (&str, lens[i]);
00320       _dbus_string_free (&str);
00321 
00322       ++i;
00323     }
00324 
00325   /* Test shortening and setting length */
00326   i = 0;
00327   while (i < _DBUS_N_ELEMENTS (lens))
00328     {
00329       int j;
00330       
00331       if (!_dbus_string_init (&str))
00332         _dbus_assert_not_reached ("failed to init string");
00333 
00334       set_max_length (&str, lens[i]);
00335       
00336       if (!_dbus_string_set_length (&str, lens[i]))
00337         _dbus_assert_not_reached ("failed to set string length");
00338 
00339       j = lens[i];
00340       while (j > 0)
00341         {
00342           _dbus_assert (_dbus_string_get_length (&str) == j);
00343           if (j > 0)
00344             {
00345               _dbus_string_shorten (&str, 1);
00346               _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
00347             }
00348           --j;
00349         }
00350       
00351       _dbus_string_free (&str);
00352 
00353       ++i;
00354     }
00355 
00356   /* Test equality */
00357   if (!_dbus_string_init (&str))
00358     _dbus_assert_not_reached ("oom");
00359 
00360   if (!_dbus_string_append (&str, "Hello World"))
00361     _dbus_assert_not_reached ("oom");
00362 
00363   _dbus_string_init_const (&other, "H");
00364   _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
00365   _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
00366   _dbus_string_init_const (&other, "Hello");
00367   _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
00368   _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
00369   _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
00370   _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
00371   _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
00372   _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
00373 
00374   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
00375   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
00376   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
00377   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
00378   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
00379   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
00380 
00381   
00382   _dbus_string_init_const (&other, "World");
00383   _dbus_assert (_dbus_string_equal_substring (&str, 6,  5, &other, 0));
00384   _dbus_assert (_dbus_string_equal_substring (&str, 7,  4, &other, 1));
00385   _dbus_assert (_dbus_string_equal_substring (&str, 8,  3, &other, 2));
00386   _dbus_assert (_dbus_string_equal_substring (&str, 9,  2, &other, 3));
00387   _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
00388   _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
00389 
00390   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
00391   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
00392   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
00393   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
00394   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
00395   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
00396   
00397   _dbus_string_free (&str);
00398   
00399   /* Test appending data */
00400   if (!_dbus_string_init (&str))
00401     _dbus_assert_not_reached ("failed to init string");
00402 
00403   i = 0;
00404   while (i < 10)
00405     {
00406       if (!_dbus_string_append (&str, "a"))
00407         _dbus_assert_not_reached ("failed to append string to string\n");
00408 
00409       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
00410 
00411       if (!_dbus_string_append_byte (&str, 'b'))
00412         _dbus_assert_not_reached ("failed to append byte to string\n");
00413 
00414       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
00415                     
00416       ++i;
00417     }
00418 
00419   _dbus_string_free (&str);
00420 
00421   /* Check steal_data */
00422   
00423   if (!_dbus_string_init (&str))
00424     _dbus_assert_not_reached ("failed to init string");
00425 
00426   if (!_dbus_string_append (&str, "Hello World"))
00427     _dbus_assert_not_reached ("could not append to string");
00428 
00429   i = _dbus_string_get_length (&str);
00430   
00431   if (!_dbus_string_steal_data (&str, &s))
00432     _dbus_assert_not_reached ("failed to steal data");
00433 
00434   _dbus_assert (_dbus_string_get_length (&str) == 0);
00435   _dbus_assert (((int)strlen (s)) == i);
00436 
00437   dbus_free (s);
00438 
00439   /* Check move */
00440   
00441   if (!_dbus_string_append (&str, "Hello World"))
00442     _dbus_assert_not_reached ("could not append to string");
00443 
00444   i = _dbus_string_get_length (&str);
00445 
00446   if (!_dbus_string_init (&other))
00447     _dbus_assert_not_reached ("could not init string");
00448   
00449   if (!_dbus_string_move (&str, 0, &other, 0))
00450     _dbus_assert_not_reached ("could not move");
00451 
00452   _dbus_assert (_dbus_string_get_length (&str) == 0);
00453   _dbus_assert (_dbus_string_get_length (&other) == i);
00454 
00455   if (!_dbus_string_append (&str, "Hello World"))
00456     _dbus_assert_not_reached ("could not append to string");
00457   
00458   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
00459     _dbus_assert_not_reached ("could not move");
00460 
00461   _dbus_assert (_dbus_string_get_length (&str) == 0);
00462   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00463 
00464     if (!_dbus_string_append (&str, "Hello World"))
00465     _dbus_assert_not_reached ("could not append to string");
00466   
00467   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00468     _dbus_assert_not_reached ("could not move");
00469 
00470   _dbus_assert (_dbus_string_get_length (&str) == 0);
00471   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00472   
00473   _dbus_string_free (&other);
00474 
00475   /* Check copy */
00476   
00477   if (!_dbus_string_append (&str, "Hello World"))
00478     _dbus_assert_not_reached ("could not append to string");
00479 
00480   i = _dbus_string_get_length (&str);
00481   
00482   if (!_dbus_string_init (&other))
00483     _dbus_assert_not_reached ("could not init string");
00484   
00485   if (!_dbus_string_copy (&str, 0, &other, 0))
00486     _dbus_assert_not_reached ("could not copy");
00487 
00488   _dbus_assert (_dbus_string_get_length (&str) == i);
00489   _dbus_assert (_dbus_string_get_length (&other) == i);
00490 
00491   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
00492     _dbus_assert_not_reached ("could not copy");
00493 
00494   _dbus_assert (_dbus_string_get_length (&str) == i);
00495   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00496   _dbus_assert (_dbus_string_equal_c_str (&other,
00497                                           "Hello WorldHello World"));
00498 
00499   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00500     _dbus_assert_not_reached ("could not copy");
00501 
00502   _dbus_assert (_dbus_string_get_length (&str) == i);
00503   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00504   _dbus_assert (_dbus_string_equal_c_str (&other,
00505                                           "Hello WorldHello WorldHello World"));
00506   
00507   _dbus_string_free (&str);
00508   _dbus_string_free (&other);
00509 
00510   /* Check replace */
00511 
00512   if (!_dbus_string_init (&str))
00513     _dbus_assert_not_reached ("failed to init string");
00514   
00515   if (!_dbus_string_append (&str, "Hello World"))
00516     _dbus_assert_not_reached ("could not append to string");
00517 
00518   i = _dbus_string_get_length (&str);
00519   
00520   if (!_dbus_string_init (&other))
00521     _dbus_assert_not_reached ("could not init string");
00522   
00523   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00524                                  &other, 0, _dbus_string_get_length (&other)))
00525     _dbus_assert_not_reached ("could not replace");
00526 
00527   _dbus_assert (_dbus_string_get_length (&str) == i);
00528   _dbus_assert (_dbus_string_get_length (&other) == i);
00529   _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
00530   
00531   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00532                                  &other, 5, 1))
00533     _dbus_assert_not_reached ("could not replace center space");
00534 
00535   _dbus_assert (_dbus_string_get_length (&str) == i);
00536   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00537   _dbus_assert (_dbus_string_equal_c_str (&other,
00538                                           "HelloHello WorldWorld"));
00539 
00540   
00541   if (!_dbus_string_replace_len (&str, 1, 1,
00542                                  &other,
00543                                  _dbus_string_get_length (&other) - 1,
00544                                  1))
00545     _dbus_assert_not_reached ("could not replace end character");
00546   
00547   _dbus_assert (_dbus_string_get_length (&str) == i);
00548   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00549   _dbus_assert (_dbus_string_equal_c_str (&other,
00550                                           "HelloHello WorldWorle"));
00551   
00552   _dbus_string_free (&str);
00553   _dbus_string_free (&other);
00554   
00555   /* Check append/get unichar */
00556   
00557   if (!_dbus_string_init (&str))
00558     _dbus_assert_not_reached ("failed to init string");
00559 
00560   ch = 0;
00561   if (!_dbus_string_append_unichar (&str, 0xfffc))
00562     _dbus_assert_not_reached ("failed to append unichar");
00563 
00564   _dbus_string_get_unichar (&str, 0, &ch, &i);
00565 
00566   _dbus_assert (ch == 0xfffc);
00567   _dbus_assert (i == _dbus_string_get_length (&str));
00568 
00569   _dbus_string_free (&str);
00570 
00571   /* Check insert/set/get byte */
00572   
00573   if (!_dbus_string_init (&str))
00574     _dbus_assert_not_reached ("failed to init string");
00575 
00576   if (!_dbus_string_append (&str, "Hello"))
00577     _dbus_assert_not_reached ("failed to append Hello");
00578 
00579   _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
00580   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
00581   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
00582   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
00583   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
00584 
00585   _dbus_string_set_byte (&str, 1, 'q');
00586   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
00587 
00588   if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
00589     _dbus_assert_not_reached ("can't insert byte");
00590 
00591   if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
00592     _dbus_assert_not_reached ("can't insert byte");
00593 
00594   if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
00595     _dbus_assert_not_reached ("can't insert byte");
00596   
00597   _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
00598   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
00599   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
00600   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
00601   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
00602   _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
00603   _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
00604   _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
00605   _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
00606   _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
00607   _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
00608 
00609   _dbus_string_free (&str);
00610   
00611   /* Check append/parse int/double */
00612   
00613   if (!_dbus_string_init (&str))
00614     _dbus_assert_not_reached ("failed to init string");
00615 
00616   if (!_dbus_string_append_int (&str, 27))
00617     _dbus_assert_not_reached ("failed to append int");
00618 
00619   i = _dbus_string_get_length (&str);
00620 
00621   if (!_dbus_string_parse_int (&str, 0, &v, &end))
00622     _dbus_assert_not_reached ("failed to parse int");
00623 
00624   _dbus_assert (v == 27);
00625   _dbus_assert (end == i);
00626 
00627   _dbus_string_free (&str);
00628   
00629   if (!_dbus_string_init (&str))
00630     _dbus_assert_not_reached ("failed to init string");
00631   
00632   if (!_dbus_string_append_double (&str, 50.3))
00633     _dbus_assert_not_reached ("failed to append float");
00634 
00635   i = _dbus_string_get_length (&str);
00636 
00637   if (!_dbus_string_parse_double (&str, 0, &d, &end))
00638     _dbus_assert_not_reached ("failed to parse float");
00639 
00640   _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
00641   _dbus_assert (end == i);
00642 
00643   _dbus_string_free (&str);
00644 
00645   /* Test find */
00646   if (!_dbus_string_init (&str))
00647     _dbus_assert_not_reached ("failed to init string");
00648 
00649   if (!_dbus_string_append (&str, "Hello"))
00650     _dbus_assert_not_reached ("couldn't append to string");
00651   
00652   if (!_dbus_string_find (&str, 0, "He", &i))
00653     _dbus_assert_not_reached ("didn't find 'He'");
00654   _dbus_assert (i == 0);
00655 
00656   if (!_dbus_string_find (&str, 0, "Hello", &i))
00657     _dbus_assert_not_reached ("didn't find 'Hello'");
00658   _dbus_assert (i == 0);
00659   
00660   if (!_dbus_string_find (&str, 0, "ello", &i))
00661     _dbus_assert_not_reached ("didn't find 'ello'");
00662   _dbus_assert (i == 1);
00663 
00664   if (!_dbus_string_find (&str, 0, "lo", &i))
00665     _dbus_assert_not_reached ("didn't find 'lo'");
00666   _dbus_assert (i == 3);
00667 
00668   if (!_dbus_string_find (&str, 2, "lo", &i))
00669     _dbus_assert_not_reached ("didn't find 'lo'");
00670   _dbus_assert (i == 3);
00671 
00672   if (_dbus_string_find (&str, 4, "lo", &i))
00673     _dbus_assert_not_reached ("did find 'lo'");
00674   
00675   if (!_dbus_string_find (&str, 0, "l", &i))
00676     _dbus_assert_not_reached ("didn't find 'l'");
00677   _dbus_assert (i == 2);
00678 
00679   if (!_dbus_string_find (&str, 0, "H", &i))
00680     _dbus_assert_not_reached ("didn't find 'H'");
00681   _dbus_assert (i == 0);
00682 
00683   if (!_dbus_string_find (&str, 0, "", &i))
00684     _dbus_assert_not_reached ("didn't find ''");
00685   _dbus_assert (i == 0);
00686   
00687   if (_dbus_string_find (&str, 0, "Hello!", NULL))
00688     _dbus_assert_not_reached ("Did find 'Hello!'");
00689 
00690   if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
00691     _dbus_assert_not_reached ("Did find 'Oh, Hello'");
00692   
00693   if (_dbus_string_find (&str, 0, "ill", NULL))
00694     _dbus_assert_not_reached ("Did find 'ill'");
00695 
00696   if (_dbus_string_find (&str, 0, "q", NULL))
00697     _dbus_assert_not_reached ("Did find 'q'");
00698 
00699   if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
00700     _dbus_assert_not_reached ("Didn't find 'He'");
00701 
00702   if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
00703     _dbus_assert_not_reached ("Did find 'Hello'");
00704 
00705   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
00706     _dbus_assert_not_reached ("Did not find 'H'");
00707   _dbus_assert (i == 0);
00708 
00709   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
00710     _dbus_assert_not_reached ("Did not find 'o'");
00711   _dbus_assert (i == _dbus_string_get_length (&str) - 1);
00712 
00713   if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
00714     _dbus_assert_not_reached ("Did find 'o'");
00715   _dbus_assert (i == -1);
00716 
00717   if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
00718     _dbus_assert_not_reached ("Did find 'e'");
00719   _dbus_assert (i == -1);
00720 
00721   if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
00722     _dbus_assert_not_reached ("Didn't find 'e'");
00723   _dbus_assert (i == 1);
00724   
00725   _dbus_string_free (&str);
00726 
00727   /* Hex encoding */
00728   _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
00729   if (!_dbus_string_init (&other))
00730     _dbus_assert_not_reached ("could not init string");
00731 
00732   if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
00733     _dbus_assert_not_reached ("deccoded bogus hex string with no error");
00734 
00735   _dbus_assert (end == 8);
00736 
00737   _dbus_string_free (&other);
00738 
00739   test_roundtrips (test_hex_roundtrip);
00740   
00741   _dbus_string_free (&str);
00742   
00743   return TRUE;
00744 }
00745 
00746 #endif /* DBUS_BUILD_TESTS */

Generated on Tue Jul 7 15:14:00 2009 for D-BUS by  doxygen 1.4.6