2005-01-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-string-util.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-string-util.c Would be in dbus-string.c, but not used in libdbus
3  * 
4  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
27 #include "dbus-string-private.h"
28
29 /**
30  * @addtogroup DBusString
31  * @{
32  */
33
34 /**
35  * Copies the contents of a DBusString into a different
36  * buffer. The resulting buffer will be nul-terminated.
37  * 
38  * @param str a string
39  * @param buffer a C buffer to copy data to
40  * @param avail_len maximum length of C buffer
41  */
42 void
43 _dbus_string_copy_to_buffer (const DBusString  *str,
44                              char              *buffer,
45                              int                avail_len)
46 {
47   int copy_len;
48   DBUS_CONST_STRING_PREAMBLE (str);
49
50   _dbus_assert (avail_len >= 0);
51
52   copy_len = MIN (avail_len, real->len+1);
53   memcpy (buffer, real->str, copy_len);
54   if (avail_len > 0 && avail_len == copy_len)
55     buffer[avail_len-1] = '\0';
56 }
57
58 /**
59  * Returns whether a string ends with the given suffix
60  *
61  * @todo memcmp might make this faster.
62  * 
63  * @param a the string
64  * @param c_str the C-style string
65  * @returns #TRUE if the string ends with the suffix
66  */
67 dbus_bool_t
68 _dbus_string_ends_with_c_str (const DBusString *a,
69                               const char       *c_str)
70 {
71   const unsigned char *ap;
72   const unsigned char *bp;
73   const unsigned char *a_end;
74   unsigned long c_str_len;
75   const DBusRealString *real_a = (const DBusRealString*) a;
76   DBUS_GENERIC_STRING_PREAMBLE (real_a);
77   _dbus_assert (c_str != NULL);
78   
79   c_str_len = strlen (c_str);
80   if (((unsigned long)real_a->len) < c_str_len)
81     return FALSE;
82   
83   ap = real_a->str + (real_a->len - c_str_len);
84   bp = (const unsigned char*) c_str;
85   a_end = real_a->str + real_a->len;
86   while (ap != a_end)
87     {
88       if (*ap != *bp)
89         return FALSE;
90       
91       ++ap;
92       ++bp;
93     }
94
95   _dbus_assert (*ap == '\0');
96   _dbus_assert (*bp == '\0');
97   
98   return TRUE;
99 }
100
101 /**
102  * Find the given byte scanning backward from the given start.
103  * Sets *found to -1 if the byte is not found.
104  *
105  * @param str the string
106  * @param start the place to start scanning (will not find the byte at this point)
107  * @param byte the byte to find
108  * @param found return location for where it was found
109  * @returns #TRUE if found
110  */
111 dbus_bool_t
112 _dbus_string_find_byte_backward (const DBusString  *str,
113                                  int                start,
114                                  unsigned char      byte,
115                                  int               *found)
116 {
117   int i;
118   DBUS_CONST_STRING_PREAMBLE (str);
119   _dbus_assert (start <= real->len);
120   _dbus_assert (start >= 0);
121   _dbus_assert (found != NULL);
122
123   i = start - 1;
124   while (i >= 0)
125     {
126       if (real->str[i] == byte)
127         break;
128       
129       --i;
130     }
131
132   if (found)
133     *found = i;
134
135   return i >= 0;
136 }
137
138 /**
139  * Skips whitespace from start, storing the first non-whitespace in *end.
140  * (whitespace is space, tab, newline, CR).
141  *
142  * @param str the string
143  * @param start where to start
144  * @param end where to store the first non-whitespace byte index
145  */
146 void
147 _dbus_string_skip_white (const DBusString *str,
148                          int               start,
149                          int              *end)
150 {
151   int i;
152   DBUS_CONST_STRING_PREAMBLE (str);
153   _dbus_assert (start <= real->len);
154   _dbus_assert (start >= 0);
155   
156   i = start;
157   while (i < real->len)
158     {
159       if (!(real->str[i] == ' ' ||
160             real->str[i] == '\n' ||
161             real->str[i] == '\r' ||
162             real->str[i] == '\t'))
163         break;
164       
165       ++i;
166     }
167
168   _dbus_assert (i == real->len || !(real->str[i] == ' ' ||
169                                     real->str[i] == '\t'));
170   
171   if (end)
172     *end = i;
173 }
174
175 /** @} */
176
177 #ifdef DBUS_BUILD_TESTS
178 #include "dbus-test.h"
179 #include <stdio.h>
180
181 static void
182 test_max_len (DBusString *str,
183               int         max_len)
184 {
185   if (max_len > 0)
186     {
187       if (!_dbus_string_set_length (str, max_len - 1))
188         _dbus_assert_not_reached ("setting len to one less than max should have worked");
189     }
190
191   if (!_dbus_string_set_length (str, max_len))
192     _dbus_assert_not_reached ("setting len to max len should have worked");
193
194   if (_dbus_string_set_length (str, max_len + 1))
195     _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
196
197   if (!_dbus_string_set_length (str, 0))
198     _dbus_assert_not_reached ("setting len to zero should have worked");
199 }
200
201 static void
202 test_hex_roundtrip (const unsigned char *data,
203                     int                  len)
204 {
205   DBusString orig;
206   DBusString encoded;
207   DBusString decoded;
208   int end;
209
210   if (len < 0)
211     len = strlen (data);
212   
213   if (!_dbus_string_init (&orig))
214     _dbus_assert_not_reached ("could not init string");
215
216   if (!_dbus_string_init (&encoded))
217     _dbus_assert_not_reached ("could not init string");
218   
219   if (!_dbus_string_init (&decoded))
220     _dbus_assert_not_reached ("could not init string");
221
222   if (!_dbus_string_append_len (&orig, data, len))
223     _dbus_assert_not_reached ("couldn't append orig data");
224
225   if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
226     _dbus_assert_not_reached ("could not encode");
227
228   if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
229     _dbus_assert_not_reached ("could not decode");
230     
231   _dbus_assert (_dbus_string_get_length (&encoded) == end);
232
233   if (!_dbus_string_equal (&orig, &decoded))
234     {
235       const char *s;
236       
237       printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
238               _dbus_string_get_length (&orig),
239               _dbus_string_get_length (&encoded),
240               _dbus_string_get_length (&decoded));
241       printf ("Original: %s\n", data);
242       s = _dbus_string_get_const_data (&decoded);
243       printf ("Decoded: %s\n", s);
244       _dbus_assert_not_reached ("original string not the same as string decoded from hex");
245     }
246   
247   _dbus_string_free (&orig);
248   _dbus_string_free (&encoded);
249   _dbus_string_free (&decoded);  
250 }
251
252 typedef void (* TestRoundtripFunc) (const unsigned char *data,
253                                     int                  len);
254 static void
255 test_roundtrips (TestRoundtripFunc func)
256 {
257   (* func) ("Hello this is a string\n", -1);
258   (* func) ("Hello this is a string\n1", -1);
259   (* func) ("Hello this is a string\n12", -1);
260   (* func) ("Hello this is a string\n123", -1);
261   (* func) ("Hello this is a string\n1234", -1);
262   (* func) ("Hello this is a string\n12345", -1);
263   (* func) ("", 0);
264   (* func) ("1", 1);
265   (* func) ("12", 2);
266   (* func) ("123", 3);
267   (* func) ("1234", 4);
268   (* func) ("12345", 5);
269   (* func) ("", 1);
270   (* func) ("1", 2);
271   (* func) ("12", 3);
272   (* func) ("123", 4);
273   (* func) ("1234", 5);
274   (* func) ("12345", 6);
275   {
276     unsigned char buf[512];
277     int i;
278     
279     i = 0;
280     while (i < _DBUS_N_ELEMENTS (buf))
281       {
282         buf[i] = i;
283         ++i;
284       }
285     i = 0;
286     while (i < _DBUS_N_ELEMENTS (buf))
287       {
288         (* func) (buf, i);
289         ++i;
290       }
291   }
292 }
293
294 #ifdef DBUS_BUILD_TESTS
295 /* The max length thing is sort of a historical artifact
296  * from a feature that turned out to be dumb; perhaps
297  * we should purge it entirely. The problem with
298  * the feature is that it looks like memory allocation
299  * failure, but is not a transient or resolvable failure.
300  */
301 static void
302 set_max_length (DBusString *str,
303                 int         max_length)
304 {
305   DBusRealString *real;
306   
307   real = (DBusRealString*) str;
308
309   real->max_length = max_length;
310 }
311 #endif /* DBUS_BUILD_TESTS */
312
313 /**
314  * @ingroup DBusStringInternals
315  * Unit test for DBusString.
316  *
317  * @todo Need to write tests for _dbus_string_copy() and
318  * _dbus_string_move() moving to/from each of start/middle/end of a
319  * string. Also need tests for _dbus_string_move_len ()
320  * 
321  * @returns #TRUE on success.
322  */
323 dbus_bool_t
324 _dbus_string_test (void)
325 {
326   DBusString str;
327   DBusString other;
328   int i, end;
329   long v;
330   double d;
331   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 };
332   char *s;
333   dbus_unichar_t ch;
334   
335   i = 0;
336   while (i < _DBUS_N_ELEMENTS (lens))
337     {
338       if (!_dbus_string_init (&str))
339         _dbus_assert_not_reached ("failed to init string");
340
341       set_max_length (&str, lens[i]);
342       
343       test_max_len (&str, lens[i]);
344       _dbus_string_free (&str);
345
346       ++i;
347     }
348
349   /* Test shortening and setting length */
350   i = 0;
351   while (i < _DBUS_N_ELEMENTS (lens))
352     {
353       int j;
354       
355       if (!_dbus_string_init (&str))
356         _dbus_assert_not_reached ("failed to init string");
357
358       set_max_length (&str, lens[i]);
359       
360       if (!_dbus_string_set_length (&str, lens[i]))
361         _dbus_assert_not_reached ("failed to set string length");
362
363       j = lens[i];
364       while (j > 0)
365         {
366           _dbus_assert (_dbus_string_get_length (&str) == j);
367           if (j > 0)
368             {
369               _dbus_string_shorten (&str, 1);
370               _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
371             }
372           --j;
373         }
374       
375       _dbus_string_free (&str);
376
377       ++i;
378     }
379
380   /* Test appending data */
381   if (!_dbus_string_init (&str))
382     _dbus_assert_not_reached ("failed to init string");
383
384   i = 0;
385   while (i < 10)
386     {
387       if (!_dbus_string_append (&str, "a"))
388         _dbus_assert_not_reached ("failed to append string to string\n");
389
390       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
391
392       if (!_dbus_string_append_byte (&str, 'b'))
393         _dbus_assert_not_reached ("failed to append byte to string\n");
394
395       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
396                     
397       ++i;
398     }
399
400   _dbus_string_free (&str);
401
402   /* Check steal_data */
403   
404   if (!_dbus_string_init (&str))
405     _dbus_assert_not_reached ("failed to init string");
406
407   if (!_dbus_string_append (&str, "Hello World"))
408     _dbus_assert_not_reached ("could not append to string");
409
410   i = _dbus_string_get_length (&str);
411   
412   if (!_dbus_string_steal_data (&str, &s))
413     _dbus_assert_not_reached ("failed to steal data");
414
415   _dbus_assert (_dbus_string_get_length (&str) == 0);
416   _dbus_assert (((int)strlen (s)) == i);
417
418   dbus_free (s);
419
420   /* Check move */
421   
422   if (!_dbus_string_append (&str, "Hello World"))
423     _dbus_assert_not_reached ("could not append to string");
424
425   i = _dbus_string_get_length (&str);
426
427   if (!_dbus_string_init (&other))
428     _dbus_assert_not_reached ("could not init string");
429   
430   if (!_dbus_string_move (&str, 0, &other, 0))
431     _dbus_assert_not_reached ("could not move");
432
433   _dbus_assert (_dbus_string_get_length (&str) == 0);
434   _dbus_assert (_dbus_string_get_length (&other) == i);
435
436   if (!_dbus_string_append (&str, "Hello World"))
437     _dbus_assert_not_reached ("could not append to string");
438   
439   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
440     _dbus_assert_not_reached ("could not move");
441
442   _dbus_assert (_dbus_string_get_length (&str) == 0);
443   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
444
445     if (!_dbus_string_append (&str, "Hello World"))
446     _dbus_assert_not_reached ("could not append to string");
447   
448   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
449     _dbus_assert_not_reached ("could not move");
450
451   _dbus_assert (_dbus_string_get_length (&str) == 0);
452   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
453   
454   _dbus_string_free (&other);
455
456   /* Check copy */
457   
458   if (!_dbus_string_append (&str, "Hello World"))
459     _dbus_assert_not_reached ("could not append to string");
460
461   i = _dbus_string_get_length (&str);
462   
463   if (!_dbus_string_init (&other))
464     _dbus_assert_not_reached ("could not init string");
465   
466   if (!_dbus_string_copy (&str, 0, &other, 0))
467     _dbus_assert_not_reached ("could not copy");
468
469   _dbus_assert (_dbus_string_get_length (&str) == i);
470   _dbus_assert (_dbus_string_get_length (&other) == i);
471
472   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
473     _dbus_assert_not_reached ("could not copy");
474
475   _dbus_assert (_dbus_string_get_length (&str) == i);
476   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
477   _dbus_assert (_dbus_string_equal_c_str (&other,
478                                           "Hello WorldHello World"));
479
480   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
481     _dbus_assert_not_reached ("could not copy");
482
483   _dbus_assert (_dbus_string_get_length (&str) == i);
484   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
485   _dbus_assert (_dbus_string_equal_c_str (&other,
486                                           "Hello WorldHello WorldHello World"));
487   
488   _dbus_string_free (&str);
489   _dbus_string_free (&other);
490
491   /* Check replace */
492
493   if (!_dbus_string_init (&str))
494     _dbus_assert_not_reached ("failed to init string");
495   
496   if (!_dbus_string_append (&str, "Hello World"))
497     _dbus_assert_not_reached ("could not append to string");
498
499   i = _dbus_string_get_length (&str);
500   
501   if (!_dbus_string_init (&other))
502     _dbus_assert_not_reached ("could not init string");
503   
504   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
505                                  &other, 0, _dbus_string_get_length (&other)))
506     _dbus_assert_not_reached ("could not replace");
507
508   _dbus_assert (_dbus_string_get_length (&str) == i);
509   _dbus_assert (_dbus_string_get_length (&other) == i);
510   _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
511   
512   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
513                                  &other, 5, 1))
514     _dbus_assert_not_reached ("could not replace center space");
515
516   _dbus_assert (_dbus_string_get_length (&str) == i);
517   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
518   _dbus_assert (_dbus_string_equal_c_str (&other,
519                                           "HelloHello WorldWorld"));
520
521   
522   if (!_dbus_string_replace_len (&str, 1, 1,
523                                  &other,
524                                  _dbus_string_get_length (&other) - 1,
525                                  1))
526     _dbus_assert_not_reached ("could not replace end character");
527   
528   _dbus_assert (_dbus_string_get_length (&str) == i);
529   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
530   _dbus_assert (_dbus_string_equal_c_str (&other,
531                                           "HelloHello WorldWorle"));
532   
533   _dbus_string_free (&str);
534   _dbus_string_free (&other);
535   
536   /* Check append/get unichar */
537   
538   if (!_dbus_string_init (&str))
539     _dbus_assert_not_reached ("failed to init string");
540
541   ch = 0;
542   if (!_dbus_string_append_unichar (&str, 0xfffc))
543     _dbus_assert_not_reached ("failed to append unichar");
544
545   _dbus_string_get_unichar (&str, 0, &ch, &i);
546
547   _dbus_assert (ch == 0xfffc);
548   _dbus_assert (i == _dbus_string_get_length (&str));
549
550   _dbus_string_free (&str);
551
552   /* Check insert/set/get byte */
553   
554   if (!_dbus_string_init (&str))
555     _dbus_assert_not_reached ("failed to init string");
556
557   if (!_dbus_string_append (&str, "Hello"))
558     _dbus_assert_not_reached ("failed to append Hello");
559
560   _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
561   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
562   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
563   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
564   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
565
566   _dbus_string_set_byte (&str, 1, 'q');
567   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
568
569   if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
570     _dbus_assert_not_reached ("can't insert byte");
571
572   if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
573     _dbus_assert_not_reached ("can't insert byte");
574
575   if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
576     _dbus_assert_not_reached ("can't insert byte");
577   
578   _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
579   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
580   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
581   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
582   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
583   _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
584   _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
585   _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
586   _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
587   _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
588   _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
589
590   _dbus_string_free (&str);
591   
592   /* Check append/parse int/double */
593   
594   if (!_dbus_string_init (&str))
595     _dbus_assert_not_reached ("failed to init string");
596
597   if (!_dbus_string_append_int (&str, 27))
598     _dbus_assert_not_reached ("failed to append int");
599
600   i = _dbus_string_get_length (&str);
601
602   if (!_dbus_string_parse_int (&str, 0, &v, &end))
603     _dbus_assert_not_reached ("failed to parse int");
604
605   _dbus_assert (v == 27);
606   _dbus_assert (end == i);
607
608   _dbus_string_free (&str);
609   
610   if (!_dbus_string_init (&str))
611     _dbus_assert_not_reached ("failed to init string");
612   
613   if (!_dbus_string_append_double (&str, 50.3))
614     _dbus_assert_not_reached ("failed to append float");
615
616   i = _dbus_string_get_length (&str);
617
618   if (!_dbus_string_parse_double (&str, 0, &d, &end))
619     _dbus_assert_not_reached ("failed to parse float");
620
621   _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
622   _dbus_assert (end == i);
623
624   _dbus_string_free (&str);
625
626   /* Test find */
627   if (!_dbus_string_init (&str))
628     _dbus_assert_not_reached ("failed to init string");
629
630   if (!_dbus_string_append (&str, "Hello"))
631     _dbus_assert_not_reached ("couldn't append to string");
632   
633   if (!_dbus_string_find (&str, 0, "He", &i))
634     _dbus_assert_not_reached ("didn't find 'He'");
635   _dbus_assert (i == 0);
636
637   if (!_dbus_string_find (&str, 0, "Hello", &i))
638     _dbus_assert_not_reached ("didn't find 'Hello'");
639   _dbus_assert (i == 0);
640   
641   if (!_dbus_string_find (&str, 0, "ello", &i))
642     _dbus_assert_not_reached ("didn't find 'ello'");
643   _dbus_assert (i == 1);
644
645   if (!_dbus_string_find (&str, 0, "lo", &i))
646     _dbus_assert_not_reached ("didn't find 'lo'");
647   _dbus_assert (i == 3);
648
649   if (!_dbus_string_find (&str, 2, "lo", &i))
650     _dbus_assert_not_reached ("didn't find 'lo'");
651   _dbus_assert (i == 3);
652
653   if (_dbus_string_find (&str, 4, "lo", &i))
654     _dbus_assert_not_reached ("did find 'lo'");
655   
656   if (!_dbus_string_find (&str, 0, "l", &i))
657     _dbus_assert_not_reached ("didn't find 'l'");
658   _dbus_assert (i == 2);
659
660   if (!_dbus_string_find (&str, 0, "H", &i))
661     _dbus_assert_not_reached ("didn't find 'H'");
662   _dbus_assert (i == 0);
663
664   if (!_dbus_string_find (&str, 0, "", &i))
665     _dbus_assert_not_reached ("didn't find ''");
666   _dbus_assert (i == 0);
667   
668   if (_dbus_string_find (&str, 0, "Hello!", NULL))
669     _dbus_assert_not_reached ("Did find 'Hello!'");
670
671   if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
672     _dbus_assert_not_reached ("Did find 'Oh, Hello'");
673   
674   if (_dbus_string_find (&str, 0, "ill", NULL))
675     _dbus_assert_not_reached ("Did find 'ill'");
676
677   if (_dbus_string_find (&str, 0, "q", NULL))
678     _dbus_assert_not_reached ("Did find 'q'");
679
680   if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
681     _dbus_assert_not_reached ("Didn't find 'He'");
682
683   if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
684     _dbus_assert_not_reached ("Did find 'Hello'");
685
686   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
687     _dbus_assert_not_reached ("Did not find 'H'");
688   _dbus_assert (i == 0);
689
690   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
691     _dbus_assert_not_reached ("Did not find 'o'");
692   _dbus_assert (i == _dbus_string_get_length (&str) - 1);
693
694   if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
695     _dbus_assert_not_reached ("Did find 'o'");
696   _dbus_assert (i == -1);
697
698   if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
699     _dbus_assert_not_reached ("Did find 'e'");
700   _dbus_assert (i == -1);
701
702   if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
703     _dbus_assert_not_reached ("Didn't find 'e'");
704   _dbus_assert (i == 1);
705   
706   _dbus_string_free (&str);
707
708   /* Hex encoding */
709   _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
710   if (!_dbus_string_init (&other))
711     _dbus_assert_not_reached ("could not init string");
712
713   if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
714     _dbus_assert_not_reached ("deccoded bogus hex string with no error");
715
716   _dbus_assert (end == 8);
717
718   _dbus_string_free (&other);
719
720   test_roundtrips (test_hex_roundtrip);
721   
722   _dbus_string_free (&str);
723   
724   return TRUE;
725 }
726
727 #endif /* DBUS_BUILD_TESTS */