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 2011/06/05 07:45:22 UTC

svn commit: r1131890 - in /incubator/mesos/trunk/src: Makefile.in params.hpp string_utils.cpp string_utils.hpp tests/test_string_utils.cpp

Author: benh
Date: Sun Jun  5 05:45:22 2011
New Revision: 1131890

URL: http://svn.apache.org/viewvc?rev=1131890&view=rev
Log:
Factored out separate StringUtils class in preparation for smarter
config parsing.

Added:
    incubator/mesos/trunk/src/string_utils.cpp
    incubator/mesos/trunk/src/string_utils.hpp
    incubator/mesos/trunk/src/tests/test_string_utils.cpp
Modified:
    incubator/mesos/trunk/src/Makefile.in
    incubator/mesos/trunk/src/params.hpp

Modified: incubator/mesos/trunk/src/Makefile.in
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/Makefile.in?rev=1131890&r1=1131889&r2=1131890&view=diff
==============================================================================
--- incubator/mesos/trunk/src/Makefile.in (original)
+++ incubator/mesos/trunk/src/Makefile.in Sun Jun  5 05:45:22 2011
@@ -111,12 +111,13 @@ MASTER_OBJ = master.o allocator_factory.
 SLAVE_OBJ = slave.o launcher.o isolation_module.o \
 	    process_based_isolation_module.o
 COMMON_OBJ = fatal.o hash_pid.o messages.o lock.o master_detector.o \
-	     url_processor.o zookeeper.o configurator.o
+	     url_processor.o zookeeper.o configurator.o string_utils.o
 EXEC_LIB_OBJ = nexus_exec.o
 SCHED_LIB_OBJ = nexus_sched.o nexus_local.o params.o
 TEST_OBJ = tests/main.o tests/test_master.o tests/test_resources.o \
 	   tests/external_test.o tests/test_sample_frameworks.o \
-	   tests/testing_utils.o tests/test_configurator.o
+	   tests/testing_utils.o tests/test_configurator.o \
+	   tests/test_string_utils.o
 
 ifeq ($(OS_NAME),solaris)
   SLAVE_OBJ += solaris_project_isolation_module.o

Modified: incubator/mesos/trunk/src/params.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/params.hpp?rev=1131890&r1=1131889&r2=1131890&view=diff
==============================================================================
--- incubator/mesos/trunk/src/params.hpp (original)
+++ incubator/mesos/trunk/src/params.hpp Sun Jun  5 05:45:22 2011
@@ -1,5 +1,5 @@
-#ifndef PARAMS_HPP
-#define PARAMS_HPP
+#ifndef __PARAMS_HPP__
+#define __PARAMS_HPP__
 
 #include <map>
 #include <sstream>
@@ -10,6 +10,7 @@
 #include <boost/foreach.hpp>
 
 #include "foreach.hpp"
