From 1e1fc363c507bbe5360979fd8319ede6ce531ab7 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 11 Nov 2018 15:29:59 +0100 Subject: Simplify reader stack pushing code --- src/reader.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/reader.h b/src/reader.h index 4edef344..506aaff2 100644 --- a/src/reader.h +++ b/src/reader.h @@ -145,27 +145,29 @@ push_byte(SerdReader* reader, SerdNode* node, const uint8_t c) { SERD_STACK_ASSERT_TOP(reader, node); uint8_t* const s = (uint8_t*)serd_stack_push(&reader->stack, 1); - if (!s) { - return SERD_ERR_OVERFLOW; + + if (s) { + ++node->n_bytes; + *(s - 1) = c; + *s = '\0'; } - ++node->n_bytes; - *(s - 1) = c; - *s = '\0'; - return SERD_SUCCESS; + return s ? SERD_SUCCESS : SERD_ERR_OVERFLOW; } static inline SerdStatus -push_bytes(SerdReader* reader, SerdNode* ref, const uint8_t* bytes, unsigned len) +push_bytes(SerdReader* reader, + SerdNode* ref, + const uint8_t* bytes, + unsigned len) { - if (reader->stack.buf_size < reader->stack.size + len) { - return SERD_ERR_OVERFLOW; - } - - for (unsigned i = 0; i < len; ++i) { - push_byte(reader, ref, bytes[i]); + const bool has_space = reader->stack.buf_size >= reader->stack.size + len; + if (has_space) { + for (unsigned i = 0; i < len; ++i) { + push_byte(reader, ref, bytes[i]); + } } - return SERD_SUCCESS; + return has_space ? SERD_SUCCESS : SERD_ERR_OVERFLOW; } #endif // SERD_READER_H -- cgit v1.2.1