use stringref

This commit is contained in:
J. Nick Koston
2026-01-09 15:40:18 -10:00
parent ab3ab6f521
commit 1fdacd9d22
5 changed files with 16 additions and 21 deletions
+2 -4
View File
@@ -442,10 +442,8 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co
}
if (traits.supports_direction())
msg.direction = static_cast<enums::FanDirection>(fan->direction);
if (traits.supports_preset_modes() && fan->has_preset_mode()) {
auto preset = fan->get_preset_mode();
msg.preset_mode = StringRef(preset.data(), preset.size());
}
if (traits.supports_preset_modes() && fan->has_preset_mode())
msg.preset_mode = fan->get_preset_mode();
return fill_and_encode_entity_state(fan, msg, FanStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
}
uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
+1 -1
View File
@@ -223,7 +223,7 @@ class FanPresetSetTrigger : public Trigger<std::string> {
}
protected:
std::string_view last_preset_mode_{};
StringRef last_preset_mode_{};
};
} // namespace fan
+3 -3
View File
@@ -179,10 +179,10 @@ bool Fan::set_preset_mode_(const std::string &preset_mode) {
return this->set_preset_mode_(preset_mode.data(), preset_mode.size());
}
bool Fan::set_preset_mode_(std::string_view preset_mode) {
bool Fan::set_preset_mode_(StringRef preset_mode) {
// Safe: find_preset_mode_ only uses the input for comparison and returns
// a pointer from traits, so the input string_view's lifetime doesn't matter.
return this->set_preset_mode_(preset_mode.data(), preset_mode.size());
// a pointer from traits, so the input StringRef's lifetime doesn't matter.
return this->set_preset_mode_(preset_mode.c_str(), preset_mode.size());
}
void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; }
+5 -8
View File
@@ -1,12 +1,11 @@
#pragma once
#include <string_view>
#include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/optional.h"
#include "esphome/core/preferences.h"
#include "esphome/core/string_ref.h"
#include "fan_traits.h"
namespace esphome {
@@ -131,12 +130,10 @@ class Fan : public EntityBase {
void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
/// Get the current preset mode.
/// Returns a view of the string stored in traits, or empty view if not set.
/// The returned view points to string literals from codegen (static storage).
/// Returns a StringRef of the string stored in traits, or empty ref if not set.
/// The returned ref points to string literals from codegen (static storage).
/// Traits are set once at startup and valid for the lifetime of the program.
std::string_view get_preset_mode() const {
return this->preset_mode_ != nullptr ? std::string_view(this->preset_mode_) : std::string_view();
}
StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
/// Check if a preset mode is currently active
bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
@@ -157,7 +154,7 @@ class Fan : public EntityBase {
bool set_preset_mode_(const char *preset_mode, size_t len);
bool set_preset_mode_(const char *preset_mode);
bool set_preset_mode_(const std::string &preset_mode);
bool set_preset_mode_(std::string_view preset_mode);
bool set_preset_mode_(StringRef preset_mode);
/// Clear the preset mode
void clear_preset_mode_();
/// Apply preset mode from a FanCall (handles speed-clears-preset convention)
+5 -5
View File
@@ -10,7 +10,7 @@ fan:
has_direction: true
speed_count: 3
# Test lambdas using get_preset_mode() which returns std::string_view
# Test lambdas using get_preset_mode() which returns StringRef
# These examples match the migration guide in the PR description
binary_sensor:
- platform: template
@@ -44,13 +44,13 @@ binary_sensor:
std::string preset = std::string(id(test_fan).get_preset_mode());
// Migration guide: Logging option 1
// Use .data() - works because string_view points to null-terminated string in traits
ESP_LOGD("test", "Preset: %s", id(test_fan).get_preset_mode().data());
// Use .c_str() - works because StringRef points to null-terminated string in traits
ESP_LOGD("test", "Preset: %s", id(test_fan).get_preset_mode().c_str());
// Migration guide: Logging option 2
// Use %.*s format (safer, no null-termination assumption)
auto preset_view = id(test_fan).get_preset_mode();
ESP_LOGD("test", "Preset: %.*s", (int)preset_view.size(), preset_view.data());
auto preset_ref = id(test_fan).get_preset_mode();
ESP_LOGD("test", "Preset: %.*s", (int)preset_ref.size(), preset_ref.c_str());
// Test != comparison
if (id(test_fan).get_preset_mode() != "Sleep") {