diff options
author | Nick Wellnhofer <wellnhofer@aevum.de> | 2021-03-04 17:44:45 +0100 |
---|---|---|
committer | Nick Wellnhofer <wellnhofer@aevum.de> | 2021-03-04 17:44:45 +0100 |
commit | b25acce858d4eea49b02b5d4e32708b914107b29 (patch) | |
tree | 2dd89c222aced1eba9d9511967f3d37cce881c6e | |
parent | ad101bb5b59c48a04ced94eec918896be47c1861 (diff) | |
download | libxml2-b25acce858d4eea49b02b5d4e32708b914107b29.tar.gz |
Speed up htmlTagLookup
Switch to binary search. This is the first time bsearch is used in the
libxml2 code base. But it's a standard library function since C89 and
should be portable.
-rw-r--r-- | HTMLparser.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/HTMLparser.c b/HTMLparser.c index c9a64c78..376fbd71 100644 --- a/HTMLparser.c +++ b/HTMLparser.c @@ -1269,6 +1269,14 @@ htmlInitAutoClose(void) { htmlStartCloseIndexinitialized = 1; } +static int +htmlCompareTags(const void *key, const void *member) { + const char *tag = (const char *) key; + const htmlElemDesc *desc = (const htmlElemDesc *) member; + + return(strcmp(tag, desc->name)); +} + /** * htmlTagLookup: * @tag: The tag name in lowercase @@ -1279,14 +1287,12 @@ htmlInitAutoClose(void) { */ const htmlElemDesc * htmlTagLookup(const xmlChar *tag) { - unsigned int i; + if (tag == NULL) + return(NULL); - for (i = 0; i < (sizeof(html40ElementTable) / - sizeof(html40ElementTable[0]));i++) { - if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name)) - return((htmlElemDescPtr) &html40ElementTable[i]); - } - return(NULL); + return((const htmlElemDesc *) bsearch(tag, html40ElementTable, + sizeof(html40ElementTable) / sizeof(htmlElemDesc), + sizeof(htmlElemDesc), htmlCompareTags)); } /** |