lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [6333] Refactor to make two functions reusable elsewhere


From: Greg Chicares
Subject: [lmi-commits] [6333] Refactor to make two functions reusable elsewhere
Date: Fri, 09 Oct 2015 01:56:22 +0000

Revision: 6333
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=6333
Author:   chicares
Date:     2015-10-09 01:56:21 +0000 (Fri, 09 Oct 2015)
Log Message:
-----------
Refactor to make two functions reusable elsewhere

Modified Paths:
--------------
    lmi/trunk/miscellany.cpp
    lmi/trunk/miscellany.hpp
    lmi/trunk/wx_table_generator.cpp

Modified: lmi/trunk/miscellany.cpp
===================================================================
--- lmi/trunk/miscellany.cpp    2015-10-07 22:30:22 UTC (rev 6332)
+++ lmi/trunk/miscellany.cpp    2015-10-09 01:56:21 UTC (rev 6333)
@@ -70,6 +70,47 @@
     return streams_are_identical(ifs0, ifs1);
 }
 
+/// Return the number of lines in a possibly multiline string.
+///
+/// Actually returns one plus the number of newlines in the string.
+/// If, say, a trailing '\n' is found, the count is one greater than
+/// the number of lines--which might be useful, e.g., for adding an
+/// extra blank line to a column header.
+
+std::size_t count_lines(std::string const& s)
+{
+    return 1u + std::count(s.begin(), s.end(), '\n');
+}
+
+/// Split a string into lines separated by newline characters.
+
+std::vector<std::string> split_into_lines(std::string const& s)
+{
+    // BOOST !! Unfortunately boost::split() can't be easily used with the
+    // current ancient version of the library (1.33), so we reimplement it
+    // here.
+    std::vector<std::string> lines;
+    std::string line;
+    for(std::string::const_iterator i = s.begin(); ; ++i)
+        {
+        if(i == s.end() || '\n' == *i)
+            {
+            lines.push_back(line);
+            if(i == s.end())
+                {
+                break;
+                }
+            line.clear();
+            }
+        else
+            {
+            line += *i;
+            }
+        }
+    LMI_ASSERT(lines.size() == count_lines(s));
+    return lines;
+}
+
 /// Escape text for html, e.g., "a < b" --> "a &lt; b".
 
 std::string htmlize(std::string const& raw_text)

Modified: lmi/trunk/miscellany.hpp
===================================================================
--- lmi/trunk/miscellany.hpp    2015-10-07 22:30:22 UTC (rev 6332)
+++ lmi/trunk/miscellany.hpp    2015-10-09 01:56:21 UTC (rev 6333)
@@ -26,6 +26,8 @@
 
 #include "config.hpp"
 
+#include "so_attributes.hpp"
+
 #include <boost/algorithm/minmax_element.hpp>
 
 #include <algorithm>
@@ -84,6 +86,16 @@
 template<typename T> bool operator< (minmax<T> m, T t) {return m.maximum() <  
t;}
 template<typename T> bool operator<=(minmax<T> m, T t) {return m.maximum() <= 
t;}
 
+/// Return the number of lines in a possibly multiline string.
+
+std::size_t LMI_SO count_lines(std::string const&);
+
+/// Split a string into lines separated by newline characters.
+
+std::vector<std::string> LMI_SO split_into_lines(std::string const&);
+
+/// Escape text for html, e.g., "a < b" --> "a &lt; b".
+
 std::string htmlize(std::string const&);
 
 inline std::ios_base::openmode ios_in_binary()

Modified: lmi/trunk/wx_table_generator.cpp
===================================================================
--- lmi/trunk/wx_table_generator.cpp    2015-10-07 22:30:22 UTC (rev 6332)
+++ lmi/trunk/wx_table_generator.cpp    2015-10-09 01:56:21 UTC (rev 6333)
@@ -30,53 +30,11 @@
 
 #include "alert.hpp"
 #include "assert_lmi.hpp"
+#include "miscellany.hpp"               // count_lines(), split_into_lines()
 
-#include <algorithm>                    // std::count()
-
 namespace
 {
 
-/// Return the number of lines in a possibly multiline string.
-///
-/// Actually returns one plus the number of newlines in the string.
-/// If, say, a trailing '\n' is found, the count is one greater than
-/// the number of lines--which might be useful, e.g., for adding an
-/// extra blank line to a column header.
-
-std::size_t count_lines(std::string const& s)
-{
-    return 1u + std::count(s.begin(), s.end(), '\n');
-}
-
-/// Split a string into lines separated by new line characters.
-
-std::vector<std::string> split_in_lines(std::string const& s)
-{
-    // BOOST !! Unfortunately boost::split() can't be easily used with the
-    // current ancient version of the library (1.33), so we reimplement it
-    // here.
-    std::vector<std::string> lines;
-    std::string line;
-    for(std::string::const_iterator i = s.begin(); ; ++i)
-        {
-        if(i == s.end() || '\n' == *i)
-            {
-            lines.push_back(line);
-            if(i == s.end())
-                {
-                break;
-                }
-            line.clear();
-            }
-        else
-            {
-            line += *i;
-            }
-        }
-    LMI_ASSERT(lines.size() == count_lines(s));
-    return lines;
-}
-
 /// Increase the first argument to the second one if it's smaller.
 
 template<typename T>
@@ -316,7 +274,7 @@
     for(std::size_t col = 0; col < num_columns; ++col)
         {
         column_info const& ci = columns_.at(col);
-        std::vector<std::string> const lines(split_in_lines(ci.header_));
+        std::vector<std::string> const lines(split_into_lines(ci.header_));
 
         // Fill the elements from the bottom line to the top one, so that a
         // single line header is shown on the last line.




reply via email to

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