diff options
| author | Leonetienne <leonetienne@hotmail.de> | 2022-03-13 15:52:53 +0100 |
|---|---|---|
| committer | Leonetienne <leonetienne@hotmail.de> | 2022-03-13 15:52:53 +0100 |
| commit | 25bd26972945f956ad4c92ce8a61bb6126c0dedb (patch) | |
| tree | 9548e39ced9cd541586a8dd5c12638a0edb6a1ca | |
| parent | 5f791b8d9f66b53e1019abfe6a13b6e86fe5ca51 (diff) | |
Implementation Split
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Exec/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | Exec/main.cpp | 6 | ||||
| -rw-r--r-- | Src/StringTools.cpp | 28 | ||||
| -rw-r--r-- | Src/StringTools.h | 34 |
5 files changed, 60 insertions, 20 deletions
@@ -5,7 +5,7 @@ .idea/ # CMake -cmake-build-*/ +*build*/ # Mongo Explorer plugin .idea/**/mongoSettings.xml diff --git a/Exec/CMakeLists.txt b/Exec/CMakeLists.txt index d42e35f..c51da8b 100644 --- a/Exec/CMakeLists.txt +++ b/Exec/CMakeLists.txt @@ -4,7 +4,11 @@ project(Exec) set(CMAKE_CXX_STANDARD 17)
include_directories(../Src)
-link_directories(../Src/cmake-build-debug)
-add_executable(Exec main.cpp)
-target_link_libraries(Exec StringTools)
+FILE(GLOB StringTools ../Src/*.cpp)
+
+add_executable(Exec
+ ${StringTools}
+
+ main.cpp
+)
diff --git a/Exec/main.cpp b/Exec/main.cpp index b833214..bb337a5 100644 --- a/Exec/main.cpp +++ b/Exec/main.cpp @@ -3,7 +3,11 @@ int main()
{
- std::cout << StringTools::Replace("Hello, ${where}!\n", "${where}", "World") << std::endl;
+ std::vector<std::string> foo =
+ StringTools::Split("Hello, lol, test", ", ");
+
+ for (const auto& it : foo)
+ std::cout << "'" << it << "'" << std::endl;
return 0;
}
diff --git a/Src/StringTools.cpp b/Src/StringTools.cpp index 931bd75..302fad9 100644 --- a/Src/StringTools.cpp +++ b/Src/StringTools.cpp @@ -92,3 +92,31 @@ std::string StringTools::Upper(const std::string& str) { return ss.str();
}
+
+std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
+ std::vector<std::string> toRet;
+
+ // Quick-accept: seperator length is 0
+ if (seperator.length() == 0) {
+ for (const char c : str)
+ toRet.push_back(std::string(&c, (&c) + 1));
+ }
+
+ else {
+ std::size_t idx = 0;
+ while (idx != std::string::npos) {
+ std::size_t lastIdx = idx;
+ idx = str.find(seperator, idx + seperator.length());
+
+ toRet.push_back(str.substr(
+ lastIdx,
+ idx - lastIdx
+ ));
+
+ if (idx != std::string::npos)
+ idx += seperator.length();
+ }
+ }
+
+ return toRet;
+}
diff --git a/Src/StringTools.h b/Src/StringTools.h index 0c08805..d65772a 100644 --- a/Src/StringTools.h +++ b/Src/StringTools.h @@ -2,32 +2,36 @@ #define STRINGTOOLS_STRINGTOOLS_H
#include <string>
+#include <vector>
/* Handy utensils to manipulate strings */
class StringTools
{
public:
- //! Will replace every occurence of `find` in `str` by `subst`.
- static std::string Replace(const std::string& str, const char find, const std::string& subst);
+ //! Will replace every occurence of `find` in `str` by `subst`.
+ static std::string Replace(const std::string& str, const char find, const std::string& subst);
- //! Will replace every occurence of `find` in `str` by `subst`.
- static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
-
- //! Will replace every occurence of `find` in `str` by `subst`.
- static std::string Replace(const std::string& str, const char find, const char subst);
+ //! Will replace every occurence of `find` in `str` by `subst`.
+ static std::string Replace(const std::string& str, const std::string& find, const std::string& subst);
- //! Will replace every occurence of `find` in `str` by `subst`.
- static std::string Replace(const std::string& str, const std::string& find, const char subst);
+ //! Will replace every occurence of `find` in `str` by `subst`.
+ static std::string Replace(const std::string& str, const char find, const char subst);
- //! Will make a string all-lowercase.
- static std::string Lower(const std::string& str);
+ //! Will replace every occurence of `find` in `str` by `subst`.
+ static std::string Replace(const std::string& str, const std::string& find, const char subst);
- //! Will make a string all-uppercase.
- static std::string Upper(const std::string& str);
+ //! Will make a string all-lowercase.
+ static std::string Lower(const std::string& str);
+
+ //! Will make a string all-uppercase.
+ static std::string Upper(const std::string& str);
+
+ //! Will split a string by a string seperator
+ static std::vector<std::string> Split(const std::string& str, const std::string& seperator);
private:
- // No instanciation! >:(
- StringTools();
+ // No instanciation! >:(
+ StringTools();
};
#endif //STRINGTOOLS_STRINGTOOLS_H
|