+#include "string_utils.hpp"
 
 
 namespace nexus { namespace internal {
@@ -67,10 +68,10 @@ public:
   void loadString(const string& str)
   {
     vector<string> lines;
-    split(str, "\n\r", lines);
+    StringUtils::split(str, "\n\r", &lines);
     foreach (string& line, lines) {
       vector<string> parts;
-      split(line, "=", parts);
+      StringUtils::split(line, "=", &parts);
       if (parts.size() != 2) {
         ostringstream oss;
         oss << "Malformed line in params: '" << line << "'";
@@ -145,23 +146,6 @@ public:
   {
     return params.find(key) != params.end();
   }
-
-private:
-  void split(const string& str, const string& delims, vector<string>& tokens)
-  {
-    // Start and end of current token; initialize these to the first token in
-    // the string, skipping any leading delimiters
-    size_t start = str.find_first_not_of(delims, 0);
-    size_t end = str.find_first_of(delims, start);
-    while (start != string::npos || end != string::npos) {
-      // Add current token to the vector
-      tokens.push_back(str.substr(start, end-start));
-      // Advance start to first non-delimiter past the current end
-      start = str.find_first_not_of(delims, end);
-      // Advance end to the next delimiter after the new start
-      end = str.find_first_of(delims, start);
-    }
-  }
 };
 
 

Added: incubator/mesos/trunk/src/string_utils.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/string_utils.cpp?rev=1131890&view=auto
==============================================================================
--- incubator/mesos/trunk/src/string_utils.cpp (added)
+++ incubator/mesos/trunk/src/string_utils.cpp Sun Jun  5 05:45:22 2011
@@ -0,0 +1,39 @@
+#include "string_utils.hpp"
+
+using std::string;
+using std::vector;
+
+using namespace nexus::internal;
+
+
+void StringUtils::split(const string& str,
+                        const string& delims,
+                        vector<string>* tokens)
+{
+  // Start and end of current token; initialize these to the first token in
+  // the string, skipping any leading delimiters
+  size_t start = str.find_first_not_of(delims, 0);
+  size_t end = str.find_first_of(delims, start);
+  while (start != string::npos || end != string::npos) {
+    // Add current token to the vector
+    tokens->push_back(str.substr(start, end - start));
+    // Advance start to first non-delimiter past the current end
+    start = str.find_first_not_of(delims, end);
+    // Advance end to the next delimiter after the new start
+    end = str.find_first_of(delims, start);
+  }
+}
+
+
+string StringUtils::trim(const string& str, const string& toRemove)
+{
+  // Start and end of current token; initialize these to the first token in
+  // the string, skipping any leading delimiters
+  size_t start = str.find_first_not_of(toRemove);
+  size_t end = str.find_last_not_of(toRemove);
+  if (start == string::npos) { // String contains only characters in toRemove
+    return "";
+  } else {
+    return str.substr(start, end + 1 - start);
+  }
+}

Added: incubator/mesos/trunk/src/string_utils.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/string_utils.hpp?rev=1131890&view=auto
==============================================================================
--- incubator/mesos/trunk/src/string_utils.hpp (added)
+++ incubator/mesos/trunk/src/string_utils.hpp Sun Jun  5 05:45:22 2011
@@ -0,0 +1,36 @@
+#ifndef __STRING_UTILS_HPP__
+#define __STRING_UTILS_HPP__
+
+#include <string>
+#include <vector>
+
+namespace nexus { namespace internal {
+
+using std::string;
+using std::vector;
+
+/**
+ * Contains utility functions for dealing with strings used throughout Mesos.
+ */
+class StringUtils
+{
+public:
+  /**
+   * Split a string around the character(s) in delims, placing the
+   * resulting substrings into the tokens vector.
+   */
+  static void split(const string& str,
+                    const string& delims,
+                    vector<string>* tokens);
+
+  /**
+   * Trim a string, removing characters contained in toRemove from both
+   * the beginning and end. If toRemove is not given, whitespace characters
+   * are used by default.
+   */
+  static string trim(const string& str, const string& toRemove = " \t\n\r");
+};
+
+}} /* namespace nexus::internal */
+
+#endif

Added: incubator/mesos/trunk/src/tests/test_string_utils.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_string_utils.cpp?rev=1131890&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/test_string_utils.cpp (added)
+++ incubator/mesos/trunk/src/tests/test_string_utils.cpp Sun Jun  5 05:45:22 2011
@@ -0,0 +1,98 @@
+#include <sstream>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include "string_utils.hpp"
+
+using namespace nexus;
+using namespace nexus::internal;
+
+
+TEST(StringUtilsTest, SplitEmptyString)
+{
+  vector<string> tokens;
+  StringUtils::split("", " ", &tokens);
+  ASSERT_EQ(0, tokens.size());
+}
+
+
+TEST(StringUtilsTest, SplitDelimOnlyString)
+{
+  vector<string> tokens;
+  StringUtils::split("   ", " ", &tokens);
+  ASSERT_EQ(0, tokens.size());
+}
+
+
+TEST(StringUtilsTest, Split)
+{
+  vector<string> tokens;
+  StringUtils::split("hello world,  what's up?", " ", &tokens);
+  ASSERT_EQ(4, tokens.size());
+  EXPECT_EQ("hello",  tokens[0]);
+  EXPECT_EQ("world,", tokens[1]);
+  EXPECT_EQ("what's", tokens[2]);
+  EXPECT_EQ("up?",    tokens[3]);
+}
+
+
+TEST(StringUtilsTest, SplitStringWithDelimsAtStart)
+{
+  vector<string> tokens;
+  StringUtils::split("  hello world,  what's up?", " ", &tokens);
+  ASSERT_EQ(4, tokens.size());
+  EXPECT_EQ("hello",  tokens[0]);
+  EXPECT_EQ("world,", tokens[1]);
+  EXPECT_EQ("what's", tokens[2]);
+  EXPECT_EQ("up?",    tokens[3]);
+}
+
+
+TEST(StringUtilsTest, SplitStringWithDelimsAtEnd)
+{
+  vector<string> tokens;
+  StringUtils::split("hello world,  what's up?  ", " ", &tokens);
+  ASSERT_EQ(4, tokens.size());
+  EXPECT_EQ("hello",  tokens[0]);
+  EXPECT_EQ("world,", tokens[1]);
+  EXPECT_EQ("what's", tokens[2]);
+  EXPECT_EQ("up?",    tokens[3]);
+}
+
+
+TEST(StringUtilsTest, SplitStringWithDelimsAtStartAndEnd)
+{
+  vector<string> tokens;
+  StringUtils::split("  hello world,  what's up?  ", " ", &tokens);
+  ASSERT_EQ(4, tokens.size());
+  EXPECT_EQ("hello",  tokens[0]);
+  EXPECT_EQ("world,", tokens[1]);
+  EXPECT_EQ("what's", tokens[2]);
+  EXPECT_EQ("up?",    tokens[3]);
+}
+
+
+TEST(StringUtilsTest, SplitWithMultipleDelims)
+{
+  vector<string> tokens;
+  StringUtils::split("hello\tworld,  \twhat's up?", " \t", &tokens);
+  ASSERT_EQ(4, tokens.size());
+  EXPECT_EQ("hello",  tokens[0]);
+  EXPECT_EQ("world,", tokens[1]);
+  EXPECT_EQ("what's", tokens[2]);
+  EXPECT_EQ("up?",    tokens[3]);
+}
+
+
+TEST(StringUtilsTest, Trim)
+{
+  EXPECT_EQ("", StringUtils::trim("", " "));
+  EXPECT_EQ("", StringUtils::trim("    ", " "));
+  EXPECT_EQ("hello world", StringUtils::trim("hello world", " "));
+  EXPECT_EQ("hello world", StringUtils::trim("  hello world", " "));
+  EXPECT_EQ("hello world", StringUtils::trim("hello world  ", " "));
+  EXPECT_EQ("hello world", StringUtils::trim("  hello world  ", " "));
+  EXPECT_EQ("hello world", StringUtils::trim(" \t hello world\t  ", " \t"));
+  EXPECT_EQ("hello world", StringUtils::trim(" \t hello world\t \n\r "));
+}