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" 49 const unsigned char *ap;
50 const unsigned char *bp;
51 const unsigned char *a_end;
52 unsigned long c_str_len;
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;
104 if (real->str[i] == byte)
118 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 119 #include "dbus-test.h" 123 test_hex_roundtrip (
const char *data,
152 _dbus_assert (_dbus_string_get_length (&encoded) == end);
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);
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];
209 (* func) ((
const char *) buf, i);
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 };
254 _dbus_assert (_dbus_string_get_length (&str) == (j - 1));
317 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 1);
322 _dbus_assert (_dbus_string_get_length (&str) == i * 2 + 2);
337 i = _dbus_string_get_length (&str);
352 i = _dbus_string_get_length (&str);
370 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
379 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
388 i = _dbus_string_get_length (&str);
403 _dbus_assert (_dbus_string_get_length (&other) == i * 2);
405 "Hello WorldHello World"));
411 _dbus_assert (_dbus_string_get_length (&other) == i * 3);
413 "Hello WorldHello WorldHello World"));
426 i = _dbus_string_get_length (&str);
432 &other, 0, _dbus_string_get_length (&other)))
444 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
446 "HelloHello WorldWorld"));
451 _dbus_string_get_length (&other) - 1,
456 _dbus_assert (_dbus_string_get_length (&other) == i * 2 - 1);
458 "HelloHello WorldWorle"));
474 i = _dbus_string_get_length (&str);
482 a = _dbus_string_get_length (&other);
489 _dbus_assert (_dbus_string_get_length (&other) == a + 6);
491 "Foo Hello String"));
495 _dbus_string_get_length (&other),
500 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
502 "Foo Hello String World"));
506 _dbus_string_get_length (&other) - 5,
511 _dbus_assert (_dbus_string_get_length (&other) == a + 6 + 6);
513 "Foo Hello String Hello"));
520 _dbus_assert (_dbus_string_get_length (&other) == a + 5);
529 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
535 _dbus_string_get_length (&other) - 5,
540 _dbus_assert (_dbus_string_get_length (&other) == a + 3);
561 _dbus_string_set_byte (&str, 1,
'q');
595 i = _dbus_string_get_length (&str);
699 test_roundtrips (test_hex_roundtrip);
704 int found, found_len;
710 if (found != 3 || found_len != 2)
715 if (found != 8 || found_len != 1)
720 else if (found != 11 || found_len != 0)
751 #define FIRST_LINE "this is a line" 752 #define SECOND_LINE "this is a second line" 754 #define THIRD_LINE "" 755 #define FOURTH_LINE "this is a fourth line" 760 if (!
_dbus_string_append (&str, FIRST_LINE
"\n" SECOND_LINE
"\r\n" THIRD_LINE
"\n" FOURTH_LINE))
794 for (i = 0; i < 10000; i++)
847 const char two_strings[] =
"one\ttwo";
861 if (strcmp (_dbus_string_get_data (&str),
"one") != 0)
864 if (strcmp (_dbus_string_get_data (&other),
"two") != 0)
872 const char upper_string[] =
"TOUPPERSTRING";
873 const char lower_string[] =
"toupperstring";
874 const char lower2_string[] =
"toupperSTRING";
904 const char lower_string[] =
"toupperstring";
905 const char upper_string[] =
"TOUPPERSTRING";
906 const char upper2_string[] =
"TOUPPERstring";
dbus_bool_t _dbus_string_insert_bytes(DBusString *str, int i, int n_bytes, unsigned char byte)
Inserts a number of bytes of a given value at the given position.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
dbus_bool_t _dbus_string_find_eol(const DBusString *str, int start, int *found, int *found_len)
Finds end of line ("\r\n" or "\n") in the string, returning TRUE and filling in the byte index where ...
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void _dbus_string_tolower_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to lower case.
#define DBUS_CONST_STRING_PREAMBLE(str)
Checks assertions about a string that may be const or locked.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
dbus_bool_t _dbus_string_ends_with_c_str(const DBusString *a, const char *c_str)
Returns whether a string ends with the given suffix.
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
#define DBUS_GENERIC_STRING_PREAMBLE(real)
Checks a bunch of assertions about a string object.
dbus_bool_t _dbus_string_replace_len(const DBusString *source, int start, int len, DBusString *dest, int replace_at, int replace_len)
Replaces a segment of dest string with a segment of source string.
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
int len
Length without nul.
dbus_bool_t _dbus_string_equal_substring(const DBusString *a, int a_start, int a_len, const DBusString *b, int b_start)
Tests two sub-parts of two DBusString for equality.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
dbus_bool_t _dbus_string_append_len(DBusString *str, const char *buffer, int len)
Appends block of bytes with the given length to a DBusString.
dbus_bool_t _dbus_string_find_to(const DBusString *str, int start, int end, const char *substr, int *found)
Finds the given substring in the string, up to a certain position, returning TRUE and filling in the ...
unsigned char * str
String data, plus nul termination.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
void _dbus_string_toupper_ascii(const DBusString *str, int start, int len)
Converts the given range of the string to upper case.
dbus_bool_t _dbus_string_split_on_byte(DBusString *source, unsigned char byte, DBusString *tail)
Looks for the first occurance of a byte, deletes that byte, and moves everything after the byte to th...