00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "dbus-internals.h"
00026 #include "dbus-string.h"
00027 #define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
00028 #include "dbus-string-private.h"
00029
00044 dbus_bool_t
00045 _dbus_string_ends_with_c_str (const DBusString *a,
00046 const char *c_str)
00047 {
00048 const unsigned char *ap;
00049 const unsigned char *bp;
00050 const unsigned char *a_end;
00051 unsigned long c_str_len;
00052 const DBusRealString *real_a = (const DBusRealString*) a;
00053 DBUS_GENERIC_STRING_PREAMBLE (real_a);
00054 _dbus_assert (c_str != NULL);
00055
00056 c_str_len = strlen (c_str);
00057 if (((unsigned long)real_a->len) < c_str_len)
00058 return FALSE;
00059
00060 ap = real_a->str + (real_a->len - c_str_len);
00061 bp = (const unsigned char*) c_str;
00062 a_end = real_a->str + real_a->len;
00063 while (ap != a_end)
00064 {
00065 if (*ap != *bp)
00066 return FALSE;
00067
00068 ++ap;
00069 ++bp;
00070 }
00071
00072 _dbus_assert (*ap == '\0');
00073 _dbus_assert (*bp == '\0');
00074
00075 return TRUE;
00076 }
00077
00088 dbus_bool_t
00089 _dbus_string_find_byte_backward (const DBusString *str,
00090 int start,
00091 unsigned char byte,
00092 int *found)
00093 {
00094 int i;
00095 DBUS_CONST_STRING_PREAMBLE (str);
00096 _dbus_assert (start <= real->len);
00097 _dbus_assert (start >= 0);
00098 _dbus_assert (found != NULL);
00099
00100 i = start - 1;
00101 while (i >= 0)
00102 {
00103 if (real->str[i] == byte)
00104 break;
00105
00106 --i;
00107 }
00108
00109 if (found)
00110 *found = i;
00111
00112 return i >= 0;
00113 }
00114
00117 #ifdef DBUS_BUILD_TESTS
00118 #include "dbus-test.h"
00119 #include <stdio.h>
00120
00121 static void
00122 test_max_len (DBusString *str,
00123 int max_len)
00124 {
00125 if (max_len > 0)
00126 {
00127 if (!_dbus_string_set_length (str, max_len - 1))
00128 _dbus_assert_not_reached ("setting len to one less than max should have worked");
00129 }
00130
00131 if (!_dbus_string_set_length (str, max_len))
00132 _dbus_assert_not_reached ("setting len to max len should have worked");
00133
00134 if (_dbus_string_set_length (str, max_len + 1))
00135 _dbus_assert_not_reached ("setting len to one more than max len should not have worked");
00136
00137 if (!_dbus_string_set_length (str, 0))
00138 _dbus_assert_not_reached ("setting len to zero should have worked");
00139 }
00140
00141 static void
00142 test_hex_roundtrip (const unsigned char *data,
00143 int len)
00144 {
00145 DBusString orig;
00146 DBusString encoded;
00147 DBusString decoded;
00148 int end;
00149
00150 if (len < 0)
00151 len = strlen (data);
00152
00153 if (!_dbus_string_init (&orig))
00154 _dbus_assert_not_reached ("could not init string");
00155
00156 if (!_dbus_string_init (&encoded))
00157 _dbus_assert_not_reached ("could not init string");
00158
00159 if (!_dbus_string_init (&decoded))
00160 _dbus_assert_not_reached ("could not init string");
00161
00162 if (!_dbus_string_append_len (&orig, data, len))
00163 _dbus_assert_not_reached ("couldn't append orig data");
00164
00165 if (!_dbus_string_hex_encode (&orig, 0, &encoded, 0))
00166 _dbus_assert_not_reached ("could not encode");
00167
00168 if (!_dbus_string_hex_decode (&encoded, 0, &end, &decoded, 0))
00169 _dbus_assert_not_reached ("could not decode");
00170
00171 _dbus_assert (_dbus_string_get_length (&encoded) == end);
00172
00173 if (!_dbus_string_equal (&orig, &decoded))
00174 {
00175 const char *s;
00176
00177 printf ("Original string %d bytes encoded %d bytes decoded %d bytes\n",
00178 _dbus_string_get_length (&orig),
00179 _dbus_string_get_length (&encoded),
00180 _dbus_string_get_length (&decoded));
00181 printf ("Original: %s\n", data);
00182 s = _dbus_string_get_const_data (&decoded);
00183 printf ("Decoded: %s\n", s);
00184 _dbus_assert_not_reached ("original string not the same as string decoded from hex");
00185 }
00186
00187 _dbus_string_free (&orig);
00188 _dbus_string_free (&encoded);
00189 _dbus_string_free (&decoded);
00190 }
00191
00192 typedef void (* TestRoundtripFunc) (const unsigned char *data,
00193 int len);
00194 static void
00195 test_roundtrips (TestRoundtripFunc func)
00196 {
00197 (* func) ("Hello this is a string\n", -1);
00198 (* func) ("Hello this is a string\n1", -1);
00199 (* func) ("Hello this is a string\n12", -1);
00200 (* func) ("Hello this is a string\n123", -1);
00201 (* func) ("Hello this is a string\n1234", -1);
00202 (* func) ("Hello this is a string\n12345", -1);
00203 (* func) ("", 0);
00204 (* func) ("1", 1);
00205 (* func) ("12", 2);
00206 (* func) ("123", 3);
00207 (* func) ("1234", 4);
00208 (* func) ("12345", 5);
00209 (* func) ("", 1);
00210 (* func) ("1", 2);
00211 (* func) ("12", 3);
00212 (* func) ("123", 4);
00213 (* func) ("1234", 5);
00214 (* func) ("12345", 6);
00215 {
00216 unsigned char buf[512];
00217 int i;
00218
00219 i = 0;
00220 while (i < _DBUS_N_ELEMENTS (buf))
00221 {
00222 buf[i] = i;
00223 ++i;
00224 }
00225 i = 0;
00226 while (i < _DBUS_N_ELEMENTS (buf))
00227 {
00228 (* func) (buf, i);
00229 ++i;
00230 }
00231 }
00232 }
00233
00234 #ifdef DBUS_BUILD_TESTS
00235
00236
00237
00238
00239
00240
00241 static void
00242 set_max_length (DBusString *str,
00243 int max_length)
00244 {
00245 DBusRealString *real;
00246
00247 real = (DBusRealString*) str;
00248
00249 real->max_length = max_length;
00250 }
00251 #endif
00252
00263 dbus_bool_t
00264 _dbus_string_test (void)
00265 {
00266 DBusString str;
00267 DBusString other;
00268 int i, end;
00269 long v;
00270 double d;
00271 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 };
00272 char *s;
00273 dbus_unichar_t ch;
00274
00275 i = 0;
00276 while (i < _DBUS_N_ELEMENTS (lens))
00277 {
00278 if (!_dbus_string_init (&str))
00279 _dbus_assert_not_reached ("failed to init string");
00280
00281 set_max_length (&str, lens[i]);
00282
00283 test_max_len (&str, lens[i]);
00284 _dbus_string_free (&str);
00285
00286 ++i;
00287 }
00288
00289
00290 i = 0;
00291 while (i < _DBUS_N_ELEMENTS (lens))
00292 {
00293 int j;
00294
00295 if (!_dbus_string_init (&str))
00296 _dbus_assert_not_reached ("failed to init string");
00297
00298 set_max_length (&str, lens[i]);
00299
00300 if (!_dbus_string_set_length (&str, lens[i]))
00301 _dbus_assert_not_reached ("failed to set string length");
00302
00303 j = lens[i];
00304 while (j > 0)
00305 {
00306 _dbus_assert (_dbus_string_get_length (&str) == j);
00307 if (j > 0)
00308 {
00309 _dbus_string_shorten (&str, 1);
00310 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
00311 }
00312 --j;
00313 }
00314
00315 _dbus_string_free (&str);
00316
00317 ++i;
00318 }
00319
00320
00321 if (!_dbus_string_init (&str))
00322 _dbus_assert_not_reached ("oom");
00323
00324 if (!_dbus_string_append (&str, "Hello World"))
00325 _dbus_assert_not_reached ("oom");
00326
00327 _dbus_string_init_const (&other, "H");
00328 _dbus_assert (_dbus_string_equal_substring (&str, 0, 1, &other, 0));
00329 _dbus_assert (_dbus_string_equal_substring (&str, 1, 0, &other, 1));
00330 _dbus_string_init_const (&other, "Hello");
00331 _dbus_assert (_dbus_string_equal_substring (&str, 0, 5, &other, 0));
00332 _dbus_assert (_dbus_string_equal_substring (&str, 1, 4, &other, 1));
00333 _dbus_assert (_dbus_string_equal_substring (&str, 2, 3, &other, 2));
00334 _dbus_assert (_dbus_string_equal_substring (&str, 3, 2, &other, 3));
00335 _dbus_assert (_dbus_string_equal_substring (&str, 4, 1, &other, 4));
00336 _dbus_assert (_dbus_string_equal_substring (&str, 5, 0, &other, 5));
00337
00338 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 0));
00339 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 1));
00340 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 2));
00341 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 3));
00342 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 4));
00343 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 5));
00344
00345
00346 _dbus_string_init_const (&other, "World");
00347 _dbus_assert (_dbus_string_equal_substring (&str, 6, 5, &other, 0));
00348 _dbus_assert (_dbus_string_equal_substring (&str, 7, 4, &other, 1));
00349 _dbus_assert (_dbus_string_equal_substring (&str, 8, 3, &other, 2));
00350 _dbus_assert (_dbus_string_equal_substring (&str, 9, 2, &other, 3));
00351 _dbus_assert (_dbus_string_equal_substring (&str, 10, 1, &other, 4));
00352 _dbus_assert (_dbus_string_equal_substring (&str, 11, 0, &other, 5));
00353
00354 _dbus_assert (_dbus_string_equal_substring (&other, 0, 5, &str, 6));
00355 _dbus_assert (_dbus_string_equal_substring (&other, 1, 4, &str, 7));
00356 _dbus_assert (_dbus_string_equal_substring (&other, 2, 3, &str, 8));
00357 _dbus_assert (_dbus_string_equal_substring (&other, 3, 2, &str, 9));
00358 _dbus_assert (_dbus_string_equal_substring (&other, 4, 1, &str, 10));
00359 _dbus_assert (_dbus_string_equal_substring (&other, 5, 0, &str, 11));
00360
00361 _dbus_string_free (&str);
00362
00363
00364 if (!_dbus_string_init (&str))
00365 _dbus_assert_not_reached ("failed to init string");
00366
00367 i = 0;
00368 while (i < 10)
00369 {
00370 if (!_dbus_string_append (&str, "a"))
00371 _dbus_assert_not_reached ("failed to append string to string\n");
00372
00373 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
00374
00375 if (!_dbus_string_append_byte (&str, 'b'))
00376 _dbus_assert_not_reached ("failed to append byte to string\n");
00377
00378 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
00379
00380 ++i;
00381 }
00382
00383 _dbus_string_free (&str);
00384
00385
00386
00387 if (!_dbus_string_init (&str))
00388 _dbus_assert_not_reached ("failed to init string");
00389
00390 if (!_dbus_string_append (&str, "Hello World"))
00391 _dbus_assert_not_reached ("could not append to string");
00392
00393 i = _dbus_string_get_length (&str);
00394
00395 if (!_dbus_string_steal_data (&str, &s))
00396 _dbus_assert_not_reached ("failed to steal data");
00397
00398 _dbus_assert (_dbus_string_get_length (&str) == 0);
00399 _dbus_assert (((int)strlen (s)) == i);
00400
00401 dbus_free (s);
00402
00403
00404
00405 if (!_dbus_string_append (&str, "Hello World"))
00406 _dbus_assert_not_reached ("could not append to string");
00407
00408 i = _dbus_string_get_length (&str);
00409
00410 if (!_dbus_string_init (&other))
00411 _dbus_assert_not_reached ("could not init string");
00412
00413 if (!_dbus_string_move (&str, 0, &other, 0))
00414 _dbus_assert_not_reached ("could not move");
00415
00416 _dbus_assert (_dbus_string_get_length (&str) == 0);
00417 _dbus_assert (_dbus_string_get_length (&other) == i);
00418
00419 if (!_dbus_string_append (&str, "Hello World"))
00420 _dbus_assert_not_reached ("could not append to string");
00421
00422 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other)))
00423 _dbus_assert_not_reached ("could not move");
00424
00425 _dbus_assert (_dbus_string_get_length (&str) == 0);
00426 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00427
00428 if (!_dbus_string_append (&str, "Hello World"))
00429 _dbus_assert_not_reached ("could not append to string");
00430
00431 if (!_dbus_string_move (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00432 _dbus_assert_not_reached ("could not move");
00433
00434 _dbus_assert (_dbus_string_get_length (&str) == 0);
00435 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00436
00437 _dbus_string_free (&other);
00438
00439
00440
00441 if (!_dbus_string_append (&str, "Hello World"))
00442 _dbus_assert_not_reached ("could not append to string");
00443
00444 i = _dbus_string_get_length (&str);
00445
00446 if (!_dbus_string_init (&other))
00447 _dbus_assert_not_reached ("could not init string");
00448
00449 if (!_dbus_string_copy (&str, 0, &other, 0))
00450 _dbus_assert_not_reached ("could not copy");
00451
00452 _dbus_assert (_dbus_string_get_length (&str) == i);
00453 _dbus_assert (_dbus_string_get_length (&other) == i);
00454
00455 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other)))
00456 _dbus_assert_not_reached ("could not copy");
00457
00458 _dbus_assert (_dbus_string_get_length (&str) == i);
00459 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
00460 _dbus_assert (_dbus_string_equal_c_str (&other,
00461 "Hello WorldHello World"));
00462
00463 if (!_dbus_string_copy (&str, 0, &other, _dbus_string_get_length (&other) / 2))
00464 _dbus_assert_not_reached ("could not copy");
00465
00466 _dbus_assert (_dbus_string_get_length (&str) == i);
00467 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
00468 _dbus_assert (_dbus_string_equal_c_str (&other,
00469 "Hello WorldHello WorldHello World"));
00470
00471 _dbus_string_free (&str);
00472 _dbus_string_free (&other);
00473
00474
00475
00476 if (!_dbus_string_init (&str))
00477 _dbus_assert_not_reached ("failed to init string");
00478
00479 if (!_dbus_string_append (&str, "Hello World"))
00480 _dbus_assert_not_reached ("could not append to string");
00481
00482 i = _dbus_string_get_length (&str);
00483
00484 if (!_dbus_string_init (&other))
00485 _dbus_assert_not_reached ("could not init string");
00486
00487 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00488 &other, 0, _dbus_string_get_length (&other)))
00489 _dbus_assert_not_reached ("could not replace");
00490
00491 _dbus_assert (_dbus_string_get_length (&str) == i);
00492 _dbus_assert (_dbus_string_get_length (&other) == i);
00493 _dbus_assert (_dbus_string_equal_c_str (&other, "Hello World"));
00494
00495 if (!_dbus_string_replace_len (&str, 0, _dbus_string_get_length (&str),
00496 &other, 5, 1))
00497 _dbus_assert_not_reached ("could not replace center space");
00498
00499 _dbus_assert (_dbus_string_get_length (&str) == i);
00500 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00501 _dbus_assert (_dbus_string_equal_c_str (&other,
00502 "HelloHello WorldWorld"));
00503
00504
00505 if (!_dbus_string_replace_len (&str, 1, 1,
00506 &other,
00507 _dbus_string_get_length (&other) - 1,
00508 1))
00509 _dbus_assert_not_reached ("could not replace end character");
00510
00511 _dbus_assert (_dbus_string_get_length (&str) == i);
00512 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
00513 _dbus_assert (_dbus_string_equal_c_str (&other,
00514 "HelloHello WorldWorle"));
00515
00516 _dbus_string_free (&str);
00517 _dbus_string_free (&other);
00518
00519
00520
00521 if (!_dbus_string_init (&str))
00522 _dbus_assert_not_reached ("failed to init string");
00523
00524 ch = 0;
00525 if (!_dbus_string_append_unichar (&str, 0xfffc))
00526 _dbus_assert_not_reached ("failed to append unichar");
00527
00528 _dbus_string_get_unichar (&str, 0, &ch, &i);
00529
00530 _dbus_assert (ch == 0xfffc);
00531 _dbus_assert (i == _dbus_string_get_length (&str));
00532
00533 _dbus_string_free (&str);
00534
00535
00536
00537 if (!_dbus_string_init (&str))
00538 _dbus_assert_not_reached ("failed to init string");
00539
00540 if (!_dbus_string_append (&str, "Hello"))
00541 _dbus_assert_not_reached ("failed to append Hello");
00542
00543 _dbus_assert (_dbus_string_get_byte (&str, 0) == 'H');
00544 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'e');
00545 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'l');
00546 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'l');
00547 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'o');
00548
00549 _dbus_string_set_byte (&str, 1, 'q');
00550 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'q');
00551
00552 if (!_dbus_string_insert_bytes (&str, 0, 1, 255))
00553 _dbus_assert_not_reached ("can't insert byte");
00554
00555 if (!_dbus_string_insert_bytes (&str, 2, 4, 'Z'))
00556 _dbus_assert_not_reached ("can't insert byte");
00557
00558 if (!_dbus_string_insert_bytes (&str, _dbus_string_get_length (&str), 1, 'W'))
00559 _dbus_assert_not_reached ("can't insert byte");
00560
00561 _dbus_assert (_dbus_string_get_byte (&str, 0) == 255);
00562 _dbus_assert (_dbus_string_get_byte (&str, 1) == 'H');
00563 _dbus_assert (_dbus_string_get_byte (&str, 2) == 'Z');
00564 _dbus_assert (_dbus_string_get_byte (&str, 3) == 'Z');
00565 _dbus_assert (_dbus_string_get_byte (&str, 4) == 'Z');
00566 _dbus_assert (_dbus_string_get_byte (&str, 5) == 'Z');
00567 _dbus_assert (_dbus_string_get_byte (&str, 6) == 'q');
00568 _dbus_assert (_dbus_string_get_byte (&str, 7) == 'l');
00569 _dbus_assert (_dbus_string_get_byte (&str, 8) == 'l');
00570 _dbus_assert (_dbus_string_get_byte (&str, 9) == 'o');
00571 _dbus_assert (_dbus_string_get_byte (&str, 10) == 'W');
00572
00573 _dbus_string_free (&str);
00574
00575
00576
00577 if (!_dbus_string_init (&str))
00578 _dbus_assert_not_reached ("failed to init string");
00579
00580 if (!_dbus_string_append_int (&str, 27))
00581 _dbus_assert_not_reached ("failed to append int");
00582
00583 i = _dbus_string_get_length (&str);
00584
00585 if (!_dbus_string_parse_int (&str, 0, &v, &end))
00586 _dbus_assert_not_reached ("failed to parse int");
00587
00588 _dbus_assert (v == 27);
00589 _dbus_assert (end == i);
00590
00591 _dbus_string_free (&str);
00592
00593 if (!_dbus_string_init (&str))
00594 _dbus_assert_not_reached ("failed to init string");
00595
00596 if (!_dbus_string_append_double (&str, 50.3))
00597 _dbus_assert_not_reached ("failed to append float");
00598
00599 i = _dbus_string_get_length (&str);
00600
00601 if (!_dbus_string_parse_double (&str, 0, &d, &end))
00602 _dbus_assert_not_reached ("failed to parse float");
00603
00604 _dbus_assert (d > (50.3 - 1e-6) && d < (50.3 + 1e-6));
00605 _dbus_assert (end == i);
00606
00607 _dbus_string_free (&str);
00608
00609
00610 if (!_dbus_string_init (&str))
00611 _dbus_assert_not_reached ("failed to init string");
00612
00613 if (!_dbus_string_append (&str, "Hello"))
00614 _dbus_assert_not_reached ("couldn't append to string");
00615
00616 if (!_dbus_string_find (&str, 0, "He", &i))
00617 _dbus_assert_not_reached ("didn't find 'He'");
00618 _dbus_assert (i == 0);
00619
00620 if (!_dbus_string_find (&str, 0, "Hello", &i))
00621 _dbus_assert_not_reached ("didn't find 'Hello'");
00622 _dbus_assert (i == 0);
00623
00624 if (!_dbus_string_find (&str, 0, "ello", &i))
00625 _dbus_assert_not_reached ("didn't find 'ello'");
00626 _dbus_assert (i == 1);
00627
00628 if (!_dbus_string_find (&str, 0, "lo", &i))
00629 _dbus_assert_not_reached ("didn't find 'lo'");
00630 _dbus_assert (i == 3);
00631
00632 if (!_dbus_string_find (&str, 2, "lo", &i))
00633 _dbus_assert_not_reached ("didn't find 'lo'");
00634 _dbus_assert (i == 3);
00635
00636 if (_dbus_string_find (&str, 4, "lo", &i))
00637 _dbus_assert_not_reached ("did find 'lo'");
00638
00639 if (!_dbus_string_find (&str, 0, "l", &i))
00640 _dbus_assert_not_reached ("didn't find 'l'");
00641 _dbus_assert (i == 2);
00642
00643 if (!_dbus_string_find (&str, 0, "H", &i))
00644 _dbus_assert_not_reached ("didn't find 'H'");
00645 _dbus_assert (i == 0);
00646
00647 if (!_dbus_string_find (&str, 0, "", &i))
00648 _dbus_assert_not_reached ("didn't find ''");
00649 _dbus_assert (i == 0);
00650
00651 if (_dbus_string_find (&str, 0, "Hello!", NULL))
00652 _dbus_assert_not_reached ("Did find 'Hello!'");
00653
00654 if (_dbus_string_find (&str, 0, "Oh, Hello", NULL))
00655 _dbus_assert_not_reached ("Did find 'Oh, Hello'");
00656
00657 if (_dbus_string_find (&str, 0, "ill", NULL))
00658 _dbus_assert_not_reached ("Did find 'ill'");
00659
00660 if (_dbus_string_find (&str, 0, "q", NULL))
00661 _dbus_assert_not_reached ("Did find 'q'");
00662
00663 if (!_dbus_string_find_to (&str, 0, 2, "He", NULL))
00664 _dbus_assert_not_reached ("Didn't find 'He'");
00665
00666 if (_dbus_string_find_to (&str, 0, 2, "Hello", NULL))
00667 _dbus_assert_not_reached ("Did find 'Hello'");
00668
00669 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'H', &i))
00670 _dbus_assert_not_reached ("Did not find 'H'");
00671 _dbus_assert (i == 0);
00672
00673 if (!_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str), 'o', &i))
00674 _dbus_assert_not_reached ("Did not find 'o'");
00675 _dbus_assert (i == _dbus_string_get_length (&str) - 1);
00676
00677 if (_dbus_string_find_byte_backward (&str, _dbus_string_get_length (&str) - 1, 'o', &i))
00678 _dbus_assert_not_reached ("Did find 'o'");
00679 _dbus_assert (i == -1);
00680
00681 if (_dbus_string_find_byte_backward (&str, 1, 'e', &i))
00682 _dbus_assert_not_reached ("Did find 'e'");
00683 _dbus_assert (i == -1);
00684
00685 if (!_dbus_string_find_byte_backward (&str, 2, 'e', &i))
00686 _dbus_assert_not_reached ("Didn't find 'e'");
00687 _dbus_assert (i == 1);
00688
00689 _dbus_string_free (&str);
00690
00691
00692 _dbus_string_init_const (&str, "cafebabe, this is a bogus hex string");
00693 if (!_dbus_string_init (&other))
00694 _dbus_assert_not_reached ("could not init string");
00695
00696 if (!_dbus_string_hex_decode (&str, 0, &end, &other, 0))
00697 _dbus_assert_not_reached ("deccoded bogus hex string with no error");
00698
00699 _dbus_assert (end == 8);
00700
00701 _dbus_string_free (&other);
00702
00703 test_roundtrips (test_hex_roundtrip);
00704
00705 _dbus_string_free (&str);
00706
00707 {
00708 int found, found_len;
00709
00710 _dbus_string_init_const (&str, "012\r\n567\n90");
00711
00712 if (!_dbus_string_find_eol (&str, 0, &found, &found_len) || found != 3 || found_len != 2)
00713 _dbus_assert_not_reached ("Did not find '\\r\\n'");
00714 if (found != 3 || found_len != 2)
00715 _dbus_assert_not_reached ("invalid return values");
00716
00717 if (!_dbus_string_find_eol (&str, 5, &found, &found_len))
00718 _dbus_assert_not_reached ("Did not find '\\n'");
00719 if (found != 8 || found_len != 1)
00720 _dbus_assert_not_reached ("invalid return values");
00721
00722 if (_dbus_string_find_eol (&str, 9, &found, &found_len))
00723 _dbus_assert_not_reached ("Found not expected '\\n'");
00724 else if (found != 11 || found_len != 0)
00725 _dbus_assert_not_reached ("invalid return values '\\n'");
00726
00727 found = -1;
00728 found_len = -1;
00729 _dbus_string_init_const (&str, "");
00730 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
00731 _dbus_assert_not_reached ("found an eol in an empty string");
00732 _dbus_assert (found == 0);
00733 _dbus_assert (found_len == 0);
00734
00735 found = -1;
00736 found_len = -1;
00737 _dbus_string_init_const (&str, "foobar");
00738 if (_dbus_string_find_eol (&str, 0, &found, &found_len))
00739 _dbus_assert_not_reached ("found eol in string that lacks one");
00740 _dbus_assert (found == 6);
00741 _dbus_assert (found_len == 0);
00742
00743 found = -1;
00744 found_len = -1;
00745 _dbus_string_init_const (&str, "foobar\n");
00746 if (!_dbus_string_find_eol (&str, 0, &found, &found_len))
00747 _dbus_assert_not_reached ("did not find eol in string that has one at end");
00748 _dbus_assert (found == 6);
00749 _dbus_assert (found_len == 1);
00750 }
00751
00752 {
00753 DBusString line;
00754
00755 #define FIRST_LINE "this is a line"
00756 #define SECOND_LINE "this is a second line"
00757
00758 #define THIRD_LINE ""
00759 #define FOURTH_LINE "this is a fourth line"
00760
00761 if (!_dbus_string_init (&str))
00762 _dbus_assert_not_reached ("no memory");
00763
00764 if (!_dbus_string_append (&str, FIRST_LINE "\n" SECOND_LINE "\r\n" THIRD_LINE "\n" FOURTH_LINE))
00765 _dbus_assert_not_reached ("no memory");
00766
00767 if (!_dbus_string_init (&line))
00768 _dbus_assert_not_reached ("no memory");
00769
00770 if (!_dbus_string_pop_line (&str, &line))
00771 _dbus_assert_not_reached ("failed to pop first line");
00772
00773 _dbus_assert (_dbus_string_equal_c_str (&line, FIRST_LINE));
00774
00775 if (!_dbus_string_pop_line (&str, &line))
00776 _dbus_assert_not_reached ("failed to pop second line");
00777
00778 _dbus_assert (_dbus_string_equal_c_str (&line, SECOND_LINE));
00779
00780 if (!_dbus_string_pop_line (&str, &line))
00781 _dbus_assert_not_reached ("failed to pop third line");
00782
00783 _dbus_assert (_dbus_string_equal_c_str (&line, THIRD_LINE));
00784
00785 if (!_dbus_string_pop_line (&str, &line))
00786 _dbus_assert_not_reached ("failed to pop fourth line");
00787
00788 _dbus_assert (_dbus_string_equal_c_str (&line, FOURTH_LINE));
00789
00790 _dbus_string_free (&str);
00791 _dbus_string_free (&line);
00792 }
00793
00794 {
00795 if (!_dbus_string_init (&str))
00796 _dbus_assert_not_reached ("no memory");
00797
00798 for (i = 0; i < 10000; i++)
00799 if (!_dbus_string_append (&str, "abcdefghijklmnopqrstuvwxyz"))
00800 _dbus_assert_not_reached ("no memory");
00801
00802 if (!_dbus_string_set_length (&str, 10))
00803 _dbus_assert_not_reached ("failed to set length");
00804
00805
00806 if (!_dbus_string_compact (&str, 2048))
00807 _dbus_assert_not_reached ("failed to compact after set_length");
00808
00809
00810 if (((DBusRealString *)&str)->allocated > 30)
00811 _dbus_assert_not_reached ("compacting string didn't do anything");
00812
00813 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
00814 _dbus_assert_not_reached ("unexpected content after compact");
00815
00816
00817 if (!_dbus_string_compact (&str, 2048))
00818 _dbus_assert_not_reached ("failed to compact 2nd time");
00819
00820 if (!_dbus_string_equal_c_str (&str, "abcdefghij"))
00821 _dbus_assert_not_reached ("unexpected content after 2nd compact");
00822
00823
00824 if (!_dbus_string_append (&str, "123456"))
00825 _dbus_assert_not_reached ("failed to append after compact");
00826
00827 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
00828 _dbus_assert_not_reached ("unexpected content after append");
00829
00830
00831 if (!_dbus_string_compact (&str, 20000))
00832 _dbus_assert_not_reached ("failed to compact after grow");
00833
00834
00835 if (!_dbus_string_compact (&str, 0))
00836 _dbus_assert_not_reached ("failed to compact after grow");
00837
00838 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456"))
00839 _dbus_assert_not_reached ("unexpected content");
00840
00841 if (!_dbus_string_append (&str, "!@#$%"))
00842 _dbus_assert_not_reached ("failed to append after compact");
00843
00844 if (!_dbus_string_equal_c_str (&str, "abcdefghij123456!@#$%"))
00845 _dbus_assert_not_reached ("unexpected content");
00846
00847 _dbus_string_free (&str);
00848 }
00849
00850 {
00851 const char two_strings[] = "one\ttwo";
00852
00853 if (!_dbus_string_init (&str))
00854 _dbus_assert_not_reached ("no memory");
00855
00856 if (!_dbus_string_init (&other))
00857 _dbus_assert_not_reached ("no memory");
00858
00859 if (!_dbus_string_append (&str, two_strings))
00860 _dbus_assert_not_reached ("no memory");
00861
00862 if (!_dbus_string_split_on_byte (&str, '\t', &other))
00863 _dbus_assert_not_reached ("no memory or delimiter not found");
00864
00865 if (strcmp (_dbus_string_get_data (&str), "one") != 0)
00866 _dbus_assert_not_reached ("left side after split on tab is wrong");
00867
00868 if (strcmp (_dbus_string_get_data (&other), "two") != 0)
00869 _dbus_assert_not_reached ("right side after split on tab is wrong");
00870
00871 _dbus_string_free (&str);
00872 _dbus_string_free (&other);
00873 }
00874
00875 return TRUE;
00876 }
00877
00878 #endif