From d299bd0ab5f2d1ede4266febe5728a0f4075b961 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 4 Jan 2016 16:56:26 +0100 Subject: epub: Speed up CSS style application by using faster strtod. CSS doesn't need precision, and doesn't support exponential notation. Use a simpler string to float conversion algorithm, which is magnitudes of order faster. --- source/html/css-apply.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'source/html/css-apply.c') diff --git a/source/html/css-apply.c b/source/html/css-apply.c index acb19244..40359d28 100644 --- a/source/html/css-apply.c +++ b/source/html/css-apply.c @@ -691,6 +691,45 @@ make_number(float v, int u) return n; } +/* Fast but inaccurate strtof. */ +static float +fz_css_strtof(char *s, char **endptr) +{ + float sign = 1; + float v = 0; + float n = 0; + float d = 1; + + if (*s == '-') + { + sign = -1; + ++s; + } + + while (*s >= '0' && *s <= '9') + { + v = v * 10 + (*s - '0'); + ++s; + } + + if (*s == '.') + { + ++s; + while (*s >= '0' && *s <= '9') + { + n = n * 10 + (*s - '0'); + d = d * 10; + ++s; + } + v += n / d; + } + + if (endptr) + *endptr = s; + + return sign * v; +} + static fz_css_number number_from_value(fz_css_value *value, float initial, int initial_unit) { @@ -700,14 +739,14 @@ number_from_value(fz_css_value *value, float initial, int initial_unit) return make_number(initial, initial_unit); if (value->type == CSS_PERCENT) - return make_number((float)fz_strtod(value->data, NULL), N_PERCENT); + return make_number(fz_css_strtof(value->data, NULL), N_PERCENT); if (value->type == CSS_NUMBER) - return make_number((float)fz_strtod(value->data, NULL), N_NUMBER); + return make_number(fz_css_strtof(value->data, NULL), N_NUMBER); if (value->type == CSS_LENGTH) { - float x = (float)fz_strtod(value->data, &p); + float x = fz_css_strtof(value->data, &p); if (p[0] == 'e' && p[1] == 'm') return make_number(x, N_SCALE); -- cgit v1.2.3