From f74389437d4874d4d835af339b2f97c9479f57f0 Mon Sep 17 00:00:00 2001 From: Valerio Mariani <valerio.mariani@unibas.ch> Date: Fri, 30 Sep 2011 17:07:24 +0200 Subject: [PATCH] Added split according to whitespace to StringRef --- modules/base/src/string_ref.cc | 20 ++++++++++++++++++++ modules/base/src/string_ref.hh | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/modules/base/src/string_ref.cc b/modules/base/src/string_ref.cc index 6a1d62254..467052565 100644 --- a/modules/base/src/string_ref.cc +++ b/modules/base/src/string_ref.cc @@ -113,6 +113,26 @@ std::vector<StringRef> StringRef::split(char p) const return result; } +std::vector<StringRef> StringRef::split() const +{ + std::vector<StringRef> result; + const char* s=begin_; + const char* l=begin_; + while (s!=end_) { + if (isspace(*s)) { + 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; +} + std::string StringRef::str_no_whitespace() const { std::string whitespaceless_string; diff --git a/modules/base/src/string_ref.hh b/modules/base/src/string_ref.hh index d72a27a85..48ab84105 100644 --- a/modules/base/src/string_ref.hh +++ b/modules/base/src/string_ref.hh @@ -147,14 +147,16 @@ public: /// \brief split string into chunks delimited by \p p std::vector<StringRef> split(char p) const; + + /// \brief split string into chunks delimited by whitespace + std::vector<StringRef> split() const; /// \brief returns a new string with all whitespace removed from /// this StringRef std::string str_no_whitespace() const; private: const char* begin_; - const char* end_; - + const char* end_; }; //std::stringstream& operator<<(std::stringstream& stream, const StringRef& strref); -- GitLab