aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-06-10 16:02:29 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-06-11 14:27:09 +0200
commit97789d374c40f4f7fc8feb19c1235ca09ad2e06e (patch)
tree38373d43cf0f5c36321fe46a1d38c7992acd3887 /src/util.cpp
parent96b733e99694e74dcd38b16112655f7e1ea2d43b (diff)
util: Add function FormatParagraph to format paragraph to fixed-width
This is to be used for the `-version` and `-help` messages.
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp
index cccf2df484..3e3dabb678 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1407,3 +1407,38 @@ std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
ss << boost::posix_time::from_time_t(nTime);
return ss.str();
}
+
+std::string FormatParagraph(const std::string in, size_t width, size_t indent)
+{
+ std::stringstream out;
+ size_t col = 0;
+ size_t ptr = 0;
+ while(ptr < in.size())
+ {
+ // Find beginning of next word
+ ptr = in.find_first_not_of(' ', ptr);
+ if (ptr == std::string::npos)
+ break;
+ // Find end of next word
+ size_t endword = in.find_first_of(' ', ptr);
+ if (endword == std::string::npos)
+ endword = in.size();
+ // Add newline and indentation if this wraps over the allowed width
+ if (col > 0)
+ {
+ if ((col + endword - ptr) > width)
+ {
+ out << '\n';
+ for(size_t i=0; i<indent; ++i)
+ out << ' ';
+ col = 0;
+ } else
+ out << ' ';
+ }
+ // Append word
+ out << in.substr(ptr, endword - ptr);
+ col += endword - ptr;
+ ptr = endword;
+ }
+ return out.str();
+}