diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2008-10-26 23:58:06 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2008-10-26 23:58:06 +0000 |
commit | 3d37c5ab719bf7038e242f8b542dee6bb1908831 (patch) | |
tree | 25dfdfc8a98017b99b4de186389636dc7be0145f /src/utils/utils.h | |
parent | 6289f4216fe8fada44cf57c0fb939029ca720ae9 (diff) | |
download | libcss-3d37c5ab719bf7038e242f8b542dee6bb1908831.tar.gz libcss-3d37c5ab719bf7038e242f8b542dee6bb1908831.tar.bz2 |
Function to extract an integer from a css_string.
Use this in font-weight parsing.
TODO: NUMBER tokens may contain [-+]? ([0-9]+ | [0-9]* '.' [0-9]+). The lexer currently doesn't cater for the [-+]? part. This is a bug (inherited from the grammar in the spec), and must be fixed.
TODO: integer_from_css_string probably wants to pass out the number of characters processed so that the client can determine if the input was valid for their context.
svn path=/trunk/libcss/; revision=5645
Diffstat (limited to 'src/utils/utils.h')
-rw-r--r-- | src/utils/utils.h | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/utils/utils.h b/src/utils/utils.h index ac19e59..85a4f9e 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -2,12 +2,14 @@ * This file is part of LibCSS. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php - * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org> + * Copyright 2007-8 John-Mark Bell <jmb@netsurf-browser.org> */ #ifndef css_utils_h_ #define css_utils_h_ +#include <libcss/types.h> + #ifndef max #define max(a,b) ((a)>(b)?(a):(b)) #endif @@ -25,4 +27,44 @@ #define UNUSED(x) ((x)=(x)) #endif +static inline int32_t integer_from_css_string(const css_string *string) +{ + size_t len; + const uint8_t *ptr; + int sign = 1; + int32_t val = 0; + + if (string == NULL || string->len == 0) + return 0; + + len = string->len; + ptr = string->ptr; + + /* Extract sign, if any */ + if (ptr[0] == '-') { + sign = -1; + len--; + ptr++; + } else if (ptr[0] == '+') { + len--; + ptr++; + } + + /** \todo check for overflow */ + + /* Now extract value, assuming base 10 */ + while (len > 0) { + /* Stop on first non-digit */ + if (ptr[0] <= '0' || '9' <= ptr[0]) + break; + + val *= 10; + val += ptr[0] - '0'; + ptr++; + len--; + } + + return val * sign; +} + #endif |