Skip to content

Commit 408eb97

Browse files
Ahmad-S792Ahmad Saleem
authored andcommitted
Remove redundant emptiness check from genericParseNumber
https://bugs.webkit.org/show_bug.cgi?id=285184 rdar://142083712 Reviewed by Darin Adler. Merge: https://source.chromium.org/chromium/chromium/src/+/ab685d36561909a7ef373183f37fc3d6fc21bb83 When the 'start = buffer.position()' condition was reached we could be sure that we had already consumed at least one character - one of '.' or '0'-'9' or whitespace (potentially also '+'/'-' as a prefix) - and that even holds true moving the definition of |start| after consuming any leading whitespace. The reason for this is the up-front check for any character in the set '0'-'9' or '.'. Remove the redundant checks - replacing them with an assert - while moving the definition of 'start' so that it doesn't point before any leading whitespace. * Source/WebCore/svg/SVGParserUtilities.cpp: (WebCore::genericParseNumber): Canonical link: https://commits.webkit.org/292567@main
1 parent 42b6e86 commit 408eb97

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

Source/WebCore/svg/SVGParserUtilities.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (C) 2006 Alexander Kellett <lypanov@kde.org>
44
* Copyright (C) 2006, 2007 Rob Buis <buis@kde.org>
55
* Copyright (C) 2007-2024 Apple Inc. All rights reserved.
6+
* Copyright (C) 2016 Google Inc. All rights reserved.
67
*
78
* This library is free software; you can redistribute it and/or
89
* modify it under the terms of the GNU Library General Public
@@ -51,7 +52,6 @@ template <typename CharacterType, typename FloatType = float> static std::option
5152
FloatType exponent = 0;
5253
int sign = 1;
5354
int expsign = 1;
54-
auto start = buffer.position();
5555

5656
// read the sign
5757
if (buffer.hasCharactersRemaining() && *buffer == '+')
@@ -65,16 +65,16 @@ template <typename CharacterType, typename FloatType = float> static std::option
6565
return std::nullopt;
6666

6767
// read the integer part, build right-to-left
68-
auto spanStartIntPart = buffer.span();
68+
auto spanDigitsStart = buffer.span();
6969

7070
// Advance to first non-digit.
7171
skipWhile<isASCIIDigit>(buffer);
7272

73-
if (buffer.position() > spanStartIntPart.data()) {
74-
size_t indexScanIntPart = buffer.position() - spanStartIntPart.data();
73+
if (buffer.position() > spanDigitsStart.data()) {
74+
size_t indexScanIntPart = buffer.position() - spanDigitsStart.data();
7575
FloatType multiplier = 1;
7676
for (size_t i = indexScanIntPart; i > 0; --i) {
77-
integer += multiplier * static_cast<FloatType>(spanStartIntPart[i - 1] - '0');
77+
integer += multiplier * static_cast<FloatType>(spanDigitsStart[i - 1] - '0');
7878
multiplier *= 10;
7979
}
8080
// Bail out early if this overflows.
@@ -94,8 +94,12 @@ template <typename CharacterType, typename FloatType = float> static std::option
9494
decimal += (*(buffer++) - '0') * (frac *= static_cast<FloatType>(0.1));
9595
}
9696

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(spanDigitsStart.data() != buffer.position());
100+
97101
// read the exponent part
98-
if (buffer.position() != start && buffer.span().size() > 1 && (*buffer == 'e' || *buffer == 'E')
102+
if (buffer.span().size() > 1 && (*buffer == 'e' || *buffer == 'E')
99103
&& (buffer[1] != 'x' && buffer[1] != 'm')) {
100104
++buffer;
101105

@@ -130,9 +134,6 @@ template <typename CharacterType, typename FloatType = float> static std::option
130134
if (!isValidRange(number))
131135
return std::nullopt;
132136

133-
if (start == buffer.position())
134-
return std::nullopt;
135-
136137
if (skip == SuffixSkippingPolicy::Skip)
137138
skipOptionalSVGSpacesOrDelimiter(buffer);
138139

0 commit comments

Comments
 (0)