1 #ifndef EL_DOM_STRING_H
2 #define EL_DOM_STRING_H
5 #include "util/memory.h"
7 /* For now DOM has it's own little string library. Mostly because there are
8 * some memory overhead associated with util/string's block-based allocation
9 * scheme which is optimized for building strings and quickly dispose of it.
10 * Also, at some point we need to switch to use mainly UTF-8 strings for DOM
11 * and it needs to be possible to adapt the string library to that. --jonas */
15 unsigned char *string
;
18 #define INIT_DOM_STRING(strvalue, strlength) \
19 { (strlength), (strvalue) }
21 #define STATIC_DOM_STRING(strvalue) \
22 { sizeof(strvalue) - 1, (strvalue) }
25 set_dom_string(struct dom_string
*string
, unsigned char *value
, size_t length
)
27 string
->string
= value
;
28 string
->length
= length
== -1 ? strlen(value
) : length
;
32 dom_string_casecmp(const struct dom_string
*string1
, const struct dom_string
*string2
)
34 size_t length
= int_min(string1
->length
, string2
->length
);
35 size_t string_diff
= c_strncasecmp(string1
->string
, string2
->string
, length
);
37 /* If the lengths or strings don't match c_strncasecmp() does the
38 * job else return which ever is bigger. */
39 return string_diff
? string_diff
: string1
->length
- string2
->length
;
43 dom_string_ncasecmp(struct dom_string
*string1
, struct dom_string
*string2
, size_t length
)
45 return c_strncasecmp(string1
->string
, string2
->string
, length
);
48 #define copy_dom_string(string1, string2) \
49 set_dom_string(string1, (string2)->string, (string2)->length)
51 static inline struct dom_string
*
52 add_to_dom_string(struct dom_string
*string
, unsigned char *str
, size_t len
)
54 unsigned char *newstring
;
56 newstring
= mem_realloc(string
->string
, string
->length
+ len
+ 1);
60 string
->string
= newstring
;
61 memcpy(string
->string
+ string
->length
, str
, len
);
62 string
->length
+= len
;
63 string
->string
[string
->length
] = 0;
68 #define init_dom_string(string, str, len) add_to_dom_string(string, str, len)
70 #define is_dom_string_set(str) ((str)->string && (str)->length)
72 #define done_dom_string(str) \
73 do { mem_free_set(&(str)->string, NULL); (str)->length = 0; } while (0)
75 #define isquote(c) ((c) == '"' || (c) == '\'')