@Test public void testTextWrap() { String s = String.format( TextHelper.LOCALE_ROOT, "alpha bravo charlie delta echo foxtrot golf hotel india%n" + "juliet kilo lima"); assertEquals(String.format( TextHelper.LOCALE_ROOT, " alpha bravo%n" + " charlie delta%n" + " echo foxtrot%n" + " golf hotel%n" + " india%n" + " juliet kilo lima"), TextHelper.wrap( new ASCIITextWidthCounter(), s, 20, 0, " ", " ")); }
public static String wrap(TextWidthCounter textWidthCounter, String s, int width, int initialOffset, String initialIndent, String subsequentIndent) { BreakIterator iter = BreakIterator.getLineInstance(); iter.setText(s); StringBuilder res = new StringBuilder(initialIndent); StringBuilder sb = new StringBuilder(); int currentWidth = initialOffset + initialIndent.length(); for (int start = iter.first(), end = iter.next(); end != BreakIterator.DONE; start = end, end = iter .next()) { String sub = s.substring(start, end); int subWidth = textWidthCounter.width(sub); currentWidth += subWidth; if (currentWidth > width) { res.append(adjustSpace(sb, width, currentWidth - subWidth)) .append(TextHelper.LINESEP).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length() + subWidth; } sb.append(sub); if (sub.endsWith(TextHelper.LINESEP) || sub.endsWith("\n")) { res.append(sb).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length(); } } res.append(sb); return res.toString(); }
TextHelper { public static String wrap(TextWidthCounter textWidthCounter, String s, int width, int initialOffset, String initialIndent, String subsequentIndent) { BreakIterator iter = BreakIterator.getLineInstance(); iter.setText(s); StringBuilder res = new StringBuilder(initialIndent); StringBuilder sb = new StringBuilder(); int currentWidth = initialOffset + initialIndent.length(); for (int start = iter.first(), end = iter.next(); end != BreakIterator.DONE; start = end, end = iter .next()) { String sub = s.substring(start, end); int subWidth = textWidthCounter.width(sub); currentWidth += subWidth; if (currentWidth > width) { res.append(adjustSpace(sb, width, currentWidth - subWidth)) .append(TextHelper.LINESEP).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length() + subWidth; } sb.append(sub); if (sub.endsWith(TextHelper.LINESEP) || sub.endsWith("\n")) { res.append(sb).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length(); } } res.append(sb); return res.toString(); } }
TextHelper { public static String wrap(TextWidthCounter textWidthCounter, String s, int width, int initialOffset, String initialIndent, String subsequentIndent) { BreakIterator iter = BreakIterator.getLineInstance(); iter.setText(s); StringBuilder res = new StringBuilder(initialIndent); StringBuilder sb = new StringBuilder(); int currentWidth = initialOffset + initialIndent.length(); for (int start = iter.first(), end = iter.next(); end != BreakIterator.DONE; start = end, end = iter .next()) { String sub = s.substring(start, end); int subWidth = textWidthCounter.width(sub); currentWidth += subWidth; if (currentWidth > width) { res.append(adjustSpace(sb, width, currentWidth - subWidth)) .append(TextHelper.LINESEP).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length() + subWidth; } sb.append(sub); if (sub.endsWith(TextHelper.LINESEP) || sub.endsWith("\n")) { res.append(sb).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length(); } } res.append(sb); return res.toString(); } private  TextHelper(); }
TextHelper { public static String wrap(TextWidthCounter textWidthCounter, String s, int width, int initialOffset, String initialIndent, String subsequentIndent) { BreakIterator iter = BreakIterator.getLineInstance(); iter.setText(s); StringBuilder res = new StringBuilder(initialIndent); StringBuilder sb = new StringBuilder(); int currentWidth = initialOffset + initialIndent.length(); for (int start = iter.first(), end = iter.next(); end != BreakIterator.DONE; start = end, end = iter .next()) { String sub = s.substring(start, end); int subWidth = textWidthCounter.width(sub); currentWidth += subWidth; if (currentWidth > width) { res.append(adjustSpace(sb, width, currentWidth - subWidth)) .append(TextHelper.LINESEP).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length() + subWidth; } sb.append(sub); if (sub.endsWith(TextHelper.LINESEP) || sub.endsWith("\n")) { res.append(sb).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length(); } } res.append(sb); return res.toString(); } private  TextHelper(); static String concat(T a[], int offset, String sep,
            String start, String end); static String concat(T a[], int offset, String sep); static String concat(Collection<T> a, int offset, String sep,
            String start, String end); static String concat(Collection<T> a, int offset, String sep); static String wrap(TextWidthCounter textWidthCounter, String s,
            int width, int initialOffset, String initialIndent,
            String subsequentIndent); static StringBuilder adjustSpace(StringBuilder sb, int width,
            int curWidth); static void printHelp(PrintWriter writer, String title, String help,
            TextWidthCounter textWidthCounter, int width); static String nonNull(String str); static List<String> findPrefix(Collection<String> src, String prefix); }
TextHelper { public static String wrap(TextWidthCounter textWidthCounter, String s, int width, int initialOffset, String initialIndent, String subsequentIndent) { BreakIterator iter = BreakIterator.getLineInstance(); iter.setText(s); StringBuilder res = new StringBuilder(initialIndent); StringBuilder sb = new StringBuilder(); int currentWidth = initialOffset + initialIndent.length(); for (int start = iter.first(), end = iter.next(); end != BreakIterator.DONE; start = end, end = iter .next()) { String sub = s.substring(start, end); int subWidth = textWidthCounter.width(sub); currentWidth += subWidth; if (currentWidth > width) { res.append(adjustSpace(sb, width, currentWidth - subWidth)) .append(TextHelper.LINESEP).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length() + subWidth; } sb.append(sub); if (sub.endsWith(TextHelper.LINESEP) || sub.endsWith("\n")) { res.append(sb).append(subsequentIndent); sb.delete(0, sb.length()); currentWidth = subsequentIndent.length(); } } res.append(sb); return res.toString(); } private  TextHelper(); static String concat(T a[], int offset, String sep,
            String start, String end); static String concat(T a[], int offset, String sep); static String concat(Collection<T> a, int offset, String sep,
            String start, String end); static String concat(Collection<T> a, int offset, String sep); static String wrap(TextWidthCounter textWidthCounter, String s,
            int width, int initialOffset, String initialIndent,
            String subsequentIndent); static StringBuilder adjustSpace(StringBuilder sb, int width,
            int curWidth); static void printHelp(PrintWriter writer, String title, String help,
            TextWidthCounter textWidthCounter, int width); static String nonNull(String str); static List<String> findPrefix(Collection<String> src, String prefix); static final Locale LOCALE_ROOT; static final String LINESEP; }