commit 3031d855807de20fc86cd3dd2375ce9473e86947
parent 9d1b0b33555da8c2587fc351f3e75c903c1fe9e2
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 6 Apr 2019 14:27:40 +0200
sfeed: gettag: simplify and use ANSI bsearch()
Diffstat:
M | sfeed.c | | | 30 | ++++++++++++++---------------- |
1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/sfeed.c b/sfeed.c
@@ -181,36 +181,34 @@ static String atomlink;
static enum TagId atomlinktype;
static int rssidpermalink;
+int
+tagcmp(const void *v1, const void *v2)
+{
+ return strcasecmp(((FeedTag *)v1)->name, ((FeedTag *)v2)->name);
+}
+
/* Unique tagid for parsed tag name. */
static enum TagId
gettag(enum FeedType feedtype, const char *name, size_t namelen)
{
- size_t i;
+ FeedTag f, *r = NULL;
+
+ f.name = (char *)name;
switch (feedtype) {
case FeedTypeRSS:
- /* optimization: these are always non-matching */
- if (namelen < 4 || namelen > 17)
- return TagUnknown;
-
- for (i = 0; i < sizeof(rsstags) / sizeof(rsstags[0]); i++)
- if (istag(rsstags[i].name, rsstags[i].len, name, namelen))
- return rsstags[i].id;
+ r = bsearch(&f, rsstags, sizeof(rsstags) / sizeof(rsstags[0]),
+ sizeof(rsstags[0]), tagcmp);
break;
case FeedTypeAtom:
- /* optimization: these are always non-matching */
- if (namelen < 2 || namelen > 17)
- return TagUnknown;
-
- for (i = 0; i < sizeof(atomtags) / sizeof(atomtags[0]); i++)
- if (istag(atomtags[i].name, atomtags[i].len, name, namelen))
- return atomtags[i].id;
+ r = bsearch(&f, atomtags, sizeof(atomtags) / sizeof(atomtags[0]),
+ sizeof(atomtags[0]), tagcmp);
break;
default:
break;
}
- return TagUnknown;
+ return r ? r->id : TagUnknown;
}
static char *