[DPL] String Trimming
authorTomasz Iwanek <t.iwanek@samsung.com>
Tue, 16 Jul 2013 09:53:16 +0000 (11:53 +0200)
committerSoo-Hyun Choi <sh9.choi@samsung.com>
Tue, 22 Oct 2013 08:19:39 +0000 (17:19 +0900)
[Issue#]   LINUXWRT-639
[Problem]  Missing trim for string
[Cause]    N/A
[Solution] String trim function

[Remarks]
    This change is extracted from VS parser because it is different
    functionality needed in next changes but in to part of VS reader itself.

[Verification] Build and run tests:
    - $> wrt-commons-tests-core --output=text --regexp='String_Trim' #all should pass

Change-Id: I4911a1698e1fe50637f2d3a2414df7f43793c384

modules/core/include/dpl/string.h
tests/core/test_string.cpp

index 31132c1..e4dc923 100644 (file)
@@ -133,6 +133,23 @@ typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator
     return std::accumulate(++begin, end, *init, func);
 }
 
     return std::accumulate(++begin, end, *init, func);
 }
 
+template<class StringType> void TrimLeft(StringType & obj, typename StringType::const_pointer separators)
+{
+    obj.erase(0, obj.find_first_not_of(separators));
+}
+
+template<class StringType> void TrimRight(StringType & obj, typename StringType::const_pointer separators)
+{
+    obj.erase(obj.find_last_not_of(separators)+1);
+}
+
+template<class StringType> void Trim(StringType & obj, typename StringType::const_pointer separators)
+{
+    TrimLeft(obj, separators);
+    TrimRight(obj, separators);
+}
+
+
 } //namespace DPL
 
 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString);
 } //namespace DPL
 
 std::ostream& operator<<(std::ostream& aStream, const DPL::String& aString);
index 69a3e4a..e124376 100644 (file)
@@ -440,3 +440,40 @@ RUNNER_TEST(String_Join)
     RUNNER_ASSERT(DPL::Join(strings.begin(), strings.end(), "delim") == "onedelimtwodelimthreedelimfour");
 }
 
     RUNNER_ASSERT(DPL::Join(strings.begin(), strings.end(), "delim") == "onedelimtwodelimthreedelimfour");
 }
 
+/*
+Name: String_Trim
+Description: tests trimming strings
+Expected: trim strings
+*/
+RUNNER_TEST(String_Trim)
+{
+    const std::string str = "  value   ";
+
+    std::string test1 = str;
+    DPL::Trim(test1, " ");
+    RUNNER_ASSERT_MSG(test1 == "value", "Full trim failed");
+
+    std::string test2 = str;
+    DPL::TrimLeft(test2, " ");
+    RUNNER_ASSERT_MSG(test2 == "value   ", "Left trim failed");
+
+    std::string test3 = str;
+    DPL::TrimRight(test3, " ");
+    RUNNER_ASSERT_MSG(test3 == "  value", "Right trim failed");
+
+    std::string test4 = str;
+    DPL::Trim(test4, " ea");
+    RUNNER_ASSERT_MSG(test4 == "valu", "Trim failed");
+
+    std::string test5 = str;
+    DPL::TrimRight(test5, " v");
+    RUNNER_ASSERT_MSG(test5 == "  value", "Trim failed");
+
+    DPL::String test6 = L"--aabbaabb--aa--bb--";
+    DPL::Trim(test6, L"-a");
+    RUNNER_ASSERT_MSG(test6 == L"bbaabb--aa--bb", "Trim failed");
+
+    DPL::String test7 = L"--aabbaabb--aa--bb--";
+    DPL::Trim(test7, L"-ab");
+    RUNNER_ASSERT_MSG(test7 == L"", "Trim to empty failed");
+}