summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/util/StringUtils.hpp2
-rw-r--r--src/libutil/StringUtils.cpp24
2 files changed, 26 insertions, 0 deletions
diff --git a/include/util/StringUtils.hpp b/include/util/StringUtils.hpp
index d0c832e..af8e82f 100644
--- a/include/util/StringUtils.hpp
+++ b/include/util/StringUtils.hpp
@@ -2,9 +2,11 @@
#define __UTIL_STRING_UTILS_H
#include <string>
+#include <vector>
#include "util/UtilExportable.hpp"
UTIL_DLL_VISIBLE bool stringicasecmp( const std::string &s1, const std::string &s2 );
+UTIL_DLL_VISIBLE std::vector<std::string> split( const std::string &s, const std::string &delimiter, bool keepEmpty = true );
#endif
diff --git a/src/libutil/StringUtils.cpp b/src/libutil/StringUtils.cpp
index 61769c9..13be8d4 100644
--- a/src/libutil/StringUtils.cpp
+++ b/src/libutil/StringUtils.cpp
@@ -21,3 +21,27 @@ bool stringicasecmp( const string &s1, const string &s2 )
return false;
}
+std::vector<std::string> split( const string &s, const string &delimiter, bool keepEmpty )
+{
+ vector<string> result;
+
+ if( delimiter.empty( ) ) {
+ result.push_back( s );
+ return result;
+ }
+
+ string::const_iterator b = s.begin( ), e;
+ while( true ) {
+ e = search( b, s.end( ), delimiter.begin( ), delimiter.end( ) );
+ string tmp( b, e );
+ if( keepEmpty || !tmp.empty( ) ) {
+ result.push_back( tmp );
+ }
+ if( e == s.end( ) ) {
+ break;
+ }
+ b = e + delimiter.size( );
+ }
+
+ return result;
+}