diff options
Diffstat (limited to 'util/src/main/java/org/jf/util/StringWrapper.java')
-rw-r--r-- | util/src/main/java/org/jf/util/StringWrapper.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/util/src/main/java/org/jf/util/StringWrapper.java b/util/src/main/java/org/jf/util/StringWrapper.java index 91808300..304c2972 100644 --- a/util/src/main/java/org/jf/util/StringWrapper.java +++ b/util/src/main/java/org/jf/util/StringWrapper.java @@ -33,9 +33,92 @@ package org.jf.util; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.io.PrintStream; +import java.text.BreakIterator; +import java.util.Iterator; public class StringWrapper { /** + * Splits the given string into lines of maximum width maxWidth. The splitting is done using the current locale's + * rules for splitting lines. + * + * @param string The string to split + * @param maxWidth The maximum length of any line + * @return An iterable of Strings containing the wrapped lines + */ + public static Iterable<String> wrapStringOnBreaks(@Nonnull final String string, final int maxWidth) { + // TODO: should we strip any trailing newlines? + final BreakIterator breakIterator = BreakIterator.getLineInstance(); + breakIterator.setText(string); + + return new Iterable<String>() { + @Override + public Iterator<String> iterator() { + return new Iterator<String>() { + private int currentLineStart = 0; + private boolean nextLineSet = false; + private String nextLine; + + @Override + public boolean hasNext() { + if (!nextLineSet) { + calculateNext(); + } + return nextLine != null; + } + + private void calculateNext() { + int lineEnd = currentLineStart; + while (true) { + lineEnd = breakIterator.following(lineEnd); + if (lineEnd == BreakIterator.DONE) { + lineEnd = breakIterator.last(); + if (lineEnd <= currentLineStart) { + nextLine = null; + nextLineSet = true; + return; + } + break; + } + + if (lineEnd - currentLineStart > maxWidth) { + lineEnd = breakIterator.preceding(lineEnd); + if (lineEnd <= currentLineStart) { + lineEnd = currentLineStart + maxWidth; + } + break; + } + + if (string.charAt(lineEnd-1) == '\n') { + nextLine = string.substring(currentLineStart, lineEnd-1); + nextLineSet = true; + currentLineStart = lineEnd; + return; + } + } + nextLine = string.substring(currentLineStart, lineEnd); + nextLineSet = true; + currentLineStart = lineEnd; + } + + @Override + public String next() { + String ret = nextLine; + nextLine = null; + nextLineSet = false; + return ret; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; + } + + /** * Splits the given string into lines using on any embedded newlines, and wrapping the text as needed to conform to * the given maximum line width. * @@ -103,4 +186,14 @@ public class StringWrapper { System.arraycopy(arr, 0, newArr, 0, arr.length); return newArr; } + + public static void printWrappedString(@Nonnull PrintStream stream, @Nonnull String string) { + printWrappedString(stream, string, ConsoleUtil.getConsoleWidth()); + } + + public static void printWrappedString(@Nonnull PrintStream stream, @Nonnull String string, int maxWidth) { + for (String str: wrapStringOnBreaks(string, maxWidth)) { + stream.println(str); + } + } } |