mirror of
https://github.com/esphome/esphome.git
synced 2026-02-18 15:35:59 -07:00
[core] Add str_endswith_ignore_case to avoid heap allocation in audio file type detection (#13313)
This commit is contained in:
@@ -185,18 +185,16 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
||||
return err;
|
||||
}
|
||||
|
||||
std::string url_string = str_lower_case(url);
|
||||
|
||||
if (str_endswith(url_string, ".wav")) {
|
||||
if (str_endswith_ignore_case(url, ".wav")) {
|
||||
file_type = AudioFileType::WAV;
|
||||
}
|
||||
#ifdef USE_AUDIO_MP3_SUPPORT
|
||||
else if (str_endswith(url_string, ".mp3")) {
|
||||
else if (str_endswith_ignore_case(url, ".mp3")) {
|
||||
file_type = AudioFileType::MP3;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_AUDIO_FLAC_SUPPORT
|
||||
else if (str_endswith(url_string, ".flac")) {
|
||||
else if (str_endswith_ignore_case(url, ".flac")) {
|
||||
file_type = AudioFileType::FLAC;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -174,6 +174,13 @@ bool str_endswith(const std::string &str, const std::string &end) {
|
||||
return str.rfind(end) == (str.size() - end.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len) {
|
||||
if (suffix_len > str_len)
|
||||
return false;
|
||||
return strncasecmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
|
||||
}
|
||||
|
||||
std::string str_truncate(const std::string &str, size_t length) {
|
||||
return str.length() > length ? str.substr(0, length) : str;
|
||||
}
|
||||
|
||||
@@ -545,6 +545,15 @@ bool str_startswith(const std::string &str, const std::string &start);
|
||||
/// Check whether a string ends with a value.
|
||||
bool str_endswith(const std::string &str, const std::string &end);
|
||||
|
||||
/// Case-insensitive check if string ends with suffix (no heap allocation).
|
||||
bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len);
|
||||
inline bool str_endswith_ignore_case(const char *str, const char *suffix) {
|
||||
return str_endswith_ignore_case(str, strlen(str), suffix, strlen(suffix));
|
||||
}
|
||||
inline bool str_endswith_ignore_case(const std::string &str, const char *suffix) {
|
||||
return str_endswith_ignore_case(str.c_str(), str.size(), suffix, strlen(suffix));
|
||||
}
|
||||
|
||||
/// Truncate a string to a specific length.
|
||||
/// @warning Allocates heap memory. Avoid in new code - causes heap fragmentation on long-running devices.
|
||||
std::string str_truncate(const std::string &str, size_t length);
|
||||
|
||||
Reference in New Issue
Block a user