From ce48a0fbea5673d1eec5d758341c20f8e66887c0 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 9 Jan 2011 17:07:53 -0500 Subject: [PATCH] Add sexp_cmp --- server/sexp.c | 19 +++++++++++++++++++ server/sexp.h | 1 + 2 files changed, 20 insertions(+) diff --git a/server/sexp.c b/server/sexp.c index 9e62490..9da78a4 100644 --- a/server/sexp.c +++ b/server/sexp.c @@ -155,6 +155,25 @@ cmsg_bytes_t sexp_data(sexp_t *x) { } } +int sexp_cmp(sexp_t *a, sexp_t *b) { + tail: + if (a == b) return 0; + if (sexp_stringp(a) && sexp_stringp(b)) { + return cmsg_bytes_cmp(sexp_data(a), sexp_data(b)); + } + if (sexp_pairp(a) && sexp_pairp(b)) { + int result = sexp_cmp(sexp_head(a), sexp_head(b)); + if (result) return result; + a = sexp_tail(a); + b = sexp_tail(b); + goto tail; + } + if (a == NULL) return -1; + if (b == NULL) return 1; + if (a->kind < b->kind) return -1; + return 1; +} + sexp_t *sexp_assoc(sexp_t *list, cmsg_bytes_t key) { while (list != NULL) { sexp_t *candidate = sexp_head(list); diff --git a/server/sexp.h b/server/sexp.h index 1ed5f8d..2c9058f 100644 --- a/server/sexp.h +++ b/server/sexp.h @@ -65,6 +65,7 @@ static inline int sexp_pairp(sexp_t *x) { } extern cmsg_bytes_t sexp_data(sexp_t *x); +extern int sexp_cmp(sexp_t *a, sexp_t *b); static inline sexp_t *sexp_head(sexp_t *x) { assert(x->kind == SEXP_PAIR);