changeset 569:b1c4933afcd0

Misc: add strip function
author David Demelier <markand@malikania.fr>
date Wed, 29 Jun 2016 19:07:40 +0200
parents 4d9cd83d821e
children dcef88285f8c
files misc/strip.hpp misc/test-all.cpp misc/test-strip.cpp
diffstat 3 files changed, 128 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/strip.hpp	Wed Jun 29 19:07:40 2016 +0200
@@ -0,0 +1,36 @@
+/*
+ * strip.hpp -- remove leading and trailing spaces
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <algorithm>
+#include <cctype>
+
+/**
+ * Remove leading and trailing spaces.
+ *
+ * \param str the string
+ * \return the removed white spaces
+ */
+std::string strip(std::string str)
+{
+    auto test = [] (char c) { return !std::isspace(c); };
+
+    str.erase(str.begin(), std::find_if(str.begin(), str.end(), test));
+    str.erase(std::find_if(str.rbegin(), str.rend(), test).base(), str.end());
+
+    return str;
+}
\ No newline at end of file
--- a/misc/test-all.cpp	Wed Jun 29 19:02:56 2016 +0200
+++ b/misc/test-all.cpp	Wed Jun 29 19:07:40 2016 +0200
@@ -19,6 +19,7 @@
 #include <gtest/gtest.h>
 
 #include "test-clamp.cpp"
+#include "test-strip.cpp"
 
 int main(int argc, char **argv)
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/test-strip.cpp	Wed Jun 29 19:07:40 2016 +0200
@@ -0,0 +1,91 @@
+/*
+ * test-strip.cpp -- test strip function
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "strip.hpp"
+
+TEST(Strip, left)
+{
+    std::string value = "   123";
+    std::string result = strip(value);
+
+    ASSERT_EQ("123", result);
+}
+
+TEST(Strip, right)
+{
+    std::string value = "123   ";
+    std::string result = strip(value);
+
+    ASSERT_EQ("123", result);
+}
+
+TEST(Strip, both)
+{
+    std::string value = "   123   ";
+    std::string result = strip(value);
+
+    ASSERT_EQ("123", result);
+}
+
+TEST(Strip, none)
+{
+    std::string value = "without";
+    std::string result = strip(value);
+
+    ASSERT_EQ("without", result);
+}
+
+TEST(Strip, betweenEmpty)
+{
+    std::string value = "one list";
+    std::string result = strip(value);
+
+    ASSERT_EQ("one list", result);
+}
+
+TEST(Strip, betweenLeft)
+{
+    std::string value = "  space at left";
+    std::string result = strip(value);
+
+    ASSERT_EQ("space at left", result);
+}
+
+TEST(Strip, betweenRight)
+{
+    std::string value = "space at right  ";
+    std::string result = strip(value);
+
+    ASSERT_EQ("space at right", result);
+}
+
+TEST(Strip, betweenBoth)
+{
+    std::string value = "  space at both  ";
+    std::string result = strip(value);
+
+    ASSERT_EQ("space at both", result);
+}
+
+TEST(Strip, empty)
+{
+    std::string value = "    ";
+    std::string result = strip(value);
+
+    ASSERT_EQ("", result);
+}
\ No newline at end of file