This commit is contained in:
J. Nick Koston
2026-01-16 22:47:33 -10:00
parent 798d3bd956
commit 1facf851b0
3 changed files with 22 additions and 5 deletions
+3 -5
View File
@@ -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
+6
View File
@@ -174,6 +174,12 @@ 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;
}
+13
View File
@@ -517,12 +517,25 @@ template<typename T> constexpr T convert_little_endian(T val) {
bool str_equals_case_insensitive(const std::string &a, const std::string &b);
/// Compare StringRefs for equality in case-insensitive manner.
bool str_equals_case_insensitive(StringRef a, StringRef b);
/// Compare C strings for equality in case-insensitive manner (no heap allocation).
inline bool str_equals_case_insensitive(const char *a, const char *b) { return strcasecmp(a, b) == 0; }
inline bool str_equals_case_insensitive(const std::string &a, const char *b) { return strcasecmp(a.c_str(), b) == 0; }
inline bool str_equals_case_insensitive(const char *a, const std::string &b) { return strcasecmp(a, b.c_str()) == 0; }
/// Check whether a string starts with a value.
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);