mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 08:50:22 +00:00
use stringref
This commit is contained in:
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user