From 47f30703ee6935de393303b7a4d6bab8ce7780f4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 24 Jun 2024 15:01:11 -0400 Subject: Replace questionable switch statements with shorter conditionals --- src/byte_source.h | 6 ++---- src/env.c | 14 +++----------- src/n3.c | 45 ++++++++++++--------------------------------- src/string.c | 19 ++++--------------- src/string_utils.h | 12 +----------- src/uri_utils.h | 11 ++--------- src/writer.c | 20 ++++++-------------- 7 files changed, 30 insertions(+), 97 deletions(-) diff --git a/src/byte_source.h b/src/byte_source.h index 3eafe4b8..02aab663 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -69,12 +69,10 @@ serd_byte_source_advance(SerdByteSource* source) { SerdStatus st = SERD_SUCCESS; - switch (serd_byte_source_peek(source)) { - case '\n': + if (serd_byte_source_peek(source) == '\n') { ++source->cur.line; source->cur.col = 0; - break; - default: + } else { ++source->cur.col; } diff --git a/src/env.c b/src/env.c index 33077c0c..75ae6485 100644 --- a/src/env.c +++ b/src/env.c @@ -250,22 +250,18 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) return SERD_NODE_NULL; } - switch (node->type) { - case SERD_NOTHING: - case SERD_LITERAL: - break; - - case SERD_URI: { + if (node->type == SERD_URI) { SerdURI ignored; return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); } - case SERD_CURIE: { + if (node->type == SERD_CURIE) { SerdChunk prefix; SerdChunk suffix; if (serd_env_expand(env, node, &prefix, &suffix)) { return SERD_NODE_NULL; } + const size_t len = prefix.len + suffix.len; uint8_t* buf = (uint8_t*)malloc(len + 1); SerdNode ret = {buf, len, 0, 0, SERD_URI}; @@ -274,10 +270,6 @@ serd_env_expand_node(const SerdEnv* const env, const SerdNode* const node) return ret; } - case SERD_BLANK: - break; - } - return SERD_NODE_NULL; } diff --git a/src/n3.c b/src/n3.c index a3cb93a6..3c63cffb 100644 --- a/src/n3.c +++ b/src/n3.c @@ -571,14 +571,10 @@ static SerdStatus read_PLX(SerdReader* const reader, const Ref dest) { const int c = peek_byte(reader); - switch (c) { - case '%': - return read_PERCENT(reader, dest); - case '\\': - return read_PN_LOCAL_ESC(reader, dest); - default: - return SERD_FAILURE; - } + + return (c == '%') ? read_PERCENT(reader, dest) + : (c == '\\') ? read_PN_LOCAL_ESC(reader, dest) + : SERD_FAILURE; } static SerdStatus @@ -753,18 +749,12 @@ read_IRIREF(SerdReader* const reader, Ref* const dest) return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid IRI escape\n"); } - switch (code) { - case 0: - case ' ': - case '<': - case '>': + if (code == ' ' || code == '<' || code == '>') { *dest = pop_node(reader, *dest); return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid escaped IRI character U+%04X\n", code); - default: - break; } break; @@ -886,13 +876,9 @@ read_number(SerdReader* const reader, if (c == 'e' || c == 'E') { // double push_byte(reader, *dest, eat_byte_safe(reader, c)); - switch ((c = peek_byte(reader))) { - case '+': - case '-': + c = peek_byte(reader); + if (c == '+' || c == '-') { push_byte(reader, *dest, eat_byte_safe(reader, c)); - break; - default: - break; } TRY(st, read_0_9(reader, *dest, true)); *datatype = push_node(reader, SERD_URI, XSD_DOUBLE, sizeof(XSD_DOUBLE) - 1); @@ -910,13 +896,12 @@ read_number(SerdReader* const reader, static SerdStatus read_iri(SerdReader* const reader, Ref* const dest, bool* const ate_dot) { - switch (peek_byte(reader)) { - case '<': + if (peek_byte(reader) == '<') { return read_IRIREF(reader, dest); - default: - *dest = push_node(reader, SERD_CURIE, "", 0); - return read_PrefixedName(reader, *dest, true, ate_dot); } + + *dest = push_node(reader, SERD_CURIE, "", 0); + return read_PrefixedName(reader, *dest, true, ate_dot); } static SerdStatus @@ -1152,13 +1137,7 @@ read_object(SerdReader* const reader, uint32_t flags = 0; const int c = peek_byte(reader); if (!fancy_syntax(reader)) { - switch (c) { - case '"': - case ':': - case '<': - case '_': - break; - default: + if (c != '"' && c != ':' && c != '<' && c != '_') { return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected: ':', '<', or '_'\n"); } } diff --git a/src/string.c b/src/string.c index b2946aac..11c12bee 100644 --- a/src/string.c +++ b/src/string.c @@ -49,16 +49,10 @@ serd_strerror(const SerdStatus status) static void serd_update_flags(const uint8_t c, SerdNodeFlags* const flags) { - switch (c) { - case '\r': - case '\n': + if (c == '\r' || c == '\n') { *flags |= SERD_HAS_NEWLINE; - break; - case '"': + } else if (c == '"') { *flags |= SERD_HAS_QUOTE; - break; - default: - break; } } @@ -116,16 +110,11 @@ read_sign(const char** const sptr) { double sign = 1.0; - switch (**sptr) { - case '-': + if (**sptr == '-') { sign = -1.0; ++(*sptr); - break; - case '+': + } else if (**sptr == '+') { ++(*sptr); - break; - default: - break; } return sign; diff --git a/src/string_utils.h b/src/string_utils.h index 4babee63..2ce90ac9 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -51,17 +51,7 @@ is_xdigit(const int c) static inline bool is_space(const char c) { - switch (c) { - case ' ': - case '\f': - case '\n': - case '\r': - case '\t': - case '\v': - return true; - default: - return false; - } + return c == ' ' || (c >= '\t' && c <= '\r'); } static inline bool diff --git a/src/uri_utils.h b/src/uri_utils.h index e2f30edb..0d3bd74e 100644 --- a/src/uri_utils.h +++ b/src/uri_utils.h @@ -101,15 +101,8 @@ uri_is_under(const SerdURI* uri, const SerdURI* root) static inline bool is_uri_scheme_char(const int c) { - switch (c) { - case ':': - case '+': - case '-': - case '.': - return true; - default: - return is_alpha(c) || is_digit(c); - } + return c == '+' || c == '-' || c == '.' || c == ':' || is_alpha(c) || + is_digit(c); } #endif // SERD_SRC_URI_UTILS_H diff --git a/src/writer.c b/src/writer.c index c0f1a4a7..1d3eed4d 100644 --- a/src/writer.c +++ b/src/writer.c @@ -859,20 +859,12 @@ write_node(SerdWriter* writer, Field field, SerdStatementFlags flags) { - switch (node->type) { - case SERD_NOTHING: - break; - case SERD_LITERAL: - return write_literal(writer, node, datatype, lang, flags); - case SERD_URI: - return write_uri_node(writer, node, field); - case SERD_CURIE: - return write_curie(writer, node); - case SERD_BLANK: - return write_blank(writer, node, field, flags); - } - - return SERD_SUCCESS; + return (node->type == SERD_LITERAL) + ? write_literal(writer, node, datatype, lang, flags) + : (node->type == SERD_URI) ? write_uri_node(writer, node, field) + : (node->type == SERD_CURIE) ? write_curie(writer, node) + : (node->type == SERD_BLANK) ? write_blank(writer, node, field, flags) + : SERD_SUCCESS; } static bool -- cgit v1.2.1