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    
017    package com.google.common.base;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    import static com.google.common.base.Preconditions.checkNotNull;
021    
022    import com.google.common.annotations.Beta;
023    import com.google.common.annotations.GwtCompatible;
024    import com.google.common.annotations.VisibleForTesting;
025    
026    import java.util.Formatter;
027    
028    import 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    @GwtCompatible
038    public 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(@Nullable String string) {
048        return (string == null) ? "" : 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
056       *     empty or null
057       */
058      public static @Nullable String emptyToNull(@Nullable String string) {
059        return isNullOrEmpty(string) ? null : 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}.
066       * If you do, you can use {@link String#isEmpty()} instead of this
067       * method, and you won't need special null-safe forms of methods like {@link
068       * String#toUpperCase} either. Or, if you'd like to normalize "in the other
069       * direction," converting empty strings to {@code null}, you can use {@link
070       * #emptyToNull}.
071       *
072       * @param string a string reference to check
073       * @return {@code true} if the string is null or is the empty string
074       */
075      public static boolean isNullOrEmpty(@Nullable String string) {
076        return string == null || string.length() == 0; // string.isEmpty() in Java 6
077      }
078    
079      /**
080       * Returns a string, of length at least {@code minLength}, consisting of
081       * {@code string} prepended with as many copies of {@code padChar} as are
082       * necessary to reach that length. For example,
083       *
084       * <ul>
085       * <li>{@code padStart("7", 3, '0')} returns {@code "007"}
086       * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"}
087       * </ul>
088       *
089       * <p>See {@link Formatter} for a richer set of formatting capabilities.
090       *
091       * @param string the string which should appear at the end of the result
092       * @param minLength the minimum length the resulting string must have. Can be
093       *     zero or negative, in which case the input string is always returned.
094       * @param padChar the character to insert at the beginning of the result until
095       *     the minimum length is reached
096       * @return the padded string
097       */
098      public static String padStart(String string, int minLength, char padChar) {
099        checkNotNull(string);  // eager for GWT.
100        if (string.length() >= minLength) {
101          return string;
102        }
103        StringBuilder sb = new StringBuilder(minLength);
104        for (int i = string.length(); i < minLength; i++) {
105          sb.append(padChar);
106        }
107        sb.append(string);
108        return sb.toString();
109      }
110    
111      /**
112       * Returns a string, of length at least {@code minLength}, consisting of
113       * {@code string} appended with as many copies of {@code padChar} as are
114       * necessary to reach that length. For example,
115       *
116       * <ul>
117       * <li>{@code padEnd("4.", 5, '0')} returns {@code "4.000"}
118       * <li>{@code padEnd("2010", 3, '!')} returns {@code "2010"}
119       * </ul>
120       *
121       * <p>See {@link Formatter} for a richer set of formatting capabilities.
122       *
123       * @param string the string which should appear at the beginning of the result
124       * @param minLength the minimum length the resulting string must have. Can be
125       *     zero or negative, in which case the input string is always returned.
126       * @param padChar the character to append to the end of the result until the
127       *     minimum length is reached
128       * @return the padded string
129       */
130      public static String padEnd(String string, int minLength, char padChar) {
131        checkNotNull(string);  // eager for GWT.
132        if (string.length() >= minLength) {
133          return string;
134        }
135        StringBuilder sb = new StringBuilder(minLength);
136        sb.append(string);
137        for (int i = string.length(); i < minLength; i++) {
138          sb.append(padChar);
139        }
140        return sb.toString();
141      }
142    
143      /**
144       * Returns a string consisting of a specific number of concatenated copies of
145       * an input string. For example, {@code repeat("hey", 3)} returns the string
146       * {@code "heyheyhey"}.
147       *
148       * @param string any non-null string
149       * @param count the number of times to repeat it; a nonnegative integer
150       * @return a string containing {@code string} repeated {@code count} times
151       *     (the empty string if {@code count} is zero)
152       * @throws IllegalArgumentException if {@code count} is negative
153       */
154      public static String repeat(String string, int count) {
155        checkNotNull(string);  // eager for GWT.
156    
157        if (count <= 1) {
158          checkArgument(count >= 0, "invalid count: %s", count);
159          return (count == 0) ? "" : string;
160        }
161    
162        // IF YOU MODIFY THE CODE HERE, you must update StringsRepeatBenchmark
163        final int len = string.length();
164        final long longSize = (long) len * (long) count;
165        final int size = (int) longSize;
166        if (size != longSize) {
167          throw new ArrayIndexOutOfBoundsException("Required array size too large: "
168              + String.valueOf(longSize));
169        }
170    
171        final char[] array = new char[size];
172        string.getChars(0, len, array, 0);
173        int n;
174        for (n = len; n < size - n; n <<= 1) {
175          System.arraycopy(array, 0, array, n, n);
176        }
177        System.arraycopy(array, 0, array, n, size - n);
178        return new String(array);
179      }
180    
181      /**
182       * Returns the longest string {@code prefix} such that
183       * {@code a.toString().startsWith(prefix) && b.toString().startsWith(prefix)},
184       * taking care not to split surrogate pairs. If {@code a} and {@code b} have
185       * no common prefix, returns the empty string.
186       *
187       * @since 11.0
188       */
189      @Beta
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      @Beta
214      public static String commonSuffix(CharSequence a, CharSequence b) {
215        checkNotNull(a);
216        checkNotNull(b);
217    
218        int maxSuffixLength = Math.min(a.length(), b.length());
219        int s = 0;
220        while (s < maxSuffixLength
221            && a.charAt(a.length() - s - 1) == b.charAt(b.length() - s - 1)) {
222          s++;
223        }
224        if (validSurrogatePairAt(a, a.length() - s - 1)
225            || validSurrogatePairAt(b, b.length() - s - 1)) {
226          s--;
227        }
228        return a.subSequence(a.length() - s, a.length()).toString();
229      }
230    
231      /**
232       * True when a valid surrogate pair starts at the given {@code index} in the
233       * given {@code string}. Out-of-range indexes return false.
234       */
235      @VisibleForTesting
236      static boolean validSurrogatePairAt(CharSequence string, int index) {
237        return index >= 0 && index <= (string.length() - 2)
238            && Character.isHighSurrogate(string.charAt(index))
239            && Character.isLowSurrogate(string.charAt(index + 1));
240      }
241    }