cleanup text wrapping code in argList

This commit is contained in:
Mark Olesen
2009-12-04 10:09:32 +01:00
parent 6e3ed58ae5
commit ebf94bfc74

View File

@ -34,6 +34,7 @@ License
#include "JobInfo.H" #include "JobInfo.H"
#include "labelList.H" #include "labelList.H"
#include <cctype>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -157,32 +158,37 @@ void Foam::argList::printOptionUsage
} }
} }
// text wrap - this could probably be made more efficient // text wrap
string::size_type pos = 0; string::size_type pos = 0;
while (pos != string::npos && strLen - pos > textWidth) while (pos != string::npos && pos + textWidth < strLen)
{ {
string::size_type prev = pos; // potential end point and next point
string::size_type wordEnd = str.find_first_of(" \t\n", pos); string::size_type curr = pos + textWidth - 1;
string::size_type next = string::npos; string::size_type next = string::npos;
while (wordEnd != string::npos && (wordEnd - pos) < textWidth) if (isspace(str[curr]) || isspace(str[curr+1]))
{ {
prev = wordEnd; // we were lucky: ended on a space or the next one is a space
next = str.find_first_not_of(" \t\n", wordEnd); next = str.find_first_not_of(" \t\n", curr + 1);
if (next == string::npos)
{
wordEnd = string::npos;
} }
else else
{ {
wordEnd = str.find_first_of(" \t\n", next); // search for end of the previous word break
string::size_type prev = str.find_last_of(" \t\n", curr);
// reposition to the end of the previous word if possible
if (prev != string::npos && prev > pos)
{
curr = prev;
} }
} }
if (pos != string::npos) if (next == string::npos)
{ {
// indent next line next = curr + 1;
}
// indent following lines (not the first one)
if (pos) if (pos)
{ {
for (string::size_type i = 0; i < usageMin; ++i) for (string::size_type i = 0; i < usageMin; ++i)
@ -191,14 +197,14 @@ void Foam::argList::printOptionUsage
} }
} }
Info<< str.substr(pos, (prev - pos)).c_str() << nl; Info<< str.substr(pos, (curr - pos)).c_str() << nl;
pos = next; pos = next;
} }
}
// output the remainder of the string
if (pos != string::npos) if (pos != string::npos)
{ {
// indent next line // indent following lines (not the first one)
if (pos) if (pos)
{ {
for (string::size_type i = 0; i < usageMin; ++i) for (string::size_type i = 0; i < usageMin; ++i)