Improved the string trim function and replaced some inlined occurrences.

This commit is contained in:
Leon Styhre 2021-09-25 18:01:41 +02:00
parent 53630e3a7a
commit 3233288a8c
5 changed files with 16 additions and 38 deletions

View file

@ -555,13 +555,7 @@ std::string CollectionSystemsManager::getValidNewCollectionName(std::string inNa
std::string name = inName;
// Trim leading and trailing whitespaces.
name.erase(name.begin(), std::find_if(name.begin(), name.end(), [](char c) {
return !std::isspace(static_cast<unsigned char>(c));
}));
name.erase(std::find_if(name.rbegin(), name.rend(),
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
.base(),
name.end());
name = Utils::String::trim(name);
if (index == 0) {
size_t remove = std::string::npos;

View file

@ -794,14 +794,7 @@ void GuiScraperSearch::openInputScreen(ScraperSearchParams& params)
{
auto searchForFunc = [&](std::string name) {
// Trim leading and trailing whitespaces.
name.erase(name.begin(), std::find_if(name.begin(), name.end(), [](char c) {
return !std::isspace(static_cast<unsigned char>(c));
}));
name.erase(std::find_if(name.rbegin(), name.rend(),
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
.base(),
name.end());
name = Utils::String::trim(name);
stop();
mRefinedSearch = true;
params.nameOverride = name;

View file

@ -160,15 +160,7 @@ void thegamesdb_generate_json_scraper_requests(
}
// Trim leading and trailing whitespaces.
cleanName.erase(cleanName.begin(),
std::find_if(cleanName.begin(), cleanName.end(), [](char c) {
return !std::isspace(static_cast<unsigned char>(c));
}));
cleanName.erase(
std::find_if(cleanName.rbegin(), cleanName.rend(),
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
.base(),
cleanName.end());
cleanName = Utils::String::trim(cleanName);
path += "/Games/ByGameName?" + apiKey +
"&fields=players,publishers,genres,overview,last_updated,rating,"

View file

@ -553,15 +553,7 @@ std::string ScreenScraperRequest::ScreenScraperConfig::getGameSearchUrl(
bool singleSearch = false;
// Trim leading and trailing whitespaces.
searchName.erase(searchName.begin(),
std::find_if(searchName.begin(), searchName.end(), [](char c) {
return !std::isspace(static_cast<unsigned char>(c));
}));
searchName.erase(
std::find_if(searchName.rbegin(), searchName.rend(),
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
.base(),
searchName.end());
searchName = Utils::String::trim(searchName);
// If only whitespaces were entered as the search string, then search using a random string
// that will not return any results. This is a quick and dirty way to avoid french error

View file

@ -564,13 +564,20 @@ namespace Utils
std::string trim(const std::string& stringArg)
{
const size_t strBegin = stringArg.find_first_not_of(" \t");
const size_t strEnd = stringArg.find_last_not_of(" \t");
std::string trimString = stringArg;
if (strBegin == std::string::npos)
return "";
// Trim leading and trailing whitespaces.
trimString.erase(trimString.begin(),
std::find_if(trimString.begin(), trimString.end(), [](char c) {
return !std::isspace(static_cast<unsigned char>(c));
}));
trimString.erase(
std::find_if(trimString.rbegin(), trimString.rend(),
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
.base(),
trimString.end());
return stringArg.substr(strBegin, strEnd - strBegin + 1);
return trimString;
}
std::string replace(const std::string& stringArg,