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. 456 * 457 * @since 10.0 458 */ 459 @Beta 460 public static final class MapSplitter { 461 private static final String INVALID_ENTRY_MESSAGE = "Chunk [%s] is not a valid entry"; 462 private final Splitter outerSplitter; 463 private final Splitter entrySplitter; 464 465 private MapSplitter(Splitter outerSplitter, Splitter entrySplitter) { 466 this.outerSplitter = outerSplitter; // only "this" is passed 467 this.entrySplitter = checkNotNull(entrySplitter); 468 } 469 470 /** 471 * Splits {@code sequence} into substrings, splits each substring into an entry, and returns an 472 * unmodifiable map with each of the entries. For example, 473 * {@code Splitter.on(';').trimResults().withKeyValueSeparator("=>").split("a=>b ; c=>b")} will 474 * return a mapping from {@code "a"} to {@code "b"} and {@code "c"} to {@code "b"}. 475 * 476 * <p>The returned map preserves the order of the entries from {@code sequence}. 477 * 478 * @throws IllegalArgumentException if the specified sequence does not split into valid map 479 * entries, or if there are duplicate keys 480 */ 481 public Map<String, String> split(CharSequence sequence) { 482 Map<String, String> map = new LinkedHashMap<String, String>(); 483 for (String entry : outerSplitter.split(sequence)) { 484 Iterator<String> entryFields = entrySplitter.splittingIterator(entry); 485 486 checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); 487 String key = entryFields.next(); 488 checkArgument(!map.containsKey(key), "Duplicate key [%s] found.", key); 489 490 checkArgument(entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); 491 String value = entryFields.next(); 492 map.put(key, value); 493 494 checkArgument(!entryFields.hasNext(), INVALID_ENTRY_MESSAGE, entry); 495 } 496 return Collections.unmodifiableMap(map); 497 } 498 } 499 500 private interface Strategy { 501 Iterator<String> iterator(Splitter splitter, CharSequence toSplit); 502 } 503 504 private abstract static class SplittingIterator extends AbstractIterator<String> { 505 final CharSequence toSplit; 506 final CharMatcher trimmer; 507 final boolean omitEmptyStrings; 508 509 /** 510 * Returns the first index in {@code toSplit} at or after {@code start} that contains the 511 * separator. 512 */ 513 abstract int separatorStart(int start); 514 515 /** 516 * Returns the first index in {@code toSplit} after {@code 517 * separatorPosition} that does not contain a separator. This method is only invoked after a 518 * call to {@code separatorStart}. 519 */ 520 abstract int separatorEnd(int separatorPosition); 521 522 int offset = 0; 523 int limit; 524 525 protected SplittingIterator(Splitter splitter, CharSequence toSplit) { 526 this.trimmer = splitter.trimmer; 527 this.omitEmptyStrings = splitter.omitEmptyStrings; 528 this.limit = splitter.limit; 529 this.toSplit = toSplit; 530 } 531 532 @Override 533 protected String computeNext() { 534 /* 535 * The returned string will be from the end of the last match to the beginning of the next 536 * one. nextStart is the start position of the returned substring, while offset is the place 537 * to start looking for a separator. 538 */ 539 int nextStart = offset; 540 while (offset != -1) { 541 int start = nextStart; 542 int end; 543 544 int separatorPosition = separatorStart(offset); 545 if (separatorPosition == -1) { 546 end = toSplit.length(); 547 offset = -1; 548 } else { 549 end = separatorPosition; 550 offset = separatorEnd(separatorPosition); 551 } 552 if (offset == nextStart) { 553 /* 554 * This occurs when some pattern has an empty match, even if it doesn't match the empty 555 * string -- for example, if it requires lookahead or the like. The offset must be 556 * increased to look for separators beyond this point, without changing the start position 557 * of the next returned substring -- so nextStart stays the same. 558 */ 559 offset++; 560 if (offset >= toSplit.length()) { 561 offset = -1; 562 } 563 continue; 564 } 565 566 while (start < end && trimmer.matches(toSplit.charAt(start))) { 567 start++; 568 } 569 while (end > start && trimmer.matches(toSplit.charAt(end - 1))) { 570 end--; 571 } 572 573 if (omitEmptyStrings && start == end) { 574 // Don't include the (unused) separator in next split string. 575 nextStart = offset; 576 continue; 577 } 578 579 if (limit == 1) { 580 // The limit has been reached, return the rest of the string as the 581 // final item. This is tested after empty string removal so that 582 // empty strings do not count towards the limit. 583 end = toSplit.length(); 584 offset = -1; 585 // Since we may have changed the end, we need to trim it again. 586 while (end > start && trimmer.matches(toSplit.charAt(end - 1))) { 587 end--; 588 } 589 } else { 590 limit--; 591 } 592 593 return toSplit.subSequence(start, end).toString(); 594 } 595 return endOfData(); 596 } 597 } 598}