From ebcef72577398fa10a6ad6545317f704bcf9a1c4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 23 Jan 2011 09:16:38 +0000 Subject: Rearrange code, put common internal stuff in serd_internal.h. git-svn-id: http://svn.drobilla.net/serd/trunk@46 490d8e77-9747-427b-9fa3-0b8f29cee8a0 --- src/reader.c | 22 ++++-------- src/serd_internal.h | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/serd_stack.h | 79 ---------------------------------------- src/uri.c | 23 +----------- src/writer.c | 3 +- 5 files changed, 110 insertions(+), 118 deletions(-) create mode 100644 src/serd_internal.h delete mode 100644 src/serd_stack.h (limited to 'src') diff --git a/src/reader.c b/src/reader.c index 9667abf5..8bae4c03 100644 --- a/src/reader.c +++ b/src/reader.c @@ -23,8 +23,7 @@ #include #include -#include "serd/serd.h" -#include "serd_stack.h" +#include "serd_internal.h" #define NS_XSD "http://www.w3.org/2001/XMLSchema#" #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -185,12 +184,6 @@ eat_string(SerdReader reader, const char* str, unsigned n) } } -static inline bool -in_range(const uchar c, const uchar min, const uchar max) -{ - return (c >= min && c <= max); -} - #ifdef STACK_DEBUG static inline bool stack_is_top_string(SerdReader reader, Ref ref) @@ -687,7 +680,7 @@ static inline uchar read_nameStartChar(SerdReader reader, bool required) { const uint8_t c = peek_byte(reader); - if (in_range(c, 'A', 'Z') || (c == '_') || in_range(c, 'a', 'z')) { + if (c == '_' || is_alpha(c)) { return eat_byte(reader, c); } else { if (required) { @@ -711,9 +704,8 @@ read_nameChar(SerdReader reader) case '5': case '6': case '7': case '8': case '9': return eat_byte(reader, c); default: - if (in_range(c, 0x300, 0x036F) || in_range(c, 0x203F, 0x2040)) { - return eat_byte(reader, c); - } + // TODO: 0x300-0x036F | 0x203F-0x2040 + return 0; } return 0; } @@ -814,10 +806,10 @@ read_0_9(SerdReader reader, Ref str, bool at_least_one) { uint8_t c; if (at_least_one) { - TRY_RET(in_range(c = peek_byte(reader), '0', '9')); + TRY_RET(is_digit((c = peek_byte(reader)))); push_byte(reader, str, eat_byte(reader, c)); } - while (in_range((c = peek_byte(reader)), '0', '9')) { + while (is_digit((c = peek_byte(reader)))) { push_byte(reader, str, eat_byte(reader, c)); } return str; @@ -900,7 +892,7 @@ read_literal(SerdReader reader, Node* dest) Ref str = 0; Node datatype = SERD_NODE_NULL; const uint8_t c = peek_byte(reader); - if (in_range(c, '0', '9') || c == '-' || c == '+') { + if (c == '-' || c == '+' || is_digit(c)) { return read_number(reader, dest); } else if (c == '\"') { str = read_quotedString(reader); diff --git a/src/serd_internal.h b/src/serd_internal.h new file mode 100644 index 00000000..bb3f3910 --- /dev/null +++ b/src/serd_internal.h @@ -0,0 +1,101 @@ +/* Serd, an RDF serialisation library. + * Copyright 2011 David Robillard + * + * Serd is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Serd is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#ifndef SERD_INTERNAL_H +#define SERD_INTERNAL_H + +#include + +#include "serd/serd.h" + +/** A dynamic stack in memory. */ +typedef struct { + uint8_t* buf; ///< Stack memory + size_t buf_size; ///< Allocated size of buf (>= size) + size_t size; ///< Conceptual size of stack in buf +} SerdStack; + +/** An offset to start the stack at. Note 0 is reserved for NULL. */ +#define SERD_STACK_BOTTOM sizeof(void*) + +static inline SerdStack +serd_stack_new(size_t size) +{ + SerdStack stack; + stack.buf = malloc(size); + stack.buf_size = size; + stack.size = SERD_STACK_BOTTOM; + return stack; +} + +static inline bool +serd_stack_is_empty(SerdStack* stack) +{ + return stack->size <= SERD_STACK_BOTTOM; +} + +static inline void +serd_stack_free(SerdStack* stack) +{ + free(stack->buf); + stack->buf = NULL; + stack->buf_size = 0; + stack->size = 0; +} + +static inline uint8_t* +serd_stack_push(SerdStack* stack, size_t n_bytes) +{ + const size_t new_size = stack->size + n_bytes; + if (stack->buf_size < new_size) { + stack->buf_size *= 2; + stack->buf = realloc(stack->buf, stack->buf_size); + } + uint8_t* const ret = (stack->buf + stack->size); + stack->size = new_size; + return ret; +} + +static inline void +serd_stack_pop(SerdStack* stack, size_t n_bytes) +{ + assert(stack->size >= n_bytes); + stack->size -= n_bytes; +} + +/** Return true if @a c lies within [min...max] (inclusive) */ +static inline bool +in_range(const uint8_t c, const uint8_t min, const uint8_t max) +{ + return (c >= min && c <= max); +} + +/** RFC2234: ALPHA := %x41-5A / %x61-7A ; A-Z / a-z */ +static inline bool +is_alpha(const uint8_t c) +{ + return in_range(c, 'A', 'Z') || in_range(c, 'a', 'z'); +} + +/** RFC2234: DIGIT ::= %x30-39 ; 0-9 */ +static inline bool +is_digit(const uint8_t c) +{ + return in_range(c, '0', '9'); +} + +#endif // SERD_INTERNAL_H diff --git a/src/serd_stack.h b/src/serd_stack.h deleted file mode 100644 index 7d8bcd07..00000000 --- a/src/serd_stack.h +++ /dev/null @@ -1,79 +0,0 @@ -/* Serd, an RDF serialisation library. - * Copyright 2011 David Robillard - * - * Serd is free software: you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Serd is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -#ifndef SERD_STACK_H -#define SERD_STACK_H - -#include - -#include "serd/serd.h" - -/** An offset to start the stack at. Note 0 is reserved for NULL. */ -#define SERD_STACK_BOTTOM sizeof(void*) - -typedef struct { - uint8_t* buf; ///< Stack memory - size_t buf_size; ///< Allocated size of buf (>= size) - size_t size; ///< Conceptual size of stack in buf -} SerdStack; - -static inline SerdStack -serd_stack_new(size_t size) -{ - SerdStack stack; - stack.buf = malloc(size); - stack.buf_size = size; - stack.size = SERD_STACK_BOTTOM; - return stack; -} - -static inline bool -serd_stack_is_empty(SerdStack* stack) -{ - return stack->size <= SERD_STACK_BOTTOM; -} - -static inline void -serd_stack_free(SerdStack* stack) -{ - free(stack->buf); - stack->buf = NULL; - stack->buf_size = 0; - stack->size = 0; -} - -static inline uint8_t* -serd_stack_push(SerdStack* stack, size_t n_bytes) -{ - const size_t new_size = stack->size + n_bytes; - if (stack->buf_size < new_size) { - stack->buf_size *= 2; - stack->buf = realloc(stack->buf, stack->buf_size); - } - uint8_t* const ret = (stack->buf + stack->size); - stack->size = new_size; - return ret; -} - -static inline void -serd_stack_pop(SerdStack* stack, size_t n_bytes) -{ - assert(stack->size >= n_bytes); - stack->size -= n_bytes; -} - -#endif // SERD_STACK_H diff --git a/src/uri.c b/src/uri.c index c738724e..5b2e4423 100644 --- a/src/uri.c +++ b/src/uri.c @@ -19,31 +19,10 @@ #include #include -#include "serd/serd.h" +#include "serd_internal.h" // #define URI_DEBUG 1 -/** Return true if @a c lies within [min...max] (inclusive) */ -static inline bool -in_range(const char c, const char min, const char max) -{ - return (c >= min && c <= max); -} - -/** RFC2234: ALPHA := %x41-5A / %x61-7A ; A-Z / a-z */ -static inline bool -is_alpha(const uint8_t c) -{ - return in_range(c, 'A', 'Z') || in_range(c, 'a', 'z'); -} - -/** RFC2234: DIGIT ::= %x30-39 ; 0-9 */ -static inline bool -is_digit(const uint8_t c) -{ - return in_range(c, '0', '9'); -} - SERD_API bool serd_uri_string_has_scheme(const uint8_t* utf8) diff --git a/src/writer.c b/src/writer.c index 93a2e4d1..94f91f49 100644 --- a/src/writer.c +++ b/src/writer.c @@ -19,8 +19,7 @@ #include #include -#include "serd/serd.h" -#include "serd_stack.h" +#include "serd_internal.h" typedef struct { const SerdString* graph; -- cgit v1.2.1