1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* Copyright 2018, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
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.
*
* This uses and assumes unix-style newlines
*
* @param str The string to split
* @param maxWidth The maximum length of any line
* @param output If given, try to use this array as the return value. If there are more values than will fit
* into the array, a new array will be allocated and returned, while the given array will be filled
* with as many lines as would fit.
* @return The split lines from the original, as an array of Strings. The returned array may be larger than the
* number of lines. If this is the case, the end of the split lines will be denoted by a null entry in the
* array. If there is no null entry, then the size of the array exactly matches the number of lines.
* The returned lines will not contain an ending newline
*/
public static String[] wrapString(@Nonnull String str, int maxWidth, @Nullable String[] output) {
if (output == null) {
output = new String[(int)((str.length() / maxWidth) * 1.5d + 1)];
}
int lineStart = 0;
int arrayIndex = 0;
int i;
for (i=0; i<str.length(); i++) {
char c = str.charAt(i);
if (c == '\n') {
output = addString(output, str.substring(lineStart, i), arrayIndex++);
lineStart = i+1;
} else if (i - lineStart == maxWidth) {
output = addString(output, str.substring(lineStart, i), arrayIndex++);
lineStart = i;
}
}
if (lineStart != i || i == 0) {
output = addString(output, str.substring(lineStart), arrayIndex++, output.length+1);
}
if (arrayIndex < output.length) {
output[arrayIndex] = null;
}
return output;
}
private static String[] addString(@Nonnull String[] arr, String str, int index) {
if (index >= arr.length) {
arr = enlargeArray(arr, (int)(Math.ceil((arr.length + 1) * 1.5)));
}
arr[index] = str;
return arr;
}
private static String[] addString(@Nonnull String[] arr, String str, int index, int newLength) {
if (index >= arr.length) {
arr = enlargeArray(arr, newLength);
}
arr[index] = str;
return arr;
}
private static String[] enlargeArray(String[] arr, int newLength) {
String[] newArr = new String[newLength];
System.arraycopy(arr, 0, newArr, 0, arr.length);
return newArr;
}
public static void printWrappedString(@Nonnull PrintStream stream, @Nonnull String string, int maxWidth) {
for (String str: wrapStringOnBreaks(string, maxWidth)) {
stream.println(str);
}
}
}
|