perf: Avoid creating strings for file extension checks (#1303)

## Summary

**What is the goal of this PR?**

This change avoids the pattern of creating a `std::string` using
`.substr` in order to compare against a file extension literal.
```c++
std::string path;
if (path.length() >= 4 && path.substr(path.length() - 4) == ".ext")
```

The `checkFileExtension` utility has moved from StringUtils to
FsHelpers, to be available to code in lib/. The signature now accepts a
`std::string_view` instead of `std::string`, which makes the single
implementation reusable for Arduino `String`.

Added utility functions for commonly repeated extensions.

These changes **save about 2 KB of flash (5,999,427 to 5,997,343)**.

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**NO**_
This commit is contained in:
Zach Nelson
2026-03-05 10:12:22 -06:00
committed by GitHub
parent ea88797c8e
commit c3f1dbfa09
19 changed files with 160 additions and 151 deletions

View File

@@ -16,7 +16,6 @@
#include "html/FilesPageHtml.generated.h"
#include "html/HomePageHtml.generated.h"
#include "html/SettingsPageHtml.generated.h"
#include "util/StringUtils.h"
namespace {
// Folders/files to hide from the web interface file browser
@@ -44,7 +43,7 @@ unsigned long wsLastCompleteAt = 0;
// Helper function to clear epub cache after upload
void clearEpubCacheIfNeeded(const String& filePath) {
// Only clear cache for .epub files
if (StringUtils::checkFileExtension(filePath, ".epub")) {
if (FsHelpers::hasEpubExtension(filePath)) {
Epub(filePath.c_str(), "/.crosspoint").clearCache();
LOG_DBG("WEB", "Cleared epub cache for: %s", filePath.c_str());
}
@@ -391,11 +390,7 @@ void CrossPointWebServer::scanFiles(const char* path, const std::function<void(F
root.close();
}
bool CrossPointWebServer::isEpubFile(const String& filename) const {
String lower = filename;
lower.toLowerCase();
return lower.endsWith(".epub");
}
bool CrossPointWebServer::isEpubFile(const String& filename) const { return FsHelpers::hasEpubExtension(filename); }
void CrossPointWebServer::handleFileList() const {
sendHtmlContent(server.get(), FilesPageHtml, sizeof(FilesPageHtml));

View File

@@ -6,8 +6,6 @@
#include <Logging.h>
#include <esp_task_wdt.h>
#include "util/StringUtils.h"
namespace {
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
@@ -801,28 +799,26 @@ bool WebDAVHandler::getOverwrite(WebServer& s) const {
}
void WebDAVHandler::clearEpubCacheIfNeeded(const String& path) const {
if (StringUtils::checkFileExtension(path, ".epub")) {
if (FsHelpers::hasEpubExtension(path)) {
Epub(path.c_str(), "/.crosspoint").clearCache();
LOG_DBG("DAV", "Cleared epub cache for: %s", path.c_str());
}
}
String WebDAVHandler::getMimeType(const String& path) const {
if (StringUtils::checkFileExtension(path, ".epub")) return "application/epub+zip";
if (StringUtils::checkFileExtension(path, ".pdf")) return "application/pdf";
if (StringUtils::checkFileExtension(path, ".txt")) return "text/plain";
if (StringUtils::checkFileExtension(path, ".html") || StringUtils::checkFileExtension(path, ".htm"))
return "text/html";
if (StringUtils::checkFileExtension(path, ".css")) return "text/css";
if (StringUtils::checkFileExtension(path, ".js")) return "application/javascript";
if (StringUtils::checkFileExtension(path, ".json")) return "application/json";
if (StringUtils::checkFileExtension(path, ".xml")) return "application/xml";
if (StringUtils::checkFileExtension(path, ".jpg") || StringUtils::checkFileExtension(path, ".jpeg"))
return "image/jpeg";
if (StringUtils::checkFileExtension(path, ".png")) return "image/png";
if (StringUtils::checkFileExtension(path, ".gif")) return "image/gif";
if (StringUtils::checkFileExtension(path, ".svg")) return "image/svg+xml";
if (StringUtils::checkFileExtension(path, ".zip")) return "application/zip";
if (StringUtils::checkFileExtension(path, ".gz")) return "application/gzip";
if (FsHelpers::hasEpubExtension(path)) return "application/epub+zip";
if (FsHelpers::checkFileExtension(path, ".pdf")) return "application/pdf";
if (FsHelpers::hasTxtExtension(path)) return "text/plain";
if (FsHelpers::checkFileExtension(path, ".html") || FsHelpers::checkFileExtension(path, ".htm")) return "text/html";
if (FsHelpers::checkFileExtension(path, ".css")) return "text/css";
if (FsHelpers::checkFileExtension(path, ".js")) return "application/javascript";
if (FsHelpers::checkFileExtension(path, ".json")) return "application/json";
if (FsHelpers::checkFileExtension(path, ".xml")) return "application/xml";
if (FsHelpers::hasJpgExtension(path)) return "image/jpeg";
if (FsHelpers::hasPngExtension(path)) return "image/png";
if (FsHelpers::hasGifExtension(path)) return "image/gif";
if (FsHelpers::checkFileExtension(path, ".svg")) return "image/svg+xml";
if (FsHelpers::checkFileExtension(path, ".zip")) return "application/zip";
if (FsHelpers::checkFileExtension(path, ".gz")) return "application/gzip";
return "application/octet-stream";
}