From 701ba8eaa578135dd16a3fac0cff20b8b317ea09 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Oct 2026 15:42:05 -1000 Subject: [PATCH] [key_collector] Stop copying the key strings (#21210) --- .../components/key_collector/key_collector.h | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/esphome/components/key_collector/key_collector.h b/esphome/components/key_collector/key_collector.h index e49cac46108..a0d8e21de1e 100644 --- a/esphome/components/key_collector/key_collector.h +++ b/esphome/components/key_collector/key_collector.h @@ -4,6 +4,7 @@ #include "esphome/components/key_provider/key_provider.h" #include "esphome/core/automation.h" #include "esphome/core/helpers.h" +#include "esphome/core/string_ref.h" namespace esphome::key_collector { @@ -14,12 +15,13 @@ class KeyCollector final : public Component { void add_provider(key_provider::KeyProvider *provider); void set_min_length(uint16_t min_length) { this->min_length_ = min_length; }; void set_max_length(uint16_t max_length) { this->max_length_ = max_length; }; - void set_start_keys(std::string start_keys) { this->start_keys_ = std::move(start_keys); }; - void set_end_keys(std::string end_keys) { this->end_keys_ = std::move(end_keys); }; + // The key setters keep the pointer; the strings must outlive the component (codegen passes literals) + void set_start_keys(const char *start_keys) { this->start_keys_ = StringRef(start_keys); }; + void set_end_keys(const char *end_keys) { this->end_keys_ = StringRef(end_keys); }; void set_end_key_required(bool end_key_required) { this->end_key_required_ = end_key_required; }; - void set_back_keys(std::string back_keys) { this->back_keys_ = std::move(back_keys); }; - void set_clear_keys(std::string clear_keys) { this->clear_keys_ = std::move(clear_keys); }; - void set_allowed_keys(std::string allowed_keys) { this->allowed_keys_ = std::move(allowed_keys); }; + void set_back_keys(const char *back_keys) { this->back_keys_ = StringRef(back_keys); }; + void set_clear_keys(const char *clear_keys) { this->clear_keys_ = StringRef(clear_keys); }; + void set_allowed_keys(const char *allowed_keys) { this->allowed_keys_ = StringRef(allowed_keys); }; template void add_on_progress_callback(F &&callback) { this->progress_callbacks_.add(std::forward(callback)); } @@ -38,12 +40,12 @@ class KeyCollector final : public Component { protected: uint16_t min_length_{0}; uint16_t max_length_{0}; - std::string start_keys_; - std::string end_keys_; + StringRef start_keys_; + StringRef end_keys_; bool end_key_required_{false}; - std::string back_keys_; - std::string clear_keys_; - std::string allowed_keys_; + StringRef back_keys_; + StringRef clear_keys_; + StringRef allowed_keys_; std::string result_; uint8_t start_key_{0}; LazyCallbackManager progress_callbacks_;