aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-12-20 13:02:24 -0500
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commit17b0fa6000573bd74a6968f5e4ff528c760f4de3 (patch)
treef411edee40a4085280267e626bf2d3b43070465c
parent80ee5b663f4b15617ad032941e626f77b289a070 (diff)
downloadserd-17b0fa6000573bd74a6968f5e4ff528c760f4de3.tar.gz
serd-17b0fa6000573bd74a6968f5e4ff528c760f4de3.tar.bz2
serd-17b0fa6000573bd74a6968f5e4ff528c760f4de3.zip
Add serd_node_compare()
-rw-r--r--serd/serd.h11
-rw-r--r--src/node.c19
2 files changed, 30 insertions, 0 deletions
diff --git a/serd/serd.h b/serd/serd.h
index d81fbd7c..fa308c49 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -835,6 +835,17 @@ bool
serd_node_equals(const SerdNode* a, const SerdNode* b);
/**
+ Compare two nodes.
+
+ Returns less than, equal to, or greater than zero if `a` is less than, equal
+ to, or greater than `b`, respectively. NULL is treated as less than any
+ other node.
+*/
+SERD_API
+int
+serd_node_compare(const SerdNode* a, const SerdNode* b);
+
+/**
@}
@name Event Handlers
@{
diff --git a/src/node.c b/src/node.c
index 98585178..e2f57a14 100644
--- a/src/node.c
+++ b/src/node.c
@@ -369,6 +369,25 @@ serd_node_equals(const SerdNode* a, const SerdNode* b)
return serd_node_total_size(b) == a_size && !memcmp(a, b, a_size);
}
+int
+serd_node_compare(const SerdNode* a, const SerdNode* b)
+{
+ if (a == b) {
+ return 0;
+ } else if (!a) {
+ return -1;
+ } else if (!b) {
+ return 1;
+ } else if (a->type != b->type) {
+ return (a->type < b->type) ? -1 : 1;
+ }
+
+ const int cmp = strcmp(serd_node_string(a), serd_node_string(b));
+ return cmp ? cmp
+ : serd_node_compare(serd_node_maybe_get_meta_c(a),
+ serd_node_maybe_get_meta_c(b));
+}
+
static size_t
serd_uri_string_length(const SerdURI* uri)
{