aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-11-11 15:29:59 +0100
committerDavid Robillard <d@drobilla.net>2018-11-25 22:12:47 +0100
commitfd021f96b68c338c3cb649c69b03f48a8f74febf (patch)
tree671288bb06e923663c6cb7f067a27a35f7ab05dd
parent27249b15a561221ae693a6f26e817eb65d19404d (diff)
downloadserd-fd021f96b68c338c3cb649c69b03f48a8f74febf.tar.gz
serd-fd021f96b68c338c3cb649c69b03f48a8f74febf.tar.bz2
serd-fd021f96b68c338c3cb649c69b03f48a8f74febf.zip
Simplify reader stack pushing code
-rw-r--r--src/reader.h30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/reader.h b/src/reader.h
index f00ecd2d..138c59b0 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -143,27 +143,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