From 7b7bc14bc900186601d47ad017fd6f08b35879aa Mon Sep 17 00:00:00 2001 From: Michael Ebert Date: Sat, 5 Aug 2023 13:09:08 -0700 Subject: [PATCH] Perf (#237) * speed up localization processing by 200% --- Localization/LocalizationDatabase.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Localization/LocalizationDatabase.cpp b/Localization/LocalizationDatabase.cpp index ace06bd..562139d 100644 --- a/Localization/LocalizationDatabase.cpp +++ b/Localization/LocalizationDatabase.cpp @@ -2,10 +2,10 @@ #include "../CommonFunctions.h" #include "../Log.h" #include "../Parser.h" +#include #include - void commonItems::LocalizationDatabase::ScrapeLocalizations(const ModFilesystem& mod_filesystem, const std::string& localization_folder) { Log(LogLevel::Info) << "Reading Localization..."; @@ -91,13 +91,17 @@ std::pair commonItems::LocalizationDatabase::Determine return {}; } - const std::string partially_stripped_line = std::regex_replace(line, std::regex("^\\s+"), std::string("")); - if (partially_stripped_line.starts_with('#')) + const auto isspace = [](char x) { + return std::isspace(x); + }; + const auto first_non_space = std::ranges::find_if_not(line, isspace); + if (first_non_space == line.end() || *first_non_space == '#') { return {}; } - const std::string stripped_line = std::regex_replace(partially_stripped_line, std::regex("\\s+$"), std::string("")); + const auto last_non_space = std::ranges::find_if_not(line.rbegin(), line.rend(), isspace); + const std::string_view stripped_line(first_non_space, last_non_space.base()); const auto separator_location = stripped_line.find_first_of(':'); if (separator_location == std::string::npos) { @@ -136,11 +140,11 @@ std::pair commonItems::LocalizationDatabase::Determine const auto quote_two_index = new_line.find_last_of('\"'); if (quote_index == std::string::npos || quote_two_index == std::string::npos || quote_two_index - quote_index == 0) { - return {key, ""}; + return {std::string(key), ""}; } const auto value = new_line.substr(quote_index + 1, quote_two_index - quote_index - 1); - return {key, value}; + return {std::string(key), std::string(value)}; } @@ -153,4 +157,4 @@ int commonItems::LocalizationDatabase::ScrapeFile(const std::string& file_path) } return ScrapeStream(file); -} \ No newline at end of file +}