Skip to content

Commit

Permalink
all: Replace container.contains + container.at w/ container.find
Browse files Browse the repository at this point in the history
Looking things up once is better than looking them up twice.
  • Loading branch information
robinlinden committed Nov 19, 2023
1 parent 4e3f832 commit 6715201
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions css/property_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ std::map<std::string_view, PropertyId> const known_properties{
} // namespace

PropertyId property_id_from_string(std::string_view id) {
if (!known_properties.contains(id)) {
return PropertyId::Unknown;
if (auto it = known_properties.find(id); it != end(known_properties)) {
return it->second;
}

return known_properties.at(id);
return PropertyId::Unknown;
}

std::string_view to_string(PropertyId id) {
Expand Down
6 changes: 3 additions & 3 deletions gfx/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ std::map<std::string_view, gfx::Color, CaseInsensitiveLess> const named_colors{
} // namespace

std::optional<Color> Color::from_css_name(std::string_view name) {
if (!named_colors.contains(name)) {
return std::nullopt;
if (auto it = named_colors.find(name); it != end(named_colors)) {
return it->second;
}

return named_colors.at(name);
return std::nullopt;
}

} // namespace gfx
4 changes: 2 additions & 2 deletions html2/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2436,8 +2436,8 @@ void Tokenizer::run() {
{0x9E, 0x017E},
{0x9F, 0x0178}};

if (replacements.contains(character_reference_code_)) {
character_reference_code_ = replacements.at(character_reference_code_);
if (auto it = replacements.find(character_reference_code_); it != replacements.end()) {
character_reference_code_ = it->second;

Check warning on line 2440 in html2/tokenizer.cpp

View check run for this annotation

Codecov / codecov/patch

html2/tokenizer.cpp#L2440

Added line #L2440 was not covered by tests
}

temporary_buffer_ = util::unicode_to_utf8(character_reference_code_);
Expand Down
4 changes: 2 additions & 2 deletions layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ std::map<std::string_view, int> const border_width_keywords{

void calculate_border(LayoutBox &box, int const font_size, int const root_font_size) {
auto as_px = [&](std::string_view border_width_property) {
if (border_width_keywords.contains(border_width_property)) {
return border_width_keywords.at(border_width_property);
if (auto it = border_width_keywords.find(border_width_property); it != border_width_keywords.end()) {
return it->second;
}

return to_px(border_width_property, font_size, root_font_size);
Expand Down
6 changes: 3 additions & 3 deletions protocol/multi_protocol_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class MultiProtocolHandler final : public IProtocolHandler {
}

[[nodiscard]] Response handle(uri::Uri const &uri) override {
if (!handlers_.contains(uri.scheme)) {
return {Error::Unhandled};
if (auto it = handlers_.find(uri.scheme); it != handlers_.end()) {
return it->second->handle(uri);
}

return handlers_[uri.scheme]->handle(uri);
return {Error::Unhandled};
}

private:
Expand Down
10 changes: 6 additions & 4 deletions style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ using namespace std::literals;
namespace style {
namespace {
bool has_class(dom::Element const &element, std::string_view needle_class) {
if (!element.attributes.contains("class")) {
auto it = element.attributes.find("class");
if (it == element.attributes.end()) {
return false;
}

auto classes = util::split(element.attributes.at("class"), " ");
auto classes = util::split(it->second, " ");
return std::ranges::any_of(classes, [&](auto const &c) { return c == needle_class; });
}
} // namespace
Expand Down Expand Up @@ -120,9 +121,10 @@ bool is_match(style::StyledNode const &node, std::string_view selector) {
return has_class(element, selector_);
}

if (selector_.starts_with('#') && element.attributes.contains("id")) {
if (selector_.starts_with('#')) {
auto it = element.attributes.find("id");
selector_.remove_prefix(1);
return element.attributes.at("id") == selector_;
return it != element.attributes.end() && it->second == selector_;
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions style/styled_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ int StyledNode::get_font_size_property() const {
}
auto raw_value = closest->first;

if (font_size_absolute_size_keywords.contains(raw_value)) {
return std::lround(font_size_absolute_size_keywords.at(raw_value) * kMediumFontSize);
if (auto it = font_size_absolute_size_keywords.find(raw_value); it != end(font_size_absolute_size_keywords)) {
return std::lround(it->second * kMediumFontSize);
}

auto parent_or_default_font_size = [&] {
Expand Down

0 comments on commit 6715201

Please sign in to comment.