aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-06-25 18:36:22 -0400
committerDavid Robillard <d@drobilla.net>2024-06-25 18:38:25 -0400
commit6f97a18e538c3390252c9d51de3f2ae92aeff7ee (patch)
treee74d96f549c114e380adefe3586a115a3a02aa5b
parentd0c6099462d40a63399d02c2a84237d7fd2f89f8 (diff)
downloadserd-6f97a18e538c3390252c9d51de3f2ae92aeff7ee.tar.gz
serd-6f97a18e538c3390252c9d51de3f2ae92aeff7ee.tar.bz2
serd-6f97a18e538c3390252c9d51de3f2ae92aeff7ee.zip
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.
-rw-r--r--NEWS3
-rw-r--r--src/n3.c34
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 <d@drobilla.net> Mon, 24 Jun 2024 19:11:54 +0000
+ -- David Robillard <d@drobilla.net> 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)) {