From f1f3155ef6f28fb8595920e5423336b39bba4ed0 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 14 Feb 2009 22:55:32 +0000 Subject: Port libcss to libwapcaplet. It passes the tests, perhaps we need more of them. Lifetimes of lwc_string objects really need attention before we can consider this finished. svn path=/trunk/libcss/; revision=6517 --- Makefile | 4 +- include/libcss/computed.h | 52 +++---- include/libcss/libcss.h | 2 + include/libcss/select.h | 36 ++--- include/libcss/stylesheet.h | 4 +- include/libcss/types.h | 8 -- src/lex/lex.h | 12 +- src/parse/language.c | 23 +++- src/parse/parse.c | 241 +++++++++++++++----------------- src/parse/parse.h | 8 +- src/parse/properties.c | 327 ++++++++++++++++++++------------------------ src/select/hash.c | 6 +- src/select/hash.h | 4 +- src/select/properties.c | 138 ++++++++----------- src/select/propset.h | 22 ++- src/select/select.c | 228 ++++++++++++++++-------------- src/stylesheet.c | 37 ++--- src/stylesheet.h | 23 ++-- src/utils/utils.c | 10 +- src/utils/utils.h | 19 ++- test/css21.c | 29 ++-- test/dump.h | 106 +++++++------- test/dump_computed.h | 6 +- test/number.c | 19 ++- test/parse-auto.c | 49 ++++--- test/parse.c | 14 +- test/parse2-auto.c | 10 +- test/select-auto.c | 287 +++++++++++++++++++------------------- 28 files changed, 855 insertions(+), 869 deletions(-) diff --git a/Makefile b/Makefile index da66dba..ce2d84b 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,11 @@ WARNFLAGS := -Wall -Wextra -Wundef -Wpointer-arith -Wcast-align \ -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes \ -Wmissing-declarations -Wnested-externs -Werror -pedantic CFLAGS += -std=c99 -D_BSD_SOURCE -I$(TOP)/include/ $(WARNFLAGS) \ - `$(PKGCONFIG) --cflags libparserutils` + `$(PKGCONFIG) --cflags libparserutils libwapcaplet` RELEASECFLAGS = $(CFLAGS) -DNDEBUG -O2 DEBUGCFLAGS = $(CFLAGS) -O0 -g ARFLAGS := -cru -LDFLAGS += `$(PKGCONFIG) --libs libparserutils` -L$(TOP)/ +LDFLAGS += `$(PKGCONFIG) --libs libparserutils libwapcaplet` -L$(TOP)/ CPFLAGS := RMFLAGS := -f diff --git a/include/libcss/computed.h b/include/libcss/computed.h index 37e0b41..e8ab3b5 100644 --- a/include/libcss/computed.h +++ b/include/libcss/computed.h @@ -8,6 +8,8 @@ #ifndef libcss_computed_h_ #define libcss_computed_h_ +#include + #include #include #include @@ -29,23 +31,23 @@ enum css_computed_content_type { typedef struct css_computed_content_item { uint8_t type; union { - css_string string; - css_string uri; - css_string attr; + lwc_string *string; + lwc_string *uri; + lwc_string *attr; struct { - css_string name; + lwc_string *name; uint8_t style; } counter; struct { - css_string name; - css_string sep; + lwc_string *name; + lwc_string *sep; uint8_t style; } counters; } data; } css_computed_content_item; typedef struct css_computed_counter { - css_string name; + lwc_string *name; css_fixed value; } css_computed_counter; @@ -137,9 +139,9 @@ typedef struct css_computed_uncommon { css_computed_counter *counter_increment; css_computed_counter *counter_reset; - css_string *quotes; + lwc_string **quotes; - css_string *cursor; + lwc_string **cursor; css_computed_content_item *content; } css_computed_uncommon; @@ -181,7 +183,7 @@ struct css_computed_style { * Dimensions are encoded as a fixed point value + 4 bits of unit data * * background_color 2 4 - * background_image 1 sizeof(css_string) + * background_image 1 sizeof(lwc_string) * background_position 1 + 2(4) 2(4) * border_top_color 2 4 * border_right_color 2 4 @@ -199,7 +201,7 @@ struct css_computed_style { * font_size 4 + 4 4 * height 2 + 4 4 * line_height 2 + 4 4 - * list_style_image 1 sizeof(css_string) + * list_style_image 1 sizeof(lwc_string) * margin_top 2 + 4 4 * margin_right 2 + 4 4 * margin_bottom 2 + 4 4 @@ -217,7 +219,7 @@ struct css_computed_style { * width 2 + 4 4 * z_index 2 4 * --- --- - * 181 bits 140 + 2sizeof(css_string) bytes + * 181 bits 140 + 2sizeof(lwc_string) bytes * * Encode font family as an array of string objects, terminated with a * blank entry. @@ -227,13 +229,13 @@ struct css_computed_style { * 3 bits sizeof(ptr) * * ___ ___ - * 267 bits 140 + 2sizeof(css_string) + + * 267 bits 140 + 2sizeof(lwc_string) + * sizeof(ptr) bytes * - * 34 bytes 140 + 2sizeof(css_string) + + * 34 bytes 140 + 2sizeof(lwc_string) + * sizeof(ptr) bytes * =================== - * 174 + 2sizeof(css_string) + sizeof(ptr) bytes + * 174 + 2sizeof(lwc_string) + sizeof(ptr) bytes * * Bit allocations: * @@ -278,7 +280,7 @@ struct css_computed_style { uint8_t unused[2]; css_color background_color; - css_string background_image; + lwc_string *background_image; css_fixed background_position[2]; css_color border_color[4]; @@ -297,7 +299,7 @@ struct css_computed_style { css_fixed line_height; - css_string list_style_image; + lwc_string *list_style_image; css_fixed margin[4]; @@ -317,7 +319,7 @@ struct css_computed_style { css_fixed z_index; - css_string *font_family; + lwc_string **font_family; css_computed_uncommon *uncommon;/**< Uncommon properties */ void *aural; /**< Aural properties */ @@ -549,7 +551,7 @@ static inline uint8_t css_computed_counter_reset( #define CURSOR_MASK 0xf8 static inline uint8_t css_computed_cursor( const css_computed_style *style, - const css_string **urls) + lwc_string ***urls) { if (style->uncommon != NULL) { uint8_t bits = style->uncommon->bits[CURSOR_INDEX]; @@ -573,7 +575,7 @@ static inline uint8_t css_computed_cursor( #define QUOTES_MASK 0x6 static inline uint8_t css_computed_quotes( const css_computed_style *style, - const css_string **quotes) + lwc_string ***quotes) { if (style->uncommon != NULL) { uint8_t bits = style->uncommon->bits[QUOTES_INDEX]; @@ -834,14 +836,14 @@ static inline uint8_t css_computed_border_left_width( #define BACKGROUND_IMAGE_MASK 0x1 static inline uint8_t css_computed_background_image( const css_computed_style *style, - const css_string **url) + lwc_string **url) { uint8_t bits = style->bits[BACKGROUND_IMAGE_INDEX]; bits &= BACKGROUND_IMAGE_MASK; bits >>= BACKGROUND_IMAGE_SHIFT; /* 1bit: type */ - *url = &style->background_image; + *url = style->background_image; return bits; } @@ -874,14 +876,14 @@ static inline uint8_t css_computed_color( #define LIST_STYLE_IMAGE_MASK 0x1 static inline uint8_t css_computed_list_style_image( const css_computed_style *style, - const css_string **url) + lwc_string **url) { uint8_t bits = style->bits[LIST_STYLE_IMAGE_INDEX]; bits &= LIST_STYLE_IMAGE_MASK; bits >>= LIST_STYLE_IMAGE_SHIFT; /* 1bit: type */ - *url = &style->list_style_image; + *url = style->list_style_image; return bits; } @@ -1805,7 +1807,7 @@ static inline uint8_t css_computed_text_decoration( #define FONT_FAMILY_MASK 0x7 static inline uint8_t css_computed_font_family( const css_computed_style *style, - const css_string **names) + lwc_string ***names) { uint8_t bits = style->bits[FONT_FAMILY_INDEX]; bits &= FONT_FAMILY_MASK; diff --git a/include/libcss/libcss.h b/include/libcss/libcss.h index 84a8be7..de82a22 100644 --- a/include/libcss/libcss.h +++ b/include/libcss/libcss.h @@ -8,6 +8,8 @@ #ifndef libcss_h_ #define libcss_h_ +#include + #include #include #include diff --git a/include/libcss/select.h b/include/libcss/select.h index bb3f1ff..6d774ec 100644 --- a/include/libcss/select.h +++ b/include/libcss/select.h @@ -8,6 +8,8 @@ #ifndef libcss_select_h_ #define libcss_select_h_ +#include + #include #include #include @@ -21,42 +23,33 @@ enum css_pseudo_element { }; typedef struct css_select_handler { - css_error (*node_name)(void *pw, void *node, const uint8_t **name, - size_t *len); + css_error (*node_name)(void *pw, void *node, + lwc_context *dict, lwc_string **name); css_error (*named_ancestor_node)(void *pw, void *node, - const uint8_t *name, size_t len, - void **ancestor); + lwc_string *name, void **ancestor); css_error (*named_parent_node)(void *pw, void *node, - const uint8_t *name, size_t len, - void **parent); + lwc_string *name, void **parent); css_error (*named_sibling_node)(void *pw, void *node, - const uint8_t *name, size_t len, - void **sibling); + lwc_string *name, void **sibling); css_error (*parent_node)(void *pw, void *node, void **parent); css_error (*sibling_node)(void *pw, void *node, void **sibling); css_error (*node_has_class)(void *pw, void *node, - const uint8_t *name, size_t len, - bool *match); + lwc_string *name, bool *match); css_error (*node_has_id)(void *pw, void *node, - const uint8_t *name, size_t len, - bool *match); + lwc_string *name, bool *match); css_error (*node_has_attribute)(void *pw, void *node, - const uint8_t *name, size_t len, - bool *match); + lwc_string *name, bool *match); css_error (*node_has_attribute_equal)(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, lwc_string *value, bool *match); css_error (*node_has_attribute_dashmatch)(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, lwc_string *value, bool *match); css_error (*node_has_attribute_includes)(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, lwc_string *value, bool *match); css_error (*node_is_first_child)(void *pw, void *node, bool *match); @@ -66,8 +59,7 @@ typedef struct css_select_handler { css_error (*node_is_active)(void *pw, void *node, bool *match); css_error (*node_is_focus)(void *pw, void *node, bool *match); css_error (*node_is_lang)(void *pw, void *node, - const uint8_t *lang, size_t len, - bool *match); + lwc_string *lang, bool *match); } css_select_handler; css_error css_select_ctx_create(css_allocator_fn alloc, void *pw, diff --git a/include/libcss/stylesheet.h b/include/libcss/stylesheet.h index eb14ad3..dd4bc81 100644 --- a/include/libcss/stylesheet.h +++ b/include/libcss/stylesheet.h @@ -13,7 +13,7 @@ css_error css_stylesheet_create(css_language_level level, const char *charset, const char *url, const char *title, - css_origin origin, uint64_t media, + css_origin origin, uint64_t media, lwc_context *dict, css_allocator_fn alloc, void *alloc_pw, css_stylesheet **stylesheet); css_error css_stylesheet_destroy(css_stylesheet *sheet); @@ -23,7 +23,7 @@ css_error css_stylesheet_append_data(css_stylesheet *sheet, css_error css_stylesheet_data_done(css_stylesheet *sheet); css_error css_stylesheet_next_pending_import(css_stylesheet *parent, - css_string *url, uint64_t *media); + lwc_string **url, uint64_t *media); css_error css_stylesheet_register_import(css_stylesheet *parent, css_stylesheet *child); diff --git a/include/libcss/types.h b/include/libcss/types.h index 95c28b4..a08ffb4 100644 --- a/include/libcss/types.h +++ b/include/libcss/types.h @@ -68,14 +68,6 @@ typedef enum css_origin { CSS_ORIGIN_AUTHOR = 2 /**< Author stylesheet */ } css_origin; -/** - * String type - */ -typedef struct css_string { - size_t len; - uint8_t *data; -} css_string; - /** CSS colour -- RRGGBBAA */ typedef uint32_t css_color; diff --git a/src/lex/lex.h b/src/lex/lex.h index b2915fc..6d0249e 100644 --- a/src/lex/lex.h +++ b/src/lex/lex.h @@ -8,12 +8,13 @@ #ifndef css_lex_lex_h_ #define css_lex_lex_h_ +#include + #include #include #include #include -#include typedef struct css_lexer css_lexer; @@ -59,10 +60,13 @@ typedef enum css_token_type { typedef struct css_token { css_token_type type; - css_string data; + struct { + uint8_t *data; + size_t len; + } data; - const parserutils_hash_entry *idata; - const parserutils_hash_entry *ilower; + lwc_string *idata; + lwc_string *ilower; uint32_t col; uint32_t line; diff --git a/src/parse/language.c b/src/parse/language.c index 11fe972..cff66df 100644 --- a/src/parse/language.c +++ b/src/parse/language.c @@ -41,7 +41,7 @@ struct css_language { /** \todo These should be statically allocated */ /** Interned strings */ - const parserutils_hash_entry *strings[LAST_KNOWN]; + lwc_string *strings[LAST_KNOWN]; css_allocator_fn alloc; /**< Memory (de)allocation function */ void *pw; /**< Client's private data */ @@ -127,6 +127,7 @@ css_error css_language_create(css_stylesheet *sheet, css_parser *parser, css_language *c; css_parser_optparams params; parserutils_error perror; + lwc_error lerror; css_error error; if (sheet == NULL || parser == NULL || alloc == NULL || @@ -147,10 +148,11 @@ css_error css_language_create(css_stylesheet *sheet, css_parser *parser, /* Intern all known strings */ for (int i = 0; i < LAST_KNOWN; i++) { - c->strings[i] = css_parser_dict_add(parser, - (const uint8_t *) stringmap[i].data, - stringmap[i].len); - if (c->strings[i] == NULL) { + lerror = lwc_context_intern(sheet->dictionary, + stringmap[i].data, + stringmap[i].len, + &(c->strings[i])); + if (lerror != lwc_error_ok) { parserutils_stack_destroy(c->context); alloc(c, 0, pw); return CSS_NOMEM; @@ -184,11 +186,18 @@ css_error css_language_create(css_stylesheet *sheet, css_parser *parser, */ css_error css_language_destroy(css_language *language) { + int i; + if (language == NULL) return CSS_BADPARM; parserutils_stack_destroy(language->context); - + + for (i = 0; i < LAST_KNOWN; ++i) { + lwc_context_string_unref(language->sheet->dictionary, + language->strings[i]); + } + language->alloc(language, 0, language->pw); return CSS_OK; @@ -1107,7 +1116,7 @@ void consumeWhitespace(const parserutils_vector *vector, int *ctx) bool tokenIsChar(const css_token *token, uint8_t c) { return token != NULL && token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && token->ilower->data[0] == c; + lwc_string_length(token->ilower) == 1 && lwc_string_data(token->ilower)[0] == c; } diff --git a/src/parse/parse.c b/src/parse/parse.c index c1b8143..50bd362 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -9,8 +9,9 @@ #include #include +#include + #include -#include #include #include @@ -91,7 +92,7 @@ struct css_parser #define STACK_CHUNK 32 parserutils_stack *states; /**< Stack of states */ - parserutils_hash *dictionary; /**< Dictionary for interned strings */ + lwc_context *dictionary; /**< Dictionary for interned strings */ parserutils_vector *tokens; /**< Vector of pending tokens */ @@ -187,7 +188,7 @@ static css_error (*parseFuncs[])(css_parser *parser) = { * CSS_NOMEM on memory exhaustion */ css_error css_parser_create(const char *charset, css_charset_source cs_source, - parserutils_hash *dictionary, css_allocator_fn alloc, void *pw, + lwc_context *dictionary, css_allocator_fn alloc, void *pw, css_parser **parser) { css_parser *p; @@ -412,31 +413,6 @@ const char *css_parser_read_charset(css_parser *parser, return parserutils_inputstream_read_charset(parser->stream, source); } -/** - * Add an entry to the parser dictionary - * - * \param parser The parser instance - * \param data Pointer to data - * \param len Length, in bytes, of data - * \return Pointer to data in dictionary, or NULL on memory exhaustion - */ -const parserutils_hash_entry *css_parser_dict_add(css_parser *parser, - const uint8_t *data, size_t len) -{ - const parserutils_hash_entry *interned; - parserutils_error perror; - - if (parser == NULL || data == NULL || len == 0) - return NULL; - - perror = parserutils_hash_insert(parser->dictionary, data, len, - &interned); - if (perror != PARSERUTILS_OK) - return NULL; - - return interned; -} - /****************************************************************************** * Helper functions * ******************************************************************************/ @@ -558,6 +534,7 @@ css_error expect(css_parser *parser, css_token_type type) css_error getToken(css_parser *parser, const css_token **token) { parserutils_error perror; + lwc_error lerror; css_error error; /* Use pushback, if it exists */ @@ -609,27 +586,25 @@ css_error getToken(css_parser *parser, const css_token **token) if (lower == true) { /* Insert lowercase version */ - perror = parserutils_hash_insert( - parser->dictionary, - temp, t->data.len, - &t->ilower); - if (perror != PARSERUTILS_OK) { - return css_error_from_parserutils_error( - perror); - } + lerror = lwc_context_intern(parser->dictionary, + (char *)temp, + t->data.len, + &t->ilower); + if (lerror != lwc_error_ok) + return css_error_from_lwc_error(lerror); } } /* Insert token text into the dictionary */ - perror = parserutils_hash_insert(parser->dictionary, - t->data.data, t->data.len, - &t->idata); - + lerror = lwc_context_intern(parser->dictionary, + (char *)t->data.data, + t->data.len, &t->idata); + if (t->ilower == NULL) t->ilower = t->idata; - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (lerror != lwc_error_ok) + return css_error_from_lwc_error(lerror); } else { t->idata = t->ilower = NULL; } @@ -855,8 +830,8 @@ css_error parseRuleset(css_parser *parser) /* The grammar's ambiguous here -- selectors may start with a * brace. We're going to assume that that won't happen, * however. */ - if (token->type == CSS_TOKEN_CHAR && token->ilower->len == 1 && - token->ilower->data[0] == '{') { + if (token->type == CSS_TOKEN_CHAR && lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == '{') { #if !defined(NDEBUG) && defined(DEBUG_EVENTS) printf("Begin ruleset\n"); #endif @@ -906,8 +881,8 @@ css_error parseRuleset(css_parser *parser) if (error != CSS_OK) return error; - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - token->ilower->data[0] != '{') { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + lwc_string_data(token->ilower)[0] != '{') { /* This should never happen, as FOLLOW(selector) * contains only '{' */ assert(0 && "Expected {"); @@ -954,9 +929,9 @@ css_error parseRulesetEnd(css_parser *parser) * attempt to parse a declaration. This will catch any invalid * input at this point and read to the start of the next * declaration. FIRST(decl-list) = (';', '}') */ - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - (token->ilower->data[0] != '}' && - token->ilower->data[0] != ';')) { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != '}' && + lwc_string_data(token->ilower)[0] != ';')) { parser_state to = { sDeclaration, Initial }; parser_state subsequent = { sRulesetEnd, DeclList }; @@ -980,8 +955,8 @@ css_error parseRulesetEnd(css_parser *parser) if (token->type == CSS_TOKEN_EOF) break; - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - token->ilower->data[0] != '}') { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + lwc_string_data(token->ilower)[0] != '}') { /* This should never happen, as FOLLOW(decl-list) * contains only '}' */ assert(0 && "Expected }"); @@ -1055,9 +1030,9 @@ css_error parseAtRule(css_parser *parser) /* Grammar ambiguity: any0 can be followed by '{',';',')',']'. * at-rule can only be followed by '{' and ';'. */ - if (token->type == CSS_TOKEN_CHAR && token->ilower->len == 1) { - if (token->ilower->data[0] == ')' || - token->ilower->data[0] == ']') { + if (token->type == CSS_TOKEN_CHAR && lwc_string_length(token->ilower) == 1) { + if (lwc_string_data(token->ilower)[0] == ')' || + lwc_string_data(token->ilower)[0] == ']') { parser_state to = { sAny0, Initial }; parser_state subsequent = { sAtRule, AfterAny }; @@ -1108,12 +1083,12 @@ css_error parseAtRuleEnd(css_parser *parser) if (error != CSS_OK) return error; - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1) { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1) { /* Should never happen FOLLOW(at-rule) == '{', ';'*/ assert(0 && "Expected { or ;"); } - if (token->ilower->data[0] == '{') { + if (lwc_string_data(token->ilower)[0] == '{') { parser_state to = { sBlock, Initial }; parser_state subsequent = { sAtRuleEnd, AfterBlock }; @@ -1122,7 +1097,7 @@ css_error parseAtRuleEnd(css_parser *parser) return error; return transition(parser, to, subsequent); - } else if (token->ilower->data[0] != ';') { + } else if (lwc_string_data(token->ilower)[0] != ';') { /* Again, should never happen */ assert(0 && "Expected ;"); } @@ -1174,8 +1149,8 @@ css_error parseBlock(css_parser *parser) parserutils_vector_clear(parser->tokens); - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - token->ilower->data[0] != '{') { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + lwc_string_data(token->ilower)[0] != '{') { /* This should never happen, as FIRST(block) == '{' */ assert(0 && "Expected {"); } @@ -1204,8 +1179,8 @@ css_error parseBlock(css_parser *parser) if (token->type == CSS_TOKEN_EOF) break; - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - token->ilower->data[0] != '}') { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + lwc_string_data(token->ilower)[0] != '}') { /* This should never happen, as * FOLLOW(block-content) == '}' */ assert(0 && "Expected }"); @@ -1257,8 +1232,8 @@ css_error parseBlockContent(css_parser *parser) if (token->type == CSS_TOKEN_ATKEYWORD) { state->substate = WS; } else if (token->type == CSS_TOKEN_CHAR) { - if (token->ilower->len == 1 && - token->ilower->data[0] == '{') { + if (lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == '{') { /* Grammar ambiguity. Assume block */ parser_state to = { sBlock, Initial }; parser_state subsequent = @@ -1281,12 +1256,12 @@ css_error parseBlockContent(css_parser *parser) return transition(parser, to, subsequent); - } else if (token->ilower->len == 1 && - token->ilower->data[0] == ';') { + } else if (lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == ';') { /* Grammar ambiguity. Assume semi */ state->substate = WS; - } else if (token->ilower->len == 1 && - token->ilower->data[0] == '}') { + } else if (lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == '}') { /* Grammar ambiguity. Assume end */ error = pushBack(parser, token); if (error != CSS_OK) @@ -1402,8 +1377,8 @@ css_error parseDeclaration(css_parser *parser) if (error != CSS_OK) return error; - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - token->ilower->data[0] != ':') { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + lwc_string_data(token->ilower)[0] != ':') { /* parse error -- expected : */ parser_state to = { sMalformedDecl, Initial }; @@ -1469,14 +1444,14 @@ css_error parseDeclList(css_parser *parser) if (token->type == CSS_TOKEN_EOF) return done(parser); - if (token->type != CSS_TOKEN_CHAR || token->ilower->len != 1 || - (token->ilower->data[0] != '}' && - token->ilower->data[0] != ';')) { + if (token->type != CSS_TOKEN_CHAR || lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != '}' && + lwc_string_data(token->ilower)[0] != ';')) { /* Should never happen */ assert(0 && "Expected ; or }"); } - if (token->ilower->data[0] == '}') { + if (lwc_string_data(token->ilower)[0] == '}') { error = pushBack(parser, token); if (error != CSS_OK) return error; @@ -1519,9 +1494,9 @@ css_error parseDeclListEnd(css_parser *parser) return error; if (token->type != CSS_TOKEN_CHAR || - token->ilower->len != 1 || - (token->ilower->data[0] != ';' && - token->ilower->data[0] != '}')) { + lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != ';' && + lwc_string_data(token->ilower)[0] != '}')) { parser_state to = { sDeclaration, Initial }; parser_state subsequent = { sDeclListEnd, AfterDeclaration }; @@ -1608,9 +1583,9 @@ css_error parseValue1(css_parser *parser) /* Grammar ambiguity -- assume ';' or '}' mark end */ if (token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && - (token->ilower->data[0] == ';' || - token->ilower->data[0] == '}')) { + lwc_string_length(token->ilower) == 1 && + (lwc_string_data(token->ilower)[0] == ';' || + lwc_string_data(token->ilower)[0] == '}')) { /* Parse error */ parser->parseError = true; @@ -1661,9 +1636,9 @@ css_error parseValue0(css_parser *parser) /* Grammar ambiguity -- assume ';' or '}' mark end */ if (token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && - (token->ilower->data[0] == ';' || - token->ilower->data[0] == '}')) { + lwc_string_length(token->ilower) == 1 && + (lwc_string_data(token->ilower)[0] == ';' || + lwc_string_data(token->ilower)[0] == '}')) { return done(parser); } @@ -1703,8 +1678,8 @@ css_error parseValue(css_parser *parser) if (token->type == CSS_TOKEN_ATKEYWORD) { state->substate = WS; } else if (token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && - token->ilower->data[0] == '{') { + lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == '{') { /* Grammar ambiguity. Assume block. */ parser_state to = { sBlock, Initial }; @@ -1767,11 +1742,11 @@ css_error parseAny0(css_parser *parser) /* Grammar ambiguity: * assume '{', ';', ')', ']' mark end */ if (token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && - (token->ilower->data[0] == '{' || - token->ilower->data[0] == ';' || - token->ilower->data[0] == ')' || - token->ilower->data[0] == ']')) { + lwc_string_length(token->ilower) == 1 && + (lwc_string_data(token->ilower)[0] == '{' || + lwc_string_data(token->ilower)[0] == ';' || + lwc_string_data(token->ilower)[0] == ')' || + lwc_string_data(token->ilower)[0] == ']')) { return done(parser); } @@ -1831,15 +1806,15 @@ css_error parseAny1(css_parser *parser) /* Grammar ambiguity: any0 can be followed by * '{', ';', ')', ']'. any1 can only be followed by '{'. */ - if (token->type == CSS_TOKEN_CHAR && token->ilower->len == 1) { - if (token->ilower->data[0] == ';' || - token->ilower->data[0] == ')' || - token->ilower->data[0] == ']') { + if (token->type == CSS_TOKEN_CHAR && lwc_string_length(token->ilower) == 1) { + if (lwc_string_data(token->ilower)[0] == ';' || + lwc_string_data(token->ilower)[0] == ')' || + lwc_string_data(token->ilower)[0] == ']') { parser_state to = { sAny, Initial }; parser_state subsequent = { sAny1, AfterAny }; return transition(parser, to, subsequent); - } else if (token->ilower->data[0] != '{') { + } else if (lwc_string_data(token->ilower)[0] != '{') { /* parse error */ parser->parseError = true; } @@ -1909,11 +1884,11 @@ css_error parseAny(css_parser *parser) parser->match_char = ')'; state->substate = WS; } else if (token->type == CSS_TOKEN_CHAR && - token->ilower->len == 1 && - (token->ilower->data[0] == '(' || - token->ilower->data[0] == '[')) { + lwc_string_length(token->ilower) == 1 && + (lwc_string_data(token->ilower)[0] == '(' || + lwc_string_data(token->ilower)[0] == '[')) { parser->match_char = - token->ilower->data[0] == '(' ? ')' : ']'; + lwc_string_data(token->ilower)[0] == '(' ? ')' : ']'; state->substate = WS; } @@ -1945,8 +1920,8 @@ css_error parseAny(css_parser *parser) return error; /* Match correct close bracket (grammar ambiguity) */ - if (token->type == CSS_TOKEN_CHAR && token->ilower->len == 1 && - token->ilower->data[0] == parser->match_char) { + if (token->type == CSS_TOKEN_CHAR && lwc_string_length(token->ilower) == 1 && + lwc_string_data(token->ilower)[0] == parser->match_char) { state->substate = WS2; goto ws2; } @@ -1989,14 +1964,14 @@ css_error parseMalformedDeclaration(css_parser *parser) break; if (token->type != CSS_TOKEN_CHAR || - token->ilower->len != 1 || - (token->ilower->data[0] != '{' && - token->ilower->data[0] != '}' && - token->ilower->data[0] != '[' && - token->ilower->data[0] != ']' && - token->ilower->data[0] != '(' && - token->ilower->data[0] != ')' && - token->ilower->data[0] != ';')) + lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != '{' && + lwc_string_data(token->ilower)[0] != '}' && + lwc_string_data(token->ilower)[0] != '[' && + lwc_string_data(token->ilower)[0] != ']' && + lwc_string_data(token->ilower)[0] != '(' && + lwc_string_data(token->ilower)[0] != ')' && + lwc_string_data(token->ilower)[0] != ';')) continue; char want; @@ -2006,17 +1981,17 @@ css_error parseMalformedDeclaration(css_parser *parser) /* If the stack is empty, then we're done if we've got * either a ';' or '}' */ if (match == NULL) { - if (token->ilower->data[0] == ';' || - token->ilower->data[0] == '}') + if (lwc_string_data(token->ilower)[0] == ';' || + lwc_string_data(token->ilower)[0] == '}') break; } /* Regardless, if we've got a semicolon, ignore it */ - if (token->ilower->data[0] == ';') + if (lwc_string_data(token->ilower)[0] == ';') continue; /* Get corresponding start tokens for end tokens */ - switch (token->ilower->data[0]) { + switch (lwc_string_data(token->ilower)[0]) { case '}': want = '{'; break; @@ -2038,7 +2013,7 @@ css_error parseMalformedDeclaration(css_parser *parser) parser->open_items, NULL); } else if (want == 0) { parserutils_stack_push(parser->open_items, - &token->ilower->data[0]); + &lwc_string_data(token->ilower)[0]); } } } @@ -2083,13 +2058,13 @@ css_error parseMalformedSelector(css_parser *parser) break; if (token->type != CSS_TOKEN_CHAR || - token->ilower->len != 1 || - (token->ilower->data[0] != '{' && - token->ilower->data[0] != '}' && - token->ilower->data[0] != '[' && - token->ilower->data[0] != ']' && - token->ilower->data[0] != '(' && - token->ilower->data[0] != ')')) + lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != '{' && + lwc_string_data(token->ilower)[0] != '}' && + lwc_string_data(token->ilower)[0] != '[' && + lwc_string_data(token->ilower)[0] != ']' && + lwc_string_data(token->ilower)[0] != '(' && + lwc_string_data(token->ilower)[0] != ')')) continue; char want; @@ -2097,7 +2072,7 @@ css_error parseMalformedSelector(css_parser *parser) parser->open_items); /* Get corresponding start tokens for end tokens */ - switch (token->ilower->data[0]) { + switch (lwc_string_data(token->ilower)[0]) { case '}': want = '{'; break; @@ -2119,7 +2094,7 @@ css_error parseMalformedSelector(css_parser *parser) parser->open_items, NULL); } else if (want == 0) { parserutils_stack_push(parser->open_items, - &token->ilower->data[0]); + &lwc_string_data(token->ilower)[0]); } /* If we encountered a '}', there was data on the stack @@ -2176,14 +2151,14 @@ css_error parseMalformedAtRule(css_parser *parser) break; if (token->type != CSS_TOKEN_CHAR || - token->ilower->len != 1 || - (token->ilower->data[0] != '{' && - token->ilower->data[0] != '}' && - token->ilower->data[0] != '[' && - token->ilower->data[0] != ']' && - token->ilower->data[0] != '(' && - token->ilower->data[0] != ')' && - token->ilower->data[0] != ';')) + lwc_string_length(token->ilower) != 1 || + (lwc_string_data(token->ilower)[0] != '{' && + lwc_string_data(token->ilower)[0] != '}' && + lwc_string_data(token->ilower)[0] != '[' && + lwc_string_data(token->ilower)[0] != ']' && + lwc_string_data(token->ilower)[0] != '(' && + lwc_string_data(token->ilower)[0] != ')' && + lwc_string_data(token->ilower)[0] != ';')) continue; char want; @@ -2192,7 +2167,7 @@ css_error parseMalformedAtRule(css_parser *parser) /* If we have a semicolon, then we're either done or * need to ignore it */ - if (token->ilower->data[0] == ';') { + if (lwc_string_data(token->ilower)[0] == ';') { if (match == NULL) break; else @@ -2200,7 +2175,7 @@ css_error parseMalformedAtRule(css_parser *parser) } /* Get corresponding start tokens for end tokens */ - switch (token->ilower->data[0]) { + switch (lwc_string_data(token->ilower)[0]) { case '}': want = '{'; break; @@ -2222,7 +2197,7 @@ css_error parseMalformedAtRule(css_parser *parser) parser->open_items, NULL); } else if (want == 0) { parserutils_stack_push(parser->open_items, - &token->ilower->data[0]); + &lwc_string_data(token->ilower)[0]); } /* If we encountered a '}', there was data on the stack diff --git a/src/parse/parse.h b/src/parse/parse.h index 5dc1b18..bb6229f 100644 --- a/src/parse/parse.h +++ b/src/parse/parse.h @@ -8,7 +8,8 @@ #ifndef css_parse_parse_h_ #define css_parse_parse_h_ -#include +#include + #include #include @@ -57,7 +58,7 @@ typedef union css_parser_optparams { } css_parser_optparams; css_error css_parser_create(const char *charset, css_charset_source cs_source, - parserutils_hash *dict, css_allocator_fn alloc, void *pw, + lwc_context *dict, css_allocator_fn alloc, void *pw, css_parser **parser); css_error css_parser_destroy(css_parser *parser); @@ -71,8 +72,5 @@ css_error css_parser_completed(css_parser *parser); const char *css_parser_read_charset(css_parser *parser, css_charset_source *source); -const parserutils_hash_entry *css_parser_dict_add(css_parser *parser, - const uint8_t *ptr, size_t len); - #endif diff --git a/src/parse/properties.c b/src/parse/properties.c index b0905a8..bd1a6a4 100644 --- a/src/parse/properties.c +++ b/src/parse/properties.c @@ -751,7 +751,7 @@ css_error parse_background_image(css_language *c, required_size = sizeof(opv); if ((flags & FLAG_INHERIT) == false && value == BACKGROUND_IMAGE_URI) - required_size += sizeof(parserutils_hash_entry *); + required_size += sizeof(lwc_string *); /* Allocate result */ error = css_stylesheet_style_create(c->sheet, required_size, result); @@ -763,7 +763,7 @@ css_error parse_background_image(css_language *c, if ((flags & FLAG_INHERIT) == false && value == BACKGROUND_IMAGE_URI) { memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), &token->idata, - sizeof(parserutils_hash_entry *)); + sizeof(lwc_string *)); } return CSS_OK; @@ -1652,7 +1652,7 @@ css_error parse_counter_increment(css_language *c, value = COUNTER_INCREMENT_NAMED; while (token != NULL) { - const parserutils_hash_entry *name = token->idata; + lwc_string *name = token->idata; css_fixed increment = INTTOFIX(1); consumeWhitespace(vector, &temp_ctx); @@ -1664,14 +1664,12 @@ css_error parse_counter_increment(css_language *c, return CSS_INVALID; if (token != NULL && token->type == CSS_TOKEN_NUMBER) { - const css_string temp = { token->ilower->len, - (uint8_t *) token->ilower->data }; size_t consumed = 0; - increment = number_from_css_string(&temp, + increment = number_from_lwc_string(token->ilower, true, &consumed); - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; parserutils_vector_iterate(vector, &temp_ctx); @@ -1730,7 +1728,7 @@ css_error parse_counter_increment(css_language *c, opv = COUNTER_INCREMENT_NAMED; while (token != NULL) { - const parserutils_hash_entry *name = token->idata; + lwc_string *name = token->idata; css_fixed increment = INTTOFIX(1); consumeWhitespace(vector, ctx); @@ -1742,14 +1740,12 @@ css_error parse_counter_increment(css_language *c, return CSS_INVALID; if (token != NULL && token->type == CSS_TOKEN_NUMBER) { - const css_string temp = { token->ilower->len, - (uint8_t *) token->ilower->data }; size_t consumed = 0; - increment = number_from_css_string(&temp, + increment = number_from_lwc_string(token->ilower, true, &consumed); - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; parserutils_vector_iterate(vector, ctx); @@ -1821,7 +1817,7 @@ css_error parse_counter_reset(css_language *c, value = COUNTER_RESET_NAMED; while (token != NULL) { - const parserutils_hash_entry *name = token->idata; + lwc_string *name = token->idata; css_fixed increment = INTTOFIX(0); consumeWhitespace(vector, &temp_ctx); @@ -1833,14 +1829,12 @@ css_error parse_counter_reset(css_language *c, return CSS_INVALID; if (token != NULL && token->type == CSS_TOKEN_NUMBER) { - const css_string temp = { token->ilower->len, - (uint8_t *) token->ilower->data }; size_t consumed = 0; - increment = number_from_css_string(&temp, + increment = number_from_lwc_string(token->ilower, true, &consumed); - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; parserutils_vector_iterate(vector, &temp_ctx); @@ -1899,7 +1893,7 @@ css_error parse_counter_reset(css_language *c, opv = COUNTER_RESET_NAMED; while (token != NULL) { - const parserutils_hash_entry *name = token->idata; + lwc_string *name = token->idata; css_fixed increment = INTTOFIX(0); consumeWhitespace(vector, ctx); @@ -1911,14 +1905,12 @@ css_error parse_counter_reset(css_language *c, return CSS_INVALID; if (token != NULL && token->type == CSS_TOKEN_NUMBER) { - const css_string temp = { token->ilower->len, - (uint8_t *) token->ilower->data }; size_t consumed = 0; - increment = number_from_css_string(&temp, + increment = number_from_lwc_string(token->ilower, true, &consumed); - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; parserutils_vector_iterate(vector, ctx); @@ -1996,7 +1988,7 @@ css_error parse_cue_after(css_language *c, required_size = sizeof(opv); if ((flags & FLAG_INHERIT) == false && value == CUE_AFTER_URI) - required_size += sizeof(parserutils_hash_entry *); + required_size += sizeof(lwc_string *); /* Allocate result */ error = css_stylesheet_style_create(c->sheet, required_size, result); @@ -2008,7 +2000,7 @@ css_error parse_cue_after(css_language *c, if ((flags & FLAG_INHERIT) == false && value == CUE_AFTER_URI) { memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), &token->idata, - sizeof(parserutils_hash_entry *)); + sizeof(lwc_string *)); } return CSS_OK; @@ -2050,7 +2042,7 @@ css_error parse_cue_before(css_language *c, required_size = sizeof(opv); if ((flags & FLAG_INHERIT) == false && value == CUE_BEFORE_URI) - required_size += sizeof(parserutils_hash_entry *); + required_size += sizeof(lwc_string *); /* Allocate result */ error = css_stylesheet_style_create(c->sheet, required_size, result); @@ -2062,7 +2054,7 @@ css_error parse_cue_before(css_language *c, if ((flags & FLAG_INHERIT) == false && value == CUE_BEFORE_URI) { memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), &token->idata, - sizeof(parserutils_hash_entry *)); + sizeof(lwc_string *)); } return CSS_OK; @@ -2101,7 +2093,7 @@ css_error parse_cursor(css_language *c, /* URI* */ while (token != NULL && token->type == CSS_TOKEN_URI) { - const parserutils_hash_entry *uri = token->idata; + lwc_string *uri = token->idata; if (first == false) { required_size += sizeof(opv); @@ -2244,7 +2236,7 @@ css_error parse_cursor(css_language *c, /* URI* */ while (token != NULL && token->type == CSS_TOKEN_URI) { - const parserutils_hash_entry *uri = token->idata; + lwc_string *uri = token->idata; if (first == false) { opv = CURSOR_URI; @@ -2693,7 +2685,7 @@ css_error parse_font_family(css_language *c, } required_size += - sizeof(parserutils_hash_entry *); + sizeof(lwc_string *); /* Skip past [ IDENT* S* ]* */ while (token != NULL) { @@ -2729,7 +2721,7 @@ css_error parse_font_family(css_language *c, } required_size += - sizeof(parserutils_hash_entry *); + sizeof(lwc_string *); } else { return CSS_INVALID; } @@ -2798,8 +2790,8 @@ css_error parse_font_family(css_language *c, while (token != NULL) { if (token->type == CSS_TOKEN_IDENT) { - const parserutils_hash_entry *name = - token->idata; + lwc_string *name = token->idata; + lwc_string *newname; if (token->ilower == c->strings[SERIF]) { opv = FONT_FAMILY_SERIF; @@ -2816,9 +2808,10 @@ css_error parse_font_family(css_language *c, c->strings[MONOSPACE]) { opv = FONT_FAMILY_MONOSPACE; } else { - uint16_t len = token->idata->len; + uint16_t len = lwc_string_length(token->idata); const css_token *temp_token = token; - parserutils_error perror; + lwc_error lerror; + temp_ctx = *ctx; @@ -2838,7 +2831,7 @@ css_error parse_font_family(css_language *c, if (temp_token->type == CSS_TOKEN_IDENT) { - len += temp_token->idata->len; + len += lwc_string_length(temp_token->idata); } else { len += 1; } @@ -2850,8 +2843,8 @@ css_error parse_font_family(css_language *c, uint8_t buf[len]; uint8_t *p = buf; - memcpy(p, token->idata->data, token->idata->len); - p += token->idata->len; + memcpy(p, lwc_string_data(token->idata), lwc_string_length(token->idata)); + p += lwc_string_length(token->idata); while (token != NULL) { token = parserutils_vector_peek( @@ -2867,9 +2860,9 @@ css_error parse_font_family(css_language *c, if (token->type == CSS_TOKEN_IDENT) { memcpy(p, - token->idata->data, - token->idata->len); - p += token->idata->len; + lwc_string_data(token->idata), + lwc_string_length(token->idata)); + p += lwc_string_length(token->idata); } else { *p++ = ' '; } @@ -2884,18 +2877,21 @@ css_error parse_font_family(css_language *c, /* Insert into hash, if it's different * from the name we already have */ - if (p - buf != name->len || - memcmp(buf, name->data, - name->len) != 0) { - perror = parserutils_hash_insert( - c->sheet->dictionary, - buf, len, &name); - if (perror != PARSERUTILS_OK) { - css_stylesheet_style_destroy(c->sheet, *result); - *result = NULL; - return css_error_from_parserutils_error(perror); - } - } + + lerror = lwc_context_intern(c->sheet->dictionary, + (char *)buf, len, &newname); + if (lerror != lwc_error_ok) { + css_stylesheet_style_destroy(c->sheet, *result); + *result = NULL; + return css_error_from_lwc_error(lerror); + } + + if (newname == name) + lwc_context_string_unref(c->sheet->dictionary, + newname); + + name = newname; + } if (first == false) { @@ -3184,11 +3180,9 @@ css_error parse_font_weight(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - css_fixed num = number_from_css_string(&tmp, true, &consumed); + css_fixed num = number_from_lwc_string(token->ilower, true, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; switch (FIXTOINT(num)) { case 100: value = FONT_WEIGHT_100; break; @@ -3454,10 +3448,8 @@ css_error parse_line_height(css_language *c, value = LINE_HEIGHT_NORMAL; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - length = number_from_css_string(&tmp, false, &consumed); - if (consumed != token->ilower->len) + length = number_from_lwc_string(token->ilower, false, &consumed); + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Negative values are illegal */ @@ -3549,7 +3541,7 @@ css_error parse_list_style_image(css_language *c, required_size = sizeof(opv); if ((flags & FLAG_INHERIT) == false && value == LIST_STYLE_IMAGE_URI) - required_size += sizeof(parserutils_hash_entry *); + required_size += sizeof(lwc_string *); /* Allocate result */ error = css_stylesheet_style_create(c->sheet, required_size, result); @@ -3561,7 +3553,7 @@ css_error parse_list_style_image(css_language *c, if ((flags & FLAG_INHERIT) == false && value == LIST_STYLE_IMAGE_URI) { memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), &token->idata, - sizeof(parserutils_hash_entry *)); + sizeof(lwc_string *)); } return CSS_OK; @@ -3974,11 +3966,9 @@ css_error parse_orphans(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, true, &consumed); + num = number_from_lwc_string(token->ilower, true, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Negative values are nonsensical */ @@ -4466,11 +4456,9 @@ css_error parse_pitch_range(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, false, &consumed); + num = number_from_lwc_string(token->ilower, false, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Must be between 0 and 100 */ @@ -4597,7 +4585,7 @@ css_error parse_play_during(css_language *c, uint16_t value = 0; uint32_t opv; uint32_t required_size; - const parserutils_hash_entry *uri; + lwc_string *uri; /* URI [ IDENT(mix) || IDENT(repeat) ]? | IDENT(auto,none,inherit) */ token = parserutils_vector_iterate(vector, ctx); @@ -4653,7 +4641,7 @@ css_error parse_play_during(css_language *c, required_size = sizeof(opv); if ((flags & FLAG_INHERIT) == false && (value & PLAY_DURING_TYPE_MASK) == PLAY_DURING_URI) - required_size += sizeof(parserutils_hash_entry *); + required_size += sizeof(lwc_string *); /* Allocate result */ error = css_stylesheet_style_create(c->sheet, required_size, result); @@ -4665,7 +4653,7 @@ css_error parse_play_during(css_language *c, if ((flags & FLAG_INHERIT) == false && (value & PLAY_DURING_TYPE_MASK) == PLAY_DURING_URI) { memcpy((uint8_t *) (*result)->bytecode + sizeof(opv), - &uri, sizeof(parserutils_hash_entry *)); + &uri, sizeof(lwc_string *)); } return CSS_OK; @@ -4749,8 +4737,8 @@ css_error parse_quotes(css_language *c, /* [ STRING STRING ] + */ while (token != NULL && token->type == CSS_TOKEN_STRING) { - const parserutils_hash_entry *open = token->idata; - const parserutils_hash_entry *close; + lwc_string *open = token->idata; + lwc_string *close; consumeWhitespace(vector, &temp_ctx); @@ -4818,8 +4806,8 @@ css_error parse_quotes(css_language *c, /* [ STRING STRING ]+ */ while (token != NULL && token->type == CSS_TOKEN_STRING) { - const parserutils_hash_entry *open = token->idata; - const parserutils_hash_entry *close; + lwc_string *open = token->idata; + lwc_string *close; consumeWhitespace(vector, ctx); @@ -4897,11 +4885,9 @@ css_error parse_richness(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, false, &consumed); + num = number_from_lwc_string(token->ilower, false, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Must be between 0 and 100 */ @@ -5214,11 +5200,9 @@ css_error parse_speech_rate(css_language *c, value = SPEECH_RATE_SLOWER; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, false, &consumed); + num = number_from_lwc_string(token->ilower, false, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Make negative values invalid */ @@ -5276,11 +5260,9 @@ css_error parse_stress(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, false, &consumed); + num = number_from_lwc_string(token->ilower, false, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; if (num < 0 || num > INTTOFIX(100)) @@ -5876,7 +5858,7 @@ css_error parse_voice_family(css_language *c, } required_size += - sizeof(parserutils_hash_entry *); + sizeof(lwc_string *); /* Skip past [ IDENT* S* ]* */ while (token != NULL) { @@ -5910,7 +5892,7 @@ css_error parse_voice_family(css_language *c, } required_size += - sizeof(parserutils_hash_entry *); + sizeof(lwc_string *); } else { return CSS_INVALID; } @@ -5979,8 +5961,8 @@ css_error parse_voice_family(css_language *c, while (token != NULL) { if (token->type == CSS_TOKEN_IDENT) { - const parserutils_hash_entry *name = - token->idata; + lwc_string *name = token->idata; + lwc_string *newname; if (token->ilower == c->strings[MALE]) { opv = VOICE_FAMILY_MALE; @@ -5990,9 +5972,9 @@ css_error parse_voice_family(css_language *c, } else if (token->ilower == c->strings[CHILD]) { opv = VOICE_FAMILY_CHILD; } else { - uint16_t len = token->idata->len; + uint16_t len = lwc_string_length(token->idata); const css_token *temp_token = token; - parserutils_error perror; + lwc_error lerror; temp_ctx = *ctx; @@ -6011,7 +5993,7 @@ css_error parse_voice_family(css_language *c, } if (temp_token != NULL && temp_token->type == CSS_TOKEN_IDENT) { - len += temp_token->idata->len; + len += lwc_string_length(temp_token->idata); } else if (temp_token != NULL) { len += 1; } @@ -6023,8 +6005,8 @@ css_error parse_voice_family(css_language *c, uint8_t buf[len]; uint8_t *p = buf; - memcpy(p, token->idata->data, token->idata->len); - p += token->idata->len; + memcpy(p, lwc_string_data(token->idata), lwc_string_length(token->idata)); + p += lwc_string_length(token->idata); while (token != NULL) { token = parserutils_vector_peek( @@ -6041,9 +6023,9 @@ css_error parse_voice_family(css_language *c, token->type == CSS_TOKEN_IDENT) { memcpy(p, - token->idata->data, - token->idata->len); - p += token->idata->len; + lwc_string_data(token->idata), + lwc_string_length(token->idata)); + p += lwc_string_length(token->idata); } else if (token != NULL) { *p++ = ' '; } @@ -6058,19 +6040,20 @@ css_error parse_voice_family(css_language *c, /* Insert into hash, if it's different * from the name we already have */ - if (p - buf != name->len || - memcmp(buf, name->data, - name->len) != 0) { - perror = parserutils_hash_insert( - c->sheet->dictionary, - buf, len, &name); - if (perror != PARSERUTILS_OK) { - css_stylesheet_style_destroy(c->sheet, *result); - *result = NULL; - return css_error_from_parserutils_error(perror); - } - } - } + lerror = lwc_context_intern(c->sheet->dictionary, + (char *)buf, len, &newname); + if (lerror != lwc_error_ok) { + css_stylesheet_style_destroy(c->sheet, *result); + *result = NULL; + return css_error_from_lwc_error(lerror); + } + + if (newname == name) + lwc_context_string_unref(c->sheet->dictionary, + newname); + + name = newname; + } if (first == false) { memcpy(ptr, &opv, sizeof(opv)); @@ -6197,10 +6180,8 @@ css_error parse_volume(css_language *c, value = VOLUME_X_LOUD; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - length = number_from_css_string(&tmp, false, &consumed); - if (consumed != token->ilower->len) + length = number_from_lwc_string(token->ilower, false, &consumed); + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Must be between 0 and 100 */ @@ -6329,11 +6310,9 @@ css_error parse_widows(css_language *c, flags |= FLAG_INHERIT; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, true, &consumed); + num = number_from_lwc_string(token->ilower, true, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; /* Negative values are nonsensical */ @@ -6530,11 +6509,9 @@ css_error parse_z_index(css_language *c, value = Z_INDEX_AUTO; } else if (token->type == CSS_TOKEN_NUMBER) { size_t consumed = 0; - css_string tmp = { token->ilower->len, - (uint8_t *) token->ilower->data }; - num = number_from_css_string(&tmp, true, &consumed); + num = number_from_lwc_string(token->ilower, true, &consumed); /* Invalid if there are trailing characters */ - if (consumed != token->ilower->len) + if (consumed != lwc_string_length(token->ilower)) return CSS_INVALID; value = Z_INDEX_SET; @@ -6612,30 +6589,30 @@ css_error parse_colour_specifier(css_language *c, if (token->type == CSS_TOKEN_IDENT) { /** \todo Parse colour names */ } else if (token->type == CSS_TOKEN_HASH) { - if (token->idata->len == 3 && - isHex(token->idata->data[0]) && - isHex(token->idata->data[1]) && - isHex(token->idata->data[2])) { - r = charToHex(token->idata->data[0]); - g = charToHex(token->idata->data[1]); - b = charToHex(token->idata->data[2]); + if (lwc_string_length(token->idata) == 3 && + isHex(lwc_string_data(token->idata)[0]) && + isHex(lwc_string_data(token->idata)[1]) && + isHex(lwc_string_data(token->idata)[2])) { + r = charToHex(lwc_string_data(token->idata)[0]); + g = charToHex(lwc_string_data(token->idata)[1]); + b = charToHex(lwc_string_data(token->idata)[2]); r |= (r << 4); g |= (g << 4); b |= (b << 4); - } else if (token->idata->len == 6 && - isHex(token->idata->data[0]) && - isHex(token->idata->data[1]) && - isHex(token->idata->data[2]) && - isHex(token->idata->data[3]) && - isHex(token->idata->data[4]) && - isHex(token->idata->data[5])) { - r = (charToHex(token->idata->data[0]) << 4); - r |= charToHex(token->idata->data[1]); - g = (charToHex(token->idata->data[2]) << 4); - g |= charToHex(token->idata->data[3]); - b = (charToHex(token->idata->data[4]) << 4); - b |= charToHex(token->idata->data[5]); + } else if (lwc_string_length(token->idata) == 6 && + isHex(lwc_string_data(token->idata)[0]) && + isHex(lwc_string_data(token->idata)[1]) && + isHex(lwc_string_data(token->idata)[2]) && + isHex(lwc_string_data(token->idata)[3]) && + isHex(lwc_string_data(token->idata)[4]) && + isHex(lwc_string_data(token->idata)[5])) { + r = (charToHex(lwc_string_data(token->idata)[0]) << 4); + r |= charToHex(lwc_string_data(token->idata)[1]); + g = (charToHex(lwc_string_data(token->idata)[2]) << 4); + g |= charToHex(lwc_string_data(token->idata)[3]); + b = (charToHex(lwc_string_data(token->idata)[4]) << 4); + b |= charToHex(lwc_string_data(token->idata)[5]); } else return CSS_INVALID; } else if (token->type == CSS_TOKEN_FUNCTION) { @@ -6643,7 +6620,6 @@ css_error parse_colour_specifier(css_language *c, css_token_type valid = CSS_TOKEN_NUMBER; for (int i = 0; i < 3; i++) { - css_string tmp; css_fixed num; size_t consumed = 0; uint8_t *component = i == 0 ? &r @@ -6664,12 +6640,10 @@ css_error parse_colour_specifier(css_language *c, else if (token->type != valid) return CSS_INVALID; - tmp.len = token->idata->len; - tmp.data = (uint8_t *) token->idata->data; - num = number_from_css_string(&tmp, + num = number_from_lwc_string(token->idata, valid == CSS_TOKEN_NUMBER, &consumed); - if (consumed != token->idata->len) + if (consumed != lwc_string_length(token->idata)) return CSS_INVALID; if (valid == CSS_TOKEN_NUMBER) { @@ -6718,7 +6692,6 @@ css_error parse_unit_specifier(css_language *c, const css_token *token; css_fixed num; size_t consumed = 0; - css_string tmp; UNUSED(c); @@ -6730,64 +6703,62 @@ css_error parse_unit_specifier(css_language *c, token->type != CSS_TOKEN_PERCENTAGE)) return CSS_INVALID; - tmp.len = token->idata->len; - tmp.data = (uint8_t *) token->idata->data; - num = number_from_css_string(&tmp, false, &consumed); + num = number_from_lwc_string(token->idata, false, &consumed); if (token->type == CSS_TOKEN_DIMENSION) { - if (token->idata->len - consumed == 4) { - if (strncasecmp((char *) token->idata->data + consumed, + if (lwc_string_length(token->idata) - consumed == 4) { + if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "grad", 4) == 0) *unit = UNIT_GRAD; else return CSS_INVALID; - } else if (token->idata->len - consumed == 3) { - if (strncasecmp((char *) token->idata->data + consumed, + } else if (lwc_string_length(token->idata) - consumed == 3) { + if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "kHz", 3) == 0) *unit = UNIT_KHZ; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "deg", 3) == 0) *unit = UNIT_DEG; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "rad", 3) == 0) *unit = UNIT_RAD; else return CSS_INVALID; - } else if (token->idata->len - consumed == 2) { - if (strncasecmp((char *) token->idata->data + consumed, + } else if (lwc_string_length(token->idata) - consumed == 2) { + if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "Hz", 2) == 0) *unit = UNIT_HZ; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "ms", 2) == 0) *unit = UNIT_MS; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "px", 2) == 0) *unit = UNIT_PX; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "ex", 2) == 0) *unit = UNIT_EX; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "em", 2) == 0) *unit = UNIT_EM; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "in", 2) == 0) *unit = UNIT_IN; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "cm", 2) == 0) *unit = UNIT_CM; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "mm", 2) == 0) *unit = UNIT_MM; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "pt", 2) == 0) *unit = UNIT_PT; - else if (strncasecmp((char *) token->idata->data + + else if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "pc", 2) == 0) *unit = UNIT_PC; else return CSS_INVALID; - } else if (token->idata->len - consumed == 1) { - if (strncasecmp((char *) token->idata->data + consumed, + } else if (lwc_string_length(token->idata) - consumed == 1) { + if (strncasecmp((char *) lwc_string_data(token->idata) + consumed, "s", 1) == 0) *unit = UNIT_S; else @@ -6803,7 +6774,7 @@ css_error parse_unit_specifier(css_language *c, return CSS_INVALID; *unit = default_unit; } else { - if (consumed != token->idata->len) + if (consumed != lwc_string_length(token->idata)) return CSS_INVALID; *unit = UNIT_PCT; } @@ -7323,7 +7294,7 @@ css_error parse_content_list(css_language *c, return CSS_INVALID; } else if (token->type == CSS_TOKEN_FUNCTION && token->ilower == c->strings[COUNTER]) { - const parserutils_hash_entry *name; + lwc_string *name; opv = CONTENT_COUNTER; @@ -7394,8 +7365,8 @@ css_error parse_content_list(css_language *c, offset += sizeof(name); } else if (token->type == CSS_TOKEN_FUNCTION && token->ilower == c->strings[COUNTERS]) { - const parserutils_hash_entry *name; - const parserutils_hash_entry *sep; + lwc_string *name; + lwc_string *sep; opv = CONTENT_COUNTERS; diff --git a/src/select/hash.c b/src/select/hash.c index 2a30a5f..f471861 100644 --- a/src/select/hash.c +++ b/src/select/hash.c @@ -29,7 +29,7 @@ struct css_selector_hash { static hash_entry empty_slot; static inline uint32_t _hash(const css_selector *sel); -static inline uint32_t _hash_name(const parserutils_hash_entry *name); +static inline uint32_t _hash_name(lwc_string *name); /** * Create a hash @@ -225,7 +225,7 @@ css_error css_selector_hash_remove(css_selector_hash *hash, * If nothing matches, CSS_OK will be returned and **matched == NULL */ css_error css_selector_hash_find(css_selector_hash *hash, - const parserutils_hash_entry *name, + lwc_string *name, const css_selector ***matched) { uint32_t index, mask; @@ -310,7 +310,7 @@ uint32_t _hash(const css_selector *sel) * \param name Name to hash * \return hash value */ -uint32_t _hash_name(const parserutils_hash_entry *name) +uint32_t _hash_name(lwc_string *name) { return (uint32_t) (((uintptr_t) name) & 0xffffffff); } diff --git a/src/select/hash.h b/src/select/hash.h index 2f388b7..b116021 100644 --- a/src/select/hash.h +++ b/src/select/hash.h @@ -8,7 +8,7 @@ #ifndef css_select_hash_h_ #define css_select_hash_h_ -#include +#include #include #include @@ -28,7 +28,7 @@ css_error css_selector_hash_remove(css_selector_hash *hash, const struct css_selector *selector); css_error css_selector_hash_find(css_selector_hash *hash, - const parserutils_hash_entry *name, + lwc_string *name, const struct css_selector ***matched); css_error css_selector_hash_iterate(css_selector_hash *hash, const struct css_selector **current, diff --git a/src/select/properties.c b/src/select/properties.c index 7ac0e36..710e3fc 100644 --- a/src/select/properties.c +++ b/src/select/properties.c @@ -11,7 +11,7 @@ static css_error cascade_bg_border_color(uint32_t opv, css_style *style, static css_error cascade_uri_none(uint32_t opv, css_style *style, css_select_state *state, css_error (*fun)(css_computed_style *, uint8_t, - const parserutils_hash_entry *)); + lwc_string *)); static css_error cascade_border_style(uint32_t opv, css_style *style, css_select_state *state, css_error (*fun)(css_computed_style *, uint8_t)); @@ -641,13 +641,12 @@ static css_error cascade_content(uint32_t opv, css_style *style, value = CSS_CONTENT_NONE; } else { value = CSS_CONTENT_SET; - - while (v != CONTENT_NORMAL) { - css_computed_content_item *temp; - parserutils_hash_entry *he = - *((parserutils_hash_entry **) style->bytecode); - - temp = state->result->alloc(content, + + while (v != CONTENT_NORMAL) { + lwc_string *he = *((lwc_string **) style->bytecode); + css_computed_content_item *temp; + + temp = state->result->alloc(content, (n_contents + 1) * sizeof(css_computed_content_item), state->result->pw); @@ -667,26 +666,23 @@ static css_error cascade_content(uint32_t opv, css_style *style, content[n_contents].type = CSS_COMPUTED_CONTENT_COUNTER; - content[n_contents].data.counter.name.data = (uint8_t *) he->data; - content[n_contents].data.counter.name.len = he->len; + content[n_contents].data.counter.name = he; content[n_contents].data.counter.style = v >> CONTENT_COUNTER_STYLE_SHIFT; break; case CONTENT_COUNTERS: { - parserutils_hash_entry *sep; + lwc_string *sep; advance_bytecode(style, sizeof(he)); - sep = *((parserutils_hash_entry **) + sep = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(sep)); content[n_contents].type = CSS_COMPUTED_CONTENT_COUNTERS; - content[n_contents].data.counters.name.data = (uint8_t *) he->data; - content[n_contents].data.counters.name.len = he->len; - content[n_contents].data.counters.sep.data = (uint8_t *) sep->data; - content[n_contents].data.counters.sep.len = sep->len; + content[n_contents].data.counters.name = he; + content[n_contents].data.counters.sep = sep; content[n_contents].data.counters.style = v >> CONTENT_COUNTERS_STYLE_SHIFT; } break; @@ -695,24 +691,21 @@ static css_error cascade_content(uint32_t opv, css_style *style, content[n_contents].type = CSS_COMPUTED_CONTENT_URI; - content[n_contents].data.uri.data = (uint8_t *) he->data; - content[n_contents].data.uri.len = he->len; + content[n_contents].data.uri = he; break; case CONTENT_ATTR: advance_bytecode(style, sizeof(he)); content[n_contents].type = CSS_COMPUTED_CONTENT_ATTR; - content[n_contents].data.attr.data = (uint8_t *) he->data; - content[n_contents].data.attr.len = he->len; + content[n_contents].data.attr = he; break; case CONTENT_STRING: advance_bytecode(style, sizeof(he)); content[n_contents].type = CSS_COMPUTED_CONTENT_STRING; - content[n_contents].data.string.data = (uint8_t *) he->data; - content[n_contents].data.string.len = he->len; + content[n_contents].data.string = he; break; case CONTENT_OPEN_QUOTE: content[n_contents].type = @@ -829,21 +822,21 @@ static css_error cascade_cursor(uint32_t opv, css_style *style, css_select_state *state) { uint16_t value = CSS_CURSOR_INHERIT; - css_string *uris = NULL; + lwc_string **uris = NULL; uint32_t n_uris = 0; if (isInherit(opv) == false) { uint32_t v = getValue(opv); while (v == CURSOR_URI) { - parserutils_hash_entry *uri; - css_string *temp; + lwc_string *uri; + lwc_string **temp; - uri = *((parserutils_hash_entry **) style->bytecode); + uri = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(uri)); temp = state->result->alloc(uris, - (n_uris + 1) * sizeof(css_string), + (n_uris + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { if (uris != NULL) { @@ -855,8 +848,7 @@ static css_error cascade_cursor(uint32_t opv, css_style *style, uris = temp; - uris[n_uris].data = (uint8_t *) uri->data; - uris[n_uris].len = uri->len; + uris[n_uris] = uri; n_uris++; @@ -921,10 +913,10 @@ static css_error cascade_cursor(uint32_t opv, css_style *style, /* Terminate array with blank entry, if needed */ if (n_uris > 0) { - css_string *temp; + lwc_string **temp; temp = state->result->alloc(uris, - (n_uris + 1) * sizeof(css_string), + (n_uris + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { state->result->alloc(uris, 0, state->result->pw); @@ -933,8 +925,7 @@ static css_error cascade_cursor(uint32_t opv, css_style *style, uris = temp; - uris[n_uris].data = NULL; - uris[n_uris].len = 0; + uris[n_uris] = NULL; } if (outranks_existing(getOpcode(opv), isImportant(opv), state)) { @@ -1163,20 +1154,20 @@ static css_error cascade_font_family(uint32_t opv, css_style *style, css_select_state *state) { uint16_t value = CSS_FONT_FAMILY_INHERIT; - css_string *fonts = NULL; + lwc_string **fonts = NULL; uint32_t n_fonts = 0; if (isInherit(opv) == false) { uint32_t v = getValue(opv); while (v != FONT_FAMILY_END) { - const parserutils_hash_entry *font = NULL; - css_string *temp; + lwc_string *font = NULL; + lwc_string **temp; switch (v) { case FONT_FAMILY_STRING: case FONT_FAMILY_IDENT_LIST: - font = *((parserutils_hash_entry **) + font = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(font)); break; @@ -1208,7 +1199,7 @@ static css_error cascade_font_family(uint32_t opv, css_style *style, /** \todo Do this at bytecode generation time? */ if (value == CSS_FONT_FAMILY_INHERIT && font != NULL) { temp = state->result->alloc(fonts, - (n_fonts + 1) * sizeof(css_string), + (n_fonts + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { if (fonts != NULL) { @@ -1220,8 +1211,7 @@ static css_error cascade_font_family(uint32_t opv, css_style *style, fonts = temp; - fonts[n_fonts].data = (uint8_t *) font->data; - fonts[n_fonts].len = font->len; + fonts[n_fonts] = font; n_fonts++; } @@ -1233,10 +1223,10 @@ static css_error cascade_font_family(uint32_t opv, css_style *style, /* Terminate array with blank entry, if needed */ if (n_fonts > 0) { - css_string *temp; + lwc_string **temp; temp = state->result->alloc(fonts, - (n_fonts + 1) * sizeof(css_string), + (n_fonts + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { state->result->alloc(fonts, 0, state->result->pw); @@ -1245,8 +1235,7 @@ static css_error cascade_font_family(uint32_t opv, css_style *style, fonts = temp; - fonts[n_fonts].data = NULL; - fonts[n_fonts].len = 0; + fonts[n_fonts] = NULL; } if (outranks_existing(getOpcode(opv), isImportant(opv), state)) { @@ -2015,14 +2004,14 @@ static css_error cascade_play_during(uint32_t opv, css_style *style, css_select_state *state) { uint16_t value = 0; - parserutils_hash_entry *uri = NULL; + lwc_string *uri = NULL; if (isInherit(opv) == false) { switch (getValue(opv)) { case PLAY_DURING_URI: value = 0; - uri = *((parserutils_hash_entry **) style->bytecode); + uri = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(uri)); break; case PLAY_DURING_AUTO: @@ -2088,7 +2077,7 @@ static css_error cascade_quotes(uint32_t opv, css_style *style, css_select_state *state) { uint16_t value = CSS_QUOTES_INHERIT; - css_string *quotes = NULL; + lwc_string **quotes = NULL; uint32_t n_quotes = 0; if (isInherit(opv) == false) { @@ -2097,14 +2086,14 @@ static css_error cascade_quotes(uint32_t opv, css_style *style, value = CSS_QUOTES_STRING; while (v != QUOTES_NONE) { - parserutils_hash_entry *quote; - css_string *temp; + lwc_string *quote; + lwc_string **temp; - quote = *((parserutils_hash_entry **) style->bytecode); + quote = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(quote)); temp = state->result->alloc(quotes, - (n_quotes + 1) * sizeof(css_string), + (n_quotes + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { if (quotes != NULL) { @@ -2116,8 +2105,7 @@ static css_error cascade_quotes(uint32_t opv, css_style *style, quotes = temp; - quotes[n_quotes].data = (uint8_t *) quote->data; - quotes[n_quotes].len = quote->len; + quotes[n_quotes] = quote; n_quotes++; @@ -2128,10 +2116,10 @@ static css_error cascade_quotes(uint32_t opv, css_style *style, /* Terminate array, if required */ if (n_quotes > 0) { - css_string *temp; + lwc_string **temp; temp = state->result->alloc(quotes, - (n_quotes + 1) * sizeof(css_string), + (n_quotes + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { state->result->alloc(quotes, 0, state->result->pw); @@ -2140,8 +2128,7 @@ static css_error cascade_quotes(uint32_t opv, css_style *style, quotes = temp; - quotes[n_quotes].data = NULL; - quotes[n_quotes].len = 0; + quotes[n_quotes] = NULL; } if (outranks_existing(getOpcode(opv), isImportant(opv), state)) { @@ -2650,20 +2637,20 @@ static css_error cascade_voice_family(uint32_t opv, css_style *style, css_select_state *state) { uint16_t value = 0; - css_string *voices = NULL; + lwc_string **voices = NULL; uint32_t n_voices = 0; if (isInherit(opv) == false) { uint32_t v = getValue(opv); while (v != VOICE_FAMILY_END) { - const parserutils_hash_entry *voice = NULL; - css_string *temp; + lwc_string *voice = NULL; + lwc_string **temp; switch (v) { case VOICE_FAMILY_STRING: case VOICE_FAMILY_IDENT_LIST: - voice = *((parserutils_hash_entry **) + voice = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(voice)); break; @@ -2687,7 +2674,7 @@ static css_error cascade_voice_family(uint32_t opv, css_style *style, /** \todo Do this at bytecode generation time? */ if (value == 0 && voice != NULL) { temp = state->result->alloc(voices, - (n_voices + 1) * sizeof(css_string), + (n_voices + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { if (voices != NULL) { @@ -2699,8 +2686,7 @@ static css_error cascade_voice_family(uint32_t opv, css_style *style, voices = temp; - voices[n_voices].data = (uint8_t *) voice->data; - voices[n_voices].len = voice->len; + voices[n_voices] = voice; n_voices++; } @@ -2712,10 +2698,10 @@ static css_error cascade_voice_family(uint32_t opv, css_style *style, /* Terminate array with blank entry, if needed */ if (n_voices > 0) { - css_string *temp; + lwc_string **temp; temp = state->result->alloc(voices, - (n_voices + 1) * sizeof(css_string), + (n_voices + 1) * sizeof(lwc_string *), state->result->pw); if (temp == NULL) { state->result->alloc(voices, 0, state->result->pw); @@ -2724,8 +2710,7 @@ static css_error cascade_voice_family(uint32_t opv, css_style *style, voices = temp; - voices[n_voices].data = NULL; - voices[n_voices].len = 0; + voices[n_voices] = NULL; } if (outranks_existing(getOpcode(opv), isImportant(opv), state)) { @@ -2940,10 +2925,10 @@ css_error cascade_bg_border_color(uint32_t opv, css_style *style, css_error cascade_uri_none(uint32_t opv, css_style *style, css_select_state *state, css_error (*fun)(css_computed_style *, uint8_t, - const parserutils_hash_entry *)) + lwc_string *)) { uint16_t value = CSS_BACKGROUND_IMAGE_INHERIT; - parserutils_hash_entry *uri = NULL; + lwc_string *uri = NULL; if (isInherit(opv) == false) { switch (getValue(opv)) { @@ -2952,7 +2937,7 @@ css_error cascade_uri_none(uint32_t opv, css_style *style, break; case BACKGROUND_IMAGE_URI: value = CSS_BACKGROUND_IMAGE_IMAGE; - uri = *((parserutils_hash_entry **) style->bytecode); + uri = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(uri)); break; } @@ -3243,10 +3228,10 @@ css_error cascade_counter_increment_reset(uint32_t opv, css_style *style, while (v != COUNTER_INCREMENT_NONE) { css_computed_counter *temp; - parserutils_hash_entry *name; + lwc_string *name; css_fixed val = 0; - name = *((parserutils_hash_entry **) + name = *((lwc_string **) style->bytecode); advance_bytecode(style, sizeof(name)); @@ -3267,9 +3252,7 @@ css_error cascade_counter_increment_reset(uint32_t opv, css_style *style, counters = temp; - counters[n_counters].name.data = - (uint8_t *) name->data; - counters[n_counters].name.len = name->len; + counters[n_counters].name = name; counters[n_counters].value = val; n_counters++; @@ -3299,8 +3282,7 @@ css_error cascade_counter_increment_reset(uint32_t opv, css_style *style, counters = temp; - counters[n_counters].name.data = NULL; - counters[n_counters].name.len = 0; + counters[n_counters].name = NULL; counters[n_counters].value = 0; } diff --git a/src/select/propset.h b/src/select/propset.h index 64f7829..730822a 100644 --- a/src/select/propset.h +++ b/src/select/propset.h @@ -219,7 +219,7 @@ static inline css_error set_counter_reset( #define CURSOR_MASK 0xf8 static inline css_error set_cursor( css_computed_style *style, uint8_t type, - css_string *urls) + lwc_string **urls) { uint8_t *bits; @@ -244,7 +244,7 @@ static inline css_error set_cursor( #define QUOTES_MASK 0x6 static inline css_error set_quotes( css_computed_style *style, uint8_t type, - css_string *quotes) + lwc_string **quotes) { uint8_t *bits; @@ -480,7 +480,7 @@ static inline css_error set_border_left_width( #define BACKGROUND_IMAGE_MASK 0x1 static inline css_error set_background_image( css_computed_style *style, uint8_t type, - const parserutils_hash_entry *url) + lwc_string *url) { uint8_t *bits = &style->bits[BACKGROUND_IMAGE_INDEX]; @@ -489,11 +489,9 @@ static inline css_error set_background_image( ((type & 0x1) << BACKGROUND_IMAGE_SHIFT); if (url != NULL) { - style->background_image.data = (uint8_t *) url->data; - style->background_image.len = url->len; + style->background_image = url; } else { - style->background_image.data = NULL; - style->background_image.len = 0; + style->background_image = NULL; } return CSS_OK; @@ -528,7 +526,7 @@ static inline css_error set_color( #define LIST_STYLE_IMAGE_MASK 0x1 static inline css_error set_list_style_image( css_computed_style *style, uint8_t type, - const parserutils_hash_entry *url) + lwc_string *url) { uint8_t *bits = &style->bits[LIST_STYLE_IMAGE_INDEX]; @@ -537,11 +535,9 @@ static inline css_error set_list_style_image( ((type & 0x1) << LIST_STYLE_IMAGE_SHIFT); if (url != NULL) { - style->list_style_image.data = (uint8_t *) url->data; - style->list_style_image.len = url->len; + style->list_style_image = url; } else { - style->list_style_image.data = NULL; - style->list_style_image.len = 0; + style->list_style_image = NULL; } return CSS_OK; @@ -1444,7 +1440,7 @@ static inline css_error set_text_decoration( #define FONT_FAMILY_MASK 0x7 static inline css_error set_font_family( css_computed_style *style, uint8_t type, - css_string *names) + lwc_string **names) { uint8_t *bits = &style->bits[FONT_FAMILY_INDEX]; diff --git a/src/select/select.c b/src/select/select.c index f48db9a..0c31ae4 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -8,8 +8,6 @@ #include #include -#include - #include #include @@ -58,17 +56,17 @@ typedef struct css_select_state { uint32_t current_specificity; /* Specificity of current rule */ /* Useful interned strings */ - const parserutils_hash_entry *universal; - const parserutils_hash_entry *first_child; - const parserutils_hash_entry *link; - const parserutils_hash_entry *visited; - const parserutils_hash_entry *hover; - const parserutils_hash_entry *active; - const parserutils_hash_entry *focus; - const parserutils_hash_entry *first_line; - const parserutils_hash_entry *first_letter; - const parserutils_hash_entry *before; - const parserutils_hash_entry *after; + lwc_string *universal; + lwc_string *first_child; + lwc_string *link; + lwc_string *visited; + lwc_string *hover; + lwc_string *active; + lwc_string *focus; + lwc_string *first_line; + lwc_string *first_letter; + lwc_string *before; + lwc_string *after; prop_state props[N_OPCODES]; } css_select_state; @@ -82,7 +80,7 @@ static css_error match_selectors_in_sheet(css_select_ctx *ctx, static css_error match_selector_chain(css_select_ctx *ctx, const css_selector *selector, css_select_state *state); static css_error match_named_combinator(css_select_ctx *ctx, - css_combinator type, const parserutils_hash_entry *name, + css_combinator type, lwc_string *name, css_select_state *state, void *node, void **next_node); static css_error match_universal_combinator(css_select_ctx *ctx, css_combinator type, const css_selector *selector, @@ -449,7 +447,7 @@ css_error css_select_style(css_select_ctx *ctx, void *node, if ((ctx->sheets[i]->media & media) != 0) { error = select_from_sheet(ctx, ctx->sheets[i], &state); if (error != CSS_OK) - return error; + goto cleanup; } } @@ -478,26 +476,63 @@ css_error css_select_style(css_select_ctx *ctx, void *node, if (properties[i].group == GROUP_NORMAL) { error = properties[i].initial(result); if (error != CSS_OK) - return error; + goto cleanup; } else if (properties[i].group == GROUP_UNCOMMON && result->uncommon != NULL) { error = properties[i].initial(result); if (error != CSS_OK) - return error; + goto cleanup; } else if (properties[i].group == GROUP_PAGE && result->page != NULL) { error = properties[i].initial(result); if (error != CSS_OK) - return error; + goto cleanup; } else if (properties[i].group == GROUP_AURAL && result->aural != NULL) { error = properties[i].initial(result); if (error != CSS_OK) - return error; + goto cleanup; } } - return CSS_OK; + error = CSS_OK; +cleanup: + if (ctx->sheets[0] != NULL) { + if (state.universal != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.universal); + if (state.first_child != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.first_child); + if (state.link != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.link); + if (state.visited != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.visited); + if (state.hover != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.hover); + if (state.active != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.active); + if (state.focus != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.focus); + if (state.first_line != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.first_line); + if (state.first_letter != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.first_letter); + if (state.before != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.before); + if (state.after != NULL) + lwc_context_string_unref(ctx->sheets[0]->dictionary, + state.after); + } + return error; } /****************************************************************************** @@ -566,77 +601,80 @@ css_error select_from_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, css_error intern_strings_for_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, css_select_state *state) { - parserutils_error perror; + lwc_error error; UNUSED(ctx); /* Universal selector */ - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "*", SLEN("*"), &state->universal); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (state->universal != NULL) + return CSS_OK; + + error = lwc_context_intern(sheet->dictionary, + "*", SLEN("*"), &state->universal); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); /* Pseudo classes */ - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "first-child", SLEN("first-child"), + error = lwc_context_intern(sheet->dictionary, + "first-child", SLEN("first-child"), &state->first_child); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "link", SLEN("link"), + error = lwc_context_intern(sheet->dictionary, + "link", SLEN("link"), &state->link); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "visited", SLEN("visited"), + error = lwc_context_intern(sheet->dictionary, + "visited", SLEN("visited"), &state->visited); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "hover", SLEN("hover"), + error = lwc_context_intern(sheet->dictionary, + "hover", SLEN("hover"), &state->hover); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "active", SLEN("active"), + error = lwc_context_intern(sheet->dictionary, + "active", SLEN("active"), &state->active); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "focus", SLEN("focus"), + error = lwc_context_intern(sheet->dictionary, + "focus", SLEN("focus"), &state->focus); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); /* Pseudo elements */ - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "first-line", SLEN("first-line"), + error = lwc_context_intern(sheet->dictionary, + "first-line", SLEN("first-line"), &state->first_line); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "first_letter", SLEN("first-letter"), + error = lwc_context_intern(sheet->dictionary, + "first_letter", SLEN("first-letter"), &state->first_letter); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "before", SLEN("before"), + error = lwc_context_intern(sheet->dictionary, + "before", SLEN("before"), &state->before); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); - perror = parserutils_hash_insert(sheet->dictionary, - (const uint8_t *) "after", SLEN("after"), + error = lwc_context_intern(sheet->dictionary, + "after", SLEN("after"), &state->after); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); + if (error != lwc_error_ok) + return css_error_from_lwc_error(error); return CSS_OK; } @@ -644,60 +682,55 @@ css_error intern_strings_for_sheet(css_select_ctx *ctx, css_error match_selectors_in_sheet(css_select_ctx *ctx, const css_stylesheet *sheet, css_select_state *state) { - const parserutils_hash_entry *element; + lwc_string *element; const css_selector **selectors; - const uint8_t *name; - size_t len; - parserutils_error perror; css_error error; /* Get node's name */ - error = state->handler->node_name(state->pw, state->node, &name, &len); + error = state->handler->node_name(state->pw, state->node, sheet->dictionary, &element); if (error != CSS_OK) return error; - /* Intern it */ - perror = parserutils_hash_insert(sheet->dictionary, - name, len, &element); - if (perror != PARSERUTILS_OK) - return css_error_from_parserutils_error(perror); - /* Find hash chain that applies to current node */ error = css_selector_hash_find(sheet->selectors, element, &selectors); if (error != CSS_OK) - return error; + goto cleanup; /* Process any matching selectors */ while (*selectors != NULL) { error = match_selector_chain(ctx, *selectors, state); if (error != CSS_OK) - return error; + goto cleanup; error = css_selector_hash_iterate(sheet->selectors, selectors, &selectors); if (error != CSS_OK) - return error; + goto cleanup; } /* Find hash chain for universal selector */ error = css_selector_hash_find(sheet->selectors, state->universal, &selectors); if (error != CSS_OK) - return error; + goto cleanup; /* Process any matching selectors */ while (*selectors != NULL) { error = match_selector_chain(ctx, *selectors, state); if (error != CSS_OK) - return error; + goto cleanup; error = css_selector_hash_iterate(sheet->selectors, selectors, &selectors); if (error != CSS_OK) - return error; + goto cleanup; } - return CSS_OK; + + error = CSS_OK; +cleanup: + lwc_context_string_unref(sheet->dictionary, element); + return error; } css_error match_selector_chain(css_select_ctx *ctx, @@ -761,7 +794,7 @@ css_error match_selector_chain(css_select_ctx *ctx, } css_error match_named_combinator(css_select_ctx *ctx, css_combinator type, - const parserutils_hash_entry *name, css_select_state *state, + lwc_string *name, css_select_state *state, void *node, void **next_node) { css_error error; @@ -771,19 +804,19 @@ css_error match_named_combinator(css_select_ctx *ctx, css_combinator type, switch (type) { case CSS_COMBINATOR_ANCESTOR: error = state->handler->named_ancestor_node(state->pw, node, - name->data, name->len, next_node); + name, next_node); if (error != CSS_OK) return error; break; case CSS_COMBINATOR_PARENT: error = state->handler->named_parent_node(state->pw, node, - name->data, name->len, next_node); + name, next_node); if (error != CSS_OK) return error; break; case CSS_COMBINATOR_SIBLING: error = state->handler->named_sibling_node(state->pw, node, - name->data, name->len, next_node); + name, next_node); if (error != CSS_OK) return error; break; @@ -890,13 +923,11 @@ css_error match_detail(css_select_ctx *ctx, void *node, switch (detail->type) { case CSS_SELECTOR_CLASS: error = state->handler->node_has_class(state->pw, node, - detail->name->data, detail->name->len, - match); + detail->name, match); break; case CSS_SELECTOR_ID: error = state->handler->node_has_id(state->pw, node, - detail->name->data, detail->name->len, - match); + detail->name, match); break; case CSS_SELECTOR_PSEUDO_CLASS: if (detail->name == state->first_child) { @@ -942,26 +973,19 @@ css_error match_detail(css_select_ctx *ctx, void *node, break; case CSS_SELECTOR_ATTRIBUTE: error = state->handler->node_has_attribute(state->pw, node, - detail->name->data, detail->name->len, - match); + detail->name, match); break; case CSS_SELECTOR_ATTRIBUTE_EQUAL: error = state->handler->node_has_attribute_equal(state->pw, - node, detail->name->data, detail->name->len, - detail->value->data, detail->value->len, - match); + node, detail->name, detail->value, match); break; case CSS_SELECTOR_ATTRIBUTE_DASHMATCH: error = state->handler->node_has_attribute_dashmatch(state->pw, - node, detail->name->data, detail->name->len, - detail->value->data, detail->value->len, - match); + node, detail->name, detail->value, match); break; case CSS_SELECTOR_ATTRIBUTE_INCLUDES: error = state->handler->node_has_attribute_includes(state->pw, - node, detail->name->data, detail->name->len, - detail->value->data, detail->value->len, - match); + node, detail->name, detail->value, match); break; } diff --git a/src/stylesheet.c b/src/stylesheet.c index 7072bfc..0344e47 100644 --- a/src/stylesheet.c +++ b/src/stylesheet.c @@ -35,11 +35,10 @@ static css_error _remove_selectors(css_stylesheet *sheet, css_rule *rule); */ css_error css_stylesheet_create(css_language_level level, const char *charset, const char *url, const char *title, - css_origin origin, uint64_t media, + css_origin origin, uint64_t media, lwc_context *dict, css_allocator_fn alloc, void *alloc_pw, css_stylesheet **stylesheet) { - parserutils_error perror; css_error error; css_stylesheet *sheet; size_t len; @@ -52,19 +51,13 @@ css_error css_stylesheet_create(css_language_level level, return CSS_NOMEM; memset(sheet, 0, sizeof(css_stylesheet)); - - perror = parserutils_hash_create((parserutils_alloc) alloc, alloc_pw, - &sheet->dictionary); - if (perror != PARSERUTILS_OK) { - alloc(sheet, 0, alloc_pw); - return css_error_from_parserutils_error(perror); - } + + sheet->dictionary = dict; error = css_parser_create(charset, charset ? CSS_CHARSET_DICTATED : CSS_CHARSET_DEFAULT, sheet->dictionary, alloc, alloc_pw, &sheet->parser); if (error != CSS_OK) { - parserutils_hash_destroy(sheet->dictionary); alloc(sheet, 0, alloc_pw); return error; } @@ -74,7 +67,6 @@ css_error css_stylesheet_create(css_language_level level, &sheet->parser_frontend); if (error != CSS_OK) { css_parser_destroy(sheet->parser); - parserutils_hash_destroy(sheet->dictionary); alloc(sheet, 0, alloc_pw); return error; } @@ -83,7 +75,6 @@ css_error css_stylesheet_create(css_language_level level, if (error != CSS_OK) { css_language_destroy(sheet->parser_frontend); css_parser_destroy(sheet->parser); - parserutils_hash_destroy(sheet->dictionary); alloc(sheet, 0, alloc_pw); return error; } @@ -94,7 +85,6 @@ css_error css_stylesheet_create(css_language_level level, css_selector_hash_destroy(sheet->selectors); css_language_destroy(sheet->parser_frontend); css_parser_destroy(sheet->parser); - parserutils_hash_destroy(sheet->dictionary); alloc(sheet, 0, alloc_pw); return CSS_NOMEM; } @@ -108,7 +98,6 @@ css_error css_stylesheet_create(css_language_level level, css_selector_hash_destroy(sheet->selectors); css_language_destroy(sheet->parser_frontend); css_parser_destroy(sheet->parser); - parserutils_hash_destroy(sheet->dictionary); alloc(sheet, 0, alloc_pw); return CSS_NOMEM; } @@ -164,8 +153,6 @@ css_error css_stylesheet_destroy(css_stylesheet *sheet) if (sheet->parser != NULL) css_parser_destroy(sheet->parser); - parserutils_hash_destroy(sheet->dictionary); - sheet->alloc(sheet, 0, sheet->pw); return CSS_OK; @@ -262,7 +249,7 @@ css_error css_stylesheet_data_done(css_stylesheet *sheet) * register an empty stylesheet with the parent in its place. */ css_error css_stylesheet_next_pending_import(css_stylesheet *parent, - css_string *url, uint64_t *media) + lwc_string **url, uint64_t *media) { const css_rule *r; @@ -278,9 +265,7 @@ css_error css_stylesheet_next_pending_import(css_stylesheet *parent, break; if (r->type == CSS_RULE_IMPORT && i->sheet == NULL) { - url->len = i->url->len; - url->data = (uint8_t *) i->url->data; - + *url = lwc_context_string_ref(parent->dictionary, i->url); *media = i->media; return CSS_OK; @@ -512,7 +497,7 @@ css_error css_stylesheet_style_destroy(css_stylesheet *sheet, css_style *style) * CSS_NOMEM on memory exhaustion */ css_error css_stylesheet_selector_create(css_stylesheet *sheet, - const parserutils_hash_entry *name, css_selector **selector) + lwc_string *name, css_selector **selector) { css_selector *sel; @@ -530,7 +515,7 @@ css_error css_stylesheet_selector_create(css_stylesheet *sheet, sel->data.value = NULL; /* Initial specificity -- 1 for an element, 0 for universal */ - if (name->len != 1 || name->data[0] != '*') + if (lwc_string_length(name) != 1 || lwc_string_data(name)[0] != '*') sel->specificity = CSS_SPECIFICITY_D; else sel->specificity = 0; @@ -585,8 +570,8 @@ css_error css_stylesheet_selector_destroy(css_stylesheet *sheet, * CSS_BADPARM on bad parameters */ css_error css_stylesheet_selector_detail_init(css_stylesheet *sheet, - css_selector_type type, const parserutils_hash_entry *name, - const parserutils_hash_entry *value, + css_selector_type type, lwc_string *name, + lwc_string *value, css_selector_detail *detail) { if (sheet == NULL || name == NULL || detail == NULL) @@ -968,7 +953,7 @@ css_error css_stylesheet_rule_append_style(css_stylesheet *sheet, * \return CSS_OK on success, appropriate error otherwise */ css_error css_stylesheet_rule_set_charset(css_stylesheet *sheet, - css_rule *rule, const parserutils_hash_entry *charset) + css_rule *rule, lwc_string *charset) { css_rule_charset *r = (css_rule_charset *) rule; @@ -995,7 +980,7 @@ css_error css_stylesheet_rule_set_charset(css_stylesheet *sheet, * \return CSS_OK on success, appropriate error otherwise */ css_error css_stylesheet_rule_set_nascent_import(css_stylesheet *sheet, - css_rule *rule, const parserutils_hash_entry *url, + css_rule *rule, lwc_string *url, uint64_t media) { css_rule_import *r = (css_rule_import *) rule; diff --git a/src/stylesheet.h b/src/stylesheet.h index d2316d1..0a859f2 100644 --- a/src/stylesheet.h +++ b/src/stylesheet.h @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include @@ -50,8 +50,8 @@ typedef enum css_combinator { } css_combinator; typedef struct css_selector_detail { - const parserutils_hash_entry *name; /**< Interned name */ - const parserutils_hash_entry *value; /**< Interned value, or NULL */ + lwc_string *name; /**< Interned name */ + lwc_string *value; /**< Interned value, or NULL */ uint32_t type : 4, /**< Type of selector */ comb : 2, /**< Type of combinator */ @@ -133,7 +133,7 @@ typedef struct css_rule_page { typedef struct css_rule_import { css_rule base; - const parserutils_hash_entry *url; + lwc_string *url; uint64_t media; css_stylesheet *sheet; @@ -142,7 +142,7 @@ typedef struct css_rule_import { typedef struct css_rule_charset { css_rule base; - const parserutils_hash_entry *encoding; /** \todo use MIB enum? */ + lwc_string *encoding; /** \todo use MIB enum? */ } css_rule_charset; struct css_stylesheet { @@ -168,7 +168,7 @@ struct css_stylesheet { css_parser *parser; /**< Core parser for sheet */ void *parser_frontend; /**< Frontend parser */ - parserutils_hash *dictionary; /**< String dictionary */ + lwc_context *dictionary; /**< String dictionary */ css_allocator_fn alloc; /**< Allocation function */ void *pw; /**< Private word */ @@ -179,13 +179,13 @@ css_error css_stylesheet_style_create(css_stylesheet *sheet, uint32_t len, css_error css_stylesheet_style_destroy(css_stylesheet *sheet, css_style *style); css_error css_stylesheet_selector_create(css_stylesheet *sheet, - const parserutils_hash_entry *name, css_selector **selector); + lwc_string *name, css_selector **selector); css_error css_stylesheet_selector_destroy(css_stylesheet *sheet, css_selector *selector); css_error css_stylesheet_selector_detail_init(css_stylesheet *sheet, - css_selector_type type, const parserutils_hash_entry *name, - const parserutils_hash_entry *value, + css_selector_type type, lwc_string *name, + lwc_string *value, css_selector_detail *detail); css_error css_stylesheet_selector_append_specific(css_stylesheet *sheet, @@ -205,11 +205,10 @@ css_error css_stylesheet_rule_append_style(css_stylesheet *sheet, css_rule *rule, css_style *style); css_error css_stylesheet_rule_set_charset(css_stylesheet *sheet, - css_rule *rule, const parserutils_hash_entry *charset); + css_rule *rule, lwc_string *charset); css_error css_stylesheet_rule_set_nascent_import(css_stylesheet *sheet, - css_rule *rule, const parserutils_hash_entry *url, - uint64_t media); + css_rule *rule, lwc_string *url, uint64_t media); /** \todo registering other rule-type data with css_rules */ diff --git a/src/utils/utils.c b/src/utils/utils.c index a8d0cb5..29c7ec1 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -7,7 +7,7 @@ #include "utils/utils.h" -css_fixed number_from_css_string(const css_string *string, +css_fixed number_from_lwc_string(lwc_string *string, bool int_only, size_t *consumed) { size_t len; @@ -17,11 +17,11 @@ css_fixed number_from_css_string(const css_string *string, int32_t fracpart = 0; int32_t pwr = 1; - if (string == NULL || string->len == 0 || consumed == NULL) + if (string == NULL || lwc_string_length(string) == 0 || consumed == NULL) return 0; - len = string->len; - ptr = string->data; + len = lwc_string_length(string); + ptr = (uint8_t *)lwc_string_data(string); /* number = [+-]? ([0-9]+ | [0-9]* '.' [0-9]+) */ @@ -91,7 +91,7 @@ css_fixed number_from_css_string(const css_string *string, } } - *consumed = ptr - string->data; + *consumed = (char *)ptr - lwc_string_data(string); if (sign > 0) { /* If the result is larger than we can represent, diff --git a/src/utils/utils.h b/src/utils/utils.h index 6a3ddad..4661442 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -8,7 +8,10 @@ #ifndef css_utils_h_ #define css_utils_h_ +#include + #include +#include #ifndef max #define max(a,b) ((a)>(b)?(a):(b)) @@ -27,7 +30,7 @@ #define UNUSED(x) ((x)=(x)) #endif -css_fixed number_from_css_string(const css_string *string, bool int_only, +css_fixed number_from_lwc_string(lwc_string *string, bool int_only, size_t *consumed); static inline bool isDigit(uint8_t c) @@ -80,4 +83,18 @@ static inline uint32_t charToHex(uint8_t c) return 0; } +static inline css_error +css_error_from_lwc_error(lwc_error err) +{ + switch (err) { + case lwc_error_ok: + return CSS_OK; + case lwc_error_oom: + return CSS_NOMEM; + case lwc_error_range: + return CSS_BADPARM; + } + return CSS_INVALID; +} + #endif diff --git a/test/css21.c b/test/css21.c index 47558bc..93200f5 100644 --- a/test/css21.c +++ b/test/css21.c @@ -11,8 +11,6 @@ #define DUMP_HASH (1) #define DUMP_CSS (1) -extern void parserutils_hash_dump(parserutils_hash *hash); - static void *myrealloc(void *ptr, size_t len, void *pw) { UNUSED(pw); @@ -28,6 +26,7 @@ int main(int argc, char **argv) #define CHUNK_SIZE (4096) uint8_t buf[CHUNK_SIZE]; css_error error; + lwc_context *ctx; if (argc != 3) { printf("Usage: %s \n", argv[0]); @@ -36,11 +35,15 @@ int main(int argc, char **argv) /* Initialise library */ assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK); - + + assert(lwc_create_context(myrealloc, NULL, &ctx) == lwc_error_ok); + + lwc_context_ref(ctx); /* Transform weak ref to a strong ref */ + for (int count = 0; count < ITERATIONS; count++) { assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", argv[2], - NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, + NULL, CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, ctx, myrealloc, NULL, &sheet) == CSS_OK); fp = fopen(argv[2], "rb"); @@ -78,7 +81,7 @@ int main(int argc, char **argv) assert(error == CSS_OK || error == CSS_IMPORTS_PENDING); while (error == CSS_IMPORTS_PENDING) { - css_string url; + lwc_string *url; uint64_t media; error = css_stylesheet_next_pending_import(sheet, @@ -87,14 +90,14 @@ int main(int argc, char **argv) if (error == CSS_OK) { css_stylesheet *import; - char buf[url.len + 1]; + char buf[lwc_string_length(url) + 1]; - memcpy(buf, url.data, url.len); - buf[url.len] = '\0'; + memcpy(buf, lwc_string_data(url), lwc_string_length(url)); + buf[lwc_string_length(url)] = '\0'; assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, - media, myrealloc, NULL, &import) == + media, ctx, myrealloc, NULL, &import) == CSS_OK); assert(css_stylesheet_data_done(import) == @@ -107,10 +110,6 @@ int main(int argc, char **argv) } } -#if DUMP_HASH - parserutils_hash_dump(sheet->dictionary); -#endif - #if DUMP_CSS char *out; size_t outlen = origlen * 2; @@ -127,7 +126,9 @@ int main(int argc, char **argv) assert(css_finalise(myrealloc, NULL) == CSS_OK); printf("PASS\n"); - + + lwc_context_unref(ctx); + return 0; } diff --git a/test/dump.h b/test/dump.h index 69689c3..ac780a9 100644 --- a/test/dump.h +++ b/test/dump.h @@ -19,7 +19,7 @@ static void dump_selector_list(css_selector *list, char **ptr); static void dump_selector(css_selector *selector, char **ptr); static void dump_selector_detail(css_selector_detail *detail, char **ptr); static void dump_bytecode(css_style *style, char **ptr); -static void dump_string(const parserutils_hash_entry *string, char **ptr); +static void dump_string(lwc_string *string, char **ptr); void dump_sheet(css_stylesheet *sheet, char *buf, size_t *buflen) { @@ -89,7 +89,7 @@ void dump_rule_import(css_rule_import *s, char **buf, size_t *buflen) char *ptr = *buf; ptr += sprintf(ptr, "| @import url(\"%.*s\")", - (int) s->url->len, (const char *) s->url->data); + (int) lwc_string_length(s->url), lwc_string_data(s->url)); /** \todo media list */ @@ -144,11 +144,12 @@ void dump_selector_detail(css_selector_detail *detail, char **ptr) { switch (detail->type) { case CSS_SELECTOR_ELEMENT: - if (detail->name->len == 1 && detail->name->data[0] == '*' && + if (lwc_string_length(detail->name) == 1 && + lwc_string_data(detail->name)[0] == '*' && detail->next == 0) { dump_string(detail->name, ptr); - } else if (detail->name->len != 1 || - detail->name->data[0] != '*') { + } else if (lwc_string_length(detail->name) != 1 || + lwc_string_data(detail->name)[0] != '*') { dump_string(detail->name, ptr); } break; @@ -448,11 +449,11 @@ static void dump_unit(css_fixed val, uint32_t unit, char **ptr) } } -static void dump_counter(const parserutils_hash_entry *name, uint32_t value, +static void dump_counter(lwc_string *name, uint32_t value, char **ptr) { - *ptr += sprintf(*ptr, - "counter(%.*s", (int) name->len, (char *) name->data); + *ptr += sprintf(*ptr, "counter(%.*s", + (int) lwc_string_length(name), lwc_string_data(name)); value >>= CONTENT_COUNTER_STYLE_SHIFT; @@ -506,13 +507,14 @@ static void dump_counter(const parserutils_hash_entry *name, uint32_t value, *ptr += sprintf(*ptr, ")"); } -static void dump_counters(const parserutils_hash_entry *name, - const parserutils_hash_entry *separator, +static void dump_counters(lwc_string *name, lwc_string *separator, uint32_t value, char **ptr) { *ptr += sprintf(*ptr, "counter(%.*s, %.*s", - (int) name->len, (char *) name->data, - (int) separator->len, (char *) separator->data); + (int) lwc_string_length(name), + lwc_string_data(name), + (int) lwc_string_length(separator), + lwc_string_data(separator)); value >>= CONTENT_COUNTER_STYLE_SHIFT; @@ -696,13 +698,13 @@ void dump_bytecode(css_style *style, char **ptr) break; case BACKGROUND_IMAGE_URI: { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "url('%.*s')", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); } break; } @@ -1032,8 +1034,8 @@ void dump_bytecode(css_style *style, char **ptr) } while (value != CONTENT_NORMAL) { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); const char *end = ""; @@ -1044,11 +1046,11 @@ void dump_bytecode(css_style *style, char **ptr) break; case CONTENT_COUNTERS: { - parserutils_hash_entry *sep; + lwc_string *sep; ADVANCE(sizeof(he)); - sep = *((parserutils_hash_entry **) bytecode); + sep = *((lwc_string **) bytecode); ADVANCE(sizeof(sep)); dump_counters(he, sep, value, @@ -1068,8 +1070,8 @@ void dump_bytecode(css_style *style, char **ptr) ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "'%.*s'%s", - (int) he->len, - (char *) he->data, + (int) lwc_string_length(he), + lwc_string_data(he), end); break; case CONTENT_OPEN_QUOTE: @@ -1103,13 +1105,13 @@ void dump_bytecode(css_style *style, char **ptr) switch (value) { case COUNTER_INCREMENT_NAMED: while (value != COUNTER_INCREMENT_NONE) { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "%.*s ", - (int) he->len, - (char *) he->data); + (int)lwc_string_length(he), + lwc_string_data(he)); css_fixed val = *((css_fixed *) bytecode); ADVANCE(sizeof(val)); @@ -1131,13 +1133,13 @@ void dump_bytecode(css_style *style, char **ptr) break; case OP_CURSOR: while (value == CURSOR_URI) { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(ptr)); *ptr += sprintf(*ptr, "url('%.*s'), ", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); value = *((uint32_t *) bytecode); ADVANCE(sizeof(value)); @@ -1317,13 +1319,13 @@ void dump_bytecode(css_style *style, char **ptr) case FONT_FAMILY_STRING: case FONT_FAMILY_IDENT_LIST: { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "'%.*s'", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); } break; case FONT_FAMILY_SERIF: @@ -1736,13 +1738,13 @@ void dump_bytecode(css_style *style, char **ptr) switch (value) { case PLAY_DURING_URI: { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "'%.*s'", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); } break; case PLAY_DURING_AUTO: @@ -1778,21 +1780,21 @@ void dump_bytecode(css_style *style, char **ptr) switch (value) { case QUOTES_STRING: while (value != QUOTES_NONE) { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, " '%.*s' ", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); he = - *((parserutils_hash_entry **) + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, " '%.*s' ", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); value = *((uint32_t *) bytecode); ADVANCE(sizeof(value)); @@ -2003,13 +2005,13 @@ void dump_bytecode(css_style *style, char **ptr) case VOICE_FAMILY_STRING: case VOICE_FAMILY_IDENT_LIST: { - parserutils_hash_entry *he = - *((parserutils_hash_entry **) + lwc_string *he = + *((lwc_string **) bytecode); ADVANCE(sizeof(he)); *ptr += sprintf(*ptr, "'%.*s'", - (int) he->len, - (char *) he->data); + (int) lwc_string_length(he), + lwc_string_data(he)); } break; case VOICE_FAMILY_MALE: @@ -2118,9 +2120,11 @@ void dump_bytecode(css_style *style, char **ptr) } -void dump_string(const parserutils_hash_entry *string, char **ptr) +void dump_string(lwc_string *string, char **ptr) { - *ptr += sprintf(*ptr, "%.*s", (int) string->len, string->data); + *ptr += sprintf(*ptr, "%.*s", + (int) lwc_string_length(string), + lwc_string_data(string)); } #endif diff --git a/test/dump_computed.h b/test/dump_computed.h index 690f6c0..7622394 100644 --- a/test/dump_computed.h +++ b/test/dump_computed.h @@ -142,7 +142,7 @@ static void dump_computed_style(const css_computed_style *style, char *buf, size_t wrote = 0; uint8_t val; css_color color; - const css_string *url; + lwc_string *url; css_fixed len1, len2; css_unit unit1, unit2; @@ -180,9 +180,9 @@ static void dump_computed_style(const css_computed_style *style, char *buf, /* background-image */ val = css_computed_background_image(style, &url); - if (val == CSS_BACKGROUND_IMAGE_IMAGE && url->data != NULL) { + if (val == CSS_BACKGROUND_IMAGE_IMAGE && url != NULL) { wrote = snprintf(ptr, *len, "background-image: url('%.*s')\n", - (int) url->len, url->data); + (int) lwc_string_length(url), lwc_string_data(url)); } else if (val == CSS_BACKGROUND_IMAGE_NONE) { wrote = snprintf(ptr, *len, "background-image: none\n"); } else { diff --git a/test/number.c b/test/number.c index 0140ce9..7bb57c3 100644 --- a/test/number.c +++ b/test/number.c @@ -8,6 +8,13 @@ #include "testutils.h" +static void *myrealloc(void *ptr, size_t len, void *pw) +{ + UNUSED(pw); + + return realloc(ptr, len); +} + typedef struct line_ctx { size_t buflen; size_t bufused; @@ -110,21 +117,27 @@ bool handle_line(const char *data, size_t datalen, void *pw) void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) { - css_string in = { len, (uint8_t *) data }; + lwc_string *in; + lwc_context *ctx; size_t consumed; css_fixed result; char buf[256]; UNUSED(exp); UNUSED(explen); - - result = number_from_css_string(&in, false, &consumed); + + assert(lwc_create_context(myrealloc, NULL, &ctx) == lwc_error_ok); + lwc_context_ref(ctx); + assert(lwc_context_intern(ctx, (const char *)data, len, &in) == lwc_error_ok); + + result = number_from_lwc_string(in, false, &consumed); print_css_fixed(buf, sizeof(buf), result); printf("got: %s expected: %.*s\n", buf, (int) explen, exp); assert(strncmp(buf, exp, explen) == 0); + lwc_context_unref(ctx); } void print_css_fixed(char *buf, size_t len, css_fixed f) diff --git a/test/parse-auto.c b/test/parse-auto.c index 4c42359..5ed0f55 100644 --- a/test/parse-auto.c +++ b/test/parse-auto.c @@ -59,7 +59,7 @@ static void validate_rule_import(css_rule_import *s, exp_entry *e, static void dump_selector_list(css_selector *list, char **ptr); static void dump_selector(css_selector *selector, char **ptr); static void dump_selector_detail(css_selector_detail *detail, char **ptr); -static void dump_string(const parserutils_hash_entry *string, char **ptr); +static void dump_string(lwc_string *string, char **ptr); static void *myrealloc(void *data, size_t len, void *pw) { @@ -306,9 +306,13 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) css_error error; size_t e; static int testnum; - + lwc_context *ctx; + + assert(lwc_create_context(myrealloc, NULL, &ctx) == lwc_error_ok); + lwc_context_ref(ctx); + assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, - CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, + CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, ctx, myrealloc, NULL, &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); @@ -321,7 +325,7 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) assert(error == CSS_OK || error == CSS_IMPORTS_PENDING); while (error == CSS_IMPORTS_PENDING) { - css_string url; + lwc_string *url; uint64_t media; error = css_stylesheet_next_pending_import(sheet, @@ -330,14 +334,14 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) if (error == CSS_OK) { css_stylesheet *import; - char buf[url.len + 1]; + char buf[lwc_string_length(url) + 1]; - memcpy(buf, url.data, url.len); - buf[url.len] = '\0'; + memcpy(buf, lwc_string_data(url), lwc_string_length(url)); + buf[lwc_string_length(url)] = '\0'; assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", buf, NULL, CSS_ORIGIN_AUTHOR, - media, myrealloc, NULL, &import) == + media, ctx, myrealloc, NULL, &import) == CSS_OK); assert(css_stylesheet_register_import(sheet, @@ -388,6 +392,7 @@ void run_test(const uint8_t *data, size_t len, exp_entry *exp, size_t explen) css_stylesheet_destroy(sheet); printf("Test %d: PASS\n", testnum); + lwc_context_unref(ctx); } void validate_rule_selector(css_rule_selector *s, exp_entry *e, int testnum) @@ -438,18 +443,18 @@ void validate_rule_selector(css_rule_selector *s, exp_entry *e, int testnum) } if (j != e->stused) { - const parserutils_hash_entry **p = + lwc_string **p = (void *) ((uint8_t *) s->style->bytecode + i); - if ((*p)->len != + if (lwc_string_length(*p) != strlen(e->stringtab[j].string) || - memcmp((*p)->data, + memcmp(lwc_string_data(*p), e->stringtab[j].string, - (*p)->len) != 0) { + lwc_string_length(*p)) != 0) { printf("%d: Got string '%.*s'. Expected '%s'\n", - testnum, (int) (*p)->len, - (char *) (*p)->data, + testnum, (int) lwc_string_length(*p), + lwc_string_data(*p), e->stringtab[j].string); assert(0 && "Strings differ"); } @@ -488,10 +493,10 @@ void validate_rule_charset(css_rule_charset *s, exp_entry *e, int testnum) void validate_rule_import(css_rule_import *s, exp_entry *e, int testnum) { - if (strncmp((const char *) s->url->data, e->name, - (int) s->url->len) != 0) { + if (strncmp(lwc_string_data(s->url), e->name, + lwc_string_length(s->url)) != 0) { printf("%d: Got URL '%.*s'. Expected '%s'\n", - testnum, (int) s->url->len, (const char *) s->url->data, + testnum, (int) lwc_string_length(s->url), lwc_string_data(s->url), e->name); assert(0 && "Mismatched URLs"); } @@ -542,11 +547,11 @@ void dump_selector_detail(css_selector_detail *detail, char **ptr) { switch (detail->type) { case CSS_SELECTOR_ELEMENT: - if (detail->name->len == 1 && detail->name->data[0] == '*' && + if (lwc_string_length(detail->name) == 1 && lwc_string_data(detail->name)[0] == '*' && detail->next == 0) { dump_string(detail->name, ptr); - } else if (detail->name->len != 1 || - detail->name->data[0] != '*') { + } else if (lwc_string_length(detail->name) != 1 || + lwc_string_data(detail->name)[0] != '*') { dump_string(detail->name, ptr); } break; @@ -621,8 +626,8 @@ void dump_selector_detail(css_selector_detail *detail, char **ptr) } } -void dump_string(const parserutils_hash_entry *string, char **ptr) +void dump_string(lwc_string *string, char **ptr) { - *ptr += sprintf(*ptr, "%.*s", (int) string->len, string->data); + *ptr += sprintf(*ptr, "%.*s", (int) lwc_string_length(string), lwc_string_data(string)); } diff --git a/test/parse.c b/test/parse.c index cd1d701..ed0deb7 100644 --- a/test/parse.c +++ b/test/parse.c @@ -76,13 +76,13 @@ static css_error event_handler(css_parser_event type, int main(int argc, char **argv) { css_parser_optparams params; - parserutils_hash *dict; css_parser *parser; FILE *fp; size_t len, origlen; #define CHUNK_SIZE (4096) uint8_t buf[CHUNK_SIZE]; css_error error; + lwc_context *ctx; if (argc != 3) { printf("Usage: %s \n", argv[0]); @@ -91,12 +91,11 @@ int main(int argc, char **argv) /* Initialise library */ assert(css_initialise(argv[1], myrealloc, NULL) == CSS_OK); + assert(lwc_create_context(myrealloc, NULL, &ctx) == lwc_error_ok); + lwc_context_ref(ctx); for (int i = 0; i < ITERATIONS; i++) { - assert(parserutils_hash_create(myrealloc, NULL, &dict) == - PARSERUTILS_OK); - - assert(css_parser_create("UTF-8", CSS_CHARSET_DICTATED, dict, + assert(css_parser_create("UTF-8", CSS_CHARSET_DICTATED, ctx, myrealloc, NULL, &parser) == CSS_OK); params.event_handler.handler = event_handler; @@ -138,13 +137,14 @@ int main(int argc, char **argv) css_parser_destroy(parser); - parserutils_hash_destroy(dict); } assert(css_finalise(myrealloc, NULL) == CSS_OK); printf("PASS\n"); - + + lwc_context_unref(ctx); + return 0; } diff --git a/test/parse2-auto.c b/test/parse2-auto.c index 80fcd24..aedc46b 100644 --- a/test/parse2-auto.c +++ b/test/parse2-auto.c @@ -164,15 +164,19 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) char *buf; size_t buflen; static int testnum; + lwc_context *ctx; buf = malloc(2 * explen); if (buf == NULL) { assert(0 && "No memory for result data"); } buflen = 2 * explen; - + + assert(lwc_create_context(myrealloc, NULL, &ctx) == lwc_error_ok); + lwc_context_ref(ctx); + assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", NULL, - CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, + CSS_ORIGIN_AUTHOR, CSS_MEDIA_ALL, ctx, myrealloc, NULL, &sheet) == CSS_OK); error = css_stylesheet_append_data(sheet, data, len); @@ -195,7 +199,7 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen) } css_stylesheet_destroy(sheet); - + lwc_context_unref(ctx); printf("Test %d: PASS\n", testnum); } diff --git a/test/select-auto.c b/test/select-auto.c index bd34a99..a0f0966 100644 --- a/test/select-auto.c +++ b/test/select-auto.c @@ -16,12 +16,12 @@ #include "testutils.h" typedef struct attribute { - char name[32]; - char value[32]; + lwc_string *name; + lwc_string *value; } attribute; typedef struct node { - char name[32]; + lwc_string *name; uint32_t n_attrs; attribute *attrs; @@ -53,6 +53,10 @@ typedef struct line_ctx { uint64_t media; uint32_t pseudo_element; node *target; + + lwc_context *dict; + lwc_string *attr_class; + lwc_string *attr_id; } line_ctx; static bool handle_line(const char *data, size_t datalen, void *pw); @@ -66,39 +70,38 @@ static void parse_expected(line_ctx *ctx, const char *data, size_t len); static void run_test(line_ctx *ctx, const char *exp, size_t explen); static void destroy_tree(node *root); -static css_error node_name(void *pw, void *node, const uint8_t **name, - size_t *len); +static css_error node_name(void *pw, void *node, lwc_context *ctx, lwc_string **name); static css_error named_ancestor_node(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, void **ancestor); static css_error named_parent_node(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, void **parent); static css_error named_sibling_node(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, void **sibling); static css_error parent_node(void *pw, void *node, void **parent); static css_error sibling_node(void *pw, void *node, void **sibling); static css_error node_has_class(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, bool *match); static css_error node_has_id(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, bool *match); static css_error node_has_attribute(void *pw, void *node, - const uint8_t *name, size_t len, + lwc_string *name, bool *match); static css_error node_has_attribute_equal(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, + lwc_string *value, bool *match); static css_error node_has_attribute_dashmatch(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, + lwc_string *value, bool *match); static css_error node_has_attribute_includes(void *pw, void *node, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, + lwc_string *value, bool *match); static css_error node_is_first_child(void *pw, void *node, bool *match); static css_error node_is_link(void *pw, void *node, bool *match); @@ -107,8 +110,7 @@ static css_error node_is_hover(void *pw, void *node, bool *match); static css_error node_is_active(void *pw, void *node, bool *match); static css_error node_is_focus(void *pw, void *node, bool *match); static css_error node_is_lang(void *pw, void *node, - const uint8_t *lang, size_t len, - bool *match); + lwc_string *lang, bool *match); static css_select_handler select_handler = { node_name, @@ -152,8 +154,11 @@ int main(int argc, char **argv) memset(&ctx, 0, sizeof(ctx)); + assert(lwc_create_context(myrealloc, NULL, &ctx.dict) == lwc_error_ok); + lwc_context_ref(ctx.dict); + assert(parse_testfile(argv[2], handle_line, &ctx) == true); - + /* and run final test */ if (ctx.tree != NULL) run_test(&ctx, ctx.exp, ctx.expused); @@ -162,8 +167,9 @@ int main(int argc, char **argv) assert(css_finalise(myrealloc, NULL) == CSS_OK); - printf("PASS\n"); - + lwc_context_unref(ctx.dict); + + printf("PASS\n"); return 0; } @@ -343,9 +349,8 @@ void parse_tree_data(line_ctx *ctx, const char *data, size_t len) assert(n != NULL); memset(n, 0, sizeof(node)); - - memcpy(n->name, name, min(namelen, sizeof(n->name))); - n->name[min(namelen, sizeof(n->name))] = '\0'; + + lwc_context_intern(ctx->dict, name, namelen, &n->name); /* Insert it into tree */ if (ctx->tree == NULL) { @@ -390,12 +395,9 @@ void parse_tree_data(line_ctx *ctx, const char *data, size_t len) ctx->current->attrs = temp; attr = &ctx->current->attrs[ctx->current->n_attrs]; - - memcpy(attr->name, name, min(namelen, sizeof(attr->name))); - attr->name[min(namelen, sizeof(attr->name))] = '\0'; - - memcpy(attr->value, value, min(valuelen, sizeof(attr->value))); - attr->value[min(valuelen, sizeof(attr->value))] = '\0'; + + lwc_context_intern(ctx->dict, name, namelen, &attr->name); + lwc_context_intern(ctx->dict, value, valuelen, &attr->value); ctx->current->n_attrs++; } @@ -439,7 +441,7 @@ void parse_sheet(line_ctx *ctx, const char *data, size_t len) /** \todo How are we going to handle @import? */ assert(css_stylesheet_create(CSS_LEVEL_21, "UTF-8", "foo", "foo", - origin, media, myrealloc, NULL, &sheet) == CSS_OK); + origin, media, ctx->dict, myrealloc, NULL, &sheet) == CSS_OK); /* Extend array of sheets and append new sheet to it */ temp = realloc(ctx->sheets, @@ -625,7 +627,7 @@ void run_test(line_ctx *ctx, const char *exp, size_t explen) testnum++; assert(css_select_style(select, ctx->target, ctx->pseudo_element, - ctx->media, computed, &select_handler, NULL) == CSS_OK); + ctx->media, computed, &select_handler, ctx) == CSS_OK); dump_computed_style(computed, buf, &buflen); @@ -674,28 +676,29 @@ void destroy_tree(node *root) } -css_error node_name(void *pw, void *n, const uint8_t **name, size_t *len) +css_error node_name(void *pw, void *n, lwc_context *ctx, lwc_string **name) { node *node = n; UNUSED(pw); - - *name = (const uint8_t *) node->name; - *len = strlen(node->name); - + + *name = lwc_context_string_ref(ctx, node->name); + return CSS_OK; } css_error named_ancestor_node(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, void **ancestor) { node *node = n; - - UNUSED(pw); + line_ctx *ctx = pw; for (node = node->parent; node != NULL; node = node->parent) { - if (strncasecmp(node->name, (const char *) name, len) == 0) + bool match; + assert(lwc_context_string_caseless_isequal(ctx->dict, + name, node->name, &match) == lwc_error_ok); + if (match == true) break; } @@ -705,36 +708,41 @@ css_error named_ancestor_node(void *pw, void *n, } css_error named_parent_node(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, void **parent) { node *node = n; - - UNUSED(pw); - - if (node->parent != NULL && strncasecmp(node->parent->name, - (const char *) name, len) == 0) - *parent = (void *) node->parent; - else - *parent = NULL; + line_ctx *ctx = pw; + + *parent = NULL; + if (node->parent != NULL) { + bool match; + assert(lwc_context_string_caseless_isequal(ctx->dict, + name, node->parent->name, + &match) == lwc_error_ok); + if (match == true) + *parent = (void *) node->parent; + } return CSS_OK; } css_error named_sibling_node(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, void **sibling) { node *node = n; - - UNUSED(pw); - - for (node = node->prev; node != NULL; node = node->prev) { - if (strncasecmp(node->name, (const char *) name, len) == 0) - break; - } - - *sibling = (void *) node; + line_ctx *ctx = pw; + + *sibling = NULL; + if (node->prev != NULL) { + bool match; + assert(lwc_context_string_caseless_isequal(ctx->dict, + name, node->prev->name, + &match) == lwc_error_ok); + if (match == true) + *sibling = (void *) node->prev; + } return CSS_OK; } @@ -762,22 +770,23 @@ css_error sibling_node(void *pw, void *n, void **sibling) } css_error node_has_class(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, bool *match) { node *node = n; uint32_t i; - - UNUSED(pw); + line_ctx *ctx = pw; for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, "class", 5) == 0) + bool amatch; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + ctx->attr_class, &amatch) == lwc_error_ok); + if (amatch == true) break; } /* Classes are case-sensitive in HTML */ - if (i != node->n_attrs && strncmp(node->attrs[i].value, - (const char *) name, len) == 0) + if (i != node->n_attrs && name == node->attrs[i].value) *match = true; else *match = false; @@ -786,22 +795,23 @@ css_error node_has_class(void *pw, void *n, } css_error node_has_id(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, bool *match) { node *node = n; uint32_t i; - - UNUSED(pw); + line_ctx *ctx = pw; for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, "id", 2) == 0) + bool amatch; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + ctx->attr_id, &amatch) == lwc_error_ok); + if (amatch == true) break; } /* IDs are case-sensitive in HTML */ - if (i != node->n_attrs && strncmp(node->attrs[i].value, - (const char *) name, len) == 0) + if (i != node->n_attrs && name == node->attrs[i].value) *match = true; else *match = false; @@ -810,83 +820,82 @@ css_error node_has_id(void *pw, void *n, } css_error node_has_attribute(void *pw, void *n, - const uint8_t *name, size_t len, + lwc_string *name, bool *match) { node *node = n; uint32_t i; - - UNUSED(pw); - + line_ctx *ctx = pw; + + *match = false; for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, - (const char *) name, len) == 0) - break; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + name, match) == lwc_error_ok); + if (*match == true) + break; } - if (i != node->n_attrs) - *match = true; - else - *match = false; - return CSS_OK; } css_error node_has_attribute_equal(void *pw, void *n, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, + lwc_string *name, + lwc_string *value, bool *match) { node *node = n; uint32_t i; + line_ctx *ctx = pw; - UNUSED(pw); - + *match = false; + for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, - (const char *) name, nlen) == 0) - break; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + name, match) == lwc_error_ok); + if (*match == true) + break; } - - /* Attribute values are (mostly) case insensitive */ - if (i != node->n_attrs && strncasecmp(node->attrs[i].value, - (const char *) value, vlen) == 0) - *match = true; - else - *match = false; - + + if (*match == true) { + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + value, match) == lwc_error_ok); + } + return CSS_OK; } -css_error node_has_attribute_dashmatch(void *pw, void *n, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, +css_error node_has_attribute_includes(void *pw, void *n, + lwc_string *name, + lwc_string *value, bool *match) { node *node = n; uint32_t i; + line_ctx *ctx = pw; + size_t vlen = lwc_string_length(value); - UNUSED(pw); - + *match = false; + for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, - (const char *) name, nlen) == 0) - break; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + name, match) == lwc_error_ok); + if (*match == true) + break; } - *match = false; - - if (i != node->n_attrs) { + if (*match == true) { const char *p; - const char *start = node->attrs[i].value; - const char *end = start + strlen(start) + 1; - - for (p = node->attrs[i].value; p < end; p++) { - if (*p == '-') { + const char *start = lwc_string_data(node->attrs[i].value); + const char *end = start + lwc_string_length(node->attrs[i].value); + + *match = false; + + for (p = start; p < end; p++) { + if (*p == ' ') { if ((size_t) (p - start) == vlen && strncasecmp(start, - (const char *) value, - vlen) == 0) { + lwc_string_data(value), + vlen) == 0) { *match = true; break; } @@ -899,35 +908,38 @@ css_error node_has_attribute_dashmatch(void *pw, void *n, return CSS_OK; } -css_error node_has_attribute_includes(void *pw, void *n, - const uint8_t *name, size_t nlen, - const uint8_t *value, size_t vlen, +css_error node_has_attribute_dashmatch(void *pw, void *n, + lwc_string *name, + lwc_string *value, bool *match) { node *node = n; uint32_t i; + line_ctx *ctx = pw; + size_t vlen = lwc_string_length(value); - UNUSED(pw); - + *match = false; + for (i = 0; i < node->n_attrs; i++) { - if (strncasecmp(node->attrs[i].name, - (const char *) name, nlen) == 0) - break; + assert(lwc_context_string_caseless_isequal(ctx->dict, node->attrs[i].name, + name, match) == lwc_error_ok); + if (*match == true) + break; } - *match = false; - - if (i != node->n_attrs) { + if (*match == true) { const char *p; - const char *start = node->attrs[i].value; - const char *end = start + strlen(start) + 1; - - for (p = node->attrs[i].value; p < end; p++) { - if (*p == ' ') { + const char *start = lwc_string_data(node->attrs[i].value); + const char *end = start + lwc_string_length(node->attrs[i].value); + + *match = false; + + for (p = start; p < end; p++) { + if (*p == '-') { if ((size_t) (p - start) == vlen && strncasecmp(start, - (const char *) value, - vlen) == 0) { + lwc_string_data(value), + vlen) == 0) { *match = true; break; } @@ -1012,7 +1024,7 @@ css_error node_is_focus(void *pw, void *n, bool *match) } css_error node_is_lang(void *pw, void *n, - const uint8_t *lang, size_t len, + lwc_string *lang, bool *match) { node *node = n; @@ -1020,7 +1032,6 @@ css_error node_is_lang(void *pw, void *n, UNUSED(pw); UNUSED(node); UNUSED(lang); - UNUSED(len); *match = false; -- cgit v1.2.3