From 6f97a18e538c3390252c9d51de3f2ae92aeff7ee Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 25 Jun 2024 18:36:22 -0400 Subject: Simplify token comparison in reader "Simplify" in terms of the operations actually performed. Since we only need to test for equality here, a simpler comparison with less branching will do. --- NEWS | 3 ++- src/n3.c | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index 798eb672..3e9c71d3 100644 --- a/NEWS +++ b/NEWS @@ -7,11 +7,12 @@ serd (0.32.3) unstable; urgency=medium * Fix rewriting special literals when datatype URIs are prefixed names * Gracefully handle errors while writing the end of anonymous nodes * Improve test suite coverage + * Simplify some character classification and comparison code * Support reading lone lists in lax mode * Treat out of range unicode characters as errors * Write blank lines between graphs and statements in TriG - -- David Robillard Mon, 24 Jun 2024 19:11:54 +0000 + -- David Robillard Tue, 25 Jun 2024 22:38:16 +0000 serd (0.32.2) stable; urgency=medium diff --git a/src/n3.c b/src/n3.c index 073f84d4..e5a06c77 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1534,19 +1534,25 @@ read_wrappedGraph(SerdReader* const reader, ReadContext* const ctx) return SERD_SUCCESS; } -static int -tokcmp(SerdReader* const reader, - const Ref ref, - const char* const tok, - const size_t n) +static bool +token_equals(SerdReader* const reader, + const Ref ref, + const char* const tok, + const size_t n) { - SerdNode* node = deref(reader, ref); + SerdNode* const node = deref(reader, ref); if (!node || node->n_bytes != n) { - return -1; + return false; + } + + const char* const node_string = (const char*)node->buf; + for (size_t i = 0U; i < n; ++i) { + if (serd_to_upper(node_string[i]) != serd_to_upper(tok[i])) { + return false; + } } - assert(node->buf[node->n_bytes] == '\0'); - return serd_strcasecmp((const char*)node->buf, tok); + return tok[n] == '\0'; } SerdStatus @@ -1588,11 +1594,11 @@ read_n3_statement(SerdReader* const reader) default: TRY_FAILING(st, read_subject(reader, ctx, &ctx.subject, &s_type)); - if (!tokcmp(reader, ctx.subject, "base", 4)) { + if (token_equals(reader, ctx.subject, "base", 4)) { st = read_base(reader, true, false); - } else if (!tokcmp(reader, ctx.subject, "prefix", 6)) { + } else if (token_equals(reader, ctx.subject, "prefix", 6)) { st = read_prefixID(reader, true, false); - } else if (!tokcmp(reader, ctx.subject, "graph", 5)) { + } else if (token_equals(reader, ctx.subject, "graph", 5)) { ctx.subject = pop_node(reader, ctx.subject); read_ws_star(reader); TRY(st, read_labelOrSubject(reader, &ctx.graph)); @@ -1601,8 +1607,8 @@ read_n3_statement(SerdReader* const reader) pop_node(reader, ctx.graph); ctx.graph = 0; read_ws_star(reader); - } else if (!tokcmp(reader, ctx.subject, "true", 4) || - !tokcmp(reader, ctx.subject, "false", 5)) { + } else if (token_equals(reader, ctx.subject, "true", 4) || + token_equals(reader, ctx.subject, "false", 5)) { return r_err(reader, SERD_ERR_BAD_SYNTAX, "expected subject\n"); } else if (read_ws_star(reader) && peek_byte(reader) == '{') { if (s_type == '(' || (s_type == '[' && !*ctx.flags)) { -- cgit v1.2.1