mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[text_sensor] Fix infinite loop in substitute filter (#11989)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
@@ -66,10 +66,14 @@ SubstituteFilter::SubstituteFilter(const std::initializer_list<Substitution> &su
|
|||||||
: substitutions_(substitutions) {}
|
: substitutions_(substitutions) {}
|
||||||
|
|
||||||
optional<std::string> SubstituteFilter::new_value(std::string value) {
|
optional<std::string> SubstituteFilter::new_value(std::string value) {
|
||||||
std::size_t pos;
|
|
||||||
for (const auto &sub : this->substitutions_) {
|
for (const auto &sub : this->substitutions_) {
|
||||||
while ((pos = value.find(sub.from)) != std::string::npos)
|
std::size_t pos = 0;
|
||||||
|
while ((pos = value.find(sub.from, pos)) != std::string::npos) {
|
||||||
value.replace(pos, sub.from.size(), sub.to);
|
value.replace(pos, sub.from.size(), sub.to);
|
||||||
|
// Advance past the replacement to avoid infinite loop when
|
||||||
|
// the replacement contains the search pattern (e.g., f -> foo)
|
||||||
|
pos += sub.to.size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user