Explorar o código

LibWeb: Return FlyString from CSS::Parser::Token::ident

Instead of a StringView. This allows us to preserve the nice O(1) string
compare property of FlyString, and not needing to allocate when one is
needed.

Ideally all other places in Token should have similar changes done, but
to prevent a huge amount of churn, just change ident for now.
Shannon Booth hai 1 ano
pai
achega
3f13a50a20

+ 3 - 3
Userland/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp

@@ -261,12 +261,12 @@ Optional<MediaFeature> Parser::parse_media_feature(TokenStream<ComponentValue>&
                 return MediaFeatureName { MediaFeatureName::Type::Normal, id.value() };
                 return MediaFeatureName { MediaFeatureName::Type::Normal, id.value() };
             }
             }
 
 
-            if (allow_min_max_prefix && (name.starts_with("min-"sv, CaseSensitivity::CaseInsensitive) || name.starts_with("max-"sv, CaseSensitivity::CaseInsensitive))) {
-                auto adjusted_name = name.substring_view(4);
+            if (allow_min_max_prefix && (name.starts_with_bytes("min-"sv, CaseSensitivity::CaseInsensitive) || name.starts_with_bytes("max-"sv, CaseSensitivity::CaseInsensitive))) {
+                auto adjusted_name = name.bytes_as_string_view().substring_view(4);
                 if (auto id = media_feature_id_from_string(adjusted_name); id.has_value() && media_feature_type_is_range(id.value())) {
                 if (auto id = media_feature_id_from_string(adjusted_name); id.has_value() && media_feature_type_is_range(id.value())) {
                     transaction.commit();
                     transaction.commit();
                     return MediaFeatureName {
                     return MediaFeatureName {
-                        name.starts_with("min-"sv, CaseSensitivity::CaseInsensitive) ? MediaFeatureName::Type::Min : MediaFeatureName::Type::Max,
+                        name.starts_with_bytes("min-"sv, CaseSensitivity::CaseInsensitive) ? MediaFeatureName::Type::Min : MediaFeatureName::Type::Max,
                         id.value()
                         id.value()
                     };
                     };
                 }
                 }

+ 5 - 5
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -1574,7 +1574,7 @@ CSSRule* Parser::convert_to_rule(NonnullRefPtr<Rule> rule)
             auto token = token_stream.next_token();
             auto token = token_stream.next_token();
             Optional<DeprecatedString> prefix = {};
             Optional<DeprecatedString> prefix = {};
             if (token.is(Token::Type::Ident)) {
             if (token.is(Token::Type::Ident)) {
-                prefix = token.token().ident();
+                prefix = token.token().ident().bytes_as_string_view();
                 token_stream.skip_whitespace();
                 token_stream.skip_whitespace();
                 token = token_stream.next_token();
                 token = token_stream.next_token();
             }
             }
@@ -2446,7 +2446,7 @@ Optional<Color> Parser::parse_color(ComponentValue const& component_value)
         else {
         else {
             if (!cv.is(Token::Type::Ident))
             if (!cv.is(Token::Type::Ident))
                 return {};
                 return {};
-            serialization = cv.token().ident();
+            serialization = cv.token().ident().bytes_as_string_view();
         }
         }
 
 
         // 4. If serialization does not consist of three or six characters, return an error.
         // 4. If serialization does not consist of three or six characters, return an error.
@@ -4178,7 +4178,7 @@ RefPtr<StyleValue> Parser::parse_font_family_value(TokenStream<ComponentValue>&
                 (void)tokens.next_token(); // Comma
                 (void)tokens.next_token(); // Comma
                 continue;
                 continue;
             }
             }
-            current_name_parts.append(tokens.next_token().token().ident());
+            current_name_parts.append(tokens.next_token().token().ident().bytes_as_string_view());
             continue;
             continue;
         }
         }
 
 
@@ -4270,7 +4270,7 @@ CSSRule* Parser::parse_font_face_rule(TokenStream<ComponentValue>& tokens)
                         had_syntax_error = true;
                         had_syntax_error = true;
                         break;
                         break;
                     }
                     }
-                    font_family_parts.append(part.token().ident());
+                    font_family_parts.append(part.token().ident().bytes_as_string_view());
                     continue;
                     continue;
                 }
                 }
 
 
