D-Bus  1.6.8
dbus-string-util.c
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-internals.h"
00027 #include "dbus-string.h"
00028 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
00029 #include "dbus-string-private.h"
00030 
00045 dbus_bool_t
00046 _dbus_string_ends_with_c_str (const DBusString *a,
00047                               const char       *c_str)
00048 {
00049   const unsigned char *ap;
00050   const unsigned char *bp;
00051   const unsigned char *a_end;
00052   unsigned long c_str_len;
00053   const DBusRealString *real_a = (const DBusRealString*) a;
00054   DBUS_GENERIC_STRING_PREAMBLE (real_a);
00055   _dbus_assert (c_str != NULL);
00056   
00057   c_str_len = strlen (c_str);
00058   if (((unsigned long)real_a->len) < c_str_len)
00059     return FALSE;
00060   
00061   ap = real_a->str + (real_a->len - c_str_len);
00062   bp = (const unsigned char*) c_str;
00063   a_end = real_a->str + real_a->len;
00064   while (ap != a_end)
00065     {
00066       if (*ap != *bp)
00067         return FALSE;
00068       
00069       ++ap;
00070       ++bp;
00071     }
00072 
00073   _dbus_assert (*ap == '\0');
00074   _dbus_assert (*bp == '\0');
00075   
00076   return TRUE;
00077 }
00078 
00089 dbus_bool_t
00090 _dbus_string_find_byte_backward (const DBusString  *str,
00091                                  int                start,
00092                                  unsigned char      byte,
00093                                  int               *found)
00094 {
00095   int i;
00096   DBUS_CONST_STRING_PREAMBLE (str);
00097   _dbus_assert (start <= real->len);
00098   _dbus_assert (start >= 0);
00099   _dbus_assert (found != NULL);
00100 
00101   i = start - 1;
00102   while (i >= 0)
00103     {
00104       if (real->str[i] == byte)
00105         break;
00106       
00107       --i;
00108     }
00109 
00110   if (found)
00111     *found = i;
00112 
00113   return i >= 0;
00114 }
00115 
00118 #ifdef DBUS_BUILD_TESTS
00119 #include "dbus-test.h"
00120 #include <stdio.h>
00121 
00122 static void
00123 test_hex_roundtrip (const unsigned char *data,
00124                     int                  len)
00125 {
00126   DBusString orig;
00127   DBusString encoded;
00128   DBusString decoded;
00129   int end;
00130 
00131   if (len < 0)
00132     len = strlen (data);
00133   
00134   if (!_dbus_string_init (&orig))
00135     _dbus_assert_not_reached ("could not init string");
00136 
00137   if (!_dbus_string_init (&encoded))
00138     _dbus_assert_not_reached ("could not init string");
00139   
00140   if (!_dbus_string_init (&decoded))
00141     _dbus_assert_not_reached ("could not init string");
00142 
00143   if (!_dbus_string_append_len (&orig, data, len))
00144     _dbus_assert_not_reached ("couldn't append orig data");
00145 
00146   if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
00147     _dbus_assert_not_reached ("could not encode");
00148 
00149   if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
00150     _dbus_assert_not_reached ("could not decode");
00151     
00152   _dbus_assert (_dbus_string_get_length (&encoded) == end);
00153 
00154   if (!_dbus_string_equal (&orig, &decoded))
00155     {
00156       const char *s;
00157       
00158       printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
00159               _dbus_string_get_length (&orig),
00160               _dbus_string_get_length (&encoded),
00161               _dbus_string_get_length (&decoded));
00162       printf ("Original: %s\n", data);
00163       s = _dbus_string_get_const_data (&decoded);
00164       printf ("Decoded: %s\n", s);
00165       _dbus_assert_not_reached ("original string not the same as string decoded from hex");
00166     }
00167   
00168   _dbus_string_free (&orig);
00169   _dbus_string_free (&encoded);
00170   _dbus_string_free (&decoded);  
00171 }
00172 
00173 typedef void (* TestRoundtripFunc) (const unsigned char *data,
00174                                     int                  len);
00175 static void
00176 test_roundtrips (TestRoundtripFunc func)
00177 {
00178   (* func) ("Hello this is a string\n", -1);
00179   (* func) ("Hello this is a string\n1", -1);
00180   (* func) ("Hello this is a string\n12", -1);
00181   (* func) ("Hello this is a string\n123", -1);
00182   (* func) ("Hello this is a string\n1234", -1);
00183   (* func) ("Hello this is a string\n12345", -1);
00184   (* func) ("", 0);
00185   (* func) ("1", 1);
00186   (* func) ("12", 2);
00187   (* func) ("123", 3);
00188   (* func) ("1234", 4);
00189   (* func) ("12345", 5);
00190   (* func) ("", 1);
00191   (* func) ("1", 2);
00192   (* func) ("12", 3);
00193   (* func) ("123", 4);
00194   (* func) ("1234", 5);
00195   (* func) ("12345", 6);
00196   {
00197     unsigned char buf[512];
00198     int i;
00199     
00200     i = 0;
00201     while (i < _DBUS_N_ELEMENTS (buf))
00202       {
00203         buf[i] = i;
00204         ++i;
00205       }
00206     i = 0;
00207     while (i < _DBUS_N_ELEMENTS (buf))
00208       {
00209         (* func) (buf, i);
00210         ++i;
00211       }
00212   }
00213 }
00214 
00225 dbus_bool_t
00226 _dbus_string_test (void)
00227 {
00228   DBusString str;
00229   DBusString other;
00230   int i, a, end;
00231   long v;
00232   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 };
00233   char *s;
00234 
00235   /* Test shortening and setting length */
00236   i = 0;
00237   while (i < _DBUS_N_ELEMENTS (lens))
00238     {
00239       int j;
00240       
00241       if (!_dbus_string_init (&str))
00242         _dbus_assert_not_reached ("failed to init string");
00243 
00244       if (!_dbus_string_set_length (&str, lens[i]))
00245         _dbus_assert_not_reached ("failed to set string length");
00246 
00247       j = lens[i];
00248       while (j > 0)
00249         {
00250           _dbus_assert (_dbus_string_get_length (&str) == j);
00251           if (j > 0)
00252             {
00253               _dbus_string_shorten (&str, 1);
00254               _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
00255             }
00256           --j;
00257         }
00258       
00259       _dbus_string_free (&str);
00260 
00261       ++i;
00262     }
00263 
00264   /* Test equality */
00265   if (!_dbus_string_init (&str))
00266     _dbus_assert_not_reached ("oom");
00267 
00268   if (!_dbus_string_append (&str, "Hello World"))
00269     _dbus_assert_not_reached ("oom");
00270 
00271   _dbus_string_init_const (&other, "H");
00272   _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
00273   _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
00274   _dbus_string_init_const (&other, "Hello");
00275   _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
00276   _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
00277   _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
00278   _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
00279   _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
00280   _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
00281 
00282   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
00283   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
00284   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
00285   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
00286   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
00287   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
00288 
00289   
00290   _dbus_string_init_const (&other, "World");
00291   _dbus_assert (_dbus_string_equal_substring (&str, 6,  5, &other, 0));
00292   _dbus_assert (_dbus_string_equal_substring (&str, 7,  4, &other, 1));
00293   _dbus_assert (_dbus_string_equal_substring (&str, 8,  3, &other, 2));
00294   _dbus_assert (_dbus_string_equal_substring (&str, 9,  2, &other, 3));
00295   _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
00296   _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
00297 
00298   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
00299   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
00300   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
00301   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
00302   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
00303   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
00304   
00305   _dbus_string_free (&str);
00306   
00307   /* Test appending data */
00308   if (!_dbus_string_init (&str))
00309     _dbus_assert_not_reached ("failed to init string");
00310 
00311   i = 0;
00312   while (i < 10)
00313     {
00314       if (!_dbus_string_append (&str, "a"))
00315         _dbus_assert_not_reached ("failed to append string to string\n");
00316 
00317       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
00318 
00319       if (!_dbus_string_append_byte (&str, 'b'))
00320         _dbus_assert_not_reached ("failed to append byte to string\n");
00321 
00322       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
00323                     
00324       ++i;
00325     }
00326 
00327   _dbus_string_free (&str);
00328 
00329   /* Check steal_data */
00330   
00331   if (!_dbus_string_init (&str))
00332     _dbus_assert_not_reached ("failed to init string");
00333 
00334   if (!_dbus_string_append (&str, "Hello World"))
00335     _dbus_assert_not_reached ("could not append to string");
00336 
00337   i = _dbus_string_get_length (&str);
00338   
00339   if (!_dbus_string_steal_data (&str, &s))
00340     _dbus_assert_not_reached ("failed to steal data");
00341 
00342   _dbus_assert (_dbus_string_get_length (&str) == 0);
00343   _dbus_assert (((int)strlen (s)) == i);
00344 
00345   dbus_free (s);
00346 
00347   /* Check move */
00348   
00349   if (!_dbus_string_append (&str, "Hello World"))
00350     _dbus_assert_not_reached ("could not append to string");
00351 
00352   i = _dbus_string_get_length (&str);
00353 
00354   if (!_dbus_string_init (&other))
00355     _dbus_assert_not_reached ("could not init string");
00356   
00357   if (!_dbus_string_move (&str, 0, &other, 0))
00358     _dbus_assert_not_reached ("could not move");
00359 
00360   _dbus_assert (_dbus_string_get_length (&str) == 0);
00361   _dbus_assert (_dbus_string_get_length (&other) == i);
00362 
00363   if (!_dbus_string_append (&str, "Hello World"))
00364     _dbus_assert_not_reached ("could not append to string");
00365   
00366   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
00367     _dbus_assert_not_reached ("could not move");
00368 
00369   _dbus_assert (_dbus_string_get_length (&str) == 0);
00370   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00371 
00372     if (!_dbus_string_append (&str, "Hello World"))
00373     _dbus_assert_not_reached ("could not append to string");
00374   
00375   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00376     _dbus_assert_not_reached ("could not move");
00377 
00378   _dbus_assert (_dbus_string_get_length (&str) == 0);
00379   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00380   
00381   _dbus_string_free (&other);
00382 
00383   /* Check copy */
00384   
00385   if (!_dbus_string_append (&str, "Hello World"))
00386     _dbus_assert_not_reached ("could not append to string");
00387 
00388   i = _dbus_string_get_length (&str);
00389   
00390   if (!_dbus_string_init (&other))
00391     _dbus_assert_not_reached ("could not init string");
00392   
00393   if (!_dbus_string_copy (&str, 0, &other, 0))
00394     _dbus_assert_not_reached ("could not copy");
00395 
00396   _dbus_assert (_dbus_string_get_length (&str) == i);
00397   _dbus_assert (_dbus_string_get_length (&other) == i);
00398 
00399   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
00400     _dbus_assert_not_reached ("could not copy");
00401 
00402   _dbus_assert (_dbus_string_get_length (&str) == i);
00403   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00404   _dbus_assert (_dbus_string_equal_c_str (&other,
00405                                           "Hello WorldHello World"));
00406 
00407   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00408     _dbus_assert_not_reached ("could not copy");
00409 
00410   _dbus_assert (_dbus_string_get_length (&str) == i);
00411   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00412   _dbus_assert (_dbus_string_equal_c_str (&other,
00413                                           "Hello WorldHello WorldHello World"));
00414   
00415   _dbus_string_free (&str);
00416   _dbus_string_free (&other);
00417 
00418   /* Check replace */
00419 
00420   if (!_dbus_string_init (&str))
00421     _dbus_assert_not_reached ("failed to init string");
00422   
00423   if (!_dbus_string_append (&str, "Hello World"))
00424     _dbus_assert_not_reached ("could not append to string");
00425 
00426   i = _dbus_string_get_length (&str);
00427   
00428   if (!_dbus_string_init (&other))
00429     _dbus_assert_not_reached ("could not init string");
00430   
00431   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00432                                  &other, 0, _dbus_string_get_length (&other)))
00433     _dbus_assert_not_reached ("could not replace");
00434 
00435   _dbus_assert (_dbus_string_get_length (&str) == i);
00436   _dbus_assert (_dbus_string_get_length (&other) == i);
00437   _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
00438   
00439   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00440                                  &other, 5, 1))
00441     _dbus_assert_not_reached ("could not replace center space");
00442 
00443   _dbus_assert (_dbus_string_get_length (&str) == i);
00444   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00445   _dbus_assert (_dbus_string_equal_c_str (&other,
00446                                           "HelloHello WorldWorld"));
00447 
00448   
00449   if (!_dbus_string_replace_len (&str, 1, 1,
00450                                  &other,
00451                                  _dbus_string_get_length (&other) - 1,
00452                                  1))
00453     _dbus_assert_not_reached ("could not replace end character");
00454   
00455   _dbus_assert (_dbus_string_get_length (&str) == i);
00456   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00457   _dbus_assert (_dbus_string_equal_c_str (&other,
00458                                           "HelloHello WorldWorle"));
00459 
00460   _dbus_string_free (&str);
00461   _dbus_string_free (&other);
00462 
00463   /* Different tests are provided because different behaviours are
00464    * implemented in _dbus_string_replace_len() in function of replacing and
00465    * replaced lengths
00466    */
00467 
00468   if (!_dbus_string_init (&str))
00469     _dbus_assert_not_reached ("failed to init string");
00470   
00471   if (!_dbus_string_append (&str, "Hello World"))
00472     _dbus_assert_not_reached ("could not append to string");
00473 
00474   i = _dbus_string_get_length (&str);
00475   
00476   if (!_dbus_string_init (&other))
00477     _dbus_assert_not_reached ("could not init string");
00478 
00479   if (!_dbus_string_append (&other, "Foo String"))
00480     _dbus_assert_not_reached ("could not append to string");
00481 
00482   a = _dbus_string_get_length (&other);
00483 
00484   if (!_dbus_string_replace_len (&str, 0, 6,
00485                                  &other, 4, 0))
00486     _dbus_assert_not_reached ("could not replace 0 length");
00487 
00488   _dbus_assert (_dbus_string_get_length (&str) == i);
00489   _dbus_assert (_dbus_string_get_length (&other) == a + 6);
00490   _dbus_assert (_dbus_string_equal_c_str (&other,
00491                                           "Foo Hello String"));
00492 
00493   if (!_dbus_string_replace_len (&str, 5, 6,
00494                                  &other,
00495                                  _dbus_string_get_length (&other),
00496                                  0))
00497     _dbus_assert_not_reached ("could not replace at the end");
00498 
00499   _dbus_assert (_dbus_string_get_length (&str) == i);
00500   _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
00501   _dbus_assert (_dbus_string_equal_c_str (&other,
00502                                           "Foo Hello String World"));
00503 
00504   if (!_dbus_string_replace_len (&str, 0, 5,
00505                                  &other,
00506                                  _dbus_string_get_length (&other) - 5,
00507                                  5))
00508     _dbus_assert_not_reached ("could not replace same length");
00509 
00510   _dbus_assert (_dbus_string_get_length (&str) == i);
00511   _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
00512   _dbus_assert (_dbus_string_equal_c_str (&other,
00513                                           "Foo Hello String Hello"));
00514 
00515   if (!_dbus_string_replace_len (&str, 6, 5,
00516                                  &other, 4, 12))
00517     _dbus_assert_not_reached ("could not replace with shorter string");
00518 
00519   _dbus_assert (_dbus_string_get_length (&str) == i);
00520   _dbus_assert (_dbus_string_get_length (&other) == a + 5);
00521   _dbus_assert (_dbus_string_equal_c_str (&other,
00522                                           "Foo World Hello"));
00523 
00524   if (!_dbus_string_replace_len (&str, 0, 1,
00525                                  &other, 0, 3))
00526     _dbus_assert_not_reached ("could not replace at the beginning");
00527 
00528   _dbus_assert (_dbus_string_get_length (&str) == i);
00529   _dbus_assert (_dbus_string_get_length (&other) == a + 3);
00530   _dbus_assert (_dbus_string_equal_c_str (&other,
00531                                           "H World Hello"));
00532 
00533   if (!_dbus_string_replace_len (&str, 6, 5,
00534                                  &other,
00535                                  _dbus_string_get_length (&other) - 5,
00536                                  5))
00537     _dbus_assert_not_reached ("could not replace same length");
00538 
00539   _dbus_assert (_dbus_string_get_length (&str) == i);
00540   _dbus_assert (_dbus_string_get_length (&other) == a + 3);
00541   _dbus_assert (_dbus_string_equal_c_str (&other,
00542                                           "H World World"));
00543 
00544   _dbus_string_free (&str);
00545   _dbus_string_free (&other);
00546 
00547   /* Check insert/set/get byte */
00548   
00549   if (!_dbus_string_init (&str))
00550     _dbus_assert_not_reached ("failed to init string");
00551 
00552   if (!_dbus_string_append (&str, "Hello"))
00553     _dbus_assert_not_reached ("failed to append Hello");
00554 
00555   _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
00556   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
00557   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
00558   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
00559   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
00560 
00561   _dbus_string_set_byte (&str, 1, 'q');
00562   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
00563 
00564   if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
00565     _dbus_assert_not_reached ("can't insert byte");
00566 
00567   if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
00568     _dbus_assert_not_reached ("can't insert byte");
00569 
00570   if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
00571     _dbus_assert_not_reached ("can't insert byte");
00572   
00573   _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
00574   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
00575   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
00576   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
00577   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
00578   _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
00579   _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
00580   _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
00581   _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
00582   _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
00583   _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
00584 
00585   _dbus_string_free (&str);
00586   
00587   /* Check append/parse int/double */
00588   
00589   if (!_dbus_string_init (&str))
00590     _dbus_assert_not_reached ("failed to init string");
00591 
00592   if (!_dbus_string_append_int (&str, 27))
00593     _dbus_assert_not_reached ("failed to append int");
00594 
00595   i = _dbus_string_get_length (&str);
00596 
00597   if (!_dbus_string_parse_int (&str, 0, &v, &end))
00598     _dbus_assert_not_reached ("failed to parse int");
00599 
00600   _dbus_assert (v == 27);
00601   _dbus_assert (end == i);
00602 
00603   _dbus_string_free (&str);
00604 
00605   /* Test find */
00606   if (!_dbus_string_init (&str))
00607     _dbus_assert_not_reached ("failed to init string");
00608 
00609   if (!_dbus_string_append (&str, "Hello"))
00610     _dbus_assert_not_reached ("couldn't append to string");
00611   
00612   if (!_dbus_string_find (&str, 0, "He", &i))
00613     _dbus_assert_not_reached ("didn't find 'He'");
00614   _dbus_assert (i == 0);
00615 
00616   if (!_dbus_string_find (&str, 0, "Hello", &i))
00617     _dbus_assert_not_reached ("didn't find 'Hello'");
00618   _dbus_assert (i == 0);
00619   
00620   if (!_dbus_string_find (&str, 0, "ello", &i))
00621     _dbus_assert_not_reached ("didn't find 'ello'");
00622   _dbus_assert (i == 1);
00623 
00624   if (!_dbus_string_find (&str, 0, "lo", &i))
00625     _dbus_assert_not_reached ("didn't find 'lo'");
00626   _dbus_assert (i == 3);
00627 
00628   if (!_dbus_string_find (&str, 2, "lo", &i))
00629     _dbus_assert_not_reached ("didn't find 'lo'");
00630   _dbus_assert (i == 3);
00631 
00632   if (_dbus_string_find (&str, 4, "lo", &i))
00633     _dbus_assert_not_reached ("did find 'lo'");
00634   
00635   if (!_dbus_string_find (&str, 0, "l", &i))
00636     _dbus_assert_not_reached ("didn't find 'l'");
00637   _dbus_assert (i == 2);
00638 
00639   if (!_dbus_string_find (&str, 0, "H", &i))
00640     _dbus_assert_not_reached ("didn't find 'H'");
00641   _dbus_assert (i == 0);
00642 
00643   if (!_dbus_string_find (&str, 0, "", &i))
00644     _dbus_assert_not_reached ("didn't find ''");
00645   _dbus_assert (i == 0);
00646   
00647   if (_dbus_string_find (&str, 0, "Hello!", NULL))
00648     _dbus_assert_not_reached ("Did find 'Hello!'");
00649 
00650   if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
00651     _dbus_assert_not_reached ("Did find 'Oh, Hello'");
00652   
00653   if (_dbus_string_find (&str, 0, "ill", NULL))
00654     _dbus_assert_not_reached ("Did find 'ill'");
00655 
00656   if (_dbus_string_find (&str, 0, "q", NULL))
00657     _dbus_assert_not_reached ("Did find 'q'");
00658 
00659   if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
00660     _dbus_assert_not_reached ("Didn't find 'He'");
00661 
00662   if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
00663     _dbus_assert_not_reached ("Did find 'Hello'");
00664 
00665   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
00666     _dbus_assert_not_reached ("Did not find 'H'");
00667   _dbus_assert (i == 0);
00668 
00669   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
00670     _dbus_assert_not_reached ("Did not find 'o'");
00671   _dbus_assert (i == _dbus_string_get_length (&str) - 1);
00672 
00673   if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
00674     _dbus_assert_not_reached ("Did find 'o'");
00675   _dbus_assert (i == -1);
00676 
00677   if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
00678     _dbus_assert_not_reached ("Did find 'e'");
00679   _dbus_assert (i == -1);
00680 
00681   if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
00682     _dbus_assert_not_reached ("Didn't find 'e'");
00683   _dbus_assert (i == 1);
00684   
00685   _dbus_string_free (&str);
00686 
00687   /* Hex encoding */
00688   _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
00689   if (!_dbus_string_init (&other))
00690     _dbus_assert_not_reached ("could not init string");
00691 
00692   if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
00693     _dbus_assert_not_reached ("deccoded bogus hex string with no error");
00694 
00695   _dbus_assert (end == 8);
00696 
00697   _dbus_string_free (&other);
00698 
00699   test_roundtrips (test_hex_roundtrip);
00700   
00701   _dbus_string_free (&str);
00702 
00703   {                                                                                           
00704     int found, found_len;  
00705 
00706     _dbus_string_init_const (&str, "012\r\n567\n90");
00707     
00708     if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
00709       _dbus_assert_not_reached ("Did not find '\\r\\n'");                                       
00710     if (found != 3 || found_len != 2)                                                           
00711       _dbus_assert_not_reached ("invalid return values");                                       
00712     
00713     if (!_dbus_string_find_eol (&str, 5, &found, &found_len))                                    
00714       _dbus_assert_not_reached ("Did not find '\\n'");                                          
00715     if (found != 8 || found_len != 1)                                                           
00716       _dbus_assert_not_reached ("invalid return values");                                       
00717     
00718     if (_dbus_string_find_eol (&str, 9, &found, &found_len))                                     
00719       _dbus_assert_not_reached ("Found not expected '\\n'");                                    
00720     else if (found != 11 || found_len != 0)                                                     
00721       _dbus_assert_not_reached ("invalid return values '\\n'");                                 
00722 
00723     found = -1;
00724     found_len = -1;
00725     _dbus_string_init_const (&str, "");
00726     if (_dbus_string_find_eol (&str, 0, &found, &found_len))
00727       _dbus_assert_not_reached ("found an eol in an empty string");
00728     _dbus_assert (found == 0);
00729     _dbus_assert (found_len == 0);
00730     
00731     found = -1;
00732     found_len = -1;
00733     _dbus_string_init_const (&str, "foobar");
00734     if (_dbus_string_find_eol (&str, 0, &found, &found_len))
00735       _dbus_assert_not_reached ("found eol in string that lacks one");
00736     _dbus_assert (found == 6);
00737     _dbus_assert (found_len == 0);
00738 
00739     found = -1;
00740     found_len = -1;
00741     _dbus_string_init_const (&str, "foobar\n");
00742     if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
00743       _dbus_assert_not_reached ("did not find eol in string that has one at end");
00744     _dbus_assert (found == 6);
00745     _dbus_assert (found_len == 1);
00746   }
00747 
00748   {
00749     DBusString line;
00750 
00751 #define FIRST_LINE "this is a line"
00752 #define SECOND_LINE "this is a second line"
00753     /* third line is empty */
00754 #define THIRD_LINE ""
00755 #define FOURTH_LINE "this is a fourth line"
00756     
00757     if (!_dbus_string_init (&str))
00758       _dbus_assert_not_reached ("no memory");
00759 
00760     if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
00761       _dbus_assert_not_reached ("no memory");
00762     
00763     if (!_dbus_string_init (&line))
00764       _dbus_assert_not_reached ("no memory");
00765     
00766     if (!_dbus_string_pop_line (&str, &line))
00767       _dbus_assert_not_reached ("failed to pop first line");
00768 
00769     _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
00770     
00771     if (!_dbus_string_pop_line (&str, &line))
00772       _dbus_assert_not_reached ("failed to pop second line");
00773 
00774     _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
00775     
00776     if (!_dbus_string_pop_line (&str, &line))
00777       _dbus_assert_not_reached ("failed to pop third line");
00778 
00779     _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
00780     
00781     if (!_dbus_string_pop_line (&str, &line))
00782       _dbus_assert_not_reached ("failed to pop fourth line");
00783 
00784     _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
00785     
00786     _dbus_string_free (&str);
00787     _dbus_string_free (&line);
00788   }
00789 
00790   {
00791     if (!_dbus_string_init (&str))
00792       _dbus_assert_not_reached ("no memory");
00793 
00794     for (i = 0; i < 10000; i++)
00795       if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
00796         _dbus_assert_not_reached ("no memory");
00797 
00798     if (!_dbus_string_set_length (&str, 10))
00799       _dbus_assert_not_reached ("failed to set length");
00800 
00801     /* actually compact */
00802     if (!_dbus_string_compact (&str, 2048))
00803       _dbus_assert_not_reached ("failed to compact after set_length");
00804 
00805     /* peek inside to make sure it worked */
00806     if (((DBusRealString *)&str)->allocated > 30)
00807       _dbus_assert_not_reached ("compacting string didn't do anything");
00808 
00809     if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
00810       _dbus_assert_not_reached ("unexpected content after compact");
00811 
00812     /* compact nothing */
00813     if (!_dbus_string_compact (&str, 2048))
00814       _dbus_assert_not_reached ("failed to compact 2nd time");
00815 
00816     if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
00817       _dbus_assert_not_reached ("unexpected content after 2nd compact");
00818 
00819     /* and make sure it still works...*/
00820     if (!_dbus_string_append (&str, "123456"))
00821       _dbus_assert_not_reached ("failed to append after compact");
00822 
00823     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
00824       _dbus_assert_not_reached ("unexpected content after append");
00825 
00826     /* after growing automatically, this should do nothing */
00827     if (!_dbus_string_compact (&str, 20000))
00828       _dbus_assert_not_reached ("failed to compact after grow");
00829 
00830     /* but this one will do something */
00831     if (!_dbus_string_compact (&str, 0))
00832       _dbus_assert_not_reached ("failed to compact after grow");
00833 
00834     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
00835       _dbus_assert_not_reached ("unexpected content");
00836 
00837     if (!_dbus_string_append (&str, "!@#$%"))
00838       _dbus_assert_not_reached ("failed to append after compact");
00839 
00840     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
00841       _dbus_assert_not_reached ("unexpected content");
00842 
00843     _dbus_string_free (&str);
00844   }
00845 
00846   {
00847     const char two_strings[] = "one\ttwo";
00848 
00849     if (!_dbus_string_init (&str))
00850       _dbus_assert_not_reached ("no memory");
00851 
00852     if (!_dbus_string_init (&other))
00853       _dbus_assert_not_reached ("no memory");
00854 
00855     if (!_dbus_string_append (&str, two_strings))
00856       _dbus_assert_not_reached ("no memory");
00857 
00858     if (!_dbus_string_split_on_byte (&str, '\t', &other))
00859       _dbus_assert_not_reached ("no memory or delimiter not found");
00860 
00861     if (strcmp (_dbus_string_get_data (&str), "one") != 0)
00862       _dbus_assert_not_reached ("left side after split on tab is wrong");
00863 
00864     if (strcmp (_dbus_string_get_data (&other), "two") != 0)
00865       _dbus_assert_not_reached ("right side after split on tab is wrong");
00866 
00867     _dbus_string_free (&str);
00868     _dbus_string_free (&other);
00869   }
00870 
00871   {
00872     const char upper_string[] = "TOUPPERSTRING";
00873     const char lower_string[] = "toupperstring";
00874     const char lower2_string[] = "toupperSTRING";
00875 
00876     if (!_dbus_string_init (&str))
00877       _dbus_assert_not_reached ("no memory");
00878 
00879     if (!_dbus_string_append (&str, upper_string))
00880       _dbus_assert_not_reached ("no memory");
00881 
00882     _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
00883 
00884     if (!_dbus_string_equal_c_str (&str, lower_string))
00885       _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
00886 
00887     _dbus_string_free (&str);
00888 
00889     if (!_dbus_string_init (&str))
00890       _dbus_assert_not_reached ("no memory");
00891 
00892     if (!_dbus_string_append (&str, upper_string))
00893       _dbus_assert_not_reached ("no memory");
00894 
00895     _dbus_string_tolower_ascii (&str, 0, 7);
00896 
00897     if (!_dbus_string_equal_c_str (&str, lower2_string))
00898       _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
00899 
00900     _dbus_string_free (&str);
00901   }
00902 
00903   {
00904     const char lower_string[] = "toupperstring";
00905     const char upper_string[] = "TOUPPERSTRING";
00906     const char upper2_string[] = "TOUPPERstring";
00907 
00908     if (!_dbus_string_init (&str))
00909       _dbus_assert_not_reached ("no memory");
00910 
00911     if (!_dbus_string_append (&str, lower_string))
00912       _dbus_assert_not_reached ("no memory");
00913 
00914     _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
00915 
00916     if (!_dbus_string_equal_c_str (&str, upper_string))
00917       _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
00918 
00919     _dbus_string_free (&str);
00920 
00921     if (!_dbus_string_init (&str))
00922       _dbus_assert_not_reached ("no memory");
00923 
00924     if (!_dbus_string_append (&str, lower_string))
00925       _dbus_assert_not_reached ("no memory");
00926 
00927     _dbus_string_toupper_ascii (&str, 0, 7);
00928 
00929     if (!_dbus_string_equal_c_str (&str, upper2_string))
00930       _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
00931 
00932     _dbus_string_free (&str);
00933   }
00934 
00935   return TRUE;
00936 }
00937 
00938 #endif /* DBUS_BUILD_TESTS */