001/* 002 * Copyright (C) 2010 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.base; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019 020import com.google.common.annotations.GwtCompatible; 021import com.google.common.annotations.VisibleForTesting; 022import org.checkerframework.checker.nullness.compatqual.NullableDecl; 023 024/** 025 * Static utility methods pertaining to {@code String} or {@code CharSequence} instances. 026 * 027 * @author Kevin Bourrillion 028 * @since 3.0 029 */ 030@GwtCompatible 031public final class Strings { 032 private Strings() {} 033 034 /** 035 * Returns the given string if it is non-null; the empty string otherwise. 036 * 037 * @param string the string to test and possibly return 038 * @return {@code string} itself if it is non-null; {@code ""} if it is null 039 */ 040 public static String nullToEmpty(@NullableDecl String string) { 041 return Platform.nullToEmpty(string); 042 } 043 044 /** 045 * Returns the given string if it is nonempty; {@code null} otherwise. 046 * 047 * @param string the string to test and possibly return 048 * @return {@code string} itself if it is nonempty; {@code null} if it is empty or null 049 */ 050 @NullableDecl 051 public static String emptyToNull(@NullableDecl String string) { 052 return Platform.emptyToNull(string); 053 } 054 055 /** 056 * Returns {@code true} if the given string is null or is the empty string. 057 * 058 * <p>Consider normalizing your string references with {@link #nullToEmpty}. If you do, you can 059 * use {@link String#isEmpty()} instead of this method, and you won't need special null-safe forms 060 * of methods like {@link String#toUpperCase} either. Or, if you'd like to normalize "in the other 061 * direction," converting empty strings to {@code null}, you can use {@link #emptyToNull}. 062 * 063 * @param string a string reference to check 064 * @return {@code true} if the string is null or is the empty string 065 */ 066 public static boolean isNullOrEmpty(@NullableDecl String string) { 067 return Platform.stringIsNullOrEmpty(string); 068 } 069 070 /** 071 * Returns a string, of length at least {@code minLength}, consisting of {@code string} prepended 072 * with as many copies of {@code padChar} as are necessary to reach that length. For example, 073 * 074 * <ul> 075 * <li>{@code padStart("7", 3, '0')} returns {@code "007"} 076 * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"} 077 * </ul> 078 * 079 * <p>See {@link java.util.Formatter} for a richer set of formatting capabilities. 080 * 081 * @param string the string which should appear at the end of the result 082 * @param minLength the minimum length the resulting string must have. Can be zero or negative, in 083 * which case the input string is always returned. 084 * @param padChar the character to insert at the beginning of the result until the minimum length 085 * is reached 086 * @return the padded string 087 */ 088 public static String padStart(String string, int minLength, char padChar) { 089 checkNotNull(string); // eager for GWT. 090 if (string.length() >= minLength) { 091 return string; 092 } 093 StringBuilder sb = new StringBuilder(minLength); 094 for (int i = string.length(); i < minLength; i++) { 095 sb.append(padChar); 096 } 097 sb.append(string); 098 return sb.toString(); 099 } 100 101 /** 102 * Returns a string, of length at least {@code minLength}, consisting of {@code string} appended 103 * with as many copies of {@code padChar} as are necessary to reach that length. For example, 104 * 105 * <ul> 106 * <li>{@code padEnd("4.", 5, '0')} returns {@code "4.000"} 107 * <li>{@code padEnd("2010", 3, '!')} returns {@code "2010"} 108 * </ul> 109 * 110 * <p>See {@link java.util.Formatter} for a richer set of formatting capabilities. 111 * 112 * @param string the string which should appear at the beginning of the result 113 * @param minLength the minimum length the resulting string must have. Can be zero or negative, in 114 * which case the input string is always returned. 115 * @param padChar the character to append to the end of the result until the minimum length is 116 * reached 117 * @return the padded string 118 */ 119 public static String padEnd(String string, int minLength, char padChar) { 120 checkNotNull(string); // eager for GWT. 121 if (string.length() >= minLength) { 122 return string; 123 } 124 StringBuilder sb = new StringBuilder(minLength); 125 sb.append(string); 126 for (int i = string.length(); i < minLength; i++) { 127 sb.append(padChar); 128 } 129 return sb.toString(); 130 } 131 132 /** 133 * Returns a string consisting of a specific number of concatenated copies of an input string. For 134 * example, {@code repeat("hey", 3)} returns the string {@code "heyheyhey"}. 135 * 136 * @param string any non-null string 137 * @param count the number of times to repeat it; a nonnegative integer 138 * @return a string containing {@code string} repeated {@code count} times (the empty string if 139 * {@code count} is zero) 140 * @throws IllegalArgumentException if {@code count} is negative 141 */ 142 public static String repeat(String string, int count) { 143 checkNotNull(string); // eager for GWT. 144 145 if (count <= 1) { 146 checkArgument(count >= 0, "invalid count: %s", count); 147 return (count == 0) ? "" : string; 148 } 149 150 // IF YOU MODIFY THE CODE HERE, you must update StringsRepeatBenchmark 151 final int len = string.length(); 152 final long longSize = (long) len * (long) count; 153 final int size = (int) longSize; 154 if (size != longSize) { 155 throw new ArrayIndexOutOfBoundsException("Required array size too large: " + longSize); 156 } 157 158 final char[] array = new char[size]; 159 string.getChars(0, len, array, 0); 160 int n; 161 for (n = len; n < size - n; n <<= 1) { 162 System.arraycopy(array, 0, array, n, n); 163 } 164 System.arraycopy(array, 0, array, n, size - n); 165 return new String(array); 166 } 167 168 /** 169 * Returns the longest string {@code prefix} such that {@code a.toString().startsWith(prefix) && 170 * b.toString().startsWith(prefix)}, taking care not to split surrogate pairs. If {@code a} and 171 * {@code b} have no common prefix, returns the empty string. 172 * 173 * @since 11.0 174 */ 175 public static String commonPrefix(CharSequence a, CharSequence b) { 176 checkNotNull(a); 177 checkNotNull(b); 178 179 int maxPrefixLength = Math.min(a.length(), b.length()); 180 int p = 0; 181 while (p < maxPrefixLength && a.charAt(p) == b.charAt(p)) { 182 p++; 183 } 184 if (validSurrogatePairAt(a, p - 1) || validSurrogatePairAt(b, p - 1)) { 185 p--; 186 } 187 return a.subSequence(0, p).toString(); 188 } 189 190 /** 191 * Returns the longest string {@code suffix} such that {@code a.toString().endsWith(suffix) && 192 * b.toString().endsWith(suffix)}, taking care not to split surrogate pairs. If {@code a} and 193 * {@code b} have no common suffix, returns the empty string. 194 * 195 * @since 11.0 196 */ 197 public static String commonSuffix(CharSequence a, CharSequence b) { 198 checkNotNull(a); 199 checkNotNull(b); 200 201 int maxSuffixLength = Math.min(a.length(), b.length()); 202 int s = 0; 203 while (s < maxSuffixLength && a.charAt(a.length() - s - 1) == b.charAt(b.length() - s - 1)) { 204 s++; 205 } 206 if (validSurrogatePairAt(a, a.length() - s - 1) 207 || validSurrogatePairAt(b, b.length() - s - 1)) { 208 s--; 209 } 210 return a.subSequence(a.length() - s, a.length()).toString(); 211 } 212 213 /** 214 * Returns the given {@code template} string with each occurrence of {@code "%s"} replaced with 215 * the corresponding argument value from {@code args}; or, if the placeholder and argument counts 216 * do not match, returns a best-effort form of that string. Will not throw an exception under any 217 * circumstances (as long as all arguments' {@code toString} methods successfully return). 218 * 219 * <p><b>Note:</b> For most string-formatting needs, use {@link String#format}, {@link 220 * PrintWriter#format}, and related methods. These support the full range of {@linkplain 221 * Formatter#syntax format specifiers}, and alert you to usage errors by throwing {@link 222 * InvalidFormatException}. 223 * 224 * <p>In certain cases, such as outputting debugging information or constructing a message to be 225 * used for another unchecked exception, an exception during string formatting would serve little 226 * purpose except to supplant the real information you were trying to provide. These are the cases 227 * this method is made for; it instead generates a best-effort string with all supplied argument 228 * values present. This method is also useful in environments such as GWT where {@code 229 * String.format} is not available. As an example, method implementations of the {@link 230 * Preconditions} class use this formatter, for both of the reasons just discussed. 231 * 232 * <p><b>Warning:</b> Only the exact two-character placeholder sequence {@code "%s"} is 233 * recognized. 234 * 235 * @param template a string containing zero or more {@code "%s"} placeholder sequences. {@code 236 * null} is treated as the four-character string {@code "null"}. 237 * @param args the arguments to be substituted into the message template. The first argument 238 * specified is substituted for the first occurrence of {@code "%s"} in the template, and so 239 * forth. A {@code null} argument is converted to the four-character string {@code "null"}; 240 * non-null values are converted to strings using {@link Object#toString()}. 241 * @since 25.1 242 */ 243 // TODO(diamondm) consider using Arrays.toString() for array parameters 244 // TODO(diamondm) capture exceptions thrown from arguments' toString methods 245 public static String lenientFormat(@NullableDecl String template, @NullableDecl Object... args) { 246 template = String.valueOf(template); // null -> "null" 247 248 args = args == null ? new Object[] {"(Object[])null"} : args; 249 250 // start substituting the arguments into the '%s' placeholders 251 StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); 252 int templateStart = 0; 253 int i = 0; 254 while (i < args.length) { 255 int placeholderStart = template.indexOf("%s", templateStart); 256 if (placeholderStart == -1) { 257 break; 258 } 259 builder.append(template, templateStart, placeholderStart); 260 builder.append(args[i++]); 261 templateStart = placeholderStart + 2; 262 } 263 builder.append(template, templateStart, template.length()); 264 265 // if we run out of placeholders, append the extra args in square braces 266 if (i < args.length) { 267 builder.append(" ["); 268 builder.append(args[i++]); 269 while (i < args.length) { 270 builder.append(", "); 271 builder.append(args[i++]); 272 } 273 builder.append(']'); 274 } 275 276 return builder.toString(); 277 } 278 279 /** 280 * True when a valid surrogate pair starts at the given {@code index} in the given {@code string}. 281 * Out-of-range indexes return false. 282 */ 283 @VisibleForTesting 284 static boolean validSurrogatePairAt(CharSequence string, int index) { 285 return index >= 0 286 && index <= (string.length() - 2) 287 && Character.isHighSurrogate(string.charAt(index)) 288 && Character.isLowSurrogate(string.charAt(index + 1)); 289 } 290}