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_max_len (DBusString *str,
128 if (!_dbus_string_set_length (str, max_len - 1))
129 _dbus_assert_not_reached ("setting len to one less than max should have worked");
132 if (!_dbus_string_set_length (str, max_len))
133 _dbus_assert_not_reached ("setting len to max len should have worked");
135 if (_dbus_string_set_length (str, max_len + 1))
136 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
138 if (!_dbus_string_set_length (str, 0))
139 _dbus_assert_not_reached ("setting len to zero should have worked");
143 test_hex_roundtrip (const unsigned char *data,
154 if (!_dbus_string_init (&orig))
155 _dbus_assert_not_reached ("could not init string");
157 if (!_dbus_string_init (&encoded))
158 _dbus_assert_not_reached ("could not init string");
160 if (!_dbus_string_init (&decoded))
161 _dbus_assert_not_reached ("could not init string");
163 if (!_dbus_string_append_len (&orig, data, len))
164 _dbus_assert_not_reached ("couldn't append orig data");
166 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
167 _dbus_assert_not_reached ("could not encode");
169 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
170 _dbus_assert_not_reached ("could not decode");
172 _dbus_assert (_dbus_string_get_length (&encoded) == end);
174 if (!_dbus_string_equal (&orig, &decoded))
178 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
179 _dbus_string_get_length (&orig),
180 _dbus_string_get_length (&encoded),
181 _dbus_string_get_length (&decoded));
182 printf ("Original: %s\n", data);
183 s = _dbus_string_get_const_data (&decoded);
184 printf ("Decoded: %s\n", s);
185 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
188 _dbus_string_free (&orig);
189 _dbus_string_free (&encoded);
190 _dbus_string_free (&decoded);
193 typedef void (* TestRoundtripFunc) (const unsigned char *data,
196 test_roundtrips (TestRoundtripFunc func)
198 (* func) ("Hello this is a string\n", -1);
199 (* func) ("Hello this is a string\n1", -1);
200 (* func) ("Hello this is a string\n12", -1);
201 (* func) ("Hello this is a string\n123", -1);
202 (* func) ("Hello this is a string\n1234", -1);
203 (* func) ("Hello this is a string\n12345", -1);
208 (* func) ("1234", 4);
209 (* func) ("12345", 5);
214 (* func) ("1234", 5);
215 (* func) ("12345", 6);
217 unsigned char buf[512];
221 while (i < _DBUS_N_ELEMENTS (buf))
227 while (i < _DBUS_N_ELEMENTS (buf))
235 #ifdef DBUS_BUILD_TESTS
236 /* The max length thing is sort of a historical artifact
237 * from a feature that turned out to be dumb; perhaps
238 * we should purge it entirely. The problem with
239 * the feature is that it looks like memory allocation
240 * failure, but is not a transient or resolvable failure.
243 set_max_length (DBusString *str,
246 DBusRealString *real;
248 real = (DBusRealString*) str;
250 real->max_length = max_length;
252 #endif /* DBUS_BUILD_TESTS */
255 * @ingroup DBusStringInternals
256 * Unit test for DBusString.
258 * @todo Need to write tests for _dbus_string_copy() and
259 * _dbus_string_move() moving to/from each of start/middle/end of a
260 * string. Also need tests for _dbus_string_move_len ()
262 * @returns #TRUE on success.
265 _dbus_string_test (void)
272 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 };
277 while (i < _DBUS_N_ELEMENTS (lens))
279 if (!_dbus_string_init (&str))
280 _dbus_assert_not_reached ("failed to init string");
282 set_max_length (&str, lens[i]);
284 test_max_len (&str, lens[i]);
285 _dbus_string_free (&str);
290 /* Test shortening and setting length */
292 while (i < _DBUS_N_ELEMENTS (lens))
296 if (!_dbus_string_init (&str))
297 _dbus_assert_not_reached ("failed to init string");
299 set_max_length (&str, lens[i]);
301 if (!_dbus_string_set_length (&str, lens[i]))
302 _dbus_assert_not_reached ("failed to set string length");
307 _dbus_assert (_dbus_string_get_length (&str) == j);
310 _dbus_string_shorten (&str, 1);
311 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
316 _dbus_string_free (&str);
322 if (!_dbus_string_init (&str))
323 _dbus_assert_not_reached ("oom");
325 if (!_dbus_string_append (&str, "Hello World"))
326 _dbus_assert_not_reached ("oom");
328 _dbus_string_init_const (&other, "H");
329 _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
330 _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
331 _dbus_string_init_const (&other, "Hello");
332 _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
333 _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
334 _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
335 _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
336 _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
337 _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
339 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
340 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
341 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
342 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
343 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
344 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
347 _dbus_string_init_const (&other, "World");
348 _dbus_assert (_dbus_string_equal_substring (&str, 6, 5, &other, 0));
349 _dbus_assert (_dbus_string_equal_substring (&str, 7, 4, &other, 1));
350 _dbus_assert (_dbus_string_equal_substring (&str, 8, 3, &other, 2));
351 _dbus_assert (_dbus_string_equal_substring (&str, 9, 2, &other, 3));
352 _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
353 _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
355 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
356 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
357 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
358 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
359 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
360 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
362 _dbus_string_free (&str);
364 /* Test appending data */
365 if (!_dbus_string_init (&str))
366 _dbus_assert_not_reached ("failed to init string");
371 if (!_dbus_string_append (&str, "a"))
372 _dbus_assert_not_reached ("failed to append string to string\n");
374 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
376 if (!_dbus_string_append_byte (&str, 'b'))
377 _dbus_assert_not_reached ("failed to append byte to string\n");
379 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
384 _dbus_string_free (&str);
386 /* Check steal_data */
388 if (!_dbus_string_init (&str))
389 _dbus_assert_not_reached ("failed to init string");
391 if (!_dbus_string_append (&str, "Hello World"))
392 _dbus_assert_not_reached ("could not append to string");
394 i = _dbus_string_get_length (&str);
396 if (!_dbus_string_steal_data (&str, &s))
397 _dbus_assert_not_reached ("failed to steal data");
399 _dbus_assert (_dbus_string_get_length (&str) == 0);
400 _dbus_assert (((int)strlen (s)) == i);
406 if (!_dbus_string_append (&str, "Hello World"))
407 _dbus_assert_not_reached ("could not append to string");
409 i = _dbus_string_get_length (&str);
411 if (!_dbus_string_init (&other))
412 _dbus_assert_not_reached ("could not init string");
414 if (!_dbus_string_move (&str, 0, &other, 0))
415 _dbus_assert_not_reached ("could not move");
417 _dbus_assert (_dbus_string_get_length (&str) == 0);
418 _dbus_assert (_dbus_string_get_length (&other) == i);
420 if (!_dbus_string_append (&str, "Hello World"))
421 _dbus_assert_not_reached ("could not append to string");
423 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
424 _dbus_assert_not_reached ("could not move");
426 _dbus_assert (_dbus_string_get_length (&str) == 0);
427 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
429 if (!_dbus_string_append (&str, "Hello World"))
430 _dbus_assert_not_reached ("could not append to string");
432 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
433 _dbus_assert_not_reached ("could not move");
435 _dbus_assert (_dbus_string_get_length (&str) == 0);
436 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
438 _dbus_string_free (&other);
442 if (!_dbus_string_append (&str, "Hello World"))
443 _dbus_assert_not_reached ("could not append to string");
445 i = _dbus_string_get_length (&str);
447 if (!_dbus_string_init (&other))
448 _dbus_assert_not_reached ("could not init string");
450 if (!_dbus_string_copy (&str, 0, &other, 0))
451 _dbus_assert_not_reached ("could not copy");
453 _dbus_assert (_dbus_string_get_length (&str) == i);
454 _dbus_assert (_dbus_string_get_length (&other) == i);
456 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
457 _dbus_assert_not_reached ("could not copy");
459 _dbus_assert (_dbus_string_get_length (&str) == i);
460 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
461 _dbus_assert (_dbus_string_equal_c_str (&other,
462 "Hello WorldHello World"));
464 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
465 _dbus_assert_not_reached ("could not copy");
467 _dbus_assert (_dbus_string_get_length (&str) == i);
468 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
469 _dbus_assert (_dbus_string_equal_c_str (&other,
470 "Hello WorldHello WorldHello World"));
472 _dbus_string_free (&str);
473 _dbus_string_free (&other);
477 if (!_dbus_string_init (&str))
478 _dbus_assert_not_reached ("failed to init string");
480 if (!_dbus_string_append (&str, "Hello World"))
481 _dbus_assert_not_reached ("could not append to string");
483 i = _dbus_string_get_length (&str);
485 if (!_dbus_string_init (&other))
486 _dbus_assert_not_reached ("could not init string");
488 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
489 &other, 0, _dbus_string_get_length (&other)))
490 _dbus_assert_not_reached ("could not replace");
492 _dbus_assert (_dbus_string_get_length (&str) == i);
493 _dbus_assert (_dbus_string_get_length (&other) == i);
494 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
496 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
498 _dbus_assert_not_reached ("could not replace center space");
500 _dbus_assert (_dbus_string_get_length (&str) == i);
501 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
502 _dbus_assert (_dbus_string_equal_c_str (&other,
503 "HelloHello WorldWorld"));
506 if (!_dbus_string_replace_len (&str, 1, 1,
508 _dbus_string_get_length (&other) - 1,
510 _dbus_assert_not_reached ("could not replace end character");
512 _dbus_assert (_dbus_string_get_length (&str) == i);
513 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
514 _dbus_assert (_dbus_string_equal_c_str (&other,
515 "HelloHello WorldWorle"));
517 _dbus_string_free (&str);
518 _dbus_string_free (&other);
520 /* Different tests are provided because different behaviours are
521 * implemented in _dbus_string_replace_len() in function of replacing and
525 if (!_dbus_string_init (&str))
526 _dbus_assert_not_reached ("failed to init string");
528 if (!_dbus_string_append (&str, "Hello World"))
529 _dbus_assert_not_reached ("could not append to string");
531 i = _dbus_string_get_length (&str);
533 if (!_dbus_string_init (&other))
534 _dbus_assert_not_reached ("could not init string");
536 if (!_dbus_string_append (&other, "Foo String"))
537 _dbus_assert_not_reached ("could not append to string");
539 a = _dbus_string_get_length (&other);
541 if (!_dbus_string_replace_len (&str, 0, 6,
543 _dbus_assert_not_reached ("could not replace 0 length");
545 _dbus_assert (_dbus_string_get_length (&str) == i);
546 _dbus_assert (_dbus_string_get_length (&other) == a + 6);
547 _dbus_assert (_dbus_string_equal_c_str (&other,
548 "Foo Hello String"));
550 if (!_dbus_string_replace_len (&str, 5, 6,
552 _dbus_string_get_length (&other),
554 _dbus_assert_not_reached ("could not replace at the end");
556 _dbus_assert (_dbus_string_get_length (&str) == i);
557 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
558 _dbus_assert (_dbus_string_equal_c_str (&other,
559 "Foo Hello String World"));
561 if (!_dbus_string_replace_len (&str, 0, 5,
563 _dbus_string_get_length (&other) - 5,
565 _dbus_assert_not_reached ("could not replace same length");
567 _dbus_assert (_dbus_string_get_length (&str) == i);
568 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
569 _dbus_assert (_dbus_string_equal_c_str (&other,
570 "Foo Hello String Hello"));
572 if (!_dbus_string_replace_len (&str, 6, 5,
574 _dbus_assert_not_reached ("could not replace with shorter string");
576 _dbus_assert (_dbus_string_get_length (&str) == i);
577 _dbus_assert (_dbus_string_get_length (&other) == a + 5);
578 _dbus_assert (_dbus_string_equal_c_str (&other,
581 if (!_dbus_string_replace_len (&str, 0, 1,
583 _dbus_assert_not_reached ("could not replace at the beginning");
585 _dbus_assert (_dbus_string_get_length (&str) == i);
586 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
587 _dbus_assert (_dbus_string_equal_c_str (&other,
590 if (!_dbus_string_replace_len (&str, 6, 5,
592 _dbus_string_get_length (&other) - 5,
594 _dbus_assert_not_reached ("could not replace same length");
596 _dbus_assert (_dbus_string_get_length (&str) == i);
597 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
598 _dbus_assert (_dbus_string_equal_c_str (&other,
601 _dbus_string_free (&str);
602 _dbus_string_free (&other);
604 /* Check append/get unichar */
606 if (!_dbus_string_init (&str))
607 _dbus_assert_not_reached ("failed to init string");
610 if (!_dbus_string_append_unichar (&str, 0xfffc))
611 _dbus_assert_not_reached ("failed to append unichar");
613 _dbus_string_get_unichar (&str, 0, &ch, &i);
615 _dbus_assert (ch == 0xfffc);
616 _dbus_assert (i == _dbus_string_get_length (&str));
618 _dbus_string_free (&str);
620 /* Check insert/set/get byte */
622 if (!_dbus_string_init (&str))
623 _dbus_assert_not_reached ("failed to init string");
625 if (!_dbus_string_append (&str, "Hello"))
626 _dbus_assert_not_reached ("failed to append Hello");
628 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
629 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
630 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
631 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
632 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
634 _dbus_string_set_byte (&str, 1, 'q');
635 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
637 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
638 _dbus_assert_not_reached ("can't insert byte");
640 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
641 _dbus_assert_not_reached ("can't insert byte");
643 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
644 _dbus_assert_not_reached ("can't insert byte");
646 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
647 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
648 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
649 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
650 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
651 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
652 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
653 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
654 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
655 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
656 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
658 _dbus_string_free (&str);
660 /* Check append/parse int/double */
662 if (!_dbus_string_init (&str))
663 _dbus_assert_not_reached ("failed to init string");
665 if (!_dbus_string_append_int (&str, 27))
666 _dbus_assert_not_reached ("failed to append int");
668 i = _dbus_string_get_length (&str);
670 if (!_dbus_string_parse_int (&str, 0, &v, &end))
671 _dbus_assert_not_reached ("failed to parse int");
673 _dbus_assert (v == 27);
674 _dbus_assert (end == i);
676 _dbus_string_free (&str);
678 if (!_dbus_string_init (&str))
679 _dbus_assert_not_reached ("failed to init string");
681 if (!_dbus_string_append_double (&str, 50.3))
682 _dbus_assert_not_reached ("failed to append float");
684 i = _dbus_string_get_length (&str);
686 if (!_dbus_string_parse_double (&str, 0, &d, &end))
687 _dbus_assert_not_reached ("failed to parse float");
689 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
690 _dbus_assert (end == i);
692 _dbus_string_free (&str);
695 if (!_dbus_string_init (&str))
696 _dbus_assert_not_reached ("failed to init string");
698 if (!_dbus_string_append (&str, "Hello"))
699 _dbus_assert_not_reached ("couldn't append to string");
701 if (!_dbus_string_find (&str, 0, "He", &i))
702 _dbus_assert_not_reached ("didn't find 'He'");
703 _dbus_assert (i == 0);
705 if (!_dbus_string_find (&str, 0, "Hello", &i))
706 _dbus_assert_not_reached ("didn't find 'Hello'");
707 _dbus_assert (i == 0);
709 if (!_dbus_string_find (&str, 0, "ello", &i))
710 _dbus_assert_not_reached ("didn't find 'ello'");
711 _dbus_assert (i == 1);
713 if (!_dbus_string_find (&str, 0, "lo", &i))
714 _dbus_assert_not_reached ("didn't find 'lo'");
715 _dbus_assert (i == 3);
717 if (!_dbus_string_find (&str, 2, "lo", &i))
718 _dbus_assert_not_reached ("didn't find 'lo'");
719 _dbus_assert (i == 3);
721 if (_dbus_string_find (&str, 4, "lo", &i))
722 _dbus_assert_not_reached ("did find 'lo'");
724 if (!_dbus_string_find (&str, 0, "l", &i))
725 _dbus_assert_not_reached ("didn't find 'l'");
726 _dbus_assert (i == 2);
728 if (!_dbus_string_find (&str, 0, "H", &i))
729 _dbus_assert_not_reached ("didn't find 'H'");
730 _dbus_assert (i == 0);
732 if (!_dbus_string_find (&str, 0, "", &i))
733 _dbus_assert_not_reached ("didn't find ''");
734 _dbus_assert (i == 0);
736 if (_dbus_string_find (&str, 0, "Hello!", NULL))
737 _dbus_assert_not_reached ("Did find 'Hello!'");
739 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
740 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
742 if (_dbus_string_find (&str, 0, "ill", NULL))
743 _dbus_assert_not_reached ("Did find 'ill'");
745 if (_dbus_string_find (&str, 0, "q", NULL))
746 _dbus_assert_not_reached ("Did find 'q'");
748 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
749 _dbus_assert_not_reached ("Didn't find 'He'");
751 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
752 _dbus_assert_not_reached ("Did find 'Hello'");
754 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
755 _dbus_assert_not_reached ("Did not find 'H'");
756 _dbus_assert (i == 0);
758 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
759 _dbus_assert_not_reached ("Did not find 'o'");
760 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
762 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
763 _dbus_assert_not_reached ("Did find 'o'");
764 _dbus_assert (i == -1);
766 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
767 _dbus_assert_not_reached ("Did find 'e'");
768 _dbus_assert (i == -1);
770 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
771 _dbus_assert_not_reached ("Didn't find 'e'");
772 _dbus_assert (i == 1);
774 _dbus_string_free (&str);
777 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
778 if (!_dbus_string_init (&other))
779 _dbus_assert_not_reached ("could not init string");
781 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
782 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
784 _dbus_assert (end == 8);
786 _dbus_string_free (&other);
788 test_roundtrips (test_hex_roundtrip);
790 _dbus_string_free (&str);
793 int found, found_len;
795 _dbus_string_init_const (&str, "012\r\n567\n90");
797 if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
798 _dbus_assert_not_reached ("Did not find '\\r\\n'");
799 if (found != 3 || found_len != 2)
800 _dbus_assert_not_reached ("invalid return values");
802 if (!_dbus_string_find_eol (&str, 5, &found, &found_len))
803 _dbus_assert_not_reached ("Did not find '\\n'");
804 if (found != 8 || found_len != 1)
805 _dbus_assert_not_reached ("invalid return values");
807 if (_dbus_string_find_eol (&str, 9, &found, &found_len))
808 _dbus_assert_not_reached ("Found not expected '\\n'");
809 else if (found != 11 || found_len != 0)
810 _dbus_assert_not_reached ("invalid return values '\\n'");
814 _dbus_string_init_const (&str, "");
815 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
816 _dbus_assert_not_reached ("found an eol in an empty string");
817 _dbus_assert (found == 0);
818 _dbus_assert (found_len == 0);
822 _dbus_string_init_const (&str, "foobar");
823 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
824 _dbus_assert_not_reached ("found eol in string that lacks one");
825 _dbus_assert (found == 6);
826 _dbus_assert (found_len == 0);
830 _dbus_string_init_const (&str, "foobar\n");
831 if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
832 _dbus_assert_not_reached ("did not find eol in string that has one at end");
833 _dbus_assert (found == 6);
834 _dbus_assert (found_len == 1);
840 #define FIRST_LINE "this is a line"
841 #define SECOND_LINE "this is a second line"
842 /* third line is empty */
843 #define THIRD_LINE ""
844 #define FOURTH_LINE "this is a fourth line"
846 if (!_dbus_string_init (&str))
847 _dbus_assert_not_reached ("no memory");
849 if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
850 _dbus_assert_not_reached ("no memory");
852 if (!_dbus_string_init (&line))
853 _dbus_assert_not_reached ("no memory");
855 if (!_dbus_string_pop_line (&str, &line))
856 _dbus_assert_not_reached ("failed to pop first line");
858 _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
860 if (!_dbus_string_pop_line (&str, &line))
861 _dbus_assert_not_reached ("failed to pop second line");
863 _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
865 if (!_dbus_string_pop_line (&str, &line))
866 _dbus_assert_not_reached ("failed to pop third line");
868 _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
870 if (!_dbus_string_pop_line (&str, &line))
871 _dbus_assert_not_reached ("failed to pop fourth line");
873 _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
875 _dbus_string_free (&str);
876 _dbus_string_free (&line);
880 if (!_dbus_string_init (&str))
881 _dbus_assert_not_reached ("no memory");
883 for (i = 0; i < 10000; i++)
884 if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
885 _dbus_assert_not_reached ("no memory");
887 if (!_dbus_string_set_length (&str, 10))
888 _dbus_assert_not_reached ("failed to set length");
890 /* actually compact */
891 if (!_dbus_string_compact (&str, 2048))
892 _dbus_assert_not_reached ("failed to compact after set_length");
894 /* peek inside to make sure it worked */
895 if (((DBusRealString *)&str)->allocated > 30)
896 _dbus_assert_not_reached ("compacting string didn't do anything");
898 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
899 _dbus_assert_not_reached ("unexpected content after compact");
901 /* compact nothing */
902 if (!_dbus_string_compact (&str, 2048))
903 _dbus_assert_not_reached ("failed to compact 2nd time");
905 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
906 _dbus_assert_not_reached ("unexpected content after 2nd compact");
908 /* and make sure it still works...*/
909 if (!_dbus_string_append (&str, "123456"))
910 _dbus_assert_not_reached ("failed to append after compact");
912 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
913 _dbus_assert_not_reached ("unexpected content after append");
915 /* after growing automatically, this should do nothing */
916 if (!_dbus_string_compact (&str, 20000))
917 _dbus_assert_not_reached ("failed to compact after grow");
919 /* but this one will do something */
920 if (!_dbus_string_compact (&str, 0))
921 _dbus_assert_not_reached ("failed to compact after grow");
923 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
924 _dbus_assert_not_reached ("unexpected content");
926 if (!_dbus_string_append (&str, "!@#$%"))
927 _dbus_assert_not_reached ("failed to append after compact");
929 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
930 _dbus_assert_not_reached ("unexpected content");
932 _dbus_string_free (&str);
936 const char two_strings[] = "one\ttwo";
938 if (!_dbus_string_init (&str))
939 _dbus_assert_not_reached ("no memory");
941 if (!_dbus_string_init (&other))
942 _dbus_assert_not_reached ("no memory");
944 if (!_dbus_string_append (&str, two_strings))
945 _dbus_assert_not_reached ("no memory");
947 if (!_dbus_string_split_on_byte (&str, '\t', &other))
948 _dbus_assert_not_reached ("no memory or delimiter not found");
950 if (strcmp (_dbus_string_get_data (&str), "one") != 0)
951 _dbus_assert_not_reached ("left side after split on tab is wrong");
953 if (strcmp (_dbus_string_get_data (&other), "two") != 0)
954 _dbus_assert_not_reached ("right side after split on tab is wrong");
956 _dbus_string_free (&str);
957 _dbus_string_free (&other);
961 const char upper_string[] = "TOUPPERSTRING";
962 const char lower_string[] = "toupperstring";
963 const char lower2_string[] = "toupperSTRING";
965 if (!_dbus_string_init (&str))
966 _dbus_assert_not_reached ("no memory");
968 if (!_dbus_string_append (&str, upper_string))
969 _dbus_assert_not_reached ("no memory");
971 _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
973 if (!_dbus_string_equal_c_str (&str, lower_string))
974 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
976 _dbus_string_free (&str);
978 if (!_dbus_string_init (&str))
979 _dbus_assert_not_reached ("no memory");
981 if (!_dbus_string_append (&str, upper_string))
982 _dbus_assert_not_reached ("no memory");
984 _dbus_string_tolower_ascii (&str, 0, 7);
986 if (!_dbus_string_equal_c_str (&str, lower2_string))
987 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
989 _dbus_string_free (&str);
993 const char lower_string[] = "toupperstring";
994 const char upper_string[] = "TOUPPERSTRING";
995 const char upper2_string[] = "TOUPPERstring";
997 if (!_dbus_string_init (&str))
998 _dbus_assert_not_reached ("no memory");
1000 if (!_dbus_string_append (&str, lower_string))
1001 _dbus_assert_not_reached ("no memory");
1003 _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
1005 if (!_dbus_string_equal_c_str (&str, upper_string))
1006 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
1008 _dbus_string_free (&str);
1010 if (!_dbus_string_init (&str))
1011 _dbus_assert_not_reached ("no memory");
1013 if (!_dbus_string_append (&str, lower_string))
1014 _dbus_assert_not_reached ("no memory");
1016 _dbus_string_toupper_ascii (&str, 0, 7);
1018 if (!_dbus_string_equal_c_str (&str, upper2_string))
1019 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
1021 _dbus_string_free (&str);
1027 #endif /* DBUS_BUILD_TESTS */