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 /* Check append/get unichar */
522 if (!_dbus_string_init (&str))
523 _dbus_assert_not_reached ("failed to init string");
526 if (!_dbus_string_append_unichar (&str, 0xfffc))
527 _dbus_assert_not_reached ("failed to append unichar");
529 _dbus_string_get_unichar (&str, 0, &ch, &i);
531 _dbus_assert (ch == 0xfffc);
532 _dbus_assert (i == _dbus_string_get_length (&str));
534 _dbus_string_free (&str);
536 /* Check insert/set/get byte */
538 if (!_dbus_string_init (&str))
539 _dbus_assert_not_reached ("failed to init string");
541 if (!_dbus_string_append (&str, "Hello"))
542 _dbus_assert_not_reached ("failed to append Hello");
544 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
545 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
546 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
547 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
548 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
550 _dbus_string_set_byte (&str, 1, 'q');
551 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
553 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
554 _dbus_assert_not_reached ("can't insert byte");
556 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
557 _dbus_assert_not_reached ("can't insert byte");
559 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
560 _dbus_assert_not_reached ("can't insert byte");
562 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
563 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
564 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
565 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
566 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
567 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
568 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
569 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
570 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
571 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
572 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
574 _dbus_string_free (&str);
576 /* Check append/parse int/double */
578 if (!_dbus_string_init (&str))
579 _dbus_assert_not_reached ("failed to init string");
581 if (!_dbus_string_append_int (&str, 27))
582 _dbus_assert_not_reached ("failed to append int");
584 i = _dbus_string_get_length (&str);
586 if (!_dbus_string_parse_int (&str, 0, &v, &end))
587 _dbus_assert_not_reached ("failed to parse int");
589 _dbus_assert (v == 27);
590 _dbus_assert (end == i);
592 _dbus_string_free (&str);
594 if (!_dbus_string_init (&str))
595 _dbus_assert_not_reached ("failed to init string");
597 if (!_dbus_string_append_double (&str, 50.3))
598 _dbus_assert_not_reached ("failed to append float");
600 i = _dbus_string_get_length (&str);
602 if (!_dbus_string_parse_double (&str, 0, &d, &end))
603 _dbus_assert_not_reached ("failed to parse float");
605 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
606 _dbus_assert (end == i);
608 _dbus_string_free (&str);
611 if (!_dbus_string_init (&str))
612 _dbus_assert_not_reached ("failed to init string");
614 if (!_dbus_string_append (&str, "Hello"))
615 _dbus_assert_not_reached ("couldn't append to string");
617 if (!_dbus_string_find (&str, 0, "He", &i))
618 _dbus_assert_not_reached ("didn't find 'He'");
619 _dbus_assert (i == 0);
621 if (!_dbus_string_find (&str, 0, "Hello", &i))
622 _dbus_assert_not_reached ("didn't find 'Hello'");
623 _dbus_assert (i == 0);
625 if (!_dbus_string_find (&str, 0, "ello", &i))
626 _dbus_assert_not_reached ("didn't find 'ello'");
627 _dbus_assert (i == 1);
629 if (!_dbus_string_find (&str, 0, "lo", &i))
630 _dbus_assert_not_reached ("didn't find 'lo'");
631 _dbus_assert (i == 3);
633 if (!_dbus_string_find (&str, 2, "lo", &i))
634 _dbus_assert_not_reached ("didn't find 'lo'");
635 _dbus_assert (i == 3);
637 if (_dbus_string_find (&str, 4, "lo", &i))
638 _dbus_assert_not_reached ("did find 'lo'");
640 if (!_dbus_string_find (&str, 0, "l", &i))
641 _dbus_assert_not_reached ("didn't find 'l'");
642 _dbus_assert (i == 2);
644 if (!_dbus_string_find (&str, 0, "H", &i))
645 _dbus_assert_not_reached ("didn't find 'H'");
646 _dbus_assert (i == 0);
648 if (!_dbus_string_find (&str, 0, "", &i))
649 _dbus_assert_not_reached ("didn't find ''");
650 _dbus_assert (i == 0);
652 if (_dbus_string_find (&str, 0, "Hello!", NULL))
653 _dbus_assert_not_reached ("Did find 'Hello!'");
655 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
656 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
658 if (_dbus_string_find (&str, 0, "ill", NULL))
659 _dbus_assert_not_reached ("Did find 'ill'");
661 if (_dbus_string_find (&str, 0, "q", NULL))
662 _dbus_assert_not_reached ("Did find 'q'");
664 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
665 _dbus_assert_not_reached ("Didn't find 'He'");
667 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
668 _dbus_assert_not_reached ("Did find 'Hello'");
670 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
671 _dbus_assert_not_reached ("Did not find 'H'");
672 _dbus_assert (i == 0);
674 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
675 _dbus_assert_not_reached ("Did not find 'o'");
676 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
678 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
679 _dbus_assert_not_reached ("Did find 'o'");
680 _dbus_assert (i == -1);
682 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
683 _dbus_assert_not_reached ("Did find 'e'");
684 _dbus_assert (i == -1);
686 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
687 _dbus_assert_not_reached ("Didn't find 'e'");
688 _dbus_assert (i == 1);
690 _dbus_string_free (&str);
693 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
694 if (!_dbus_string_init (&other))
695 _dbus_assert_not_reached ("could not init string");
697 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
698 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
700 _dbus_assert (end == 8);
702 _dbus_string_free (&other);
704 test_roundtrips (test_hex_roundtrip);
706 _dbus_string_free (&str);
709 int found, found_len;
711 _dbus_string_init_const (&str, "012\r\n567\n90");
713 if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
714 _dbus_assert_not_reached ("Did not find '\\r\\n'");
715 if (found != 3 || found_len != 2)
716 _dbus_assert_not_reached ("invalid return values");
718 if (!_dbus_string_find_eol (&str, 5, &found, &found_len))
719 _dbus_assert_not_reached ("Did not find '\\n'");
720 if (found != 8 || found_len != 1)
721 _dbus_assert_not_reached ("invalid return values");
723 if (_dbus_string_find_eol (&str, 9, &found, &found_len))
724 _dbus_assert_not_reached ("Found not expected '\\n'");
725 else if (found != 11 || found_len != 0)
726 _dbus_assert_not_reached ("invalid return values '\\n'");
730 _dbus_string_init_const (&str, "");
731 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
732 _dbus_assert_not_reached ("found an eol in an empty string");
733 _dbus_assert (found == 0);
734 _dbus_assert (found_len == 0);
738 _dbus_string_init_const (&str, "foobar");
739 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
740 _dbus_assert_not_reached ("found eol in string that lacks one");
741 _dbus_assert (found == 6);
742 _dbus_assert (found_len == 0);
746 _dbus_string_init_const (&str, "foobar\n");
747 if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
748 _dbus_assert_not_reached ("did not find eol in string that has one at end");
749 _dbus_assert (found == 6);
750 _dbus_assert (found_len == 1);
756 #define FIRST_LINE "this is a line"
757 #define SECOND_LINE "this is a second line"
758 /* third line is empty */
759 #define THIRD_LINE ""
760 #define FOURTH_LINE "this is a fourth line"
762 if (!_dbus_string_init (&str))
763 _dbus_assert_not_reached ("no memory");
765 if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
766 _dbus_assert_not_reached ("no memory");
768 if (!_dbus_string_init (&line))
769 _dbus_assert_not_reached ("no memory");
771 if (!_dbus_string_pop_line (&str, &line))
772 _dbus_assert_not_reached ("failed to pop first line");
774 _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
776 if (!_dbus_string_pop_line (&str, &line))
777 _dbus_assert_not_reached ("failed to pop second line");
779 _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
781 if (!_dbus_string_pop_line (&str, &line))
782 _dbus_assert_not_reached ("failed to pop third line");
784 _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
786 if (!_dbus_string_pop_line (&str, &line))
787 _dbus_assert_not_reached ("failed to pop fourth line");
789 _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
791 _dbus_string_free (&str);
792 _dbus_string_free (&line);
796 if (!_dbus_string_init (&str))
797 _dbus_assert_not_reached ("no memory");
799 for (i = 0; i < 10000; i++)
800 if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
801 _dbus_assert_not_reached ("no memory");
803 if (!_dbus_string_set_length (&str, 10))
804 _dbus_assert_not_reached ("failed to set length");
806 /* actually compact */
807 if (!_dbus_string_compact (&str, 2048))
808 _dbus_assert_not_reached ("failed to compact after set_length");
810 /* peek inside to make sure it worked */
811 if (((DBusRealString *)&str)->allocated > 30)
812 _dbus_assert_not_reached ("compacting string didn't do anything");
814 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
815 _dbus_assert_not_reached ("unexpected content after compact");
817 /* compact nothing */
818 if (!_dbus_string_compact (&str, 2048))
819 _dbus_assert_not_reached ("failed to compact 2nd time");
821 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
822 _dbus_assert_not_reached ("unexpected content after 2nd compact");
824 /* and make sure it still works...*/
825 if (!_dbus_string_append (&str, "123456"))
826 _dbus_assert_not_reached ("failed to append after compact");
828 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
829 _dbus_assert_not_reached ("unexpected content after append");
831 /* after growing automatically, this should do nothing */
832 if (!_dbus_string_compact (&str, 20000))
833 _dbus_assert_not_reached ("failed to compact after grow");
835 /* but this one will do something */
836 if (!_dbus_string_compact (&str, 0))
837 _dbus_assert_not_reached ("failed to compact after grow");
839 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
840 _dbus_assert_not_reached ("unexpected content");
842 if (!_dbus_string_append (&str, "!@#$%"))
843 _dbus_assert_not_reached ("failed to append after compact");
845 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
846 _dbus_assert_not_reached ("unexpected content");
848 _dbus_string_free (&str);
852 const char two_strings[] = "one\ttwo";
854 if (!_dbus_string_init (&str))
855 _dbus_assert_not_reached ("no memory");
857 if (!_dbus_string_init (&other))
858 _dbus_assert_not_reached ("no memory");
860 if (!_dbus_string_append (&str, two_strings))
861 _dbus_assert_not_reached ("no memory");
863 if (!_dbus_string_split_on_byte (&str, '\t', &other))
864 _dbus_assert_not_reached ("no memory or delimiter not found");
866 if (strcmp (_dbus_string_get_data (&str), "one") != 0)
867 _dbus_assert_not_reached ("left side after split on tab is wrong");
869 if (strcmp (_dbus_string_get_data (&other), "two") != 0)
870 _dbus_assert_not_reached ("right side after split on tab is wrong");
872 _dbus_string_free (&str);
873 _dbus_string_free (&other);
877 const char upper_string[] = "TOUPPERSTRING";
878 const char lower_string[] = "toupperstring";
879 const char lower2_string[] = "toupperSTRING";
881 if (!_dbus_string_init (&str))
882 _dbus_assert_not_reached ("no memory");
884 if (!_dbus_string_append (&str, upper_string))
885 _dbus_assert_not_reached ("no memory");
887 _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
889 if (!_dbus_string_equal_c_str (&str, lower_string))
890 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
892 _dbus_string_free (&str);
894 if (!_dbus_string_init (&str))
895 _dbus_assert_not_reached ("no memory");
897 if (!_dbus_string_append (&str, upper_string))
898 _dbus_assert_not_reached ("no memory");
900 _dbus_string_tolower_ascii (&str, 0, 7);
902 if (!_dbus_string_equal_c_str (&str, lower2_string))
903 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
905 _dbus_string_free (&str);
909 const char lower_string[] = "toupperstring";
910 const char upper_string[] = "TOUPPERSTRING";
911 const char upper2_string[] = "TOUPPERstring";
913 if (!_dbus_string_init (&str))
914 _dbus_assert_not_reached ("no memory");
916 if (!_dbus_string_append (&str, lower_string))
917 _dbus_assert_not_reached ("no memory");
919 _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
921 if (!_dbus_string_equal_c_str (&str, upper_string))
922 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
924 _dbus_string_free (&str);
926 if (!_dbus_string_init (&str))
927 _dbus_assert_not_reached ("no memory");
929 if (!_dbus_string_append (&str, lower_string))
930 _dbus_assert_not_reached ("no memory");
932 _dbus_string_toupper_ascii (&str, 0, 7);
934 if (!_dbus_string_equal_c_str (&str, upper2_string))
935 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
937 _dbus_string_free (&str);
943 #endif /* DBUS_BUILD_TESTS */