rewrite: update default dumb and smart prefixes
[elinks/elinks-j605.git] / src / dom / string.h
blobf6032538d5a28c20817f622fca397edbb5471c91
1 #ifndef EL_DOM_STRING_H
2 #define EL_DOM_STRING_H
4 #include "util/conv.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 */
13 struct dom_string {
14 unsigned int length;
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) }
24 static inline void
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;
31 static inline int
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;
42 static inline int
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);
57 if (!newstring)
58 return NULL;
60 string->string = newstring;
61 memcpy(string->string + string->length, str, len);
62 string->length += len;
63 string->string[string->length] = 0;
65 return string;
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) == '\'')
77 #endif