mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-04 14:47:37 +03:00
## Summary * Truncating chapter titles using utf8 safe functions (Cyrillic titles were split mid codepoint) * refactoring of lib/Utf8 --- ### 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? _**< PARTIALLY >**_
65 lines
1.7 KiB
C++
65 lines
1.7 KiB
C++
#include "StringUtils.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace StringUtils {
|
|
|
|
std::string sanitizeFilename(const std::string& name, size_t maxLength) {
|
|
std::string result;
|
|
result.reserve(name.size());
|
|
|
|
for (char c : name) {
|
|
// Replace invalid filename characters with underscore
|
|
if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|') {
|
|
result += '_';
|
|
} else if (c >= 32 && c < 127) {
|
|
// Keep printable ASCII characters
|
|
result += c;
|
|
}
|
|
// Skip non-printable characters
|
|
}
|
|
|
|
// Trim leading/trailing spaces and dots
|
|
size_t start = result.find_first_not_of(" .");
|
|
if (start == std::string::npos) {
|
|
return "book"; // Fallback if name is all invalid characters
|
|
}
|
|
size_t end = result.find_last_not_of(" .");
|
|
result = result.substr(start, end - start + 1);
|
|
|
|
// Limit filename length
|
|
if (result.length() > maxLength) {
|
|
result.resize(maxLength);
|
|
}
|
|
|
|
return result.empty() ? "book" : result;
|
|
}
|
|
|
|
bool checkFileExtension(const std::string& fileName, const char* extension) {
|
|
if (fileName.length() < strlen(extension)) {
|
|
return false;
|
|
}
|
|
|
|
const std::string fileExt = fileName.substr(fileName.length() - strlen(extension));
|
|
for (size_t i = 0; i < fileExt.length(); i++) {
|
|
if (tolower(fileExt[i]) != tolower(extension[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool checkFileExtension(const String& fileName, const char* extension) {
|
|
if (fileName.length() < strlen(extension)) {
|
|
return false;
|
|
}
|
|
|
|
String localFile(fileName);
|
|
String localExtension(extension);
|
|
localFile.toLowerCase();
|
|
localExtension.toLowerCase();
|
|
return localFile.endsWith(localExtension);
|
|
}
|
|
|
|
} // namespace StringUtils
|