From 42e6b4326fc2052ff5f8a2c52f549a16c97dfc4b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Oct 2025 18:51:19 -0500 Subject: [PATCH] simplify --- esphome/components/climate/climate_traits.h | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/esphome/components/climate/climate_traits.h b/esphome/components/climate/climate_traits.h index 7405918fea..1d4d8b6097 100644 --- a/esphome/components/climate/climate_traits.h +++ b/esphome/components/climate/climate_traits.h @@ -9,6 +9,16 @@ namespace esphome { namespace climate { +// Lightweight linear search for small vectors (1-20 items) of const char* pointers +// Avoids std::find template overhead +inline bool vector_contains(const std::vector &vec, const char *value) { + for (const char *item : vec) { + if (strcmp(item, value) == 0) + return true; + } + return false; +} + // Type aliases for climate enum bitmasks // These replace std::set to eliminate red-black tree overhead // For contiguous enums starting at 0, DefaultBitPolicy provides 1:1 mapping (enum value = bit position) @@ -136,11 +146,7 @@ class ClimateTraits { } const std::vector &get_supported_custom_fan_modes() const { return this->supported_custom_fan_modes_; } bool supports_custom_fan_mode(const char *custom_fan_mode) const { - for (const char *mode : this->supported_custom_fan_modes_) { - if (strcmp(mode, custom_fan_mode) == 0) - return true; - } - return false; + return vector_contains(this->supported_custom_fan_modes_, custom_fan_mode); } bool supports_custom_fan_mode(const std::string &custom_fan_mode) const { return this->supports_custom_fan_mode(custom_fan_mode.c_str()); @@ -163,11 +169,7 @@ class ClimateTraits { } const std::vector &get_supported_custom_presets() const { return this->supported_custom_presets_; } bool supports_custom_preset(const char *custom_preset) const { - for (const char *preset : this->supported_custom_presets_) { - if (strcmp(preset, custom_preset) == 0) - return true; - } - return false; + return vector_contains(this->supported_custom_presets_, custom_preset); } bool supports_custom_preset(const std::string &custom_preset) const { return this->supports_custom_preset(custom_preset.c_str());