Add sexp_cmp

This commit is contained in:
Tony Garnock-Jones 2011-01-09 17:07:53 -05:00
parent 76cf11c69a
commit ce48a0fbea
2 changed files with 20 additions and 0 deletions

View File

@ -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);

View File

@ -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);