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 24 matching lines...) Expand all Loading... |
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*& ptr, const CharType* end, FloatT
ype& 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 const CharType* start = ptr; | |
46 | 45 |
47 exponent = 0; | 46 exponent = 0; |
48 integer = 0; | 47 integer = 0; |
49 frac = 1; | 48 frac = 1; |
50 decimal = 0; | 49 decimal = 0; |
51 sign = 1; | 50 sign = 1; |
52 expsign = 1; | 51 expsign = 1; |
53 | 52 |
54 if (mode & AllowLeadingWhitespace) | 53 if (mode & AllowLeadingWhitespace) |
55 skipOptionalSVGSpaces(ptr, end); | 54 skipOptionalSVGSpaces(ptr, end); |
56 | 55 |
57 // read the sign | 56 // read the sign |
58 if (ptr < end && *ptr == '+') | 57 if (ptr < end && *ptr == '+') |
59 ptr++; | 58 ptr++; |
60 else if (ptr < end && *ptr == '-') { | 59 else if (ptr < end && *ptr == '-') { |
61 ptr++; | 60 ptr++; |
62 sign = -1; | 61 sign = -1; |
63 } | 62 } |
64 | 63 |
65 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) | 64 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) |
66 // The first character of a number must be one of [0-9+-.] | 65 // The first character of a number must be one of [0-9+-.] |
67 return false; | 66 return false; |
68 | 67 |
69 // read the integer part, build right-to-left | 68 // read the integer part, build right-to-left |
70 const CharType* ptrStartIntPart = ptr; | 69 const CharType* digitsStart = ptr; |
71 while (ptr < end && *ptr >= '0' && *ptr <= '9') | 70 while (ptr < end && *ptr >= '0' && *ptr <= '9') |
72 ++ptr; // Advance to first non-digit. | 71 ++ptr; // Advance to first non-digit. |
73 | 72 |
74 if (ptr != ptrStartIntPart) { | 73 if (ptr != digitsStart) { |
75 const CharType* ptrScanIntPart = ptr - 1; | 74 const CharType* ptrScanIntPart = ptr - 1; |
76 FloatType multiplier = 1; | 75 FloatType multiplier = 1; |
77 while (ptrScanIntPart >= ptrStartIntPart) { | 76 while (ptrScanIntPart >= digitsStart) { |
78 integer += multiplier * static_cast<FloatType>(*(ptrScanIntPart--) -
'0'); | 77 integer += multiplier * static_cast<FloatType>(*(ptrScanIntPart--) -
'0'); |
79 multiplier *= 10; | 78 multiplier *= 10; |
80 } | 79 } |
81 // Bail out early if this overflows. | 80 // Bail out early if this overflows. |
82 if (!isValidRange(integer)) | 81 if (!isValidRange(integer)) |
83 return false; | 82 return false; |
84 } | 83 } |
85 | 84 |
86 if (ptr < end && *ptr == '.') { // read the decimals | 85 if (ptr < end && *ptr == '.') { // read the decimals |
87 ptr++; | 86 ptr++; |
88 | 87 |
89 // There must be a least one digit following the . | 88 // There must be a least one digit following the . |
90 if (ptr >= end || *ptr < '0' || *ptr > '9') | 89 if (ptr >= end || *ptr < '0' || *ptr > '9') |
91 return false; | 90 return false; |
92 | 91 |
93 while (ptr < end && *ptr >= '0' && *ptr <= '9') | 92 while (ptr < end && *ptr >= '0' && *ptr <= '9') |
94 decimal += (*(ptr++) - '0') * (frac *= static_cast<FloatType>(0.1)); | 93 decimal += (*(ptr++) - '0') * (frac *= static_cast<FloatType>(0.1)); |
95 } | 94 } |
96 | 95 |
| 96 // When we get here we should have consumed either a digit for the integer |
| 97 // part or a fractional part (with at least one digit after the '.'.) |
| 98 ASSERT(digitsStart != ptr); |
| 99 |
97 // read the exponent part | 100 // read the exponent part |
98 if (ptr != start && ptr + 1 < end && (*ptr == 'e' || *ptr == 'E') | 101 if (ptr + 1 < end && (*ptr == 'e' || *ptr == 'E') |
99 && (ptr[1] != 'x' && ptr[1] != 'm')) { | 102 && (ptr[1] != 'x' && ptr[1] != 'm')) { |
100 ptr++; | 103 ptr++; |
101 | 104 |
102 // read the sign of the exponent | 105 // read the sign of the exponent |
103 if (*ptr == '+') | 106 if (*ptr == '+') |
104 ptr++; | 107 ptr++; |
105 else if (*ptr == '-') { | 108 else if (*ptr == '-') { |
106 ptr++; | 109 ptr++; |
107 expsign = -1; | 110 expsign = -1; |
108 } | 111 } |
(...skipping 15 matching lines...) Expand all Loading... |
124 number = integer + decimal; | 127 number = integer + decimal; |
125 number *= sign; | 128 number *= sign; |
126 | 129 |
127 if (exponent) | 130 if (exponent) |
128 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); | 131 number *= static_cast<FloatType>(pow(10.0, expsign * static_cast<int>(ex
ponent))); |
129 | 132 |
130 // Don't return Infinity() or NaN(). | 133 // Don't return Infinity() or NaN(). |
131 if (!isValidRange(number)) | 134 if (!isValidRange(number)) |
132 return false; | 135 return false; |
133 | 136 |
134 if (start == ptr) | |
135 return false; | |
136 | |
137 if (mode & AllowTrailingWhitespace) | 137 if (mode & AllowTrailingWhitespace) |
138 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 138 skipOptionalSVGSpacesOrDelimiter(ptr, end); |
139 | 139 |
140 return true; | 140 return true; |
141 } | 141 } |
142 | 142 |
143 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM
ode mode) | 143 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM
ode mode) |
144 { | 144 { |
145 return genericParseNumber(ptr, end, number, mode); | 145 return genericParseNumber(ptr, end, number, mode); |
146 } | 146 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 const LChar* ptr = string.characters8(); | 240 const LChar* ptr = string.characters8(); |
241 const LChar* end = ptr + string.length(); | 241 const LChar* end = ptr + string.length(); |
242 return genericParseNumberOrPercentage(ptr, end, number); | 242 return genericParseNumberOrPercentage(ptr, end, number); |
243 } | 243 } |
244 const UChar* ptr = string.characters16(); | 244 const UChar* ptr = string.characters16(); |
245 const UChar* end = ptr + string.length(); | 245 const UChar* end = ptr + string.length(); |
246 return genericParseNumberOrPercentage(ptr, end, number); | 246 return genericParseNumberOrPercentage(ptr, end, number); |
247 } | 247 } |
248 | 248 |
249 } | 249 } |
OLD | NEW |