srctree

Robin Linden parent 4e3f8326 67152010
all: Replace container.contains + container.at w/ container.find

Looking things up once is better than looking them up twice.

inlinesplit
css/property_id.cpp added: 28, removed: 26, total 2
@@ -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) {
 
gfx/color.cpp added: 28, removed: 26, total 2
@@ -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
 
html2/tokenizer.cpp added: 28, removed: 26, total 2
@@ -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;
}
 
temporary_buffer_ = util::unicode_to_utf8(character_reference_code_);
 
layout/layout.cpp added: 28, removed: 26, total 2
@@ -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);
 
protocol/multi_protocol_handler.h added: 28, removed: 26, total 2
@@ -19,11 +19,11 @@ public:
}
 
[[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:
 
style/style.cpp added: 28, removed: 26, total 2
@@ -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
@@ -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;
 
style/styled_node.cpp added: 28, removed: 26, total 2
@@ -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 = [&] {