diff --git a/modules/base/src/string_ref.cc b/modules/base/src/string_ref.cc index 547c38deefdfed9219c359e0a7544742bd8e9c04..af19d7b8fafe5e2e632f955cc3a46b9625d34703 100644 --- a/modules/base/src/string_ref.cc +++ b/modules/base/src/string_ref.cc @@ -93,4 +93,24 @@ std::ostream& operator<<(std::ostream& stream, const StringRef& strref) return stream; } +std::vector<StringRef> StringRef::split(char p) const +{ + std::vector<StringRef> result; + const char* s=begin_; + const char* l=begin_; + while (s!=end_) { + if (*s==p) { + if (l!=s) { + result.push_back(StringRef(l, s-l)); + } + l=s+1; + } + ++s; + } + if (l!=s) { + result.push_back(StringRef(l, s-l)); + } + return result; +} + } diff --git a/modules/base/src/string_ref.hh b/modules/base/src/string_ref.hh index b1a38dcf0dba2767454084e83c8c03e405ed3486..c6150b9224d65f606c344e50ea1c19be16aa6012 100644 --- a/modules/base/src/string_ref.hh +++ b/modules/base/src/string_ref.hh @@ -27,6 +27,7 @@ #include <iostream> #include <ost/base.hh> #include <string.h> +#include <vector> #include <ost/module_config.hh> @@ -133,6 +134,8 @@ public: bool empty() const { return begin_==end_; } + /// \brief split string into chunks delimited by \p p + std::vector<StringRef> split(char p) const; private: const char* begin_; const char* end_;