You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/01/29 17:58:03 UTC

svn commit: r1439998 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/HelpFormatter.java test/java/org/apache/commons/cli/HelpFormatterTest.java

Author: tn
Date: Tue Jan 29 16:58:02 2013
New Revision: 1439998

URL: http://svn.apache.org/viewvc?rev=1439998&view=rev
Log:
Fix checkstyle warning wrt inner assignments, added unit test.

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java?rev=1439998&r1=1439997&r2=1439998&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java Tue Jan 29 16:58:02 2013
@@ -981,29 +981,32 @@ public class HelpFormatter
      */
     protected int findWrapPos(String text, int width, int startPos)
     {
-        int pos;
-
         // the line ends before the max wrap pos or a new line char found
-        if (((pos = text.indexOf('\n', startPos)) != -1 && pos <= width)
-                || ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width))
+        int pos = text.indexOf('\n', startPos);
+        if (pos != -1 && pos <= width)
         {
             return pos + 1;
         }
-        else if (startPos + width >= text.length())
+
+        pos = text.indexOf('\t', startPos);
+        if (pos != -1 && pos <= width)
         {
-            return -1;
+            return pos + 1;
         }
 
+        if (startPos + width >= text.length())
+        {
+            return -1;
+        }
 
         // look for the last whitespace character before startPos+width
-        pos = startPos + width;
-
-        char c;
-
-        while ((pos >= startPos) && ((c = text.charAt(pos)) != ' ')
-                && (c != '\n') && (c != '\r'))
+        for (pos = startPos + width; pos >= startPos; --pos)
         {
-            --pos;
+            final char c = text.charAt(pos);
+            if (c == ' ' || c == '\n' || c == '\r')
+            {
+                break;
+            }
         }
 
         // if we found it - just return

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java?rev=1439998&r1=1439997&r2=1439998&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java Tue Jan 29 16:58:02 2013
@@ -54,6 +54,12 @@ public class HelpFormatterTest extends T
         text = "aaaaaa aaaaaa";
         assertEquals("wrap position 4", 6, hf.findWrapPos(text, 6, 0));
         assertEquals("wrap position 4", -1, hf.findWrapPos(text, 6, 7));
+        
+        text = "aaaaaa\n aaaaaa";
+        assertEquals("wrap position 5", 7, hf.findWrapPos(text, 6, 0));
+        
+        text = "aaaaaa\t aaaaaa";
+        assertEquals("wrap position 6", 7, hf.findWrapPos(text, 6, 0));
     }
 
     public void testRenderWrappedTextWordCut()