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
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
32 * @addtogroup DBusString
37 * Returns whether a string ends with the given suffix
39 * @todo memcmp might make this faster.
42 * @param c_str the C-style string
43 * @returns #TRUE if the string ends with the suffix
46 _dbus_string_ends_with_c_str (const DBusString *a,
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);
57 c_str_len = strlen (c_str);
58 if (((unsigned long)real_a->len) < c_str_len)
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;
73 _dbus_assert (*ap == '\0');
74 _dbus_assert (*bp == '\0');
80 * Find the given byte scanning backward from the given start.
81 * Sets *found to -1 if the byte is not found.
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
90 _dbus_string_find_byte_backward (const DBusString *str,
96 DBUS_CONST_STRING_PREAMBLE (str);
97 _dbus_assert (start <= real->len);
98 _dbus_assert (start >= 0);
99 _dbus_assert (found != NULL);
104 if (real->str[i] == byte)
118 #ifdef DBUS_BUILD_TESTS
119 #include "dbus-test.h"
123 test_hex_roundtrip (const unsigned char *data,
134 if (!_dbus_string_init (&orig))
135 _dbus_assert_not_reached ("could not init string");
137 if (!_dbus_string_init (&encoded))
138 _dbus_assert_not_reached ("could not init string");
140 if (!_dbus_string_init (&decoded))
141 _dbus_assert_not_reached ("could not init string");
143 if (!_dbus_string_append_len (&orig, data, len))
144 _dbus_assert_not_reached ("couldn't append orig data");
146 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
147 _dbus_assert_not_reached ("could not encode");
149 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
150 _dbus_assert_not_reached ("could not decode");
152 _dbus_assert (_dbus_string_get_length (&encoded) == end);
154 if (!_dbus_string_equal (&orig, &decoded))
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");
168 _dbus_string_free (&orig);
169 _dbus_string_free (&encoded);
170 _dbus_string_free (&decoded);
173 typedef void (* TestRoundtripFunc) (const unsigned char *data,
176 test_roundtrips (TestRoundtripFunc func)
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);
188 (* func) ("1234", 4);
189 (* func) ("12345", 5);
194 (* func) ("1234", 5);
195 (* func) ("12345", 6);
197 unsigned char buf[512];
201 while (i < _DBUS_N_ELEMENTS (buf))
207 while (i < _DBUS_N_ELEMENTS (buf))
216 * @ingroup DBusStringInternals
217 * Unit test for DBusString.
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 ()
223 * @returns #TRUE on success.
226 _dbus_string_test (void)
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 };
236 /* Test shortening and setting length */
238 while (i < _DBUS_N_ELEMENTS (lens))
242 if (!_dbus_string_init (&str))
243 _dbus_assert_not_reached ("failed to init string");
245 if (!_dbus_string_set_length (&str, lens[i]))
246 _dbus_assert_not_reached ("failed to set string length");
251 _dbus_assert (_dbus_string_get_length (&str) == j);
254 _dbus_string_shorten (&str, 1);
255 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
260 _dbus_string_free (&str);
266 if (!_dbus_string_init (&str))
267 _dbus_assert_not_reached ("oom");
269 if (!_dbus_string_append (&str, "Hello World"))
270 _dbus_assert_not_reached ("oom");
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));
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));
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));
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));
306 _dbus_string_free (&str);
308 /* Test appending data */
309 if (!_dbus_string_init (&str))
310 _dbus_assert_not_reached ("failed to init string");
315 if (!_dbus_string_append (&str, "a"))
316 _dbus_assert_not_reached ("failed to append string to string\n");
318 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
320 if (!_dbus_string_append_byte (&str, 'b'))
321 _dbus_assert_not_reached ("failed to append byte to string\n");
323 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
328 _dbus_string_free (&str);
330 /* Check steal_data */
332 if (!_dbus_string_init (&str))
333 _dbus_assert_not_reached ("failed to init string");
335 if (!_dbus_string_append (&str, "Hello World"))
336 _dbus_assert_not_reached ("could not append to string");
338 i = _dbus_string_get_length (&str);
340 if (!_dbus_string_steal_data (&str, &s))
341 _dbus_assert_not_reached ("failed to steal data");
343 _dbus_assert (_dbus_string_get_length (&str) == 0);
344 _dbus_assert (((int)strlen (s)) == i);
350 if (!_dbus_string_append (&str, "Hello World"))
351 _dbus_assert_not_reached ("could not append to string");
353 i = _dbus_string_get_length (&str);
355 if (!_dbus_string_init (&other))
356 _dbus_assert_not_reached ("could not init string");
358 if (!_dbus_string_move (&str, 0, &other, 0))
359 _dbus_assert_not_reached ("could not move");
361 _dbus_assert (_dbus_string_get_length (&str) == 0);
362 _dbus_assert (_dbus_string_get_length (&other) == i);
364 if (!_dbus_string_append (&str, "Hello World"))
365 _dbus_assert_not_reached ("could not append to string");
367 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
368 _dbus_assert_not_reached ("could not move");
370 _dbus_assert (_dbus_string_get_length (&str) == 0);
371 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
373 if (!_dbus_string_append (&str, "Hello World"))
374 _dbus_assert_not_reached ("could not append to string");
376 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
377 _dbus_assert_not_reached ("could not move");
379 _dbus_assert (_dbus_string_get_length (&str) == 0);
380 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
382 _dbus_string_free (&other);
386 if (!_dbus_string_append (&str, "Hello World"))
387 _dbus_assert_not_reached ("could not append to string");
389 i = _dbus_string_get_length (&str);
391 if (!_dbus_string_init (&other))
392 _dbus_assert_not_reached ("could not init string");
394 if (!_dbus_string_copy (&str, 0, &other, 0))
395 _dbus_assert_not_reached ("could not copy");
397 _dbus_assert (_dbus_string_get_length (&str) == i);
398 _dbus_assert (_dbus_string_get_length (&other) == i);
400 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
401 _dbus_assert_not_reached ("could not copy");
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"));
408 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
409 _dbus_assert_not_reached ("could not copy");
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"));
416 _dbus_string_free (&str);
417 _dbus_string_free (&other);
421 if (!_dbus_string_init (&str))
422 _dbus_assert_not_reached ("failed to init string");
424 if (!_dbus_string_append (&str, "Hello World"))
425 _dbus_assert_not_reached ("could not append to string");
427 i = _dbus_string_get_length (&str);
429 if (!_dbus_string_init (&other))
430 _dbus_assert_not_reached ("could not init string");
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");
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"));
440 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
442 _dbus_assert_not_reached ("could not replace center space");
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"));
450 if (!_dbus_string_replace_len (&str, 1, 1,
452 _dbus_string_get_length (&other) - 1,
454 _dbus_assert_not_reached ("could not replace end character");
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"));
461 _dbus_string_free (&str);
462 _dbus_string_free (&other);
464 /* Different tests are provided because different behaviours are
465 * implemented in _dbus_string_replace_len() in function of replacing and
469 if (!_dbus_string_init (&str))
470 _dbus_assert_not_reached ("failed to init string");
472 if (!_dbus_string_append (&str, "Hello World"))
473 _dbus_assert_not_reached ("could not append to string");
475 i = _dbus_string_get_length (&str);
477 if (!_dbus_string_init (&other))
478 _dbus_assert_not_reached ("could not init string");
480 if (!_dbus_string_append (&other, "Foo String"))
481 _dbus_assert_not_reached ("could not append to string");
483 a = _dbus_string_get_length (&other);
485 if (!_dbus_string_replace_len (&str, 0, 6,
487 _dbus_assert_not_reached ("could not replace 0 length");
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"));
494 if (!_dbus_string_replace_len (&str, 5, 6,
496 _dbus_string_get_length (&other),
498 _dbus_assert_not_reached ("could not replace at the end");
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"));
505 if (!_dbus_string_replace_len (&str, 0, 5,
507 _dbus_string_get_length (&other) - 5,
509 _dbus_assert_not_reached ("could not replace same length");
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"));
516 if (!_dbus_string_replace_len (&str, 6, 5,
518 _dbus_assert_not_reached ("could not replace with shorter string");
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,
525 if (!_dbus_string_replace_len (&str, 0, 1,
527 _dbus_assert_not_reached ("could not replace at the beginning");
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,
534 if (!_dbus_string_replace_len (&str, 6, 5,
536 _dbus_string_get_length (&other) - 5,
538 _dbus_assert_not_reached ("could not replace same length");
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,
545 _dbus_string_free (&str);
546 _dbus_string_free (&other);
548 /* Check insert/set/get byte */
550 if (!_dbus_string_init (&str))
551 _dbus_assert_not_reached ("failed to init string");
553 if (!_dbus_string_append (&str, "Hello"))
554 _dbus_assert_not_reached ("failed to append Hello");
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');
562 _dbus_string_set_byte (&str, 1, 'q');
563 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
565 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
566 _dbus_assert_not_reached ("can't insert byte");
568 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
569 _dbus_assert_not_reached ("can't insert byte");
571 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
572 _dbus_assert_not_reached ("can't insert byte");
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');
586 _dbus_string_free (&str);
588 /* Check append/parse int/double */
590 if (!_dbus_string_init (&str))
591 _dbus_assert_not_reached ("failed to init string");
593 if (!_dbus_string_append_int (&str, 27))
594 _dbus_assert_not_reached ("failed to append int");
596 i = _dbus_string_get_length (&str);
598 if (!_dbus_string_parse_int (&str, 0, &v, &end))
599 _dbus_assert_not_reached ("failed to parse int");
601 _dbus_assert (v == 27);
602 _dbus_assert (end == i);
604 _dbus_string_free (&str);
606 if (!_dbus_string_init (&str))
607 _dbus_assert_not_reached ("failed to init string");
609 if (!_dbus_string_append_double (&str, 50.3))
610 _dbus_assert_not_reached ("failed to append float");
612 i = _dbus_string_get_length (&str);
614 if (!_dbus_string_parse_double (&str, 0, &d, &end))
615 _dbus_assert_not_reached ("failed to parse float");
617 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
618 _dbus_assert (end == i);
620 _dbus_string_free (&str);
623 if (!_dbus_string_init (&str))
624 _dbus_assert_not_reached ("failed to init string");
626 if (!_dbus_string_append (&str, "Hello"))
627 _dbus_assert_not_reached ("couldn't append to string");
629 if (!_dbus_string_find (&str, 0, "He", &i))
630 _dbus_assert_not_reached ("didn't find 'He'");
631 _dbus_assert (i == 0);
633 if (!_dbus_string_find (&str, 0, "Hello", &i))
634 _dbus_assert_not_reached ("didn't find 'Hello'");
635 _dbus_assert (i == 0);
637 if (!_dbus_string_find (&str, 0, "ello", &i))
638 _dbus_assert_not_reached ("didn't find 'ello'");
639 _dbus_assert (i == 1);
641 if (!_dbus_string_find (&str, 0, "lo", &i))
642 _dbus_assert_not_reached ("didn't find 'lo'");
643 _dbus_assert (i == 3);
645 if (!_dbus_string_find (&str, 2, "lo", &i))
646 _dbus_assert_not_reached ("didn't find 'lo'");
647 _dbus_assert (i == 3);
649 if (_dbus_string_find (&str, 4, "lo", &i))
650 _dbus_assert_not_reached ("did find 'lo'");
652 if (!_dbus_string_find (&str, 0, "l", &i))
653 _dbus_assert_not_reached ("didn't find 'l'");
654 _dbus_assert (i == 2);
656 if (!_dbus_string_find (&str, 0, "H", &i))
657 _dbus_assert_not_reached ("didn't find 'H'");
658 _dbus_assert (i == 0);
660 if (!_dbus_string_find (&str, 0, "", &i))
661 _dbus_assert_not_reached ("didn't find ''");
662 _dbus_assert (i == 0);
664 if (_dbus_string_find (&str, 0, "Hello!", NULL))
665 _dbus_assert_not_reached ("Did find 'Hello!'");
667 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
668 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
670 if (_dbus_string_find (&str, 0, "ill", NULL))
671 _dbus_assert_not_reached ("Did find 'ill'");
673 if (_dbus_string_find (&str, 0, "q", NULL))
674 _dbus_assert_not_reached ("Did find 'q'");
676 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
677 _dbus_assert_not_reached ("Didn't find 'He'");
679 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
680 _dbus_assert_not_reached ("Did find 'Hello'");
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);
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);
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);
694 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
695 _dbus_assert_not_reached ("Did find 'e'");
696 _dbus_assert (i == -1);
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);
702 _dbus_string_free (&str);
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");
709 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
710 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
712 _dbus_assert (end == 8);
714 _dbus_string_free (&other);
716 test_roundtrips (test_hex_roundtrip);
718 _dbus_string_free (&str);
721 int found, found_len;
723 _dbus_string_init_const (&str, "012\r\n567\n90");
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");
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");
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'");
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);
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);
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);
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"
774 if (!_dbus_string_init (&str))
775 _dbus_assert_not_reached ("no memory");
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");
780 if (!_dbus_string_init (&line))
781 _dbus_assert_not_reached ("no memory");
783 if (!_dbus_string_pop_line (&str, &line))
784 _dbus_assert_not_reached ("failed to pop first line");
786 _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
788 if (!_dbus_string_pop_line (&str, &line))
789 _dbus_assert_not_reached ("failed to pop second line");
791 _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
793 if (!_dbus_string_pop_line (&str, &line))
794 _dbus_assert_not_reached ("failed to pop third line");
796 _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
798 if (!_dbus_string_pop_line (&str, &line))
799 _dbus_assert_not_reached ("failed to pop fourth line");
801 _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
803 _dbus_string_free (&str);
804 _dbus_string_free (&line);
808 if (!_dbus_string_init (&str))
809 _dbus_assert_not_reached ("no memory");
811 for (i = 0; i < 10000; i++)
812 if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
813 _dbus_assert_not_reached ("no memory");
815 if (!_dbus_string_set_length (&str, 10))
816 _dbus_assert_not_reached ("failed to set length");
818 /* actually compact */
819 if (!_dbus_string_compact (&str, 2048))
820 _dbus_assert_not_reached ("failed to compact after set_length");
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");
826 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
827 _dbus_assert_not_reached ("unexpected content after compact");
829 /* compact nothing */
830 if (!_dbus_string_compact (&str, 2048))
831 _dbus_assert_not_reached ("failed to compact 2nd time");
833 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
834 _dbus_assert_not_reached ("unexpected content after 2nd compact");
836 /* and make sure it still works...*/
837 if (!_dbus_string_append (&str, "123456"))
838 _dbus_assert_not_reached ("failed to append after compact");
840 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
841 _dbus_assert_not_reached ("unexpected content after append");
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");
847 /* but this one will do something */
848 if (!_dbus_string_compact (&str, 0))
849 _dbus_assert_not_reached ("failed to compact after grow");
851 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
852 _dbus_assert_not_reached ("unexpected content");
854 if (!_dbus_string_append (&str, "!@#$%"))
855 _dbus_assert_not_reached ("failed to append after compact");
857 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
858 _dbus_assert_not_reached ("unexpected content");
860 _dbus_string_free (&str);
864 const char two_strings[] = "one\ttwo";
866 if (!_dbus_string_init (&str))
867 _dbus_assert_not_reached ("no memory");
869 if (!_dbus_string_init (&other))
870 _dbus_assert_not_reached ("no memory");
872 if (!_dbus_string_append (&str, two_strings))
873 _dbus_assert_not_reached ("no memory");
875 if (!_dbus_string_split_on_byte (&str, '\t', &other))
876 _dbus_assert_not_reached ("no memory or delimiter not found");
878 if (strcmp (_dbus_string_get_data (&str), "one") != 0)
879 _dbus_assert_not_reached ("left side after split on tab is wrong");
881 if (strcmp (_dbus_string_get_data (&other), "two") != 0)
882 _dbus_assert_not_reached ("right side after split on tab is wrong");
884 _dbus_string_free (&str);
885 _dbus_string_free (&other);
889 const char upper_string[] = "TOUPPERSTRING";
890 const char lower_string[] = "toupperstring";
891 const char lower2_string[] = "toupperSTRING";
893 if (!_dbus_string_init (&str))
894 _dbus_assert_not_reached ("no memory");
896 if (!_dbus_string_append (&str, upper_string))
897 _dbus_assert_not_reached ("no memory");
899 _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
901 if (!_dbus_string_equal_c_str (&str, lower_string))
902 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
904 _dbus_string_free (&str);
906 if (!_dbus_string_init (&str))
907 _dbus_assert_not_reached ("no memory");
909 if (!_dbus_string_append (&str, upper_string))
910 _dbus_assert_not_reached ("no memory");
912 _dbus_string_tolower_ascii (&str, 0, 7);
914 if (!_dbus_string_equal_c_str (&str, lower2_string))
915 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
917 _dbus_string_free (&str);
921 const char lower_string[] = "toupperstring";
922 const char upper_string[] = "TOUPPERSTRING";
923 const char upper2_string[] = "TOUPPERstring";
925 if (!_dbus_string_init (&str))
926 _dbus_assert_not_reached ("no memory");
928 if (!_dbus_string_append (&str, lower_string))
929 _dbus_assert_not_reached ("no memory");
931 _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
933 if (!_dbus_string_equal_c_str (&str, upper_string))
934 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
936 _dbus_string_free (&str);
938 if (!_dbus_string_init (&str))
939 _dbus_assert_not_reached ("no memory");
941 if (!_dbus_string_append (&str, lower_string))
942 _dbus_assert_not_reached ("no memory");
944 _dbus_string_toupper_ascii (&str, 0, 7);
946 if (!_dbus_string_equal_c_str (&str, upper2_string))
947 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
949 _dbus_string_free (&str);
955 #endif /* DBUS_BUILD_TESTS */