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_ENABLE_EMBEDDED_TESTS
119 #include "dbus-test.h"
123 test_hex_roundtrip (const 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 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))
209 (* func) ((const char *) buf, i);
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)
232 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 };
235 /* Test shortening and setting length */
237 while (i < _DBUS_N_ELEMENTS (lens))
241 if (!_dbus_string_init (&str))
242 _dbus_assert_not_reached ("failed to init string");
244 if (!_dbus_string_set_length (&str, lens[i]))
245 _dbus_assert_not_reached ("failed to set string length");
250 _dbus_assert (_dbus_string_get_length (&str) == j);
253 _dbus_string_shorten (&str, 1);
254 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
259 _dbus_string_free (&str);
265 if (!_dbus_string_init (&str))
266 _dbus_assert_not_reached ("oom");
268 if (!_dbus_string_append (&str, "Hello World"))
269 _dbus_assert_not_reached ("oom");
271 _dbus_string_init_const (&other, "H");
272 _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
273 _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
274 _dbus_string_init_const (&other, "Hello");
275 _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
276 _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
277 _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
278 _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
279 _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
280 _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
282 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
283 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
284 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
285 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
286 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
287 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
290 _dbus_string_init_const (&other, "World");
291 _dbus_assert (_dbus_string_equal_substring (&str, 6, 5, &other, 0));
292 _dbus_assert (_dbus_string_equal_substring (&str, 7, 4, &other, 1));
293 _dbus_assert (_dbus_string_equal_substring (&str, 8, 3, &other, 2));
294 _dbus_assert (_dbus_string_equal_substring (&str, 9, 2, &other, 3));
295 _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
296 _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
298 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
299 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
300 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
301 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
302 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
303 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
305 _dbus_string_free (&str);
307 /* Test appending data */
308 if (!_dbus_string_init (&str))
309 _dbus_assert_not_reached ("failed to init string");
314 if (!_dbus_string_append (&str, "a"))
315 _dbus_assert_not_reached ("failed to append string to string");
317 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
319 if (!_dbus_string_append_byte (&str, 'b'))
320 _dbus_assert_not_reached ("failed to append byte to string");
322 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
327 _dbus_string_free (&str);
329 /* Check steal_data */
331 if (!_dbus_string_init (&str))
332 _dbus_assert_not_reached ("failed to init string");
334 if (!_dbus_string_append (&str, "Hello World"))
335 _dbus_assert_not_reached ("could not append to string");
337 i = _dbus_string_get_length (&str);
339 if (!_dbus_string_steal_data (&str, &s))
340 _dbus_assert_not_reached ("failed to steal data");
342 _dbus_assert (_dbus_string_get_length (&str) == 0);
343 _dbus_assert (((int)strlen (s)) == i);
349 if (!_dbus_string_append (&str, "Hello World"))
350 _dbus_assert_not_reached ("could not append to string");
352 i = _dbus_string_get_length (&str);
354 if (!_dbus_string_init (&other))
355 _dbus_assert_not_reached ("could not init string");
357 if (!_dbus_string_move (&str, 0, &other, 0))
358 _dbus_assert_not_reached ("could not move");
360 _dbus_assert (_dbus_string_get_length (&str) == 0);
361 _dbus_assert (_dbus_string_get_length (&other) == i);
363 if (!_dbus_string_append (&str, "Hello World"))
364 _dbus_assert_not_reached ("could not append to string");
366 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
367 _dbus_assert_not_reached ("could not move");
369 _dbus_assert (_dbus_string_get_length (&str) == 0);
370 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
372 if (!_dbus_string_append (&str, "Hello World"))
373 _dbus_assert_not_reached ("could not append to string");
375 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
376 _dbus_assert_not_reached ("could not move");
378 _dbus_assert (_dbus_string_get_length (&str) == 0);
379 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
381 _dbus_string_free (&other);
385 if (!_dbus_string_append (&str, "Hello World"))
386 _dbus_assert_not_reached ("could not append to string");
388 i = _dbus_string_get_length (&str);
390 if (!_dbus_string_init (&other))
391 _dbus_assert_not_reached ("could not init string");
393 if (!_dbus_string_copy (&str, 0, &other, 0))
394 _dbus_assert_not_reached ("could not copy");
396 _dbus_assert (_dbus_string_get_length (&str) == i);
397 _dbus_assert (_dbus_string_get_length (&other) == i);
399 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
400 _dbus_assert_not_reached ("could not copy");
402 _dbus_assert (_dbus_string_get_length (&str) == i);
403 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
404 _dbus_assert (_dbus_string_equal_c_str (&other,
405 "Hello WorldHello World"));
407 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
408 _dbus_assert_not_reached ("could not copy");
410 _dbus_assert (_dbus_string_get_length (&str) == i);
411 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
412 _dbus_assert (_dbus_string_equal_c_str (&other,
413 "Hello WorldHello WorldHello World"));
415 _dbus_string_free (&str);
416 _dbus_string_free (&other);
420 if (!_dbus_string_init (&str))
421 _dbus_assert_not_reached ("failed to init string");
423 if (!_dbus_string_append (&str, "Hello World"))
424 _dbus_assert_not_reached ("could not append to string");
426 i = _dbus_string_get_length (&str);
428 if (!_dbus_string_init (&other))
429 _dbus_assert_not_reached ("could not init string");
431 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
432 &other, 0, _dbus_string_get_length (&other)))
433 _dbus_assert_not_reached ("could not replace");
435 _dbus_assert (_dbus_string_get_length (&str) == i);
436 _dbus_assert (_dbus_string_get_length (&other) == i);
437 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
439 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
441 _dbus_assert_not_reached ("could not replace center space");
443 _dbus_assert (_dbus_string_get_length (&str) == i);
444 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
445 _dbus_assert (_dbus_string_equal_c_str (&other,
446 "HelloHello WorldWorld"));
449 if (!_dbus_string_replace_len (&str, 1, 1,
451 _dbus_string_get_length (&other) - 1,
453 _dbus_assert_not_reached ("could not replace end character");
455 _dbus_assert (_dbus_string_get_length (&str) == i);
456 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
457 _dbus_assert (_dbus_string_equal_c_str (&other,
458 "HelloHello WorldWorle"));
460 _dbus_string_free (&str);
461 _dbus_string_free (&other);
463 /* Different tests are provided because different behaviours are
464 * implemented in _dbus_string_replace_len() in function of replacing and
468 if (!_dbus_string_init (&str))
469 _dbus_assert_not_reached ("failed to init string");
471 if (!_dbus_string_append (&str, "Hello World"))
472 _dbus_assert_not_reached ("could not append to string");
474 i = _dbus_string_get_length (&str);
476 if (!_dbus_string_init (&other))
477 _dbus_assert_not_reached ("could not init string");
479 if (!_dbus_string_append (&other, "Foo String"))
480 _dbus_assert_not_reached ("could not append to string");
482 a = _dbus_string_get_length (&other);
484 if (!_dbus_string_replace_len (&str, 0, 6,
486 _dbus_assert_not_reached ("could not replace 0 length");
488 _dbus_assert (_dbus_string_get_length (&str) == i);
489 _dbus_assert (_dbus_string_get_length (&other) == a + 6);
490 _dbus_assert (_dbus_string_equal_c_str (&other,
491 "Foo Hello String"));
493 if (!_dbus_string_replace_len (&str, 5, 6,
495 _dbus_string_get_length (&other),
497 _dbus_assert_not_reached ("could not replace at the end");
499 _dbus_assert (_dbus_string_get_length (&str) == i);
500 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
501 _dbus_assert (_dbus_string_equal_c_str (&other,
502 "Foo Hello String World"));
504 if (!_dbus_string_replace_len (&str, 0, 5,
506 _dbus_string_get_length (&other) - 5,
508 _dbus_assert_not_reached ("could not replace same length");
510 _dbus_assert (_dbus_string_get_length (&str) == i);
511 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
512 _dbus_assert (_dbus_string_equal_c_str (&other,
513 "Foo Hello String Hello"));
515 if (!_dbus_string_replace_len (&str, 6, 5,
517 _dbus_assert_not_reached ("could not replace with shorter string");
519 _dbus_assert (_dbus_string_get_length (&str) == i);
520 _dbus_assert (_dbus_string_get_length (&other) == a + 5);
521 _dbus_assert (_dbus_string_equal_c_str (&other,
524 if (!_dbus_string_replace_len (&str, 0, 1,
526 _dbus_assert_not_reached ("could not replace at the beginning");
528 _dbus_assert (_dbus_string_get_length (&str) == i);
529 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
530 _dbus_assert (_dbus_string_equal_c_str (&other,
533 if (!_dbus_string_replace_len (&str, 6, 5,
535 _dbus_string_get_length (&other) - 5,
537 _dbus_assert_not_reached ("could not replace same length");
539 _dbus_assert (_dbus_string_get_length (&str) == i);
540 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
541 _dbus_assert (_dbus_string_equal_c_str (&other,
544 _dbus_string_free (&str);
545 _dbus_string_free (&other);
547 /* Check insert/set/get byte */
549 if (!_dbus_string_init (&str))
550 _dbus_assert_not_reached ("failed to init string");
552 if (!_dbus_string_append (&str, "Hello"))
553 _dbus_assert_not_reached ("failed to append Hello");
555 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
556 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
557 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
558 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
559 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
561 _dbus_string_set_byte (&str, 1, 'q');
562 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
564 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
565 _dbus_assert_not_reached ("can't insert byte");
567 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
568 _dbus_assert_not_reached ("can't insert byte");
570 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
571 _dbus_assert_not_reached ("can't insert byte");
573 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
574 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
575 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
576 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
577 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
578 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
579 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
580 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
581 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
582 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
583 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
585 _dbus_string_free (&str);
587 /* Check append/parse int/double */
589 if (!_dbus_string_init (&str))
590 _dbus_assert_not_reached ("failed to init string");
592 if (!_dbus_string_append_int (&str, 27))
593 _dbus_assert_not_reached ("failed to append int");
595 i = _dbus_string_get_length (&str);
597 if (!_dbus_string_parse_int (&str, 0, &v, &end))
598 _dbus_assert_not_reached ("failed to parse int");
600 _dbus_assert (v == 27);
601 _dbus_assert (end == i);
603 _dbus_string_free (&str);
606 if (!_dbus_string_init (&str))
607 _dbus_assert_not_reached ("failed to init string");
609 if (!_dbus_string_append (&str, "Hello"))
610 _dbus_assert_not_reached ("couldn't append to string");
612 if (!_dbus_string_find (&str, 0, "He", &i))
613 _dbus_assert_not_reached ("didn't find 'He'");
614 _dbus_assert (i == 0);
616 if (!_dbus_string_find (&str, 0, "Hello", &i))
617 _dbus_assert_not_reached ("didn't find 'Hello'");
618 _dbus_assert (i == 0);
620 if (!_dbus_string_find (&str, 0, "ello", &i))
621 _dbus_assert_not_reached ("didn't find 'ello'");
622 _dbus_assert (i == 1);
624 if (!_dbus_string_find (&str, 0, "lo", &i))
625 _dbus_assert_not_reached ("didn't find 'lo'");
626 _dbus_assert (i == 3);
628 if (!_dbus_string_find (&str, 2, "lo", &i))
629 _dbus_assert_not_reached ("didn't find 'lo'");
630 _dbus_assert (i == 3);
632 if (_dbus_string_find (&str, 4, "lo", &i))
633 _dbus_assert_not_reached ("did find 'lo'");
635 if (!_dbus_string_find (&str, 0, "l", &i))
636 _dbus_assert_not_reached ("didn't find 'l'");
637 _dbus_assert (i == 2);
639 if (!_dbus_string_find (&str, 0, "H", &i))
640 _dbus_assert_not_reached ("didn't find 'H'");
641 _dbus_assert (i == 0);
643 if (!_dbus_string_find (&str, 0, "", &i))
644 _dbus_assert_not_reached ("didn't find ''");
645 _dbus_assert (i == 0);
647 if (_dbus_string_find (&str, 0, "Hello!", NULL))
648 _dbus_assert_not_reached ("Did find 'Hello!'");
650 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
651 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
653 if (_dbus_string_find (&str, 0, "ill", NULL))
654 _dbus_assert_not_reached ("Did find 'ill'");
656 if (_dbus_string_find (&str, 0, "q", NULL))
657 _dbus_assert_not_reached ("Did find 'q'");
659 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
660 _dbus_assert_not_reached ("Didn't find 'He'");
662 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
663 _dbus_assert_not_reached ("Did find 'Hello'");
665 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
666 _dbus_assert_not_reached ("Did not find 'H'");
667 _dbus_assert (i == 0);
669 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
670 _dbus_assert_not_reached ("Did not find 'o'");
671 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
673 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
674 _dbus_assert_not_reached ("Did find 'o'");
675 _dbus_assert (i == -1);
677 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
678 _dbus_assert_not_reached ("Did find 'e'");
679 _dbus_assert (i == -1);
681 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
682 _dbus_assert_not_reached ("Didn't find 'e'");
683 _dbus_assert (i == 1);
685 _dbus_string_free (&str);
688 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
689 if (!_dbus_string_init (&other))
690 _dbus_assert_not_reached ("could not init string");
692 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
693 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
695 _dbus_assert (end == 8);
697 _dbus_string_free (&other);
699 test_roundtrips (test_hex_roundtrip);
701 _dbus_string_free (&str);
704 int found, found_len;
706 _dbus_string_init_const (&str, "012\r\n567\n90");
708 if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
709 _dbus_assert_not_reached ("Did not find '\\r\\n'");
710 if (found != 3 || found_len != 2)
711 _dbus_assert_not_reached ("invalid return values");
713 if (!_dbus_string_find_eol (&str, 5, &found, &found_len))
714 _dbus_assert_not_reached ("Did not find '\\n'");
715 if (found != 8 || found_len != 1)
716 _dbus_assert_not_reached ("invalid return values");
718 if (_dbus_string_find_eol (&str, 9, &found, &found_len))
719 _dbus_assert_not_reached ("Found not expected '\\n'");
720 else if (found != 11 || found_len != 0)
721 _dbus_assert_not_reached ("invalid return values '\\n'");
725 _dbus_string_init_const (&str, "");
726 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
727 _dbus_assert_not_reached ("found an eol in an empty string");
728 _dbus_assert (found == 0);
729 _dbus_assert (found_len == 0);
733 _dbus_string_init_const (&str, "foobar");
734 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
735 _dbus_assert_not_reached ("found eol in string that lacks one");
736 _dbus_assert (found == 6);
737 _dbus_assert (found_len == 0);
741 _dbus_string_init_const (&str, "foobar\n");
742 if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
743 _dbus_assert_not_reached ("did not find eol in string that has one at end");
744 _dbus_assert (found == 6);
745 _dbus_assert (found_len == 1);
751 #define FIRST_LINE "this is a line"
752 #define SECOND_LINE "this is a second line"
753 /* third line is empty */
754 #define THIRD_LINE ""
755 #define FOURTH_LINE "this is a fourth line"
757 if (!_dbus_string_init (&str))
758 _dbus_assert_not_reached ("no memory");
760 if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
761 _dbus_assert_not_reached ("no memory");
763 if (!_dbus_string_init (&line))
764 _dbus_assert_not_reached ("no memory");
766 if (!_dbus_string_pop_line (&str, &line))
767 _dbus_assert_not_reached ("failed to pop first line");
769 _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
771 if (!_dbus_string_pop_line (&str, &line))
772 _dbus_assert_not_reached ("failed to pop second line");
774 _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
776 if (!_dbus_string_pop_line (&str, &line))
777 _dbus_assert_not_reached ("failed to pop third line");
779 _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
781 if (!_dbus_string_pop_line (&str, &line))
782 _dbus_assert_not_reached ("failed to pop fourth line");
784 _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
786 _dbus_string_free (&str);
787 _dbus_string_free (&line);
791 if (!_dbus_string_init (&str))
792 _dbus_assert_not_reached ("no memory");
794 for (i = 0; i < 10000; i++)
795 if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
796 _dbus_assert_not_reached ("no memory");
798 if (!_dbus_string_set_length (&str, 10))
799 _dbus_assert_not_reached ("failed to set length");
801 /* actually compact */
802 if (!_dbus_string_compact (&str, 2048))
803 _dbus_assert_not_reached ("failed to compact after set_length");
805 /* peek inside to make sure it worked */
806 if (((DBusRealString *)&str)->allocated > 30)
807 _dbus_assert_not_reached ("compacting string didn't do anything");
809 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
810 _dbus_assert_not_reached ("unexpected content after compact");
812 /* compact nothing */
813 if (!_dbus_string_compact (&str, 2048))
814 _dbus_assert_not_reached ("failed to compact 2nd time");
816 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
817 _dbus_assert_not_reached ("unexpected content after 2nd compact");
819 /* and make sure it still works...*/
820 if (!_dbus_string_append (&str, "123456"))
821 _dbus_assert_not_reached ("failed to append after compact");
823 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
824 _dbus_assert_not_reached ("unexpected content after append");
826 /* after growing automatically, this should do nothing */
827 if (!_dbus_string_compact (&str, 20000))
828 _dbus_assert_not_reached ("failed to compact after grow");
830 /* but this one will do something */
831 if (!_dbus_string_compact (&str, 0))
832 _dbus_assert_not_reached ("failed to compact after grow");
834 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
835 _dbus_assert_not_reached ("unexpected content");
837 if (!_dbus_string_append (&str, "!@#$%"))
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");
843 _dbus_string_free (&str);
847 const char two_strings[] = "one\ttwo";
849 if (!_dbus_string_init (&str))
850 _dbus_assert_not_reached ("no memory");
852 if (!_dbus_string_init (&other))
853 _dbus_assert_not_reached ("no memory");
855 if (!_dbus_string_append (&str, two_strings))
856 _dbus_assert_not_reached ("no memory");
858 if (!_dbus_string_split_on_byte (&str, '\t', &other))
859 _dbus_assert_not_reached ("no memory or delimiter not found");
861 if (strcmp (_dbus_string_get_data (&str), "one") != 0)
862 _dbus_assert_not_reached ("left side after split on tab is wrong");
864 if (strcmp (_dbus_string_get_data (&other), "two") != 0)
865 _dbus_assert_not_reached ("right side after split on tab is wrong");
867 _dbus_string_free (&str);
868 _dbus_string_free (&other);
872 const char upper_string[] = "TOUPPERSTRING";
873 const char lower_string[] = "toupperstring";
874 const char lower2_string[] = "toupperSTRING";
876 if (!_dbus_string_init (&str))
877 _dbus_assert_not_reached ("no memory");
879 if (!_dbus_string_append (&str, upper_string))
880 _dbus_assert_not_reached ("no memory");
882 _dbus_string_tolower_ascii (&str, 0, _dbus_string_get_length(&str));
884 if (!_dbus_string_equal_c_str (&str, lower_string))
885 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed");
887 _dbus_string_free (&str);
889 if (!_dbus_string_init (&str))
890 _dbus_assert_not_reached ("no memory");
892 if (!_dbus_string_append (&str, upper_string))
893 _dbus_assert_not_reached ("no memory");
895 _dbus_string_tolower_ascii (&str, 0, 7);
897 if (!_dbus_string_equal_c_str (&str, lower2_string))
898 _dbus_assert_not_reached ("_dbus_string_tolower_ascii failed in partial conversion");
900 _dbus_string_free (&str);
904 const char lower_string[] = "toupperstring";
905 const char upper_string[] = "TOUPPERSTRING";
906 const char upper2_string[] = "TOUPPERstring";
908 if (!_dbus_string_init (&str))
909 _dbus_assert_not_reached ("no memory");
911 if (!_dbus_string_append (&str, lower_string))
912 _dbus_assert_not_reached ("no memory");
914 _dbus_string_toupper_ascii (&str, 0, _dbus_string_get_length(&str));
916 if (!_dbus_string_equal_c_str (&str, upper_string))
917 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed");
919 _dbus_string_free (&str);
921 if (!_dbus_string_init (&str))
922 _dbus_assert_not_reached ("no memory");
924 if (!_dbus_string_append (&str, lower_string))
925 _dbus_assert_not_reached ("no memory");
927 _dbus_string_toupper_ascii (&str, 0, 7);
929 if (!_dbus_string_equal_c_str (&str, upper2_string))
930 _dbus_assert_not_reached ("_dbus_string_toupper_ascii failed in partial conversion");
932 _dbus_string_free (&str);
938 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */