From 9403cc42f9d0455100722814ace98390165d67ef Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 18 Nov 2021 12:30:46 +0000 Subject: 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. --- Userland/Libraries/LibWeb/CSS/Parser/Token.cpp | 16 ++++---- Userland/Libraries/LibWeb/CSS/Parser/Token.h | 19 +++++---- Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp | 47 ++++++++++++++-------- 3 files changed, 48 insertions(+), 34 deletions(-) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Token.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Token.cpp index 9eb7c68186..38a3262c4b 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Token.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Token.cpp @@ -22,7 +22,7 @@ String Token::to_debug_string() const break; case Type::Ident: builder.append("Identifier: "); - builder.append(m_value.to_string()); + builder.append(m_value); return builder.to_string(); case Type::Function: builder.append("Function"); @@ -32,11 +32,11 @@ String Token::to_debug_string() const break; case Type::Hash: builder.append("Hash: "); - builder.append(m_value.to_string()); + builder.append(m_value); return builder.to_string(); case Type::String: builder.append("String: "); - builder.append(m_value.to_string()); + builder.append(m_value); return builder.to_string(); case Type::BadString: builder.append("Invalid String"); @@ -49,21 +49,21 @@ String Token::to_debug_string() const break; case Type::Delim: builder.append("Delimiter: "); - builder.append(m_value.to_string()); + builder.append(m_value); return builder.to_string(); case Type::Number: builder.append("Number: "); - builder.append(m_value.to_string()); + builder.append(m_value); builder.append(m_number_type == NumberType::Integer ? " (int)" : " (float)"); return builder.to_string(); case Type::Percentage: builder.append("Percentage: "); - builder.append(m_value.to_string()); + builder.append(m_value); builder.append('%'); return builder.to_string(); case Type::Dimension: builder.append("Dimension: "); - builder.append(m_value.to_string()); + builder.append(m_value); builder.append(m_unit); return builder.to_string(); case Type::Whitespace: @@ -111,7 +111,7 @@ String Token::to_debug_string() const builder.append(" "); builder.append(" { value: '"); - builder.append(m_value.to_string()); + builder.append(m_value); if (m_type == Token::Type::Hash) { builder.append("', hash_type: '"); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Token.h b/Userland/Libraries/LibWeb/CSS/Parser/Token.h index e42e0ee378..99886308a8 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Token.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Token.h @@ -9,7 +9,6 @@ #include #include -#include #include namespace Web::CSS { @@ -68,37 +67,37 @@ public: StringView ident() const { VERIFY(m_type == Type::Ident); - return m_value.string_view(); + return m_value.view(); } StringView function() const { VERIFY(m_type == Type::Function); - return m_value.string_view(); + return m_value.view(); } StringView delim() const { VERIFY(m_type == Type::Delim); - return m_value.string_view(); + return m_value.view(); } StringView string() const { VERIFY(m_type == Type::String); - return m_value.string_view(); + return m_value.view(); } StringView url() const { VERIFY(m_type == Type::Url); - return m_value.string_view(); + return m_value.view(); } StringView at_keyword() const { VERIFY(m_type == Type::AtKeyword); - return m_value.string_view(); + return m_value.view(); } HashType hash_type() const @@ -109,14 +108,14 @@ public: StringView hash_value() const { 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; } StringView number_string_value() const { VERIFY(m_type == Type::Number); - return m_value.string_view(); + return m_value.view(); } double number_value() const { @@ -175,7 +174,7 @@ private: Type m_type { Type::Invalid }; - StringBuilder m_value; + FlyString m_value; FlyString m_unit; HashType m_hash_type { HashType::Unrestricted }; NumberType m_number_type { NumberType::Integer }; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp index 269c572213..248613a033 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp @@ -325,7 +325,7 @@ Token Tokenizer::create_value_token(Token::Type type, String value) { Token token; token.m_type = type; - token.m_value.append(move(value)); + token.m_value = move(value); return token; } @@ -333,7 +333,10 @@ Token Tokenizer::create_value_token(Token::Type type, u32 value) { Token token = {}; 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; } @@ -582,18 +585,24 @@ Token Tokenizer::consume_a_url_token() { auto token = create_new_token(Token::Type::Url); consume_as_much_whitespace_as_possible(); + StringBuilder builder; + + auto make_token = [&]() { + token.m_value = builder.to_string(); + return token; + }; for (;;) { auto input = peek_code_point(); if (is_eof(input)) { log_parse_error(); - return token; + return make_token(); } if (is_right_paren(input)) { (void)next_code_point(); - return token; + return make_token(); } if (is_whitespace(input)) { @@ -602,11 +611,11 @@ Token Tokenizer::consume_a_url_token() if (is_eof(input)) { log_parse_error(); - return token; + return make_token(); } if (is_right_paren(input)) { - return token; + return make_token(); } consume_the_remnants_of_a_bad_url(); @@ -622,7 +631,7 @@ Token Tokenizer::consume_a_url_token() if (is_reverse_solidus(input)) { 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 { log_parse_error(); (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(); if (would_start_an_identifier()) { 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_value = number.value; @@ -694,14 +703,14 @@ Token Tokenizer::consume_a_numeric_token() (void)next_code_point(); 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_value = number.value; return token; } 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_value = number.value; return token; @@ -779,17 +788,23 @@ bool Tokenizer::would_start_an_identifier(U32Triplet values) Token Tokenizer::consume_string_token(u32 ending_code_point) { auto token = create_new_token(Token::Type::String); + StringBuilder builder; + + auto make_token = [&]() { + token.m_value = builder.to_string(); + return token; + }; for (;;) { auto input = next_code_point(); if (is_eof(input)) { log_parse_error(); - return token; + return make_token(); } if (input == ending_code_point) - return token; + return make_token(); if (is_newline(input)) { reconsume_current_input_code_point(); @@ -807,10 +822,10 @@ Token Tokenizer::consume_string_token(u32 ending_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; auto name = consume_a_name(); - token.m_value.append(name); + token.m_value = move(name); return token; } -- cgit v1.2.3