@@ -6800,7 +6800,7 @@ bool Parser::expand_variables(DOM::Element& element, Optional<Selector::PseudoEl
         if (!custom_property_name_token.is(Token::Type::Ident))
         if (!custom_property_name_token.is(Token::Type::Ident))
             return false;
             return false;
         auto custom_property_name = custom_property_name_token.token().ident();
         auto custom_property_name = custom_property_name_token.token().ident();
-        if (!custom_property_name.starts_with("--"sv))
+        if (!custom_property_name.bytes_as_string_view().starts_with("--"sv))
             return false;
             return false;
 
 
         // Detect dependency cycles. https://www.w3.org/TR/css-variables-1/#cycles
         // Detect dependency cycles. https://www.w3.org/TR/css-variables-1/#cycles

+ 9 - 9
Userland/Libraries/LibWeb/CSS/Parser/SelectorParsing.cpp

@@ -729,10 +729,10 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
         if (!value.is(Token::Type::Ident))
         if (!value.is(Token::Type::Ident))
             return false;
             return false;
         auto ident = value.token().ident();
         auto ident = value.token().ident();
-        if (!ident.starts_with("n-"sv, CaseSensitivity::CaseInsensitive))
+        if (!ident.starts_with_bytes("n-"sv, CaseSensitivity::CaseInsensitive))
             return false;
             return false;
-        for (size_t i = 2; i < ident.length(); ++i) {
-            if (!is_ascii_digit(ident[i]))
+        for (size_t i = 2; i < ident.bytes_as_string_view().length(); ++i) {
+            if (!is_ascii_digit(ident.bytes_as_string_view()[i]))
                 return false;
                 return false;
         }
         }
         return true;
         return true;
@@ -741,12 +741,12 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
         if (!value.is(Token::Type::Ident))
         if (!value.is(Token::Type::Ident))
             return false;
             return false;
         auto ident = value.token().ident();
         auto ident = value.token().ident();
-        if (!ident.starts_with("-n-"sv, CaseSensitivity::CaseInsensitive))
+        if (!ident.starts_with_bytes("-n-"sv, CaseSensitivity::CaseInsensitive))
             return false;
             return false;
-        if (ident.length() == 3)
+        if (ident.bytes_as_string_view().length() == 3)
             return false;
             return false;
-        for (size_t i = 3; i < ident.length(); ++i) {
-            if (!is_ascii_digit(ident[i]))
+        for (size_t i = 3; i < ident.bytes_as_string_view().length(); ++i) {
+            if (!is_ascii_digit(ident.bytes_as_string_view()[i]))
                 return false;
                 return false;
         }
         }
         return true;
         return true;
@@ -844,7 +844,7 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
     }
     }
     // <dashndashdigit-ident>
     // <dashndashdigit-ident>
     if (is_dashndashdigit_ident(first_value)) {
     if (is_dashndashdigit_ident(first_value)) {
-        auto maybe_b = first_value.token().ident().substring_view(2).to_int();
+        auto maybe_b = first_value.token().ident().bytes_as_string_view().substring_view(2).to_int();
         if (maybe_b.has_value()) {
         if (maybe_b.has_value()) {
             transaction.commit();
             transaction.commit();
             return Selector::SimpleSelector::ANPlusBPattern { -1, maybe_b.value() };
             return Selector::SimpleSelector::ANPlusBPattern { -1, maybe_b.value() };
@@ -957,7 +957,7 @@ Optional<Selector::SimpleSelector::ANPlusBPattern> Parser::parse_a_n_plus_b_patt
 
 
     // '+'?† <ndashdigit-ident>
     // '+'?† <ndashdigit-ident>
     if (is_ndashdigit_ident(first_after_plus)) {
     if (is_ndashdigit_ident(first_after_plus)) {
-        auto maybe_b = first_after_plus.token().ident().substring_view(1).to_int();
+        auto maybe_b = first_after_plus.token().ident().bytes_as_string_view().substring_view(1).to_int();
         if (maybe_b.has_value()) {
         if (maybe_b.has_value()) {
             transaction.commit();
             transaction.commit();
             return Selector::SimpleSelector::ANPlusBPattern { 1, maybe_b.value() };
             return Selector::SimpleSelector::ANPlusBPattern { 1, maybe_b.value() };

+ 2 - 2
Userland/Libraries/LibWeb/CSS/Parser/Token.h

@@ -59,10 +59,10 @@ public:
     Type type() const { return m_type; }
     Type type() const { return m_type; }
     bool is(Type type) const { return m_type == type; }
     bool is(Type type) const { return m_type == type; }
 
 
-    StringView ident() const
+    FlyString const& ident() const
     {
     {
         VERIFY(m_type == Type::Ident);
         VERIFY(m_type == Type::Ident);
-        return m_value.bytes_as_string_view();
+        return m_value;
     }
     }
 
 
     StringView function() const
     StringView function() const