From 4d615b9e01880e4a97437d0351a4855e2d6c3ab2 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Fri, 24 Oct 2008 14:20:37 -0200 Subject: [PATCH] strings: Add strings__find and strings__cmp Signed-off-by: Arnaldo Carvalho de Melo --- strings.c | 21 +++++++++++++++++++++ strings.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/strings.c b/strings.c index 40c52aa..e5a254d 100644 --- a/strings.c +++ b/strings.c @@ -88,3 +88,24 @@ strings_t strings__add(struct strings *self, const char *str) return index; } + +strings_t strings__find(struct strings *self, const char *str) +{ + strings_t *s; + struct search_key key = { + .self = self, + .str = str, + }; + + if (str == NULL) + return 0; + + s = tfind(&key, &self->tree, strings__compare); + return s ? *s : 0; +} + +int strings__cmp(const struct strings *self, strings_t a, strings_t b) +{ + return a == b ? 0 : strcmp(strings__ptr(self, a), + strings__ptr(self, b)); +} diff --git a/strings.h b/strings.h index 460a013..5e4860c 100644 --- a/strings.h +++ b/strings.h @@ -22,6 +22,9 @@ struct strings *strings__new(void); void strings__delete(struct strings *self); strings_t strings__add(struct strings *self, const char *str); +strings_t strings__find(struct strings *self, const char *str); + +int strings__cmp(const struct strings *self, strings_t a, strings_t b); static inline const char *strings__ptr(const struct strings *self, strings_t s) {