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
+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") {