Repository: mesos Updated Branches: refs/heads/master bf7262be2 -> 4505b6599
Add strings::Mode to strings::trim. Review: https://reviews.apache.org/r/36189 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4505b659 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4505b659 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4505b659 Branch: refs/heads/master Commit: 4505b65997338bd59eb8231fdfa4c6c60fa222cc Parents: bf7262b Author: Benjamin Hindman <[email protected]> Authored: Sun Jul 5 18:50:10 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Wed Jul 8 16:39:45 2015 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/strings.hpp | 41 +++++++++++++++++--- .../3rdparty/stout/tests/strings_tests.cpp | 10 +++++ 2 files changed, 46 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/4505b659/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 963029b..81f6e50 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/strings.hpp @@ -27,7 +27,7 @@ namespace strings { -// Flags indicating how remove should operate. +// Flags indicating how 'remove' or 'trim' should operate. enum Mode { PREFIX, SUFFIX, @@ -63,15 +63,46 @@ inline std::string remove( inline std::string trim( const std::string& from, + Mode mode = ANY, const std::string& chars = " \t\n\r") { - size_t start = from.find_first_not_of(chars); - size_t end = from.find_last_not_of(chars); - if (start == std::string::npos) { // Contains only characters in chars. + size_t start = 0; + Option<size_t> end = None(); + + if (mode == ANY) { + start = from.find_first_not_of(chars); + end = from.find_last_not_of(chars); + } else if (mode == PREFIX) { + start = from.find_first_not_of(chars); + } else if (mode == SUFFIX) { + end = from.find_last_not_of(chars); + } + + // Bail early if 'from' contains only characters in 'chars'. + if (start == std::string::npos) { return ""; } - return from.substr(start, end + 1 - start); + // Calculate the length of the substring, defaulting to the "end" of + // string if there were no characters to remove from the suffix. + size_t length = std::string::npos; + + // Found characters to trim at the end. + if (end.isSome() && end.get() != std::string::npos) { + length = end.get() + 1 - start; + } + + return from.substr(start, length); +} + + +// Helper providing some syntactic sugar for when 'mode' is ANY but +// the 'chars' are specified. +inline std::string trim( + const std::string& from, + const std::string& chars) +{ + return trim(from, ANY, chars); } http://git-wip-us.apache.org/repos/asf/mesos/blob/4505b659/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 9733b2e..f65c525 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/strings_tests.cpp @@ -72,6 +72,16 @@ TEST(StringsTest, Trim) EXPECT_EQ("hello world", strings::trim(" hello world ", " ")); EXPECT_EQ("hello world", strings::trim(" \t hello world\t ", " \t")); EXPECT_EQ("hello world", strings::trim(" \t hello world\t \n\r ")); + + // Also test trimming from just the prefix or suffix. + EXPECT_EQ("hello world\t \n\r ", + strings::trim(" \t hello world\t \n\r ", strings::PREFIX)); + EXPECT_EQ(" \t hello world", + strings::trim(" \t hello world\t \n\r ", strings::SUFFIX)); + EXPECT_EQ("\t hello world\t \n\r ", + strings::trim(" \t hello world\t \n\r ", strings::PREFIX, " ")); + EXPECT_EQ(" \t hello world\t \n", + strings::trim(" \t hello world\t \n\r ", strings::SUFFIX, " \r")); }
