2005-12-28 08:05:14 -05:00
|
|
|
#ifndef EL_DOM_STRING_H
|
|
|
|
#define EL_DOM_STRING_H
|
2005-12-10 12:37:47 -05:00
|
|
|
|
2008-11-01 14:40:00 -04:00
|
|
|
#include "util/conv.h"
|
2005-12-28 15:20:55 -05:00
|
|
|
#include "util/memory.h"
|
2005-12-28 09:19:10 -05:00
|
|
|
|
2020-10-05 14:14:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2006-01-28 16:55:15 -05:00
|
|
|
/* For now DOM has it's own little string library. Mostly because there are
|
|
|
|
* some memory overhead associated with util/string's block-based allocation
|
|
|
|
* scheme which is optimized for building strings and quickly dispose of it.
|
|
|
|
* Also, at some point we need to switch to use mainly UTF-8 strings for DOM
|
|
|
|
* and it needs to be possible to adapt the string library to that. --jonas */
|
|
|
|
|
2005-12-10 12:37:47 -05:00
|
|
|
struct dom_string {
|
2006-02-09 19:25:48 -05:00
|
|
|
unsigned int length;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *string;
|
2005-12-10 12:37:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#define INIT_DOM_STRING(strvalue, strlength) \
|
2022-01-20 12:42:03 -05:00
|
|
|
{ (strlength), (char *)(strvalue) }
|
2006-01-28 16:55:15 -05:00
|
|
|
|
|
|
|
#define STATIC_DOM_STRING(strvalue) \
|
2022-01-20 12:42:03 -05:00
|
|
|
{ sizeof(strvalue) - 1, (char *)(strvalue) }
|
2005-12-10 12:37:47 -05:00
|
|
|
|
|
|
|
static inline void
|
2021-01-02 10:20:27 -05:00
|
|
|
set_dom_string(struct dom_string *string, char *value, size_t length)
|
2005-12-10 12:37:47 -05:00
|
|
|
{
|
|
|
|
string->string = value;
|
2005-12-12 11:42:26 -05:00
|
|
|
string->length = length == -1 ? strlen(value) : length;
|
2005-12-10 12:37:47 -05:00
|
|
|
}
|
|
|
|
|
2005-12-10 15:49:33 -05:00
|
|
|
static inline int
|
2005-12-28 15:20:55 -05:00
|
|
|
dom_string_casecmp(const struct dom_string *string1, const struct dom_string *string2)
|
2005-12-10 15:49:33 -05:00
|
|
|
{
|
|
|
|
size_t length = int_min(string1->length, string2->length);
|
2008-10-18 21:25:00 -04:00
|
|
|
size_t string_diff = c_strncasecmp(string1->string, string2->string, length);
|
2005-12-10 15:49:33 -05:00
|
|
|
|
2008-10-18 21:25:00 -04:00
|
|
|
/* If the lengths or strings don't match c_strncasecmp() does the
|
2005-12-10 15:49:33 -05:00
|
|
|
* job else return which ever is bigger. */
|
|
|
|
return string_diff ? string_diff : string1->length - string2->length;
|
|
|
|
}
|
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
static inline int
|
|
|
|
dom_string_ncasecmp(struct dom_string *string1, struct dom_string *string2, size_t length)
|
|
|
|
{
|
2008-10-18 21:25:00 -04:00
|
|
|
return c_strncasecmp(string1->string, string2->string, length);
|
2005-12-24 22:38:30 -05:00
|
|
|
}
|
|
|
|
|
2005-12-28 10:23:36 -05:00
|
|
|
#define copy_dom_string(string1, string2) \
|
|
|
|
set_dom_string(string1, (string2)->string, (string2)->length)
|
|
|
|
|
2005-12-28 09:19:10 -05:00
|
|
|
static inline struct dom_string *
|
2022-01-31 09:58:19 -05:00
|
|
|
add_to_dom_string(struct dom_string *string, const char *str, size_t len)
|
2005-12-28 09:19:10 -05:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *newstring;
|
2006-01-07 21:40:54 -05:00
|
|
|
|
2022-01-16 13:38:30 -05:00
|
|
|
newstring = (char *)mem_realloc(string->string, string->length + len + 1);
|
2006-01-07 21:40:54 -05:00
|
|
|
if (!newstring)
|
2005-12-28 13:49:22 -05:00
|
|
|
return NULL;
|
|
|
|
|
2006-01-07 21:40:54 -05:00
|
|
|
string->string = newstring;
|
|
|
|
memcpy(string->string + string->length, str, len);
|
|
|
|
string->length += len;
|
|
|
|
string->string[string->length] = 0;
|
|
|
|
|
2005-12-28 13:49:22 -05:00
|
|
|
return string;
|
2005-12-28 09:19:10 -05:00
|
|
|
}
|
|
|
|
|
2006-01-07 21:40:54 -05:00
|
|
|
#define init_dom_string(string, str, len) add_to_dom_string(string, str, len)
|
|
|
|
|
2005-12-10 12:42:54 -05:00
|
|
|
#define is_dom_string_set(str) ((str)->string && (str)->length)
|
|
|
|
|
2006-01-07 21:40:54 -05:00
|
|
|
#define done_dom_string(str) \
|
|
|
|
do { mem_free_set(&(str)->string, NULL); (str)->length = 0; } while (0)
|
2005-12-10 12:37:47 -05:00
|
|
|
|
2005-12-28 15:20:55 -05:00
|
|
|
#define isquote(c) ((c) == '"' || (c) == '\'')
|
|
|
|
|
2020-10-05 14:14:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-12-10 12:37:47 -05:00
|
|
|
#endif
|