LibWeb: Skip some redundant UTF-8 validation in CSS tokenizer

If we're just adding code points to a StringBuilder, there's no need to
revalidate the result.
This commit is contained in:
Andreas Kling 2024-03-23 11:33:26 +01:00
parent a88799c032
commit 8d7a1e5654
Notes: sideshowbarker 2024-07-17 05:00:08 +09:00
5 changed files with 15 additions and 2 deletions

View file

@ -31,6 +31,11 @@ ErrorOr<FlyString> FlyString::from_utf8(StringView string)
return FlyString { TRY(String::from_utf8(string)) };
}
FlyString FlyString::from_utf8_without_validation(ReadonlyBytes string)
{
return FlyString { String::from_utf8_without_validation(string) };
}
FlyString::FlyString(String const& string)
{
if (string.is_short_string()) {

View file

@ -23,6 +23,7 @@ public:
FlyString() = default;
static ErrorOr<FlyString> from_utf8(StringView);
static FlyString from_utf8_without_validation(ReadonlyBytes);
template<typename T>
requires(IsOneOf<RemoveCVReference<T>, ByteString, DeprecatedFlyString, FlyString, String>)
static ErrorOr<String> from_utf8(T&&) = delete;

View file

@ -161,6 +161,11 @@ String StringBuilder::to_string_without_validation() const
return String::from_utf8_without_validation(string_view().bytes());
}
FlyString StringBuilder::to_fly_string_without_validation() const
{
return FlyString::from_utf8_without_validation(string_view().bytes());
}
ErrorOr<FlyString> StringBuilder::to_fly_string() const
{
return FlyString::from_utf8(string_view());

View file

@ -75,6 +75,8 @@ public:
[[nodiscard]] String to_string_without_validation() const;
ErrorOr<String> to_string() const;
[[nodiscard]] FlyString to_fly_string_without_validation() const;
ErrorOr<FlyString> to_fly_string() const;
[[nodiscard]] ErrorOr<ByteBuffer> to_byte_buffer() const;

View file

@ -238,7 +238,7 @@ ErrorOr<Vector<Token>> Tokenizer::tokenize(StringView input, StringView encoding
}
return {};
}));
return builder.to_string();
return builder.to_string_without_validation();
};
Tokenizer tokenizer { TRY(filter_code_points(input, encoding)) };
@ -623,7 +623,7 @@ ErrorOr<FlyString> Tokenizer::consume_an_ident_sequence()
break;
}
return result.to_fly_string();
return result.to_fly_string_without_validation();
}
// https://www.w3.org/TR/css-syntax-3/#consume-url-token