Commit 6edea8b7 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

Optimize integer parsing in HTMLFontElement and HTMLParserIdioms.

We don't need to create new string with StringBuilder to collect digits.
Also, use ParsingUtilities functions to simplify the code.

Change-Id: Id98ff2298db5c447f53e13a21c1d9e597e971098
Reviewed-on: https://chromium-review.googlesource.com/580211Reviewed-by: default avatarTakayoshi Kochi <kochi@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488584}
parent b01aeaab
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#include "core/css/StylePropertySet.h" #include "core/css/StylePropertySet.h"
#include "core/css/parser/CSSParser.h" #include "core/css/parser/CSSParser.h"
#include "core/html/parser/HTMLParserIdioms.h" #include "core/html/parser/HTMLParserIdioms.h"
#include "platform/wtf/text/StringBuilder.h" #include "platform/wtf/text/ParsingUtilities.h"
#include "platform/wtf/text/StringToNumber.h" #include "platform/wtf/text/StringToNumber.h"
namespace blink { namespace blink {
...@@ -54,11 +54,7 @@ static bool ParseFontSize(const CharacterType* characters, ...@@ -54,11 +54,7 @@ static bool ParseFontSize(const CharacterType* characters,
const CharacterType* end = characters + length; const CharacterType* end = characters + length;
// Step 3 // Step 3
while (position < end) { skipWhile<CharacterType, IsHTMLSpace<CharacterType>>(position, end);
if (!IsHTMLSpace<CharacterType>(*position))
break;
++position;
}
// Step 4 // Step 4
if (position == end) if (position == end)
...@@ -83,28 +79,16 @@ static bool ParseFontSize(const CharacterType* characters, ...@@ -83,28 +79,16 @@ static bool ParseFontSize(const CharacterType* characters,
} }
// Step 6 // Step 6
StringBuilder digits; const CharacterType* digits_start = position;
digits.ReserveCapacity(16); skipWhile<CharacterType, IsASCIIDigit>(position, end);
while (position < end) {
if (!IsASCIIDigit(*position))
break;
digits.Append(*position++);
}
// Step 7 // Step 7
if (digits.IsEmpty()) if (digits_start == position)
return false; return false;
// Step 8 // Step 8
int value; int value =
CharactersToIntStrict(digits_start, position - digits_start, nullptr);
if (digits.Is8Bit()) {
value =
CharactersToIntStrict(digits.Characters8(), digits.length(), nullptr);
} else {
value =
CharactersToIntStrict(digits.Characters16(), digits.length(), nullptr);
}
// Step 9 // Step 9
if (mode == kRelativePlus) if (mode == kRelativePlus)
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include "platform/wtf/MathExtras.h" #include "platform/wtf/MathExtras.h"
#include "platform/wtf/text/AtomicString.h" #include "platform/wtf/text/AtomicString.h"
#include "platform/wtf/text/ParsingUtilities.h" #include "platform/wtf/text/ParsingUtilities.h"
#include "platform/wtf/text/StringBuilder.h"
#include "platform/wtf/text/StringHash.h" #include "platform/wtf/text/StringHash.h"
#include "platform/wtf/text/StringToNumber.h" #include "platform/wtf/text/StringToNumber.h"
#include "platform/wtf/text/TextEncoding.h" #include "platform/wtf/text/TextEncoding.h"
...@@ -242,11 +241,7 @@ static WTF::NumberParsingState ParseHTMLNonNegativeIntegerInternal( ...@@ -242,11 +241,7 @@ static WTF::NumberParsingState ParseHTMLNonNegativeIntegerInternal(
int sign = 1; int sign = 1;
// Step 4: Skip whitespace. // Step 4: Skip whitespace.
while (position < end) { skipWhile<CharacterType, IsHTMLSpace<CharacterType>>(position, end);
if (!IsHTMLSpace<CharacterType>(*position))
break;
++position;
}
// Step 5: If position is past the end of input, return an error. // Step 5: If position is past the end of input, return an error.
if (position == end) if (position == end)
...@@ -272,22 +267,12 @@ static WTF::NumberParsingState ParseHTMLNonNegativeIntegerInternal( ...@@ -272,22 +267,12 @@ static WTF::NumberParsingState ParseHTMLNonNegativeIntegerInternal(
return WTF::NumberParsingState::kError; return WTF::NumberParsingState::kError;
// Step 8: Collect a sequence of characters ... // Step 8: Collect a sequence of characters ...
StringBuilder digits; const CharacterType* digits_start = position;
while (position < end) { skipWhile<CharacterType, IsASCIIDigit>(position, end);
if (!IsASCIIDigit(*position))
break;
digits.Append(*position++);
}
WTF::NumberParsingState state; WTF::NumberParsingState state;
unsigned digits_value; unsigned digits_value =
if (digits.Is8Bit()) { CharactersToUIntStrict(digits_start, position - digits_start, &state);
digits_value =
CharactersToUIntStrict(digits.Characters8(), digits.length(), &state);
} else {
digits_value =
CharactersToUIntStrict(digits.Characters16(), digits.length(), &state);
}
// TODO(tkent): The following code to adjust NumberParsingState is not simple // TODO(tkent): The following code to adjust NumberParsingState is not simple
// due to "-0" behavior difference between CharactersToUIntStrict() and // due to "-0" behavior difference between CharactersToUIntStrict() and
// ParseHTMLNonNegativeIntegerInternal(). Simplify the code by updating // ParseHTMLNonNegativeIntegerInternal(). Simplify the code by updating
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment