From 6ce0d588eebb0fab35aa3ede3ea0a94071ffa19e Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 9 Dec 2023 09:24:41 +1300 Subject: [PATCH] Everywhere: Avoid calling from_utf8 on FlyString or String We already have a String :^) --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 30 ++++++------------- .../LibWeb/CSS/Parser/SelectorParsing.cpp | 4 +-- .../LibWeb/HTML/WindowOrWorkerGlobalScope.cpp | 2 +- .../Services/DeviceMapper/DeviceEventLoop.cpp | 2 +- Userland/Shell/ImmediateFunctions.cpp | 4 +-- Userland/Shell/PosixParser.cpp | 6 ++-- 6 files changed, 18 insertions(+), 30 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 792b73fb52..4239e0d6dd 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2283,7 +2283,7 @@ RefPtr Parser::parse_ratio_value(TokenStream& tokens RefPtr Parser::parse_string_value(ComponentValue const& component_value) { if (component_value.is(Token::Type::String)) - return StringStyleValue::create(MUST(String::from_utf8(component_value.token().string()))); + return StringStyleValue::create(component_value.token().string().to_string()); return nullptr; } @@ -4099,7 +4099,7 @@ RefPtr Parser::parse_font_family_value(TokenStream& (void)tokens.next_token(); // String if (!next_is_comma_or_eof()) return nullptr; - font_families.append(StringStyleValue::create(MUST(String::from_utf8(peek.token().string())))); + font_families.append(StringStyleValue::create(peek.token().string().to_string())); (void)tokens.next_token(); // Comma continue; } @@ -5178,10 +5178,7 @@ Optional Parser::parse_repeat(Vector const& com TokenStream block_tokens { token.block().values() }; while (block_tokens.has_next_token()) { auto current_block_token = block_tokens.next_token(); - auto maybe_string = String::from_utf8(current_block_token.token().ident()); - if (maybe_string.is_error()) - return {}; - line_names.append(maybe_string.value()); + line_names.append(current_block_token.token().ident().to_string()); block_tokens.skip_whitespace(); } line_names_list.append(line_names); @@ -5289,10 +5286,7 @@ RefPtr Parser::parse_grid_track_size_list(Vector con block_tokens.skip_whitespace(); while (block_tokens.has_next_token()) { auto current_block_token = block_tokens.next_token(); - auto maybe_string = String::from_utf8(current_block_token.token().ident()); - if (maybe_string.is_error()) - return nullptr; - line_names.append(maybe_string.value()); + line_names.append(current_block_token.token().ident().to_string()); block_tokens.skip_whitespace(); } line_names_list.append(line_names); @@ -5415,11 +5409,8 @@ RefPtr Parser::parse_grid_track_placement(Vector con return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(1)); if (is_valid_integer(current_token)) return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast(current_token.token().number_value()), {})); - if (is_identifier(current_token)) { - auto maybe_string = String::from_utf8(current_token.token().ident()); - if (!maybe_string.is_error()) - return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line({}, maybe_string.value())); - } + if (is_identifier(current_token)) + return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line({}, current_token.token().to_string())); return nullptr; } @@ -5443,10 +5434,7 @@ RefPtr Parser::parse_grid_track_placement(Vector con } if (is_identifier(current_token)) { if (identifier_value.is_empty()) { - auto maybe_string = String::from_utf8(current_token.token().ident()); - if (maybe_string.is_error()) - return nullptr; - identifier_value = maybe_string.release_value(); + identifier_value = current_token.token().ident().to_string(); } else { return nullptr; } @@ -5666,7 +5654,7 @@ RefPtr Parser::parse_grid_template_areas_value(TokenStream grid_area_columns; - auto const parts = MUST(MUST(String::from_utf8(tokens.next_token().token().string())).split(' ')); + auto const parts = MUST(tokens.next_token().token().string().to_string().split(' ')); for (auto& part : parts) { grid_area_columns.append(part); } @@ -6147,7 +6135,7 @@ Optional Parser::parse_css_value_for_properties(Readon if (peek_token.is(Token::Type::String)) { if (auto property = any_property_accepts_type(property_ids, ValueType::String); property.has_value()) - return PropertyAndValue { *property, StringStyleValue::create(MUST(String::from_utf8(tokens.next_token().token().string()))) }; + return PropertyAndValue { *property, StringStyleValue::create(tokens.next_token().token().string().to_string()) }; } if (auto property = any_property_accepts_type(property_ids, ValueType::Url); property.has_value()) { diff --git a/Userland/Libraries/LibWeb/CSS/Parser/SelectorParsing.cpp b/Userland/Libraries/LibWeb/CSS/Parser/SelectorParsing.cpp index cd2a7ec7fd..0bdd239aea 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/SelectorParsing.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/SelectorParsing.cpp @@ -295,8 +295,8 @@ Parser::ParseErrorOr Parser::parse_attribute_simple_se dbgln_if(CSS_PARSER_DEBUG, "Expected a string or ident for the value to match attribute against, got: '{}'", value_part.to_debug_string()); return ParseError::SyntaxError; } - auto value_string_view = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string(); - simple_selector.attribute().value = String::from_utf8(value_string_view).release_value_but_fixme_should_propagate_errors(); + auto const& value_string = value_part.token().is(Token::Type::Ident) ? value_part.token().ident() : value_part.token().string(); + simple_selector.attribute().value = value_string.to_string(); attribute_tokens.skip_whitespace(); // Handle case-sensitivity suffixes. https://www.w3.org/TR/selectors-4/#attribute-case diff --git a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp index 72e2beda60..536936b4ee 100644 --- a/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp +++ b/Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp @@ -343,7 +343,7 @@ void WindowOrWorkerGlobalScopeMixin::queue_performance_entry(JS::NonnullGCPtroptions_list().find_if([&entry_type](PerformanceTimeline::PerformanceObserverInit const& entry) { if (entry.entry_types.has_value()) - return entry.entry_types->contains_slow(String::from_utf8(entry_type).release_value_but_fixme_should_propagate_errors()); + return entry.entry_types->contains_slow(entry_type.to_string()); VERIFY(entry.type.has_value()); return entry.type.value() == entry_type; diff --git a/Userland/Services/DeviceMapper/DeviceEventLoop.cpp b/Userland/Services/DeviceMapper/DeviceEventLoop.cpp index fbbdc11fc6..02909b99d6 100644 --- a/Userland/Services/DeviceMapper/DeviceEventLoop.cpp +++ b/Userland/Services/DeviceMapper/DeviceEventLoop.cpp @@ -131,7 +131,7 @@ ErrorOr DeviceEventLoop::register_new_device(DeviceNodeFamily::Type unix_d } auto allocated_suffix_index = possible_allocated_suffix_index.release_value(); - auto path = TRY(String::from_utf8(path_pattern)); + auto path = path_pattern; if (match.path_pattern.contains("%digit"sv)) { auto replacement = TRY(build_suffix_with_numbers(allocated_suffix_index)); path = TRY(path.replace("%digit"sv, replacement, ReplaceMode::All)); diff --git a/Userland/Shell/ImmediateFunctions.cpp b/Userland/Shell/ImmediateFunctions.cpp index dc3acba5c2..bdb76c280d 100644 --- a/Userland/Shell/ImmediateFunctions.cpp +++ b/Userland/Shell/ImmediateFunctions.cpp @@ -256,7 +256,7 @@ ErrorOr> Shell::immediate_remove_suffix(AST::ImmediateExpressi Vector> nodes; for (auto& value_str : values) { - String removed = TRY(String::from_utf8(value_str)); + String removed = value_str; if (value_str.bytes_as_string_view().ends_with(suffix_str)) removed = TRY(removed.substring_from_byte_offset(0, value_str.bytes_as_string_view().length() - suffix_str.bytes_as_string_view().length())); @@ -288,7 +288,7 @@ ErrorOr> Shell::immediate_remove_prefix(AST::ImmediateExpressi Vector> nodes; for (auto& value_str : values) { - String removed = TRY(String::from_utf8(value_str)); + String removed = value_str; if (value_str.bytes_as_string_view().starts_with(prefix_str)) removed = TRY(removed.substring_from_byte_offset(prefix_str.bytes_as_string_view().length())); diff --git a/Userland/Shell/PosixParser.cpp b/Userland/Shell/PosixParser.cpp index 07f1732bb8..e88d99ba23 100644 --- a/Userland/Shell/PosixParser.cpp +++ b/Userland/Shell/PosixParser.cpp @@ -213,7 +213,7 @@ void Parser::handle_heredoc_contents() return make_ref_counted( token.position.value_or(empty_position()), - TRY(String::from_utf8(token.value)), + token.value, AST::StringLiteral::EnclosureType::None); }(); @@ -929,7 +929,7 @@ ErrorOr> Parser::parse_function_definition() return make_ref_counted( name.position.value_or(empty_position()).with_end(peek().position.value_or(empty_position())), - AST::NameWithPosition { TRY(String::from_utf8(name.value)), name.position.value_or(empty_position()) }, + AST::NameWithPosition { name.value, name.position.value_or(empty_position()) }, Vector {}, body.release_nonnull()); } @@ -1603,7 +1603,7 @@ ErrorOr> Parser::parse_word() token.position.value_or(empty_position()), make_ref_counted( token.position.value_or(empty_position()), - TRY(String::from_utf8(x.source_expression)), + x.source_expression, AST::StringLiteral::EnclosureType::DoubleQuotes)), }, Optional {});