OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2002, 2003 The Karbon Developers | 2 * Copyright (C) 2002, 2003 The Karbon Developers |
3 * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org> | 3 * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org> |
4 * Copyright (C) 2006, 2007 Rob Buis <buis@kde.org> | 4 * Copyright (C) 2006, 2007 Rob Buis <buis@kde.org> |
5 * Copyright (C) 2007, 2009, 2013 Apple Inc. All rights reserved. | 5 * Copyright (C) 2007, 2009, 2013 Apple Inc. All rights reserved. |
6 * | 6 * |
7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
(...skipping 20 matching lines...) Expand all Loading... |
31 static inline bool isValidRange(const FloatType& x) | 31 static inline bool isValidRange(const FloatType& x) |
32 { | 32 { |
33 static const FloatType max = std::numeric_limits<FloatType>::max(); | 33 static const FloatType max = std::numeric_limits<FloatType>::max(); |
34 return x >= -max && x <= max; | 34 return x >= -max && x <= max; |
35 } | 35 } |
36 | 36 |
37 // We use this generic parseNumber function to allow the Path parsing code to wo
rk | 37 // We use this generic parseNumber function to allow the Path parsing code to wo
rk |
38 // at a higher precision internally, without any unnecessary runtime cost or cod
e | 38 // at a higher precision internally, without any unnecessary runtime cost or cod
e |
39 // complexity. | 39 // complexity. |
40 template <typename CharType, typename FloatType> | 40 template <typename CharType, typename FloatType> |
41 static bool genericParseNumber(const CharType*& ptr, const CharType* end, FloatT
ype& number, WhitespaceMode mode) | 41 static bool genericParseNumber(const CharType*& cursor, const CharType* end, Flo
atType& number, WhitespaceMode mode) |
42 { | 42 { |
43 FloatType integer, decimal, frac, exponent; | 43 FloatType integer, decimal, frac, exponent; |
44 int sign, expsign; | 44 int sign, expsign; |
45 | 45 |
46 exponent = 0; | 46 exponent = 0; |
47 integer = 0; | 47 integer = 0; |
48 frac = 1; | 48 frac = 1; |
49 decimal = 0; | 49 decimal = 0; |
50 sign = 1; | 50 sign = 1; |
51 expsign = 1; | 51 expsign = 1; |
52 | 52 |
53 if (mode & AllowLeadingWhitespace) | 53 if (mode & AllowLeadingWhitespace) |
54 skipOptionalSVGSpaces(ptr, end); | 54 skipOptionalSVGSpaces(cursor, end); |
55 | 55 |
| 56 const CharType* ptr = cursor; |
56 // read the sign | 57 // read the sign |
57 if (ptr < end && *ptr == '+') | 58 if (ptr < end && *ptr == '+') |
58 ptr++; | 59 ptr++; |
59 else if (ptr < end && *ptr == '-') { | 60 else if (ptr < end && *ptr == '-') { |
60 ptr++; | 61 ptr++; |
61 sign = -1; | 62 sign = -1; |
62 } | 63 } |
63 | 64 |
64 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) | 65 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) |
65 // The first character of a number must be one of [0-9+-.] | 66 // The first character of a number must be one of [0-9+-.] |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 number = integer + decimal; | 128 number = integer + decimal; |
128 number *= sign; | 129 number *= sign; |
129 | 130 |
130 if (exponent) | 131 if (exponent) |
131 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); | 132 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); |
132 | 133 |
133 // Don't return Infinity() or NaN(). | 134 // Don't return Infinity() or NaN(). |
134 if (!isValidRange(number)) | 135 if (!isValidRange(number)) |
135 return false; | 136 return false; |
136 | 137 |
| 138 // A valid number has been parsed. Commit cursor. |
| 139 cursor = ptr; |
| 140 |
137 if (mode & AllowTrailingWhitespace) | 141 if (mode & AllowTrailingWhitespace) |
138 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 142 skipOptionalSVGSpacesOrDelimiter(cursor, end); |
139 | 143 |
140 return true; | 144 return true; |
141 } | 145 } |
142 | 146 |
143 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM
ode mode) | 147 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM
ode mode) |
144 { | 148 { |
145 return genericParseNumber(ptr, end, number, mode); | 149 return genericParseNumber(ptr, end, number, mode); |
146 } | 150 } |
147 | 151 |
148 bool parseNumber(const UChar*& ptr, const UChar* end, float& number, WhitespaceM
ode mode) | 152 bool parseNumber(const UChar*& ptr, const UChar* end, float& number, WhitespaceM
ode mode) |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 const LChar* ptr = string.characters8(); | 244 const LChar* ptr = string.characters8(); |
241 const LChar* end = ptr + string.length(); | 245 const LChar* end = ptr + string.length(); |
242 return genericParseNumberOrPercentage(ptr, end, number); | 246 return genericParseNumberOrPercentage(ptr, end, number); |
243 } | 247 } |
244 const UChar* ptr = string.characters16(); | 248 const UChar* ptr = string.characters16(); |
245 const UChar* end = ptr + string.length(); | 249 const UChar* end = ptr + string.length(); |
246 return genericParseNumberOrPercentage(ptr, end, number); | 250 return genericParseNumberOrPercentage(ptr, end, number); |
247 } | 251 } |
248 | 252 |
249 } | 253 } |
OLD | NEW |