You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/06/29 21:50:26 UTC

[3/4] git commit: Split version where N tokens can be specified.

Split version where N tokens can be specified.

Updated 'split' function in stout/strings so we can choose how many
tokens we want to retreive.

Review: https://reviews.apache.org/r/22825


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7124e5db
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7124e5db
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7124e5db

Branch: refs/heads/master
Commit: 7124e5db9dc8e7578dfcb31d39a13e90c559679c
Parents: 58cbed1
Author: Isabel Jimenez <co...@isabeljimenez.com>
Authored: Sun Jun 29 09:50:06 2014 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sun Jun 29 09:50:07 2014 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/strings.hpp    | 26 ++++++-
 .../3rdparty/stout/tests/strings_tests.cpp      | 74 ++++++++++++++++++++
 2 files changed, 97 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/7124e5db/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
index 08428b8..43ae7be 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp
@@ -21,6 +21,7 @@
 
 #include "foreach.hpp"
 #include "format.hpp"
+#include "option.hpp"
 #include "stringify.hpp"
 
 namespace strings {
@@ -96,6 +97,8 @@ inline std::string replace(
 
 // Tokenizes the string using the delimiters.
 // Empty tokens will not be included in the result.
+// TODO(ijimenez) support maximum number of tokens
+// to be returned
 inline std::vector<std::string> tokenize(
     const std::string& s,
     const std::string& delims)
@@ -124,24 +127,41 @@ inline std::vector<std::string> tokenize(
 
 
 // Splits the string using the provided delimiters.
+// The string is split each time at the first character
+// that matches any of the characters specified in delims.
 // Empty tokens are allowed in the result.
+// Optinally, maximum number of tokens to be returned
+// can be specified.
 inline std::vector<std::string> split(
     const std::string& s,
-    const std::string& delims)
+    const std::string& delims,
+    Option<int> n = None())
 {
   std::vector<std::string> tokens;
   size_t offset = 0;
   size_t next = 0;
+  int _n = 0;
+  bool some = n.isSome();
 
-  while (true) {
+  if (some) {
+     _n =  n.get();
+  }
+
+  while (!some || _n > 0) {
+    if (_n == 1) {
+      tokens.push_back(s.substr(offset));
+      break;
+    }
     next = s.find_first_of(delims, offset);
     if (next == std::string::npos) {
       tokens.push_back(s.substr(offset));
       break;
     }
-
     tokens.push_back(s.substr(offset, next - offset));
     offset = next + 1;
+    if (some) {
+      --_n;
+    }
   }
   return tokens;
 }

http://git-wip-us.apache.org/repos/asf/mesos/blob/7124e5db/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp
index c83156e..51008e5 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp
@@ -263,6 +263,80 @@ TEST(StringsTest, SplitWithMultipleDelims)
 }
 
 
+TEST(StringsTest, SplitNZero)
+{
+  vector<string> tokens = strings::split("foo,bar,,,", ",", 0);
+  ASSERT_EQ(0u, tokens.size());
+}
+
+
+TEST(StringsTest, SplitNDelimOnlyString)
+{
+  vector<string> tokens = strings::split(",,,", ",", 2);
+  ASSERT_EQ(2u, tokens.size());
+  EXPECT_EQ("",   tokens[0]);
+  EXPECT_EQ(",,", tokens[1]);
+}
+
+
+TEST(StringsTest, SplitN)
+{
+  vector<string> tokens = strings::split("foo,bar,,baz", ",", 2);
+  ASSERT_EQ(2u, tokens.size());
+  EXPECT_EQ("foo",      tokens[0]);
+  EXPECT_EQ("bar,,baz", tokens[1]);
+}
+
+
+TEST(StringsTest, SplitNStringWithDelimsAtStart)
+{
+  vector<string> tokens = strings::split(",,foo,bar,,baz", ",", 5);
+  ASSERT_EQ(5u, tokens.size());
+  EXPECT_EQ("",     tokens[0]);
+  EXPECT_EQ("",     tokens[1]);
+  EXPECT_EQ("foo",  tokens[2]);
+  EXPECT_EQ("bar",  tokens[3]);
+  EXPECT_EQ(",baz", tokens[4]);
+}
+
+
+TEST(StringsTest, SplitNStringWithDelimsAtEnd)
+{
+  vector<string> tokens = strings::split("foo,bar,,baz,,", ",", 4);
+  ASSERT_EQ(4u, tokens.size());
+  EXPECT_EQ("foo",   tokens[0]);
+  EXPECT_EQ("bar",   tokens[1]);
+  EXPECT_EQ("",      tokens[2]);
+  EXPECT_EQ("baz,,", tokens[3]);
+}
+
+
+TEST(StringsTest, SplitNStringWithDelimsAtStartAndEnd)
+{
+  vector<string> tokens = strings::split(",,foo,bar,,", ",", 6);
+  ASSERT_EQ(6u, tokens.size());
+  EXPECT_EQ("",    tokens[0]);
+  EXPECT_EQ("",    tokens[1]);
+  EXPECT_EQ("foo", tokens[2]);
+  EXPECT_EQ("bar", tokens[3]);
+  EXPECT_EQ("",    tokens[4]);
+  EXPECT_EQ("",    tokens[5]);
+}
+
+
+TEST(StringsTest, SplitNWithMultipleDelims)
+{
+  vector<string> tokens = strings::split("foo.bar,.,.baz.", ",.", 6);
+  ASSERT_EQ(6u, tokens.size());
+  EXPECT_EQ("foo",  tokens[0]);
+  EXPECT_EQ("bar",  tokens[1]);
+  EXPECT_EQ("",     tokens[2]);
+  EXPECT_EQ("",     tokens[3]);
+  EXPECT_EQ("",     tokens[4]);
+  EXPECT_EQ("baz.", tokens[5]);
+}
+
+
 TEST(StringsTest, Pairs)
 {
   map<string, vector<string> > pairs = strings::pairs("one=1,two=2", ",", "=");