From a135e87d96bcf33c27f633f0ecfc5d158a4e0571 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 6 Jan 2016 11:55:33 +0100 Subject: epub: Optimize CSS selector matching. Remove strcpy and strtok calls when matching class conditions. --- source/html/css-apply.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/html/css-apply.c b/source/html/css-apply.c index 40359d28..345a307f 100644 --- a/source/html/css-apply.c +++ b/source/html/css-apply.c @@ -217,15 +217,18 @@ static int match_class_condition(fz_xml *node, const char *p) { const char *s = fz_xml_att(node, "class"); - char buf[1024]; + const char *ss; + int n; if (s) { - strcpy(buf, s); - s = strtok(buf, " "); - while (s) { - if (!strcmp(s, p)) - return 1; - s = strtok(NULL, " "); - } + /* Try matching whole property first. */ + if (!strcmp(s, p)) + return 1; + + /* Look for matching words. */ + n = strlen(p); + ss = strstr(s, p); + if (ss && (ss[n] == ' ' || ss[n] == 0) && (ss == s || ss[-1] == ' ')) + return 1; } return 0; } -- cgit v1.2.3