1 /* 2 * Copyright 2013, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.util; 33 34 import javax.annotation.Nonnull; 35 import javax.annotation.Nullable; 36 37 public class StringWrapper { 38 /** 39 * Splits the given string into lines using on any embedded newlines, and wrapping the text as needed to conform to 40 * the given maximum line width. 41 * 42 * This uses and assumes unix-style newlines 43 * 44 * @param str The string to split 45 * @param maxWidth The maximum length of any line 46 * @param output If given, try to use this array as the return value. If there are more values than will fit 47 * into the array, a new array will be allocated and returned, while the given array will be filled 48 * with as many lines as would fit. 49 * @return The split lines from the original, as an array of Strings. The returned array may be larger than the 50 * number of lines. If this is the case, the end of the split lines will be denoted by a null entry in the 51 * array. If there is no null entry, then the size of the array exactly matches the number of lines. 52 * The returned lines will not contain an ending newline 53 */ 54 public static String[] wrapString(@Nonnull String str, int maxWidth, @Nullable String[] output) { 55 if (output == null) { 56 output = new String[(int)((str.length() / maxWidth) * 1.5d + 1)]; 57 } 58 59 int lineStart = 0; 60 int arrayIndex = 0; 61 int i; 62 for (i=0; i<str.length(); i++) { 63 char c = str.charAt(i); 64 65 if (c == '\n') { 66 output = addString(output, str.substring(lineStart, i), arrayIndex++); 67 lineStart = i+1; 68 } else if (i - lineStart == maxWidth) { 69 output = addString(output, str.substring(lineStart, i), arrayIndex++); 70 lineStart = i; 71 } 72 } 73 if (lineStart != i || i == 0) { 74 output = addString(output, str.substring(lineStart), arrayIndex++, output.length+1); 75 } 76 77 if (arrayIndex < output.length) { 78 output[arrayIndex] = null; 79 } 80 return output; 81 } 82 83 private static String[] addString(@Nonnull String[] arr, String str, int index) { 84 if (index >= arr.length) { 85 arr = enlargeArray(arr, (int)(Math.ceil((arr.length + 1) * 1.5))); 86 } 87 88 arr[index] = str; 89 return arr; 90 } 91 92 private static String[] addString(@Nonnull String[] arr, String str, int index, int newLength) { 93 if (index >= arr.length) { 94 arr = enlargeArray(arr, newLength); 95 } 96 97 arr[index] = str; 98 return arr; 99 } 100 101 private static String[] enlargeArray(String[] arr, int newLength) { 102 String[] newArr = new String[newLength]; 103 System.arraycopy(arr, 0, newArr, 0, arr.length); 104 return newArr; 105 } 106 } 107