001/*
002 * Copyright (C) 2009 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.Beta;
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Iterator;
026import java.util.LinkedHashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.regex.Pattern;
030
031/**
032 * Extracts non-overlapping substrings from an input string, typically by recognizing appearances of
033 * a <i>separator</i> sequence. This separator can be specified as a single {@linkplain #on(char)
034 * character}, fixed {@linkplain #on(String) string}, {@linkplain #onPattern regular expression} or
035 * {@link #on(CharMatcher) CharMatcher} instance. Or, instead of using a separator at all, a
036 * splitter can extract adjacent substrings of a given {@linkplain #fixedLength fixed length}.
037 *
038 * <p>For example, this expression: <pre>   {@code
039 *
040 *   Splitter.on(',').split("foo,bar,qux")}</pre>
041 *
042 * ... produces an {@code Iterable} containing {@code "foo"}, {@code "bar"} and {@code "qux"}, in
043 * that order.
044 *
045 * <p>By default, {@code Splitter}'s behavior is simplistic and unassuming. The following
046 * expression: <pre>   {@code
047 *
048 *   Splitter.on(',').split(" foo,,,  bar ,")}</pre>
049 *
050 * ... yields the substrings {@code [" foo", "", "", "  bar ", ""]}. If this is not the desired
051 * behavior, use configuration methods to obtain a <i>new</i> splitter instance with modified
052 * behavior: <pre>   {@code
053 *
054 *   private static final Splitter MY_SPLITTER = Splitter.on(',')
055 *       .trimResults()
056 *       .omitEmptyStrings();}</pre>
057 *
058 * <p>Now {@code MY_SPLITTER.split("foo,,,  bar ,")} returns just {@code ["foo",
059 * "bar"]}. Note that the order in which these configuration methods are called is never
060 * significant.
061 *
062 * <p><b>Warning:</b> Splitter instances are immutable. Invoking a configuration method has no
063 * effect on the receiving instance; you must store and use the new splitter instance it returns
064 * instead. <pre>   {@code
065 *
066 *   // Do NOT do this
067 *   Splitter splitter = Splitter.on('/');
068 *   splitter.trimResults(); // does nothing!
069 *   return splitter.split("wrong / wrong / wrong");}</pre>
070 *
071 * <p>For separator-based splitters that do not use {@code omitEmptyStrings}, an input string
072 * containing {@code n} occurrences of the separator naturally yields an iterable of size
073 * {@code n + 1}. So if the separator does not occur anywhere in the input, a single substring is
074 * returned containing the entire input. Consequently, all splitters split the empty string to
075 * {@code [""]} (note: even fixed-length splitters).
076 *
077 * <p>Splitter instances are thread-safe immutable, and are therefore safe to store as
078 * {@code static final} constants.
079 *
080 * <p>The {@link Joiner} class provides the inverse operation to splitting, but note that a
081 * round-trip between the two should be assumed to be lossy.
082 *
083 * <p>See the Guava User Guide article on
084 * <a href="https://github.com/google/guava/wiki/StringsExplained#splitter">{@code Splitter}</a>.
085 *
086 * @author Julien Silland
087 * @author Jesse Wilson
088 * @author Kevin Bourrillion
089 * @author Louis Wasserman
090 * @since 1.0
091 */
092@GwtCompatible(emulated = true)
093public final class Splitter {
094  private final CharMatcher trimmer;
095  private final boolean omitEmptyStrings;
096  private final Strategy strategy;
097  private final int limit;
098
099  private Splitter(Strategy strategy) {
100    this(strategy, false, CharMatcher.none(), Integer.MAX_VALUE);
101  }
102
103  private Splitter(Strategy strategy, boolean omitEmptyStrings, CharMatcher trimmer, int limit) {
104    this.strategy = strategy;
105    this.omitEmptyStrings = omitEmptyStrings;
106    this.trimmer = trimmer;
107    this.limit = limit;
108  }
109
110  /**
111   * Returns a splitter that uses the given single-character separator. For example,
112   * {@code Splitter.on(',').split("foo,,bar")} returns an iterable containing
113   * {@code ["foo", "", "bar"]}.
114   *
115   * @param separator the character to recognize as a separator
116   * @return a splitter, with default settings, that recognizes that separator
117   */
118  public static Splitter on(char separator) {
119    return on(CharMatcher.is(separator));
120  }
121
122  /**
123   * Returns a splitter that considers any single character matched by the given {@code CharMatcher}
124   * to be a separator. For example, {@code
125   * Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux")} returns an iterable containing
126   * {@code ["foo", "", "bar", "quux"]}.
127   *
128   * @param separatorMatcher a {@link CharMatcher} that determines whether a character is a
129   *     separator
130   * @return a splitter, with default settings, that uses this matcher
131   */
132  public static Splitter on(final CharMatcher separatorMatcher) {
133    checkNotNull(separatorMatcher);
134
135    return new Splitter(
136        new Strategy() {
137          @Override
138          public SplittingIterator iterator(Splitter splitter, final CharSequence toSplit) {
139            return new SplittingIterator(splitter, toSplit) {
140              @Override
141              int separatorStart(int start) {
142                return separatorMatcher.indexIn(toSplit, start);
143              }
144
145              @Override
146              int separatorEnd(int separatorPosition) {
147                return separatorPosition + 1;
148              }
149            };
150          }
151        });
152  }
153
154  /**
155   * Returns a splitter that uses the given fixed string as a separator. For example,
156   * {@code Splitter.on(", ").split("foo, bar,baz")} returns an iterable containing
157   * {@code ["foo", "bar,baz"]}.
158   *
159   * @param separator the literal, nonempty string to recognize as a separator
160   * @return a splitter, with default settings, that recognizes that separator
161   */
162  public static Splitter on(final String separator) {
163    checkArgument(separator.length() != 0, "The separator may not be the empty string.");
164    if (separator.length() == 1) {
165      return Splitter.on(separator.charAt(0));
166    }
167    return new Splitter(
168        new Strategy() {
169          @Override
170          public SplittingIterator iterator(Splitter splitter, CharSequence toSplit) {
171            return new SplittingIterator(splitter, toSplit) {
172              @Override
173              public int separatorStart(int start) {
174                int separatorLength = separator.length();
175
176                positions:
177                for (int p = start, last = toSplit.length() - separatorLength; p <= last; p++) {
178                  for (int i = 0; i < separatorLength; i++) {
179                    if (toSplit.charAt(i + p) != separator.charAt(i)) {
180                      continue positions;
181                    }
182                  }
183                  return p;
184                }
185                return -1;
186              }
187
188              @Override
189              public int separatorEnd(int separatorPosition) {
190                return separatorPosition + separator.length();
191              }
192            };
193          }
194        });
195  }
196
197  /**
198   * Returns a splitter that considers any subsequence matching {@code pattern} to be a separator.
199   * For example, {@code Splitter.on(Pattern.compile("\r?\n")).split(entireFile)} splits a string
200   * into lines whether it uses DOS-style or UNIX-style line terminators.
201   *
202   * @param separatorPattern the pattern that determines whether a subsequence is a separator. This
203   *     pattern may not match the empty string.
204   * @return a splitter, with default settings, that uses this pattern
205   * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string
206   */
207  @GwtIncompatible // java.util.regex
208  public static Splitter on(Pattern separatorPattern) {
209    return on(new JdkPattern(separatorPattern));
210  }
211
212  private static Splitter on(final CommonPattern separatorPattern) {
213    checkArgument(
214        !separatorPattern.matcher("").matches(),
215        "The pattern may not match the empty string: %s",
216        separatorPattern);
217
218    return new Splitter(
219        new Strategy() {
220          @Override
221          public SplittingIterator iterator(final Splitter splitter, CharSequence toSplit) {
222            final CommonMatcher matcher = separatorPattern.matcher(toSplit);
223            return new SplittingIterator(splitter, toSplit) {
224              @Override
225              public int separatorStart(int start) {
226                return matcher.find(start) ? matcher.start() : -1;
227              }
228
229              @Override
230              public int separatorEnd(int separatorPosition) {
231                return matcher.end();
232              }
233            };
234          }
235        });
236  }
237
238  /**
239   * Returns a splitter that considers any subsequence matching a given pattern (regular expression)
240   * to be a separator. For example, {@code Splitter.onPattern("\r?\n").split(entireFile)} splits a
241   * string into lines whether it uses DOS-style or UNIX-style line terminators. This is equivalent
242   * to {@code Splitter.on(Pattern.compile(pattern))}.
243   *
244   * @param separatorPattern the pattern that determines whether a subsequence is a separator. This
245   *     pattern may not match the empty string.
246   * @return a splitter, with default settings, that uses this pattern
247   * @throws IllegalArgumentException if {@code separatorPattern} matches the empty string or is a
248   *     malformed expression
249   */
250  @GwtIncompatible // java.util.regex
251  public static Splitter onPattern(String separatorPattern) {
252    return on(Platform.compilePattern(separatorPattern));
253  }
254
255  /**
256   * Returns a splitter that divides strings into pieces of the given length. For example,
257   * {@code Splitter.fixedLength(2).split("abcde")} returns an iterable containing
258   * {@code ["ab", "cd", "e"]}. The last piece can be smaller than {@code length} but will never be
259   * empty.
260   *
261   * <p><b>Exception:</b> for consistency with separator-based splitters, {@code
262   * split("")} does not yield an empty iterable, but an iterable containing {@code ""}. This is the
263   * only case in which {@code
264   * Iterables.size(split(input))} does not equal {@code
265   * IntMath.divide(input.length(), length, CEILING)}. To avoid this behavior, use
266   * {@code omitEmptyStrings}.
267   *
268   * @param length the desired length of pieces after splitting, a positive integer
269   * @return a splitter, with default settings, that can split into fixed sized pieces
270   * @throws IllegalArgumentException if {@code length} is zero or negative
271   */
272  public static Splitter fixedLength(final int length) {
273    checkArgument(length > 0, "The length may not be less than 1");
274
275    return new Splitter(
276        new Strategy() {
277          @Override
278          public SplittingIterator iterator(final Splitter splitter, CharSequence toSplit) {
279            return new SplittingIterator(splitter, toSplit) {
280              @Override
281              public int separatorStart(int start) {
282                int nextChunkStart = start + length;
283                return (nextChunkStart < toSplit.length() ? nextChunkStart : -1);
284              }
285
286              @Override
287              public int separatorEnd(int separatorPosition) {
288                return separatorPosition;
289              }
290            };
291          }
292        });
293  }
294
295  /**
296   * Returns a splitter that behaves equivalently to {@code this} splitter, but automatically omits
297   * empty strings from the results. For example, {@code
298   * Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,")} returns an iterable containing only
299   * {@code ["a", "b", "c"]}.
300   *
301   * <p>If either {@code trimResults} option is also specified when creating a splitter, that
302   * splitter always trims results first before checking for emptiness. So, for example, {@code
303   * Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ")} returns an empty iterable.
304   *
305   * <p>Note that it is ordinarily not possible for {@link #split(CharSequence)} to return an empty
306   * iterable, but when using this option, it can (if the input sequence consists of nothing but
307   * separators).
308   *
309   * @return a splitter with the desired configuration
310   */
311  public Splitter omitEmptyStrings() {
312    return new Splitter(strategy, true, trimmer, limit);
313  }
314
315  /**
316   * Returns a splitter that behaves equivalently to {@code this} splitter but stops splitting after
317   * it reaches the limit. The limit defines the maximum number of items returned by the iterator,
318   * or the maximum size of the list returned by {@link #splitToList}.
319   *
320   * <p>For example, {@code Splitter.on(',').limit(3).split("a,b,c,d")} returns an iterable
321   * containing {@code ["a", "b", "c,d"]}. When omitting empty strings, the omitted strings do not
322   * count. Hence, {@code Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d")} returns
323   * an iterable containing {@code ["a", "b", "c,d"}. When trim is requested, all entries are
324   * trimmed, including the last. Hence
325   * {@code Splitter.on(',').limit(3).trimResults().split(" a , b , c , d ")} results in
326   * {@code ["a", "b", "c , d"]}.
327   *
328   * @param limit the maximum number of items returned
329   * @return a splitter with the desired configuration
330   * @since 9.0
331   */
332  public Splitter limit(int limit) {
333    checkArgument(limit > 0, "must be greater than zero: %s", limit);
334    return new Splitter(strategy, omitEmptyStrings, trimmer, limit);
335  }
336
337  /**
338   * Returns a splitter that behaves equivalently to {@code this} splitter, but automatically
339   * removes leading and trailing {@linkplain CharMatcher#whitespace whitespace} from each returned
340   * substring; equivalent to {@code trimResults(CharMatcher.whitespace())}. For example, {@code
341   * Splitter.on(',').trimResults().split(" a, b ,c ")} returns an iterable containing
342   * {@code ["a", "b", "c"]}.
343   *
344   * @return a splitter with the desired configuration
345   */
346  public Splitter trimResults() {
347    return trimResults(CharMatcher.whitespace());
348  }
349
350  /**
351   * Returns a splitter that behaves equivalently to {@code this} splitter, but removes all leading
352   * or trailing characters matching the given {@code
353   * CharMatcher} from each returned substring. For example, {@code
354   * Splitter.on(',').trimResults(CharMatcher.is('_')).split("_a ,_b_ ,c__")} returns an iterable
355   * containing {@code ["a ", "b_ ", "c"]}.
356   *
357   * @param trimmer a {@link CharMatcher} that determines whether a character should be removed from
358   *     the beginning/end of a subsequence
359   * @return a splitter with the desired configuration
360   */
361  // TODO(kevinb): throw if a trimmer was already specified!
362  public Splitter trimResults(CharMatcher trimmer) {
363    checkNotNull(trimmer);
364    return new Splitter(strategy, omitEmptyStrings, trimmer, limit);
365  }
366
367  /**
368   * Splits {@code sequence} into string components and makes them available through an
369   * {@link Iterator}, which may be lazily evaluated. If you want an eagerly computed {@link List},
370   * use {@link #splitToList(CharSequence)}.
371   *
372   * @param sequence the sequence of characters to split
373   * @return an iteration over the segments split from the parameter.
374   */
375  public Iterable<String> split(final CharSequence sequence) {
376    checkNotNull(sequence);
377
378    return new Iterable<String>() {
379      @Override
380      public Iterator<String> iterator() {
381        return splittingIterator(sequence);
382      }
383
384      @Override
385      public String toString() {
386        return Joiner.on(", ")
387            .appendTo(new StringBuilder().append('['), this)
388            .append(']')
389            .toString();
390      }
391    };
392  }
393
394  private Iterator<String> splittingIterator(CharSequence sequence) {
395    return strategy.iterator(this, sequence);
396  }
397
398  /**
399   * Splits {@code sequence} into string components and returns them as an immutable list. If you
400   * want an {@link Iterable} which may be lazily evaluated, use {@link #split(CharSequence)}.
401   *
402   * @param sequence the sequence of characters to split
403   * @return an immutable list of the segments split from the parameter
404   * @since 15.0
405   */
406  @Beta
407  public List<String> splitToList(CharSequence sequence) {
408    checkNotNull(sequence);
409
410    Iterator<String> iterator = splittingIterator(sequence);
411    List<String> result = new ArrayList<String>();
412
413    while (iterator.hasNext()) {
414      result.add(iterator.next());
415    }
416
417    return Collections.unmodifiableList(result);
418  }
419
420  /**
421   * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries
422   * into keys and values using the specified separator.
423   *
424   * @since 10.0
425   */
426  @Beta
427  public MapSplitter withKeyValueSeparator(String separator) {
428    return withKeyValueSeparator(on(separator));
429  }
430
431  /**
432   * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries
433   * into keys and values using the specified separator.
434   *
435   * @since 14.0
436   */
437  @Beta
438  public MapSplitter withKeyValueSeparator(char separator) {
439    return withKeyValueSeparator(on(separator));
440  }
441
442  /**
443   * Returns a {@code MapSplitter} which splits entries based on this splitter, and splits entries
444   * into keys and values using the specified key-value splitter.
445   *
446   * @since 10.0
447   */
448  @Beta
449  public MapSplitter withKeyValueSeparator(Splitter keyValueSplitter) {
450    return new MapSplitter(this, keyValueSplitter);
451  }
452
453  /**
454   * An object that splits strings into maps as {@code Splitter} splits iterables and lists. Like
455   * {@code Splitter}, it is thread-safe and immutable. The common way to build instances is by
456   * providing an additional {@linkplain Splitter#withKeyValueSeparator key-value separator} to
457   * {@link Splitter}.
458   *
459   * @since 10.0
460   */
461  @Beta
462  public static final class MapSplitter {
463    private static final String INVALID_ENTRY_MESSAGE = "Chunk [%s] is not a valid entry";
464    private final Splitter outerSplitter;
465    private final Splitter entrySplitter;
466
467    private MapSplitter(Splitter outerSplitter, Splitter entrySplitter) {
468      this.outerSplitter = outerSplitter; // only "this" is passed
469      this.entrySplitter = checkNotNull(entrySplitter);
470    }
471
472    /**
473     * Splits {@code sequence} into substrings, splits each substring into an entry, and returns an
474     * unmodifiable map with each of the entries. For example,
475     * {@code Splitter.on(';').trimResults().withKeyValueSeparator("=>").split("a=>b ; c=>b")} will
476     * return a mapping from {@code "a"} to {@code "b"} and {@code "c"} to {@code "b"}.
477     *
478     * <p>The returned map preserves the order of the entries from {@code sequence}.
479     *
480     * @throws IllegalArgumentException if the specified sequence does not split into valid map
481     *     entries, or if there are duplicate keys
482     */
483    public Map<String, String> split(CharSequence sequence) {
484      Map<String, String> map = new LinkedHashMap<String, String>();
485      for (String entry : outerSplitter.split(sequence)) {
486        Iterator<String> entryFields = entrySplitter.splittingIterator(entry);
487
488        checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
489        String key = entryFields.next();
490        checkArgument(!map.containsKey(key), "Duplicate key [%s] found.", key);
491
492        checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
493        String value = entryFields.next();
494        map.put(key, value);
495
496        checkArgument(!entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry);
497      }
498      return Collections.unmodifiableMap(map);
499    }
500  }
501
502  private interface Strategy {
503    Iterator<String> iterator(Splitter splitter, CharSequence toSplit);
504  }
505
506  private abstract static class SplittingIterator extends AbstractIterator<String> {
507    final CharSequence toSplit;
508    final CharMatcher trimmer;
509    final boolean omitEmptyStrings;
510
511    /**
512     * Returns the first index in {@code toSplit} at or after {@code start} that contains the
513     * separator.
514     */
515    abstract int separatorStart(int start);
516
517    /**
518     * Returns the first index in {@code toSplit} after {@code
519     * separatorPosition} that does not contain a separator. This method is only invoked after a
520     * call to {@code separatorStart}.
521     */
522    abstract int separatorEnd(int separatorPosition);
523
524    int offset = 0;
525    int limit;
526
527    protected SplittingIterator(Splitter splitter, CharSequence toSplit) {
528      this.trimmer = splitter.trimmer;
529      this.omitEmptyStrings = splitter.omitEmptyStrings;
530      this.limit = splitter.limit;
531      this.toSplit = toSplit;
532    }
533
534    @Override
535    protected String computeNext() {
536      /*
537       * The returned string will be from the end of the last match to the beginning of the next
538       * one. nextStart is the start position of the returned substring, while offset is the place
539       * to start looking for a separator.
540       */
541      int nextStart = offset;
542      while (offset != -1) {
543        int start = nextStart;
544        int end;
545
546        int separatorPosition = separatorStart(offset);
547        if (separatorPosition == -1) {
548          end = toSplit.length();
549          offset = -1;
550        } else {
551          end = separatorPosition;
552          offset = separatorEnd(separatorPosition);
553        }
554        if (offset == nextStart) {
555          /*
556           * This occurs when some pattern has an empty match, even if it doesn't match the empty
557           * string -- for example, if it requires lookahead or the like. The offset must be
558           * increased to look for separators beyond this point, without changing the start position
559           * of the next returned substring -- so nextStart stays the same.
560           */
561          offset++;
562          if (offset > toSplit.length()) {
563            offset = -1;
564          }
565          continue;
566        }
567
568        while (start < end && trimmer.matches(toSplit.charAt(start))) {
569          start++;
570        }
571        while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
572          end--;
573        }
574
575        if (omitEmptyStrings && start == end) {
576          // Don't include the (unused) separator in next split string.
577          nextStart = offset;
578          continue;
579        }
580
581        if (limit == 1) {
582          // The limit has been reached, return the rest of the string as the
583          // final item. This is tested after empty string removal so that
584          // empty strings do not count towards the limit.
585          end = toSplit.length();
586          offset = -1;
587          // Since we may have changed the end, we need to trim it again.
588          while (end > start && trimmer.matches(toSplit.charAt(end - 1))) {
589            end--;
590          }
591        } else {
592          limit--;
593        }
594
595        return toSplit.subSequence(start, end).toString();
596      }
597      return endOfData();
598    }
599  }
600}