lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [6581] Trim pasted strings


From: gchicares
Subject: [lmi-commits] [6581] Trim pasted strings
Date: Wed, 11 May 2016 23:55:37 +0000 (UTC)

Revision: 6581
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=6581
Author:   chicares
Date:     2016-05-11 23:55:37 +0000 (Wed, 11 May 2016)
Log Message:
-----------
Trim pasted strings

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/miscellany.cpp
    lmi/trunk/miscellany.hpp
    lmi/trunk/miscellany_test.cpp
    lmi/trunk/skeleton.cpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2016-05-11 22:38:20 UTC (rev 6580)
+++ lmi/trunk/ChangeLog 2016-05-11 23:55:37 UTC (rev 6581)
@@ -39087,3 +39087,11 @@
   skeleton.cpp
 Restrict multiline text-paste handler to input seq entries (VZ).
 
+20160511T2355Z <address@hidden> [451]
+
+  miscellany.cpp
+  miscellany.hpp
+  miscellany_test.cpp
+  skeleton.cpp
+Trim pasted strings.
+

Modified: lmi/trunk/miscellany.cpp
===================================================================
--- lmi/trunk/miscellany.cpp    2016-05-11 22:38:20 UTC (rev 6580)
+++ lmi/trunk/miscellany.cpp    2016-05-11 23:55:37 UTC (rev 6581)
@@ -128,6 +128,32 @@
     return html;
 }
 
+void ltrim(std::string& s, char const* superfluous)
+{
+    std::string::size_type p = s.find_first_not_of(superfluous);
+    if(std::string::npos != p)
+        {
+        s.erase(0, p);
+        }
+    else
+        {
+        s.clear();
+        }
+}
+
+void rtrim(std::string& s, char const* superfluous)
+{
+    std::string::size_type p = s.find_last_not_of(superfluous);
+    if(std::string::npos != p)
+        {
+        s.erase(1 + p);
+        }
+    else
+        {
+        s.clear();
+        }
+}
+
 /// 
http://groups.google.com/group/borland.public.cpp.borlandcpp/msg/638d1f25e66472d9
 ///   [2001-07-18T22:25:15Z from Greg Chicares]
 

Modified: lmi/trunk/miscellany.hpp
===================================================================
--- lmi/trunk/miscellany.hpp    2016-05-11 22:38:20 UTC (rev 6580)
+++ lmi/trunk/miscellany.hpp    2016-05-11 23:55:37 UTC (rev 6581)
@@ -98,6 +98,14 @@
 
 std::string htmlize(std::string const&);
 
+/// Remove superfluous characters from beginning of string.
+
+void ltrim(std::string& s, char const* superfluous);
+
+/// Remove superfluous characters from end of string.
+
+void rtrim(std::string& s, char const* superfluous);
+
 inline std::ios_base::openmode ios_in_binary()
 {
     return

Modified: lmi/trunk/miscellany_test.cpp
===================================================================
--- lmi/trunk/miscellany_test.cpp       2016-05-11 22:38:20 UTC (rev 6580)
+++ lmi/trunk/miscellany_test.cpp       2016-05-11 23:55:37 UTC (rev 6581)
@@ -99,10 +99,44 @@
 #endif // !defined __BORLANDC__
 }
 
+void test_trimming()
+{
+    char const*const superfluous = " ;";
+
+    std::string s = "";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "");
+
+    s = " ";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "");
+
+    s = " ;; ";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "");
+
+    s = "a";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "a");
+
+    s = "; ;a; ;";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "a");
+
+    s = "a; ;";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "a");
+
+    s = "; ;a";
+    ltrim(s, superfluous); rtrim(s, superfluous);
+    BOOST_TEST_EQUAL(s, "a");
+}
+
 int test_main(int, char*[])
 {
     test_files_are_identical();
     test_minmax();
+    test_trimming();
 
     return 0;
 }

Modified: lmi/trunk/skeleton.cpp
===================================================================
--- lmi/trunk/skeleton.cpp      2016-05-11 22:38:20 UTC (rev 6580)
+++ lmi/trunk/skeleton.cpp      2016-05-11 23:55:37 UTC (rev 6581)
@@ -904,7 +904,8 @@
                 }
             }
 
-        new_text.resize(1 + new_text.find_last_not_of(';'));
+        ltrim(new_text, " ;");
+        rtrim(new_text, " ;");
 
         return new_text;
         }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]