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