This is the actual implementation of #precomputed, but we bounce calls through a method on Platform so that we can have different behavior in GWT. This implementation tries to be smart in a number of ways. It recognizes cases where the negation is cheaper to precompute than the m
()
| 561 | */ |
| 562 | |
| 563 | @GwtIncompatible // java.util.BitSet |
| 564 | CharMatcher precomputedInternal() { |
| 565 | final BitSet table = new BitSet(); |
| 566 | setBits(table); |
| 567 | int totalCharacters = table.cardinality(); |
| 568 | if (totalCharacters * 2 <= DISTINCT_CHARS) { |
| 569 | return precomputedPositive(totalCharacters, table, toString()); |
| 570 | } |
| 571 | else { |
| 572 | // TODO(lowasser): is it worth it to worry about the last character of large matchers? |
| 573 | table.flip(Character.MIN_VALUE, Character.MAX_VALUE + 1); |
| 574 | int negatedCharacters = DISTINCT_CHARS - totalCharacters; |
| 575 | String suffix = ".negate()"; |
| 576 | final String description = toString(); |
| 577 | String negatedDescription = |
| 578 | description.endsWith(suffix) |
| 579 | ? description.substring(0, description.length() - suffix.length()) |
| 580 | : description + suffix; |
| 581 | return new NegatedFastMatcher(precomputedPositive(negatedCharacters, table, negatedDescription)) { |
| 582 | @Override |
| 583 | public String toString() { |
| 584 | return description; |
| 585 | } |
| 586 | }; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Helper method for {@link #precomputedInternal} that doesn't test if the negation is cheaper. |
nothing calls this directly
no test coverage detected