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 javax.annotation.Nullable;
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(@Nullable String string) {
041    return (string == null) ? "" : 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  @Nullable
051  public static String emptyToNull(@Nullable String string) {
052    return isNullOrEmpty(string) ? null : 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(@Nullable 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
170   * {@code a.toString().startsWith(prefix) && b.toString().startsWith(prefix)}, taking care not to
171   * split surrogate pairs. If {@code a} and {@code b} have no common prefix, returns the empty
172   * string.
173   *
174   * @since 11.0
175   */
176  public static String commonPrefix(CharSequence a, CharSequence b) {
177    checkNotNull(a);
178    checkNotNull(b);
179
180    int maxPrefixLength = Math.min(a.length(), b.length());
181    int p = 0;
182    while (p < maxPrefixLength && a.charAt(p) == b.charAt(p)) {
183      p++;
184    }
185    if (validSurrogatePairAt(a, p - 1) || validSurrogatePairAt(b, p - 1)) {
186      p--;
187    }
188    return a.subSequence(0, p).toString();
189  }
190
191  /**
192   * Returns the longest string {@code suffix} such that
193   * {@code a.toString().endsWith(suffix) && b.toString().endsWith(suffix)}, taking care not to
194   * split surrogate pairs. If {@code a} and {@code b} have no common suffix, returns the empty
195   * 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}