Created
October 3, 2010 20:58
-
-
Save unclebob/608933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Wrapper { | |
public static String wrap(String s, int col) { | |
return new Wrapper(col).wrap(s); | |
} | |
private int col; | |
private Wrapper(int col) { | |
this.col = col; | |
} | |
private String wrap(String s) { | |
if (s.length() <= col) | |
return s; | |
int space = (s.substring(0, col).lastIndexOf(' ')); | |
if (space != -1) | |
return breakLine(s, space, 1); | |
else if (s.charAt(col) == ' ') | |
return breakLine(s, col, 1); | |
else | |
return breakLine(s, col, 0); | |
} | |
private String breakLine(String s, int pos, int gap) { | |
return s.substring(0, pos) + "\n" + wrap(s.substring(pos + gap), col); | |
} | |
} |
I've cloned this gist and changed some things:
git://gist.github.com/1274003.git
This is a more efficient last-index computation for line 15:
int space = s.lastIndexOf(' ', col);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find confusing the 3-arguments function breakLine. Furthermore, the gap argument looks like a flag for me and, as you pointed out in Clean Code, we must avoid flag arguments.
Would you do this kata differently nowadays?