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