LANG-995: Fix bug with stripping spaces on last line in WordUtils.wrap(). This fixes #18 from github. Thanks to Andrey Khobnya

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1586649 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2014-04-11 13:28:30 +00:00
parent d99f581745
commit fbb0f7f88c
3 changed files with 12 additions and 1 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-995" type="fix" dev="britter" due-to="Andrey Khobnya">Fix bug with stripping spaces on last line in WordUtils.wrap()</action>
</release>
<release version="3.3.2" date="2014-04-09" description="Bugfix for a bug in NumberUtils introduced in 3.3.1">

View File

@ -183,11 +183,15 @@ public class WordUtils {
int offset = 0;
final StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);
while (inputLineLength - offset > wrapLength) {
while (offset < inputLineLength) {
if (str.charAt(offset) == ' ') {
offset++;
continue;
}
// only last line without leading spaces is left
if(inputLineLength - offset <= wrapLength) {
break;
}
int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= offset) {

View File

@ -71,6 +71,12 @@ public class WordUtilsTest {
expected = "Click here," + systemNewLine + "http://commons.apache.org," + systemNewLine
+ "to jump to the" + systemNewLine + "commons website";
assertEquals(expected, WordUtils.wrap(input, 20));
// leading spaces on a new line are stripped
// trailing spaces are not stripped
input = "word1 word2 word3";
expected = "word1 " + systemNewLine + "word2 " + systemNewLine + "word3";
assertEquals(expected, WordUtils.wrap(input, 7));
}
@Test