_dbus_string_append_unichar, _dbus_string_get_unichar: remove
[platform/upstream/dbus.git] / dbus / dbus-string-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
28 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
29 #include "dbus-string-private.h"
30
31 /**
32  * @addtogroup DBusString
33  * @{
34  */
35
36 /**
37  * Returns whether a string ends with the given suffix
38  *
39  * @todo memcmp might make this faster.
40  * 
41  * @param a the string
42  * @param c_str the C-style string
43  * @returns #TRUE if the string ends with the suffix
44  */
45 dbus_bool_t
46 _dbus_string_ends_with_c_str (const DBusString *a,
47                               const char       *c_str)
48 {
49   const unsigned char *ap;
50   const unsigned char *bp;
51   const unsigned char *a_end;
52   unsigned long c_str_len;
53   const DBusRealString *real_a = (const DBusRealString*) a;
54   DBUS_GENERIC_STRING_PREAMBLE (real_a);
55   _dbus_assert (c_str != NULL);
56   
57   c_str_len = strlen (c_str);
58   if (((unsigned long)real_a->len) < c_str_len)
59     return FALSE;
60   
61   ap = real_a->str + (real_a->len - c_str_len);
62   bp = (const unsigned char*) c_str;
63   a_end = real_a->str + real_a->len;
64   while (ap != a_end)
65     {
66       if (*ap != *bp)
67         return FALSE;
68       
69       ++ap;
70       ++bp;
71     }
72
73   _dbus_assert (*ap == '\0');
74   _dbus_assert (*bp == '\0');
75   
76   return TRUE;
77 }
78
79 /**
80  * Find the given byte scanning backward from the given start.
81  * Sets *found to -1 if the byte is not found.
82  *
83  * @param str the string
84  * @param start the place to start scanning (will not find the byte at this point)
85  * @param byte the byte to find
86  * @param found return location for where it was found
87  * @returns #TRUE if found
88  */
89 dbus_bool_t
90 _dbus_string_find_byte_backward (const DBusString  *str,
91                                  int                start,
92                                  unsigned char      byte,
93                                  int               *found)
94 {
95   int i;
96   DBUS_CONST_STRING_PREAMBLE (str);
97   _dbus_assert (start <= real->len);
98   _dbus_assert (start >= 0);
99   _dbus_assert (found != NULL);
100
101   i = start - 1;
102   while (i >= 0)
103     {
104       if (real->str[i] == byte)
105         break;
106       
107       --i;
108     }
109
110   if (found)
111     *found = i;
112
113   return i >= 0;
114 }
115
116 /** @} */
117
118 #ifdef DBUS_BUILD_TESTS
119 #include "dbus-test.h"
120 #include <stdio.h>
121
122 static void
123 test_hex_roundtrip (const unsigned char *data,
124                     int                  len)
125 {
126   DBusString orig;
127   DBusString encoded;
128   DBusString decoded;
129   int end;
130
131   if (len < 0)
132     len = strlen (data);
133   
134   if (!_dbus_string_init (&orig))
135     _dbus_assert_not_reached ("could not init string");
136
137   if (!_dbus_string_init (&encoded))
138     _dbus_assert_not_reached ("could not init string");
139   
140   if (!_dbus_string_init (&decoded))
141     _dbus_assert_not_reached ("could not init string");
142
143   if (!_dbus_string_append_len (&orig, data, len))
144     _dbus_assert_not_reached ("couldn't append orig data");
145
146   if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
147     _dbus_assert_not_reached ("could not encode");
148
149   if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
150     _dbus_assert_not_reached ("could not decode");
151     
152   _dbus_assert (_dbus_string_get_length (&encoded) == end);
153
154   if (!_dbus_string_equal (&orig, &decoded))
155     {
156       const char *s;
157       
158       printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
159               _dbus_string_get_length (&orig),
160               _dbus_string_get_length (&encoded),
161               _dbus_string_get_length (&decoded));
162       printf ("Original: %s\n", data);
163       s = _dbus_string_get_const_data (&decoded);
164       printf ("Decoded: %s\n", s);
165       _dbus_assert_not_reached ("original string not the same as string decoded from hex");
166     }
167   
168   _dbus_string_free (&orig);
169   _dbus_string_free (&encoded);
170   _dbus_string_free (&decoded);  
171 }
172
173 typedef void (* TestRoundtripFunc) (const unsigned char *data,
174                                     int                  len);
175 static void
176 test_roundtrips (TestRoundtripFunc func)
177 {
178   (* func) ("Hello this is a string\n", -1);
179   (* func) ("Hello this is a string\n1", -1);
180   (* func) ("Hello this is a string\n12", -1);
181   (* func) ("Hello this is a string\n123", -1);
182   (* func) ("Hello this is a string\n1234", -1);
183   (* func) ("Hello this is a string\n12345", -1);
184   (* func) ("", 0);
185   (* func) ("1", 1);
186   (* func) ("12", 2);
187   (* func) ("123", 3);
188   (* func) ("1234", 4);
189   (* func) ("12345", 5);
190   (* func) ("", 1);
191   (* func) ("1", 2);
192   (* func) ("12", 3);
193   (* func) ("123", 4);
194   (* func) ("1234", 5);
195   (* func) ("12345", 6);
196   {
197     unsigned char buf[512];
198     int i;
199     
200     i = 0;
201     while (i < _DBUS_N_ELEMENTS (buf))
202       {
203         buf[i] = i;
204         ++i;
205       }
206     i = 0;
207     while (i < _DBUS_N_ELEMENTS (buf))
208       {
209         (* func) (buf, i);
210         ++i;
211       }
212   }
213 }
214
215 /**
216  * @ingroup DBusStringInternals
217  * Unit test for DBusString.
218  *
219  * @todo Need to write tests for _dbus_string_copy() and
220  * _dbus_string_move() moving to/from each of start/middle/end of a
221  * string. Also need tests for _dbus_string_move_len ()
222  * 
223  * @returns #TRUE on success.
224  */
225 dbus_bool_t
226 _dbus_string_test (void)
227 {
228   DBusString str;
229   DBusString other;
230   int i, a, end;
231   long v;
232   double d;
233   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 };
234   char *s;
235
236   /* Test shortening and setting length */
237   i = 0;
238   while (i < _DBUS_N_ELEMENTS (lens))
239     {
240       int j;
241       
242       if (!_dbus_string_init (&str))
243         _dbus_assert_not_reached ("failed to init string");
244
245       if (!_dbus_string_set_length (&str, lens[i]))
246         _dbus_assert_not_reached ("failed to set string length");
247
248       j = lens[i];
249       while (j > 0)
250         {
251           _dbus_assert (_dbus_string_get_length (&str) == j);
252           if (j > 0)
253             {
254               _dbus_string_shorten (&str, 1);
255               _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
256             }
257           --j;
258         }
259       
260       _dbus_string_free (&str);
261
262       ++i;
263     }
264
265   /* Test equality */
266   if (!_dbus_string_init (&str))
267     _dbus_assert_not_reached ("oom");
268
269   if (!_dbus_string_append (&str, "Hello World"))
270     _dbus_assert_not_reached ("oom");
271
272   _dbus_string_init_const (&other, "H");
273   _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
274   _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
275   _dbus_string_init_const (&other, "Hello");
276   _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
277   _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
278   _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
279   _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
280   _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
281   _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
282
283   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
284   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
285   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
286   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
287   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
288   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
289
290   
291   _dbus_string_init_const (&other, "World");
292   _dbus_assert (_dbus_string_equal_substring (&str, 6,  5, &other, 0));
293   _dbus_assert (_dbus_string_equal_substring (&str, 7,  4, &other, 1));
294   _dbus_assert (_dbus_string_equal_substring (&str, 8,  3, &other, 2));
295   _dbus_assert (_dbus_string_equal_substring (&str, 9,  2, &other, 3));
296   _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
297   _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
298
299   _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
300   _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
301   _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
302   _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
303   _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
304   _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
305   
306   _dbus_string_free (&str);
307   
308   /* Test appending data */
309   if (!_dbus_string_init (&str))
310     _dbus_assert_not_reached ("failed to init string");
311
312   i = 0;
313   while (i < 10)
314     {
315       if (!_dbus_string_append (&str, "a"))
316         _dbus_assert_not_reached ("failed to append string to string\n");
317
318       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
319
320       if (!_dbus_string_append_byte (&str, 'b'))
321         _dbus_assert_not_reached ("failed to append byte to string\n");
322
323       _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
324                     
325       ++i;
326     }
327
328   _dbus_string_free (&str);
329
330   /* Check steal_data */
331   
332   if (!_dbus_string_init (&str))
333     _dbus_assert_not_reached ("failed to init string");
334
335   if (!_dbus_string_append (&str, "Hello World"))
336     _dbus_assert_not_reached ("could not append to string");
337
338   i = _dbus_string_get_length (&str);
339   
340   if (!_dbus_string_steal_data (&str, &s))
341     _dbus_assert_not_reached ("failed to steal data");
342
343   _dbus_assert (_dbus_string_get_length (&str) == 0);
344   _dbus_assert (((int)strlen (s)) == i);
345
346   dbus_free (s);
347
348   /* Check move */
349   
350   if (!_dbus_string_append (&str, "Hello World"))
351     _dbus_assert_not_reached ("could not append to string");
352
353   i = _dbus_string_get_length (&str);
354
355   if (!_dbus_string_init (&other))
356     _dbus_assert_not_reached ("could not init string");
357   
358   if (!_dbus_string_move (&str, 0, &other, 0))
359     _dbus_assert_not_reached ("could not move");
360
361   _dbus_assert (_dbus_string_get_length (&str) == 0);
362   _dbus_assert (_dbus_string_get_length (&other) == i);
363
364   if (!_dbus_string_append (&str, "Hello World"))
365     _dbus_assert_not_reached ("could not append to string");
366   
367   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
368     _dbus_assert_not_reached ("could not move");
369
370   _dbus_assert (_dbus_string_get_length (&str) == 0);
371   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
372
373     if (!_dbus_string_append (&str, "Hello World"))
374     _dbus_assert_not_reached ("could not append to string");
375   
376   if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
377     _dbus_assert_not_reached ("could not move");
378
379   _dbus_assert (_dbus_string_get_length (&str) == 0);
380   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
381   
382   _dbus_string_free (&other);
383
384   /* Check copy */
385   
386   if (!_dbus_string_append (&str, "Hello World"))
387     _dbus_assert_not_reached ("could not append to string");
388
389   i = _dbus_string_get_length (&str);
390   
391   if (!_dbus_string_init (&other))
392     _dbus_assert_not_reached ("could not init string");
393   
394   if (!_dbus_string_copy (&str, 0, &other, 0))
395     _dbus_assert_not_reached ("could not copy");
396
397   _dbus_assert (_dbus_string_get_length (&str) == i);
398   _dbus_assert (_dbus_string_get_length (&other) == i);
399
400   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
401     _dbus_assert_not_reached ("could not copy");
402
403   _dbus_assert (_dbus_string_get_length (&str) == i);
404   _dbus_assert (_dbus_string_get_length (&other) == i * 2);
405   _dbus_assert (_dbus_string_equal_c_str (&other,
406                                           "Hello WorldHello World"));
407
408   if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
409     _dbus_assert_not_reached ("could not copy");
410
411   _dbus_assert (_dbus_string_get_length (&str) == i);
412   _dbus_assert (_dbus_string_get_length (&other) == i * 3);
413   _dbus_assert (_dbus_string_equal_c_str (&other,
414                                           "Hello WorldHello WorldHello World"));
415   
416   _dbus_string_free (&str);
417   _dbus_string_free (&other);
418
419   /* Check replace */
420
421   if (!_dbus_string_init (&str))
422     _dbus_assert_not_reached ("failed to init string");
423   
424   if (!_dbus_string_append (&str, "Hello World"))
425     _dbus_assert_not_reached ("could not append to string");
426
427   i = _dbus_string_get_length (&str);
428   
429   if (!_dbus_string_init (&other))
430     _dbus_assert_not_reached ("could not init string");
431   
432   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
433                                  &other, 0, _dbus_string_get_length (&other)))
434     _dbus_assert_not_reached ("could not replace");
435
436   _dbus_assert (_dbus_string_get_length (&str) == i);
437   _dbus_assert (_dbus_string_get_length (&other) == i);
438   _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
439   
440   if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
441                                  &other, 5, 1))
442     _dbus_assert_not_reached ("could not replace center space");
443
444   _dbus_assert (_dbus_string_get_length (&str) == i);
445   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
446   _dbus_assert (_dbus_string_equal_c_str (&other,
447                                           "HelloHello WorldWorld"));
448
449   
450   if (!_dbus_string_replace_len (&str, 1, 1,
451                                  &other,
452                                  _dbus_string_get_length (&other) - 1,
453                                  1))
454     _dbus_assert_not_reached ("could not replace end character");
455   
456   _dbus_assert (_dbus_string_get_length (&str) == i);
457   _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
458   _dbus_assert (_dbus_string_equal_c_str (&other,
459                                           "HelloHello WorldWorle"));
460
461   _dbus_string_free (&str);
462   _dbus_string_free (&other);
463
464   /* Different tests are provided because different behaviours are
465    * implemented in _dbus_string_replace_len() in function of replacing and
466    * replaced lengths
467    */
468
469   if (!_dbus_string_init (&str))
470     _dbus_assert_not_reached ("failed to init string");
471   
472   if (!_dbus_string_append (&str, "Hello World"))
473     _dbus_assert_not_reached ("could not append to string");
474
475   i = _dbus_string_get_length (&str);
476   
477   if (!_dbus_string_init (&other))
478     _dbus_assert_not_reached ("could not init string");
479
480   if (!_dbus_string_append (&other, "Foo String"))
481     _dbus_assert_not_reached ("could not append to string");
482
483   a = _dbus_string_get_length (&other);
484
485   if (!_dbus_string_replace_len (&str, 0, 6,
486                                  &other, 4, 0))
487     _dbus_assert_not_reached ("could not replace 0 length");
488
489   _dbus_assert (_dbus_string_get_length (&str) == i);
490   _dbus_assert (_dbus_string_get_length (&other) == a + 6);
491   _dbus_assert (_dbus_string_equal_c_str (&other,
492                                           "Foo Hello String"));
493
494   if (!_dbus_string_replace_len (&str, 5, 6,
495                                  &other,
496                                  _dbus_string_get_length (&other),
497                                  0))
498     _dbus_assert_not_reached ("could not replace at the end");
499
500   _dbus_assert (_dbus_string_get_length (&str) == i);
501   _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
502   _dbus_assert (_dbus_string_equal_c_str (&other,
503                                           "Foo Hello String World"));
504
505   if (!_dbus_string_replace_len (&str, 0, 5,
506                                  &other,
507                                  _dbus_string_get_length (&other) - 5,
508                                  5))
509     _dbus_assert_not_reached ("could not replace same length");
510
511   _dbus_assert (_dbus_string_get_length (&str) == i);
512   _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
513   _dbus_assert (_dbus_string_equal_c_str (&other,
514                                           "Foo Hello String Hello"));
515
516   if (!_dbus_string_replace_len (&str, 6, 5,
517                                  &other, 4, 12))
518     _dbus_assert_not_reached ("could not replace with shorter string");
519
520   _dbus_assert (_dbus_string_get_length (&str) == i);
521   _dbus_assert (_dbus_string_get_length (&other) == a + 5);
522   _dbus_assert (_dbus_string_equal_c_str (&other,
523                                           "Foo World Hello"));
524
525   if (!_dbus_string_replace_len (&str, 0, 1,
526                                  &other, 0, 3))
527     _dbus_assert_not_reached ("could not replace at the beginning");
528
529   _dbus_assert (_dbus_string_get_length (&str) == i);
530   _dbus_assert (_dbus_string_get_length (&other) == a + 3);
531   _dbus_assert (_dbus_string_equal_c_str (&other,
532                                           "H World Hello"));
533
534   if (!_dbus_string_replace_len (&str, 6, 5,
535                                  &other,
536                                  _dbus_string_get_length (&other) - 5,
537                                  5))
538     _dbus_assert_not_reached ("could not replace same length");
539
540   _dbus_assert (_dbus_string_get_length (&str) == i);
541   _dbus_assert (_dbus_string_get_length (&other) == a + 3);
542   _dbus_assert (_dbus_string_equal_c_str (&other,
543                                           "H World World"));
544
545   _dbus_string_free (&str);
546   _dbus_string_free (&other);
547
548   /* Check insert/set/get byte */
549   
550   if (!_dbus_string_init (&str))
551     _dbus_assert_not_reached ("failed to init string");
552
553   if (!_dbus_string_append (&str, "Hello"))
554     _dbus_assert_not_reached ("failed to append Hello");
555
556   _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
557   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
558   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
559   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
560   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
561
562   _dbus_string_set_byte (&str, 1, 'q');
563   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
564
565   if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
566     _dbus_assert_not_reached ("can't insert byte");
567
568   if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
569     _dbus_assert_not_reached ("can't insert byte");
570
571   if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
572     _dbus_assert_not_reached ("can't insert byte");
573   
574   _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
575   _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
576   _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
577   _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
578   _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
579   _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
580   _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
581   _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
582   _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
583   _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
584   _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
585
586   _dbus_string_free (&str);
587   
588   /* Check append/parse int/double */
589   
590   if (!_dbus_string_init (&str))
591     _dbus_assert_not_reached ("failed to init string");
592
593   if (!_dbus_string_append_int (&str, 27))
594     _dbus_assert_not_reached ("failed to append int");
595
596   i = _dbus_string_get_length (&str);
597
598   if (!_dbus_string_parse_int (&str, 0, &v, &end))
599     _dbus_assert_not_reached ("failed to parse int");
600
601   _dbus_assert (v == 27);
602   _dbus_assert (end == i);
603
604   _dbus_string_free (&str);
605   
606   if (!_dbus_string_init (&str))
607     _dbus_assert_not_reached ("failed to init string");
608   
609   if (!_dbus_string_append_double (&str, 50.3))
610     _dbus_assert_not_reached ("failed to append float");
611
612   i = _dbus_string_get_length (&str);
613
614   if (!_dbus_string_parse_double (&str, 0, &d, &end))
615     _dbus_assert_not_reached ("failed to parse float");
616
617   _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
618   _dbus_assert (end == i);
619
620   _dbus_string_free (&str);
621
622   /* Test find */
623   if (!_dbus_string_init (&str))
624     _dbus_assert_not_reached ("failed to init string");
625
626   if (!_dbus_string_append (&str, "Hello"))
627     _dbus_assert_not_reached ("couldn't append to string");
628   
629   if (!_dbus_string_find (&str, 0, "He", &i))
630     _dbus_assert_not_reached ("didn't find 'He'");
631   _dbus_assert (i == 0);
632
633   if (!_dbus_string_find (&str, 0, "Hello", &i))
634     _dbus_assert_not_reached ("didn't find 'Hello'");
635   _dbus_assert (i == 0);
636   
637   if (!_dbus_string_find (&str, 0, "ello", &i))
638     _dbus_assert_not_reached ("didn't find 'ello'");
639   _dbus_assert (i == 1);
640
641   if (!_dbus_string_find (&str, 0, "lo", &i))
642     _dbus_assert_not_reached ("didn't find 'lo'");
643   _dbus_assert (i == 3);
644
645   if (!_dbus_string_find (&str, 2, "lo", &i))
646     _dbus_assert_not_reached ("didn't find 'lo'");
647   _dbus_assert (i == 3);
648
649   if (_dbus_string_find (&str, 4, "lo", &i))
650     _dbus_assert_not_reached ("did find 'lo'");
651   
652   if (!_dbus_string_find (&str, 0, "l", &i))
653     _dbus_assert_not_reached ("didn't find 'l'");
654   _dbus_assert (i == 2);
655
656   if (!_dbus_string_find (&str, 0, "H", &i))
657     _dbus_assert_not_reached ("didn't find 'H'");
658   _dbus_assert (i == 0);
659
660   if (!_dbus_string_find (&str, 0, "", &i))
661     _dbus_assert_not_reached ("didn't find ''");
662   _dbus_assert (i == 0);
663   
664   if (_dbus_string_find (&str, 0, "Hello!", NULL))
665     _dbus_assert_not_reached ("Did find 'Hello!'");
666
667   if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
668     _dbus_assert_not_reached ("Did find 'Oh, Hello'");
669   
670   if (_dbus_string_find (&str, 0, "ill", NULL))
671     _dbus_assert_not_reached ("Did find 'ill'");
672
673   if (_dbus_string_find (&str, 0, "q", NULL))
674     _dbus_assert_not_reached ("Did find 'q'");
675
676   if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
677     _dbus_assert_not_reached ("Didn't find 'He'");
678
679   if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
680     _dbus_assert_not_reached ("Did find 'Hello'");
681
682   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
683     _dbus_assert_not_reached ("Did not find 'H'");
684   _dbus_assert (i == 0);
685
686   if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
687     _dbus_assert_not_reached ("Did not find 'o'");
688   _dbus_assert (i == _dbus_string_get_length (&str) - 1);
689
690   if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
691     _dbus_assert_not_reached ("Did find 'o'");
692   _dbus_assert (i == -1);
693
694   if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
695     _dbus_assert_not_reached ("Did find 'e'");
696   _dbus_assert (i == -1);
697
698   if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
699     _dbus_assert_not_reached ("Didn't find 'e'");
700   _dbus_assert (i == 1);
701   
702   _dbus_string_free (&str);
703
704   /* Hex encoding */
705   _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
706   if (!_dbus_string_init (&other))
707     _dbus_assert_not_reached ("could not init string");
708
709   if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
710     _dbus_assert_not_reached ("deccoded bogus hex string with no error");
711
712   _dbus_assert (end == 8);
713
714   _dbus_string_free (&other);
715
716   test_roundtrips (test_hex_roundtrip);
717   
718   _dbus_string_free (&str);
719
720   {                                                                                           
721     int found, found_len;  
722
723     _dbus_string_init_const (&str, "012\r\n567\n90");
724     
725     if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
726       _dbus_assert_not_reached ("Did not find '\\r\\n'");                                       
727     if (found != 3 || found_len != 2)                                                           
728       _dbus_assert_not_reached ("invalid return values");                                       
729     
730     if (!_dbus_string_find_eol (&str, 5, &found, &found_len))                                    
731       _dbus_assert_not_reached ("Did not find '\\n'");                                          
732     if (found != 8 || found_len != 1)                                                           
733       _dbus_assert_not_reached ("invalid return values");                                       
734     
735     if (_dbus_string_find_eol (&str, 9, &found, &found_len))                                     
736       _dbus_assert_not_reached ("Found not expected '\\n'");                                    
737     else if (found != 11 || found_len != 0)                                                     
738       _dbus_assert_not_reached ("invalid return values '\\n'");                                 
739
740     found = -1;
741     found_len = -1;
742     _dbus_string_init_const (&str, "");
743     if (_dbus_string_find_eol (&str, 0, &found, &found_len))
744       _dbus_assert_not_reached ("found an eol in an empty string");
745     _dbus_assert (found == 0);
746     _dbus_assert (found_len == 0);
747     
748     found = -1;
749     found_len = -1;
750     _dbus_string_init_const (&str, "foobar");
751     if (_dbus_string_find_eol (&str, 0, &found, &found_len))
752       _dbus_assert_not_reached ("found eol in string that lacks one");
753     _dbus_assert (found == 6);
754     _dbus_assert (found_len == 0);
755
756     found = -1;
757     found_len = -1;
758     _dbus_string_init_const (&str, "foobar\n");
759     if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
760       _dbus_assert_not_reached ("did not find eol in string that has one at end");
761     _dbus_assert (found == 6);
762     _dbus_assert (found_len == 1);
763   }
764
765   {
766     DBusString line;
767
768 #define FIRST_LINE "this is a line"
769 #define SECOND_LINE "this is a second line"
770     /* third line is empty */
771 #define THIRD_LINE ""
772 #define FOURTH_LINE "this is a fourth line"
773     
774     if (!_dbus_string_init (&str))
775       _dbus_assert_not_reached ("no memory");
776
777     if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
778       _dbus_assert_not_reached ("no memory");
779     
780     if (!_dbus_string_init (&line))
781       _dbus_assert_not_reached ("no memory");
782     
783     if (!_dbus_string_pop_line (&str, &line))
784       _dbus_assert_not_reached ("failed to pop first line");
785
786     _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
787     
788     if (!_dbus_string_pop_line (&str, &line))
789       _dbus_assert_not_reached ("failed to pop second line");
790
791     _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
792     
793     if (!_dbus_string_pop_line (&str, &line))
794       _dbus_assert_not_reached ("failed to pop third line");
795
796     _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
797     
798     if (!_dbus_string_pop_line (&str, &line))
799       _dbus_assert_not_reached ("failed to pop fourth line");
800
801     _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
802     
803     _dbus_string_free (&str);
804     _dbus_string_free (&line);
805   }
806
807   {
808     if (!_dbus_string_init (&str))
809       _dbus_assert_not_reached ("no memory");
810
811     for (i = 0; i < 10000; i++)
812       if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
813         _dbus_assert_not_reached ("no memory");
814
815     if (!_dbus_string_set_length (&str, 10))
816       _dbus_assert_not_reached ("failed to set length");
817
818     /* actually compact */
819     if (!_dbus_string_compact (&str, 2048))
820       _dbus_assert_not_reached ("failed to compact after set_length");
821
822     /* peek inside to make sure it worked */
823     if (((DBusRealString *)&str)->allocated > 30)
824       _dbus_assert_not_reached ("compacting string didn't do anything");
825
826     if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
827       _dbus_assert_not_reached ("unexpected content after compact");
828
829     /* compact nothing */
830     if (!_dbus_string_compact (&str, 2048))
831       _dbus_assert_not_reached ("failed to compact 2nd time");
832
833     if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
834       _dbus_assert_not_reached ("unexpected content after 2nd compact");
835
836     /* and make sure it still works...*/
837     if (!_dbus_string_append (&str, "123456"))
838       _dbus_assert_not_reached ("failed to append after compact");
839
840     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
841       _dbus_assert_not_reached ("unexpected content after append");
842
843     /* after growing automatically, this should do nothing */
844     if (!_dbus_string_compact (&str, 20000))
845       _dbus_assert_not_reached ("failed to compact after grow");
846
847     /* but this one will do something */
848     if (!_dbus_string_compact (&str, 0))
849       _dbus_assert_not_reached ("failed to compact after grow");
850
851     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
852       _dbus_assert_not_reached ("unexpected content");
853
854     if (!_dbus_string_append (&str, "!@#$%"))
855       _dbus_assert_not_reached ("failed to append after compact");
856
857     if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
858       _dbus_assert_not_reached ("unexpected content");
859
860     _dbus_string_free (&str);
861   }
862
863   {
864     const char two_strings[] = "one\ttwo";
865
866     if (!_dbus_string_init (&str))
867       _dbus_assert_not_reached ("no memory");
868
869     if (!_dbus_string_init (&other))
870       _dbus_assert_not_reached ("no memory");
871
872     if (!_dbus_string_append (&str, two_strings))
873       _dbus_assert_not_reached ("no memory");
874
875     if (!_dbus_string_split_on_byte (&str, '\t', &other))
876       _dbus_assert_not_reached ("no memory or delimiter not found");
877
878     if (strcmp (_dbus_string_get_data (&str), "one") != 0)
879       _dbus_assert_not_reached ("left side after split on tab is wrong");
880
881     if (strcmp (_dbus_string_get_data (&other), "two") != 0)
882       _dbus_assert_not_reached ("right side after split on tab is wrong");
883
884     _dbus_string_free (&str);
885     _dbus_string_free (&other);
886   }
887
888   {
889     const char upper_string[] = "TOUPPERSTRING";
890     const char lower_string[] = "toupperstring";
891     const char lower2_string[] = "toupperSTRING";
892
893     if (!_dbus_string_init (&str))
894       _dbus_assert_not_reached ("no memory");
895
896     if (!_dbus_string_append (&str, upper_string))
897       _dbus_assert_not_reached ("no memory");
898
899     _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
900
901     if (!_dbus_string_equal_c_str (&str, lower_string))
902       _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
903
904     _dbus_string_free (&str);
905
906     if (!_dbus_string_init (&str))
907       _dbus_assert_not_reached ("no memory");
908
909     if (!_dbus_string_append (&str, upper_string))
910       _dbus_assert_not_reached ("no memory");
911
912     _dbus_string_tolower_ascii (&str, 0, 7);
913
914     if (!_dbus_string_equal_c_str (&str, lower2_string))
915       _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
916
917     _dbus_string_free (&str);
918   }
919
920   {
921     const char lower_string[] = "toupperstring";
922     const char upper_string[] = "TOUPPERSTRING";
923     const char upper2_string[] = "TOUPPERstring";
924
925     if (!_dbus_string_init (&str))
926       _dbus_assert_not_reached ("no memory");
927
928     if (!_dbus_string_append (&str, lower_string))
929       _dbus_assert_not_reached ("no memory");
930
931     _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
932
933     if (!_dbus_string_equal_c_str (&str, upper_string))
934       _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
935
936     _dbus_string_free (&str);
937
938     if (!_dbus_string_init (&str))
939       _dbus_assert_not_reached ("no memory");
940
941     if (!_dbus_string_append (&str, lower_string))
942       _dbus_assert_not_reached ("no memory");
943
944     _dbus_string_toupper_ascii (&str, 0, 7);
945
946     if (!_dbus_string_equal_c_str (&str, upper2_string))
947       _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
948
949     _dbus_string_free (&str);
950   }
951
952   return TRUE;
953 }
954
955 #endif /* DBUS_BUILD_TESTS */