Chromium Code Reviews| 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 |
| 64 const CharType* digitsStart = ptr; | |
| 65 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) | 65 if (ptr == end || ((*ptr < '0' || *ptr > '9') && *ptr != '.')) |
| 66 // 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+-.] |
| 67 return false; | 67 return false; |
| 68 | 68 |
| 69 // read the integer part, build right-to-left | 69 // read the integer part, build right-to-left |
| 70 const CharType* ptrStartIntPart = ptr; | 70 const CharType* ptrStartIntPart = ptr; |
| 71 while (ptr < end && *ptr >= '0' && *ptr <= '9') | 71 while (ptr < end && *ptr >= '0' && *ptr <= '9') |
| 72 ++ptr; // Advance to first non-digit. | 72 ++ptr; // Advance to first non-digit. |
| 73 | 73 |
| 74 if (ptr != ptrStartIntPart) { | 74 if (ptr != ptrStartIntPart) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 87 ptr++; | 87 ptr++; |
| 88 | 88 |
| 89 // There must be a least one digit following the . | 89 // There must be a least one digit following the . |
| 90 if (ptr >= end || *ptr < '0' || *ptr > '9') | 90 if (ptr >= end || *ptr < '0' || *ptr > '9') |
| 91 return false; | 91 return false; |
| 92 | 92 |
| 93 while (ptr < end && *ptr >= '0' && *ptr <= '9') | 93 while (ptr < end && *ptr >= '0' && *ptr <= '9') |
| 94 decimal += (*(ptr++) - '0') * (frac *= static_cast<FloatType>(0.1)); | 94 decimal += (*(ptr++) - '0') * (frac *= static_cast<FloatType>(0.1)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // When we get here we should have consumed either a digit for the integer | |
| 98 // part or a fractional part (with at least one digit after the '.'.) | |
| 99 ASSERT_UNUSED(digitsStart, digitsStart != ptr); | |
|
pdr.
2016/01/13 18:58:11
Can we reuse/rename ptrStartIntPart and change thi
fs
2016/01/13 19:16:27
Yepp. Like so.
| |
| 100 | |
| 97 // read the exponent part | 101 // read the exponent part |
| 98 if (ptr != start && ptr + 1 < end && (*ptr == 'e' || *ptr == 'E') | 102 if (ptr + 1 < end && (*ptr == 'e' || *ptr == 'E') |
| 99 && (ptr[1] != 'x' && ptr[1] != 'm')) { | 103 && (ptr[1] != 'x' && ptr[1] != 'm')) { |
| 100 ptr++; | 104 ptr++; |
| 101 | 105 |
| 102 // read the sign of the exponent | 106 // read the sign of the exponent |
| 103 if (*ptr == '+') | 107 if (*ptr == '+') |
| 104 ptr++; | 108 ptr++; |
| 105 else if (*ptr == '-') { | 109 else if (*ptr == '-') { |
| 106 ptr++; | 110 ptr++; |
| 107 expsign = -1; | 111 expsign = -1; |
| 108 } | 112 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 124 number = integer + decimal; | 128 number = integer + decimal; |
| 125 number *= sign; | 129 number *= sign; |
| 126 | 130 |
| 127 if (exponent) | 131 if (exponent) |
| 128 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))); |
| 129 | 133 |
| 130 // Don't return Infinity() or NaN(). | 134 // Don't return Infinity() or NaN(). |
| 131 if (!isValidRange(number)) | 135 if (!isValidRange(number)) |
| 132 return false; | 136 return false; |
| 133 | 137 |
| 134 if (start == ptr) | |
| 135 return false; | |
| 136 | |
| 137 if (mode & AllowTrailingWhitespace) | 138 if (mode & AllowTrailingWhitespace) |
| 138 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 139 skipOptionalSVGSpacesOrDelimiter(ptr, end); |
| 139 | 140 |
| 140 return true; | 141 return true; |
| 141 } | 142 } |
| 142 | 143 |
| 143 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM ode mode) | 144 bool parseNumber(const LChar*& ptr, const LChar* end, float& number, WhitespaceM ode mode) |
| 144 { | 145 { |
| 145 return genericParseNumber(ptr, end, number, mode); | 146 return genericParseNumber(ptr, end, number, mode); |
| 146 } | 147 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 const LChar* ptr = string.characters8(); | 241 const LChar* ptr = string.characters8(); |
| 241 const LChar* end = ptr + string.length(); | 242 const LChar* end = ptr + string.length(); |
| 242 return genericParseNumberOrPercentage(ptr, end, number); | 243 return genericParseNumberOrPercentage(ptr, end, number); |
| 243 } | 244 } |
| 244 const UChar* ptr = string.characters16(); | 245 const UChar* ptr = string.characters16(); |
| 245 const UChar* end = ptr + string.length(); | 246 const UChar* end = ptr + string.length(); |
| 246 return genericParseNumberOrPercentage(ptr, end, number); | 247 return genericParseNumberOrPercentage(ptr, end, number); |
| 247 } | 248 } |
| 248 | 249 |
| 249 } | 250 } |
| OLD | NEW |