fixup! Fix trailing whitespace in text reformat

We still got the extra (double) newline after the paragraph.
This commit is contained in:
Allan Odgaard
2012-08-18 22:07:48 +02:00
parent 1477288795
commit 12d700a8a9

View File

@@ -141,6 +141,14 @@ namespace transform
return "";
}
static size_t length_excl_whitespace (std::string const& buffer, size_t bol, size_t eol)
{
size_t len = eol - bol;
while(len > 0 && strchr(" \n", buffer[bol + len - 1]))
--len;
return len;
}
std::string reformat::operator() (std::string const& src) const
{
std::string res;
@@ -149,17 +157,13 @@ namespace transform
std::string const fillStr = fill_string(unwrapped);
citerate(offset, text::soft_breaks(unwrapped, wrap, tabSize, fillStr.size()))
{
size_t len = *offset - from;
while(len > 0 && unwrapped[from + len - 1] == ' ')
--len;
res += unwrapped.substr(from, len);
res += unwrapped.substr(from, length_excl_whitespace(unwrapped, from, *offset));
res += "\n";
if(*offset != unwrapped.size())
res += fillStr;
from = *offset;
}
res += unwrapped.substr(from);
res += unwrapped.substr(from, length_excl_whitespace(unwrapped, from, unwrapped.size()));
return newline ? res + "\n" : res;
}
@@ -173,15 +177,11 @@ namespace transform
std::string const str = std::string(it->first, it->second);
citerate(offset, text::soft_breaks(str, wrap, tabSize))
{
size_t len = *offset - from;
while(len > 0 && unwrapped[from + len - 1] == ' ')
--len;
res += justify_line(unwrapped.substr(from, len), wrap, tabSize);
res += justify_line(unwrapped.substr(from, length_excl_whitespace(str, from, *offset)), wrap, tabSize);
res += "\n";
from = *offset;
}
res += str.substr(from);
res += str.substr(from, length_excl_whitespace(str, from, str.size()));
}
return newline ? res + "\n" : res;
}