1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:27:34 +00:00

LibWeb: Convert CSS Token::m_value from StringBuilder to FlyString

Again, this value does not change once we have finished creating the
Token, so it can be more lightweight.
This commit is contained in:
Sam Atkins 2021-11-18 12:30:46 +00:00 committed by Andreas Kling
parent 75e7c2c5c0
commit 9403cc42f9
3 changed files with 48 additions and 34 deletions

View file

@ -22,7 +22,7 @@ String Token::to_debug_string() const
break; break;
case Type::Ident: case Type::Ident:
builder.append("Identifier: "); builder.append("Identifier: ");
builder.append(m_value.to_string()); builder.append(m_value);
return builder.to_string(); return builder.to_string();
case Type::Function: case Type::Function:
builder.append("Function"); builder.append("Function");
@ -32,11 +32,11 @@ String Token::to_debug_string() const
break; break;
case Type::Hash: case Type::Hash:
builder.append("Hash: "); builder.append("Hash: ");
builder.append(m_value.to_string()); builder.append(m_value);
return builder.to_string(); return builder.to_string();
case Type::String: case Type::String:
builder.append("String: "); builder.append("String: ");
builder.append(m_value.to_string()); builder.append(m_value);
return builder.to_string(); return builder.to_string();
case Type::BadString: case Type::BadString:
builder.append("Invalid String"); builder.append("Invalid String");
@ -49,21 +49,21 @@ String Token::to_debug_string() const
break; break;
case Type::Delim: case Type::Delim:
builder.append("Delimiter: "); builder.append("Delimiter: ");
builder.append(m_value.to_string()); builder.append(m_value);
return builder.to_string(); return builder.to_string();
case Type::Number: case Type::Number:
builder.append("Number: "); builder.append("Number: ");
builder.append(m_value.to_string()); builder.append(m_value);
builder.append(m_number_type == NumberType::Integer ? " (int)" : " (float)"); builder.append(m_number_type == NumberType::Integer ? " (int)" : " (float)");
return builder.to_string(); return builder.to_string();
case Type::Percentage: case Type::Percentage:
builder.append("Percentage: "); builder.append("Percentage: ");
builder.append(m_value.to_string()); builder.append(m_value);
builder.append('%'); builder.append('%');
return builder.to_string(); return builder.to_string();
case Type::Dimension: case Type::Dimension:
builder.append("Dimension: "); builder.append("Dimension: ");
builder.append(m_value.to_string()); builder.append(m_value);
builder.append(m_unit); builder.append(m_unit);
return builder.to_string(); return builder.to_string();
case Type::Whitespace: case Type::Whitespace:
@ -111,7 +111,7 @@ String Token::to_debug_string() const
builder.append(" "); builder.append(" ");
builder.append(" { value: '"); builder.append(" { value: '");
builder.append(m_value.to_string()); builder.append(m_value);
if (m_type == Token::Type::Hash) { if (m_type == Token::Type::Hash) {
builder.append("', hash_type: '"); builder.append("', hash_type: '");

View file

@ -9,7 +9,6 @@
#include <AK/FlyString.h> #include <AK/FlyString.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h>
#include <math.h> #include <math.h>
namespace Web::CSS { namespace Web::CSS {
@ -68,37 +67,37 @@ public:
StringView ident() const StringView ident() const
{ {
VERIFY(m_type == Type::Ident); VERIFY(m_type == Type::Ident);
return m_value.string_view(); return m_value.view();
} }
StringView function() const StringView function() const
{ {
VERIFY(m_type == Type::Function); VERIFY(m_type == Type::Function);
return m_value.string_view(); return m_value.view();
} }
StringView delim() const StringView delim() const
{ {
VERIFY(m_type == Type::Delim); VERIFY(m_type == Type::Delim);
return m_value.string_view(); return m_value.view();
} }
StringView string() const StringView string() const
{ {
VERIFY(m_type == Type::String); VERIFY(m_type == Type::String);
return m_value.string_view(); return m_value.view();
} }
StringView url() const StringView url() const
{ {
VERIFY(m_type == Type::Url); VERIFY(m_type == Type::Url);
return m_value.string_view(); return m_value.view();
} }
StringView at_keyword() const StringView at_keyword() const
{ {
VERIFY(m_type == Type::AtKeyword); VERIFY(m_type == Type::AtKeyword);
return m_value.string_view(); return m_value.view();
} }
HashType hash_type() const HashType hash_type() const
@ -109,14 +108,14 @@ public:
StringView hash_value() const StringView hash_value() const
{ {
VERIFY(m_type == Type::Hash); VERIFY(m_type == Type::Hash);
return m_value.string_view(); return m_value.view();
} }
bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; } bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; }
StringView number_string_value() const StringView number_string_value() const
{ {
VERIFY(m_type == Type::Number); VERIFY(m_type == Type::Number);
return m_value.string_view(); return m_value.view();
} }
double number_value() const double number_value() const
{ {
@ -175,7 +174,7 @@ private:
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };
StringBuilder m_value; FlyString m_value;
FlyString m_unit; FlyString m_unit;
HashType m_hash_type { HashType::Unrestricted }; HashType m_hash_type { HashType::Unrestricted };
NumberType m_number_type { NumberType::Integer }; NumberType m_number_type { NumberType::Integer };

View file

@ -325,7 +325,7 @@ Token Tokenizer::create_value_token(Token::Type type, String value)
{ {
Token token; Token token;
token.m_type = type; token.m_type = type;
token.m_value.append(move(value)); token.m_value = move(value);
return token; return token;
} }
@ -333,7 +333,10 @@ Token Tokenizer::create_value_token(Token::Type type, u32 value)
{ {
Token token = {}; Token token = {};
token.m_type = type; token.m_type = type;
token.m_value.append_code_point(value); // FIXME: Avoid temporary StringBuilder here
StringBuilder builder;
builder.append_code_point(value);
token.m_value = builder.to_string();
return token; return token;
} }
@ -582,18 +585,24 @@ Token Tokenizer::consume_a_url_token()
{ {
auto token = create_new_token(Token::Type::Url); auto token = create_new_token(Token::Type::Url);
consume_as_much_whitespace_as_possible(); consume_as_much_whitespace_as_possible();
StringBuilder builder;
auto make_token = [&]() {
token.m_value = builder.to_string();
return token;
};
for (;;) { for (;;) {
auto input = peek_code_point(); auto input = peek_code_point();
if (is_eof(input)) { if (is_eof(input)) {
log_parse_error(); log_parse_error();
return token; return make_token();
} }
if (is_right_paren(input)) { if (is_right_paren(input)) {
(void)next_code_point(); (void)next_code_point();
return token; return make_token();
} }
if (is_whitespace(input)) { if (is_whitespace(input)) {
@ -602,11 +611,11 @@ Token Tokenizer::consume_a_url_token()
if (is_eof(input)) { if (is_eof(input)) {
log_parse_error(); log_parse_error();
return token; return make_token();
} }
if (is_right_paren(input)) { if (is_right_paren(input)) {
return token; return make_token();
} }
consume_the_remnants_of_a_bad_url(); consume_the_remnants_of_a_bad_url();
@ -622,7 +631,7 @@ Token Tokenizer::consume_a_url_token()
if (is_reverse_solidus(input)) { if (is_reverse_solidus(input)) {
if (is_valid_escape_sequence(peek_twin())) { if (is_valid_escape_sequence(peek_twin())) {
token.m_value.append_code_point(consume_escaped_code_point()); builder.append_code_point(consume_escaped_code_point());
} else { } else {
log_parse_error(); log_parse_error();
(void)next_code_point(); (void)next_code_point();
@ -631,7 +640,7 @@ Token Tokenizer::consume_a_url_token()
} }
} }
token.m_value.append_code_point(next_code_point()); builder.append_code_point(next_code_point());
} }
} }
@ -679,7 +688,7 @@ Token Tokenizer::consume_a_numeric_token()
auto number = consume_a_number(); auto number = consume_a_number();
if (would_start_an_identifier()) { if (would_start_an_identifier()) {
auto token = create_new_token(Token::Type::Dimension); auto token = create_new_token(Token::Type::Dimension);
token.m_value.append(number.string); token.m_value = move(number.string);
token.m_number_type = number.type; token.m_number_type = number.type;
token.m_number_value = number.value; token.m_number_value = number.value;
@ -694,14 +703,14 @@ Token Tokenizer::consume_a_numeric_token()
(void)next_code_point(); (void)next_code_point();
auto token = create_new_token(Token::Type::Percentage); auto token = create_new_token(Token::Type::Percentage);
token.m_value.append(number.string); token.m_value = move(number.string);
token.m_number_type = number.type; token.m_number_type = number.type;
token.m_number_value = number.value; token.m_number_value = number.value;
return token; return token;
} }
auto token = create_new_token(Token::Type::Number); auto token = create_new_token(Token::Type::Number);
token.m_value.append(number.string); token.m_value = move(number.string);
token.m_number_type = number.type; token.m_number_type = number.type;
token.m_number_value = number.value; token.m_number_value = number.value;
return token; return token;
@ -779,17 +788,23 @@ bool Tokenizer::would_start_an_identifier(U32Triplet values)
Token Tokenizer::consume_string_token(u32 ending_code_point) Token Tokenizer::consume_string_token(u32 ending_code_point)
{ {
auto token = create_new_token(Token::Type::String); auto token = create_new_token(Token::Type::String);
StringBuilder builder;
auto make_token = [&]() {
token.m_value = builder.to_string();
return token;
};
for (;;) { for (;;) {
auto input = next_code_point(); auto input = next_code_point();
if (is_eof(input)) { if (is_eof(input)) {
log_parse_error(); log_parse_error();
return token; return make_token();
} }
if (input == ending_code_point) if (input == ending_code_point)
return token; return make_token();
if (is_newline(input)) { if (is_newline(input)) {
reconsume_current_input_code_point(); reconsume_current_input_code_point();
@ -807,10 +822,10 @@ Token Tokenizer::consume_string_token(u32 ending_code_point)
} }
auto escaped = consume_escaped_code_point(); auto escaped = consume_escaped_code_point();
token.m_value.append_code_point(escaped); builder.append_code_point(escaped);
} }
token.m_value.append_code_point(input); builder.append_code_point(input);
} }
} }
@ -877,7 +892,7 @@ Token Tokenizer::consume_a_token()
token.m_hash_type = Token::HashType::Id; token.m_hash_type = Token::HashType::Id;
auto name = consume_a_name(); auto name = consume_a_name();
token.m_value.append(name); token.m_value = move(name);
return token; return token;
} }