Skip to content

Commit 6ba1a28

Browse files
committed
Fix checkstyle warning wrt inner assignments, added unit test.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1439998 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5b75fcd commit 6ba1a28

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

src/main/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -981,29 +981,32 @@ private Appendable renderWrappedTextBlock(StringBuffer sb, int width, int nextLi
981981
*/
982982
protected int findWrapPos(String text, int width, int startPos)
983983
{
984-
int pos;
985-
986984
// the line ends before the max wrap pos or a new line char found
987-
if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
988-
|| ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
985+
int pos = text.indexOf('\n', startPos);
986+
if (pos != -1 && pos <= width)
989987
{
990988
return pos + 1;
991989
}
992-
else if (startPos + width >= text.length())
990+
991+
pos = text.indexOf('\t', startPos);
992+
if (pos != -1 && pos <= width)
993993
{
994-
return -1;
994+
return pos + 1;
995995
}
996996

997+
if (startPos + width >= text.length())
998+
{
999+
return -1;
1000+
}
9971001

9981002
// look for the last whitespace character before startPos+width
999-
pos = startPos + width;
1000-
1001-
char c;
1002-
1003-
while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ')
1004-
&& (c != '\n') && (c != '\r'))
1003+
for (pos = startPos + width; pos >= startPos; --pos)
10051004
{
1006-
--pos;
1005+
final char c = text.charAt(pos);
1006+
if (c == ' ' || c == '\n' || c == '\r')
1007+
{
1008+
break;
1009+
}
10071010
}
10081011

10091012
// if we found it - just return

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public void testFindWrapPos() throws Exception
5454
text = "aaaaaa aaaaaa";
5555
assertEquals("wrap position 4", 6, hf.findWrapPos(text, 6, 0));
5656
assertEquals("wrap position 4", -1, hf.findWrapPos(text, 6, 7));
57+
58+
text = "aaaaaa\n aaaaaa";
59+
assertEquals("wrap position 5", 7, hf.findWrapPos(text, 6, 0));
60+
61+
text = "aaaaaa\t aaaaaa";
62+
assertEquals("wrap position 6", 7, hf.findWrapPos(text, 6, 0));
5763
}
5864

5965
public void testRenderWrappedTextWordCut()

0 commit comments

Comments
 (0)