001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.base;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.VisibleForTesting;
024
025import java.util.Formatter;
026
027import javax.annotation.CheckReturnValue;
028import javax.annotation.Nullable;
029
030/**
031 * Static utility methods pertaining to {@code String} or {@code CharSequence}
032 * instances.
033 *
034 * @author Kevin Bourrillion
035 * @since 3.0
036 */
037@CheckReturnValue
038@GwtCompatible
039public final class Strings {
040  private Strings() {}
041
042  /**
043   * Returns the given string if it is non-null; the empty string otherwise.
044   *
045   * @param string the string to test and possibly return
046   * @return {@code string} itself if it is non-null; {@code ""} if it is null
047   */
048  public static String nullToEmpty(@Nullable String string) {
049    return (string == null) ? "" : string;
050  }
051
052  /**
053   * Returns the given string if it is nonempty; {@code null} otherwise.
054   *
055   * @param string the string to test and possibly return
056   * @return {@code string} itself if it is nonempty; {@code null} if it is
057   *     empty or null
058   */
059  @Nullable
060  public static String emptyToNull(@Nullable String string) {
061    return isNullOrEmpty(string) ? null : string;
062  }
063
064  /**
065   * Returns {@code true} if the given string is null or is the empty string.
066   *
067   * <p>Consider normalizing your string references with {@link #nullToEmpty}.
068   * If you do, you can use {@link String#isEmpty()} instead of this
069   * method, and you won't need special null-safe forms of methods like {@link
070   * String#toUpperCase} either. Or, if you'd like to normalize "in the other
071   * direction," converting empty strings to {@code null}, you can use {@link
072   * #emptyToNull}.
073   *
074   * @param string a string reference to check
075   * @return {@code true} if the string is null or is the empty string
076   */
077  public static boolean isNullOrEmpty(@Nullable String string) {
078    return string == null || string.length() == 0; // string.isEmpty() in Java 6
079  }
080
081  /**
082   * Returns a string, of length at least {@code minLength}, consisting of
083   * {@code string} prepended with as many copies of {@code padChar} as are
084   * necessary to reach that length. For example,
085   *
086   * <ul>
087   * <li>{@code padStart("7", 3, '0')} returns {@code "007"}
088   * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"}
089   * </ul>
090   *
091   * <p>See {@link Formatter} for a richer set of formatting capabilities.
092   *
093   * @param string the string which should appear at the end of the result
094   * @param minLength the minimum length the resulting string must have. Can be
095   *     zero or negative, in which case the input string is always returned.
096   * @param padChar the character to insert at the beginning of the result until
097   *     the minimum length is reached
098   * @return the padded string
099   */
100  public static String padStart(String string, int minLength, char padChar) {
101    checkNotNull(string); // eager for GWT.
102    if (string.length() >= minLength) {
103      return string;
104    }
105    StringBuilder sb = new StringBuilder(minLength);
106    for (int i = string.length(); i < minLength; i++) {
107      sb.append(padChar);
108    }
109    sb.append(string);
110    return sb.toString();
111  }
112
113  /**
114   * Returns a string, of length at least {@code minLength}, consisting of
115   * {@code string} appended with as many copies of {@code padChar} as are
116   * necessary to reach that length. For example,
117   *
118   * <ul>
119   * <li>{@code padEnd("4.", 5, '0')} returns {@code "4.000"}
120   * <li>{@code padEnd("2010", 3, '!')} returns {@code "2010"}
121   * </ul>
122   *
123   * <p>See {@link Formatter} for a richer set of formatting capabilities.
124   *
125   * @param string the string which should appear at the beginning of the result
126   * @param minLength the minimum length the resulting string must have. Can be
127   *     zero or negative, in which case the input string is always returned.
128   * @param padChar the character to append to the end of the result until the
129   *     minimum length is reached
130   * @return the padded string
131   */
132  public static String padEnd(String string, int minLength, char padChar) {
133    checkNotNull(string); // eager for GWT.
134    if (string.length() >= minLength) {
135      return string;
136    }
137    StringBuilder sb = new StringBuilder(minLength);
138    sb.append(string);
139    for (int i = string.length(); i < minLength; i++) {
140      sb.append(padChar);
141    }
142    return sb.toString();
143  }
144
145  /**
146   * Returns a string consisting of a specific number of concatenated copies of
147   * an input string. For example, {@code repeat("hey", 3)} returns the string
148   * {@code "heyheyhey"}.
149   *
150   * @param string any non-null string
151   * @param count the number of times to repeat it; a nonnegative integer
152   * @return a string containing {@code string} repeated {@code count} times
153   *     (the empty string if {@code count} is zero)
154   * @throws IllegalArgumentException if {@code count} is negative
155   */
156  public static String repeat(String string, int count) {
157    checkNotNull(string); // eager for GWT.
158
159    if (count <= 1) {
160      checkArgument(count >= 0, "invalid count: %s", count);
161      return (count == 0) ? "" : string;
162    }
163
164    // IF YOU MODIFY THE CODE HERE, you must update StringsRepeatBenchmark
165    final int len = string.length();
166    final long longSize = (long) len * (long) count;
167    final int size = (int) longSize;
168    if (size != longSize) {
169      throw new ArrayIndexOutOfBoundsException("Required array size too large: " + longSize);
170    }
171
172    final char[] array = new char[size];
173    string.getChars(0, len, array, 0);
174    int n;
175    for (n = len; n < size - n; n <<= 1) {
176      System.arraycopy(array, 0, array, n, n);
177    }
178    System.arraycopy(array, 0, array, n, size - n);
179    return new String(array);
180  }
181
182  /**
183   * Returns the longest string {@code prefix} such that
184   * {@code a.toString().startsWith(prefix) && b.toString().startsWith(prefix)},
185   * taking care not to split surrogate pairs. If {@code a} and {@code b} have
186   * no common prefix, returns the empty string.
187   *
188   * @since 11.0
189   */
190  public static String commonPrefix(CharSequence a, CharSequence b) {
191    checkNotNull(a);
192    checkNotNull(b);
193
194    int maxPrefixLength = Math.min(a.length(), b.length());
195    int p = 0;
196    while (p < maxPrefixLength && a.charAt(p) == b.charAt(p)) {
197      p++;
198    }
199    if (validSurrogatePairAt(a, p - 1) || validSurrogatePairAt(b, p - 1)) {
200      p--;
201    }
202    return a.subSequence(0, p).toString();
203  }
204
205  /**
206   * Returns the longest string {@code suffix} such that
207   * {@code a.toString().endsWith(suffix) && b.toString().endsWith(suffix)},
208   * taking care not to split surrogate pairs. If {@code a} and {@code b} have
209   * no common suffix, returns the empty string.
210   *
211   * @since 11.0
212   */
213  public static String commonSuffix(CharSequence a, CharSequence b) {
214    checkNotNull(a);
215    checkNotNull(b);
216
217    int maxSuffixLength = Math.min(a.length(), b.length());
218    int s = 0;
219    while (s < maxSuffixLength && a.charAt(a.length() - s - 1) == b.charAt(b.length() - s - 1)) {
220      s++;
221    }
222    if (validSurrogatePairAt(a, a.length() - s - 1)
223        || validSurrogatePairAt(b, b.length() - s - 1)) {
224      s--;
225    }
226    return a.subSequence(a.length() - s, a.length()).toString();
227  }
228
229  /**
230   * True when a valid surrogate pair starts at the given {@code index} in the
231   * given {@code string}. Out-of-range indexes return false.
232   */
233  @VisibleForTesting
234  static boolean validSurrogatePairAt(CharSequence string, int index) {
235    return index >= 0
236        && index <= (string.length() - 2)
237        && Character.isHighSurrogate(string.charAt(index))
238        && Character.isLowSurrogate(string.charAt(index + 1));
239  }
240}