You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2018/05/30 00:28:11 UTC

mesos git commit: Added overloads for strings::startsWith and strings::endsWith().

Repository: mesos
Updated Branches:
  refs/heads/master 0ffafd47f -> 521c8fe38


Added overloads for strings::startsWith and strings::endsWith().

This saves an unnecessary memory allocation when
testing if a string starts or ends with a string literal.

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


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

Branch: refs/heads/master
Commit: 521c8fe38054eee3ba87f100c7d7496794a89c3b
Parents: 0ffafd4
Author: Benno Evers <be...@mesosphere.com>
Authored: Tue May 29 17:27:51 2018 -0700
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Tue May 29 17:27:51 2018 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/strings.hpp | 17 +++++++++++++++++
 3rdparty/stout/tests/strings_tests.cpp   | 14 ++++++++++++++
 2 files changed, 31 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/521c8fe3/3rdparty/stout/include/stout/strings.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/strings.hpp b/3rdparty/stout/include/stout/strings.hpp
index 067a792..1af3818 100644
--- a/3rdparty/stout/include/stout/strings.hpp
+++ b/3rdparty/stout/include/stout/strings.hpp
@@ -14,6 +14,7 @@
 #define __STOUT_STRINGS_HPP__
 
 #include <algorithm>
+#include <cstring>
 #include <map>
 #include <sstream>
 #include <string>
@@ -384,6 +385,14 @@ inline bool startsWith(const std::string& s, const std::string& prefix)
 }
 
 
+inline bool startsWith(const std::string& s, const char* prefix)
+{
+  size_t len = ::strnlen(prefix, s.size() + 1);
+  return s.size() >= len &&
+         std::equal(s.begin(), s.begin() + len, prefix);
+}
+
+
 inline bool startsWith(const std::string& s, char c)
 {
   return !s.empty() && s.front() == c;
@@ -397,6 +406,14 @@ inline bool endsWith(const std::string& s, const std::string& suffix)
 }
 
 
+inline bool endsWidth(const std::string& s, const char* suffix)
+{
+  size_t len = ::strnlen(suffix, s.size() + 1);
+  return s.size() >= len &&
+         std::equal(s.end() - len, s.end(), suffix);
+}
+
+
 inline bool endsWith(const std::string& s, char c)
 {
   return !s.empty() && s.back() == c;

http://git-wip-us.apache.org/repos/asf/mesos/blob/521c8fe3/3rdparty/stout/tests/strings_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/strings_tests.cpp b/3rdparty/stout/tests/strings_tests.cpp
index 395540a..a5e446e 100644
--- a/3rdparty/stout/tests/strings_tests.cpp
+++ b/3rdparty/stout/tests/strings_tests.cpp
@@ -485,6 +485,20 @@ TEST(StringsTest, StartsWith)
   EXPECT_TRUE(strings::startsWith("hello world", "hello"));
   EXPECT_FALSE(strings::startsWith("hello world", "no"));
   EXPECT_FALSE(strings::startsWith("hello world", "ello"));
+
+  EXPECT_TRUE(strings::startsWith("hello world", std::string("hello")));
+  EXPECT_FALSE(strings::startsWith("hello world", std::string("ello")));
+}
+
+
+TEST(StringsTest, EndsWith)
+{
+  EXPECT_TRUE(strings::endsWith("hello world", "world"));
+  EXPECT_FALSE(strings::endsWith("hello world", "no"));
+  EXPECT_FALSE(strings::endsWith("hello world", "ello"));
+
+  EXPECT_TRUE(strings::endsWith("hello world", std::string("world")));
+  EXPECT_FALSE(strings::endsWith("hello world", std::string("worl")));
 }