diff options
-rw-r--r-- | src/unittests/utils/stringutils.cc | 21 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 31 | ||||
-rw-r--r-- | src/utils/stringutils.h | 2 |
3 files changed, 54 insertions, 0 deletions
diff --git a/src/unittests/utils/stringutils.cc b/src/unittests/utils/stringutils.cc index 0a865d633..cf0fb9764 100644 --- a/src/unittests/utils/stringutils.cc +++ b/src/unittests/utils/stringutils.cc @@ -1451,6 +1451,27 @@ TEST_CASE("stringuntils secureChatCommand", "") REQUIRE(str == "_#test"); } +TEST_CASE("stringuntils rfindSepatator", "") +{ + REQUIRE(rfindSepatator("test1") == std::string("test1").rfind('/')); + REQUIRE(rfindSepatator("test2/") == std::string("test2/").rfind('/')); + REQUIRE(rfindSepatator("test3\\") == std::string("test3\\").rfind('\\')); + REQUIRE(rfindSepatator("test4/qqq") == + std::string("test4/qqq").rfind('/')); + REQUIRE(rfindSepatator("test5\\qqq") == + std::string("test5\\qqq").rfind('\\')); + REQUIRE(rfindSepatator("test6/qqq/www") == + std::string("test6/qqq/www").rfind('/')); + REQUIRE(rfindSepatator("test7\\qqq\\www") == + std::string("test7\\qqq\\www").rfind('\\')); + REQUIRE(rfindSepatator("/test8") == std::string("/test8").rfind('/')); + REQUIRE(rfindSepatator("\\test9") == std::string("\\test9").rfind('\\')); + REQUIRE(rfindSepatator("test10/qqq\\www") == + std::string("test10/qqq\\www").rfind('\\')); + REQUIRE(rfindSepatator("test11\\qqq/www") == + std::string("test11\\qqq/www").rfind('/')); +} + #ifdef ENABLE_NLS TEST_CASE("stringuntils timeDiffToString", "") { diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 45be9ef9c..3af63f96e 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -1157,6 +1157,37 @@ std::string urlJoin(std::string str1, } } +size_t rfindSepatator(const std::string &str1) +{ + const size_t idx1 = str1.rfind('/'); + const size_t idx2 = str1.rfind('\\'); + if (idx1 != std::string::npos) + { // idx1 + if (idx2 != std::string::npos) + { // idx1, idx2 + if (idx1 >= idx2) + return idx1; + else + return idx2; + } + else + { // idx1, not idx2 + return idx1; + } + } + else + { // not idx1 + if (idx2 != std::string::npos) + { // not idx1, idx2 + return idx2; + } + else + { // not idx1, not idx2 + return std::string::npos; + } + } +} + #ifndef DYECMD void replaceItemLinks(std::string &msg) { diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index bb5b370a8..8cd714dd3 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -293,4 +293,6 @@ std::string pathJoin(std::string str1, std::string urlJoin(std::string str1, const std::string &str2); +size_t rfindSepatator(const std::string &str1); + #endif // UTILS_STRINGUTILS_H |