From 42e5d98a483463493409465f90ce3c52fd842f27 Mon Sep 17 00:00:00 2001 From: Force Charlie Date: Fri, 22 Dec 2017 17:52:50 +0800 Subject: [PATCH] fix split_by_extenstion parse error extenstion --- include/spdlog/details/file_helper.h | 15 ++++++++------- tests/file_helper.cpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 79150531..3b8df9c1 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -118,17 +118,18 @@ public: // // "my_folder/.mylog" => ("my_folder/.mylog") // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") - static std::tuple split_by_extenstion(const filename_t& fname) { auto index = fname.rfind('.'); - bool found_ext = index != filename_t::npos && index !=0 && fname[index - 1] != details::os::folder_sep; - if (found_ext) - return std::make_tuple(fname.substr(0, index), fname.substr(index)); - else - return std::make_tuple(fname, filename_t()); + if (index != filename_t::npos && index != fname.size() - 1 &&index !=0 && fname[index - 1] != details::os::folder_sep) + { + auto index2 = fname.find(details::os::folder_sep, index); + if (index2 == fname.npos) { + return std::make_tuple(fname.substr(0, index), fname.substr(index)); + } + } + return std::make_tuple(fname, std::string()); } - private: FILE* _fd; filename_t _filename; diff --git a/tests/file_helper.cpp b/tests/file_helper.cpp index 62997e3e..6c8ee79f 100644 --- a/tests/file_helper.cpp +++ b/tests/file_helper.cpp @@ -145,5 +145,19 @@ TEST_CASE("file_helper_split_by_extenstion7", "[file_helper::split_by_extenstion } +TEST_CASE("file_helper_split_by_extenstion8", "[file_helper::split_by_extenstion(hidden_file)]]") +{ +#ifdef _WIN32 + auto filename = "folder.ext\\mylog"; + auto expected_basename = "folder.ext\\mylog"; +#else + auto filename = "folder.ext/mylog"; + auto expected_basename = "folder.ext/mylog"; +#endif + std::string basename, ext; + std::tie(basename, ext) = file_helper::split_by_extenstion(filename); + REQUIRE(basename == expected_basename); + REQUIRE(ext == ""); +}