From 36e368ff9928d20b42ef3a53a0472af577f8554b Mon Sep 17 00:00:00 2001 From: BreadFish64 Date: Sun, 10 Mar 2019 19:18:09 -0500 Subject: [PATCH] remove Common::TrimSourcePath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wwylele / 白疾風Today at 6:14 PM I doubt the performance of constructing regex everytime the function is called Is TrimSourcePath only called by logging? if so, you can move the implementation into logging, and cache the regex object into global This function is probably too specific to be in common anyway --- src/common/logging/backend.cpp | 5 ++++- src/common/string_util.cpp | 15 --------------- src/common/string_util.h | 12 ------------ 3 files changed, 4 insertions(+), 28 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index f0062779b..d440d910a 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #ifdef _WIN32 @@ -253,13 +254,15 @@ Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsign using std::chrono::duration_cast; using std::chrono::steady_clock; + // matches from the beginning up to the last '../' or 'src/' + static const std::regex trim_source_path(R"(.*([\/\\]|^)((\.\.)|(src))[\/\\])"); static steady_clock::time_point time_origin = steady_clock::now(); Entry entry; entry.timestamp = duration_cast(steady_clock::now() - time_origin); entry.log_class = log_class; entry.log_level = log_level; - entry.filename = Common::TrimSourcePath(filename, {R"(\.\.)", "src"}).data(); + entry.filename = std::regex_replace(filename, trim_source_path, ""); entry.line_num = line_nr; entry.function = function; entry.message = std::move(message); diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 9306c8bed..f3c64884a 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include "common/common_paths.h" #include "common/logging/log.h" @@ -210,18 +209,4 @@ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t return std::string(buffer, len); } - -std::string TrimSourcePath(const std::string& file_path, const std::vector& roots) { - // match from beginning of path to dir sep - std::string regex_src = R"(.*([\/\\]|^)()"; - // plus the last occurrence of any root - for (auto root = roots.begin(); root < roots.end() - 1; ++root) { - regex_src += '(' + *root + ")|"; - } - regex_src += '(' + roots.back() + ')'; - // plus dir sep - regex_src += R"()[\/\\])"; - std::regex regex(regex_src); - return std::regex_replace(file_path, regex, ""); -} } // namespace Common diff --git a/src/common/string_util.h b/src/common/string_util.h index aa0d147a4..110715dce 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -64,16 +64,4 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) { */ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len); -/** - * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's - * intended to be used to strip a system-specific build directory from the `__FILE__` macro, - * leaving only the path relative to the sources root. - * - * @param path The input file path as a string - * @param roots The name of the root source directorys as a vector of strings. Path up to and - * including the last occurrence of these names will be stripped - * @return The trimmed path as a string - */ -std::string TrimSourcePath(const std::string& file_path, const std::vector& roots); - } // namespace Common