[PPL-devel] [GIT] ppl/ppl(master): Added a new helper function for the wrapping of lines.

Roberto Bagnara bagnara at cs.unipr.it
Sat Mar 21 21:37:11 CET 2009


Module: ppl/ppl
Branch: master
Commit: aca3e12a7959550abe459bafdffc0dab09fa65f2
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=aca3e12a7959550abe459bafdffc0dab09fa65f2

Author: Roberto Bagnara <bagnara at cs.unipr.it>
Date:   Sat Mar 21 21:36:38 2009 +0100

Added a new helper function for the wrapping of lines.

---

 src/pretty_print.cc |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/pretty_print.hh |   23 +++++++++++++++++++++
 2 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/src/pretty_print.cc b/src/pretty_print.cc
index fbcdfb9..697f5e6 100644
--- a/src/pretty_print.cc
+++ b/src/pretty_print.cc
@@ -184,6 +184,62 @@ size_t wrap(Write_Function& wfunc,
   return written;
 }
 
+void
+wrap(std::string& dst, const std::string& src_string,
+     unsigned indent_depth,
+     unsigned preferred_first_line_length,
+     unsigned preferred_line_length) {
+  dst.clear();
+  const char *src = src_string.c_str();
+  for (int line = 0; ; ++line) {
+    int linelen = (line == 0
+                   ? preferred_first_line_length
+                   : preferred_line_length);
+    int last_comma = -1;
+    int last_space = -1;
+    int split_pos = -1;
+    int i;
+    for (i = 0; i <= linelen; ++i) {
+      if (src[i] == '\0' || src[i] == '\n') {
+	split_pos = i;
+	break;
+      }
+      if (src[i] == ',' && i < linelen)
+	last_comma = i;
+      if (isspace(src[i]) && (i == 0 || !isspace(src[i-1])))
+	last_space = i;
+    }
+    if (split_pos < 0) {
+      if (last_comma >= 0)
+	split_pos = last_comma + 1;
+      else if (last_space >= 0)
+	split_pos = last_space;
+      else {
+	for ( ; src[i]; ++i) {
+	  if (src[i] == ',') {
+	    ++i;
+	    break;
+	  }
+	  if (isspace(src[i]))
+	    break;
+	}
+	split_pos = i;
+      }
+    }
+    if (split_pos > 0 && line > 0 && indent_depth > 0)
+	dst.append(indent_depth, ' ');
+    dst.append(src, split_pos);
+    src += split_pos;
+    if (isspace(*src))
+      ++src;
+    while (*src == ' ')
+      ++src;
+    if (*src == '\0')
+      break;
+    dst.push_back('\n');
+  }
+}
+
 } // namespace IO_Operators
 
 } // namespace Parma_Polyhedra_Library
diff --git a/src/pretty_print.hh b/src/pretty_print.hh
index 1156d85..de8aa57 100644
--- a/src/pretty_print.hh
+++ b/src/pretty_print.hh
@@ -138,6 +138,29 @@ c_pretty_print(const T& o, write_function wfunc, void* data,
                unsigned preferred_first_line_length,
                unsigned preferred_line_length);
 
+//! Helper function for the wrapping of lines.
+/*!
+  \param dst
+  The destination string.
+
+  \param src
+  The source string holding the lines to wrap.
+
+  \param indent_depth
+  The indentation depth.
+
+  \param preferred_first_line_length
+  The preferred length for the first line of text.
+
+  \param preferred_line_length
+  The preferred length for all the lines but the first one.
+*/
+void
+wrap(std::string& dst, const std::string& src_string,
+     unsigned indent_depth,
+     unsigned preferred_first_line_length,
+     unsigned preferred_line_length);
+
 } // namespace IO_Operators
 
 } // namespace Parma_Polyhedra_Library




More information about the PPL-devel mailing list