SOURCE guava/src/com/google/common/base/CharMatcher.java SHA 8868c096cfdabbe38170b6e395369c315cfb72a1
26: import java.util.Arrays;
27: import java.util.BitSet;
28: 
29: /**
30:  * Determines a true or false value for any Java {@code char} value, just as {@link Predicate} does
31:  * for any {@link Object}. Also offers basic text processing methods based on this function.
32:  * Implementations are strongly encouraged to be side-effect-free and immutable.
33:  *
34:  * <p>Throughout the documentation of this class, the phrase "matching character" is used to mean
35:  * "any {@code char} value {@code c} for which {@code this.matches(c)} returns {@code true}".
36:  *
37:  * <p><b>Warning:</b> This class deals only with {@code char} values, that is, <a
38:  * href="http://www.unicode.org/glossary/#BMP_character">BMP characters</a>. It does not understand
39:  * <a href="http://www.unicode.org/glossary/#supplementary_code_point">supplementary Unicode code
40:  * points</a> in the range {@code 0x10000} to {@code 0x10FFFF} which includes the majority of
41:  * assigned characters, including important CJK characters and emoji.
42:  *
43:  * <p>Supplementary characters are <a
44:  * href="https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#supplementary">encoded
45:  * into a {@code String} using surrogate pairs</a>, and a {@code CharMatcher} treats these just as
46:  * two separate characters. {@link #countIn} counts each supplementary character as 2 {@code char}s.
47:  *
48:  * <p>For up-to-date Unicode character properties (digit, letter, etc.) and support for
49:  * supplementary code points, use ICU4J UCharacter and UnicodeSet (freeze() after building). For
132:   /**
133:    * Determines whether a character is whitespace according to the latest Unicode standard, as
134:    * illustrated <a
135:    * href="http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5Cp%7Bwhitespace%7D">here</a>.
136:    * This is not the same definition used by other Java APIs. (See a <a
137:    * href="https://docs.google.com/spreadsheets/d/1kq4ECwPjHX9B8QUCTPclgsDCXYaj7T-FlT4tB5q3ahk/edit">comparison
138:    * of several definitions of "whitespace"</a>.)
139:    *
140:    * <p>All Unicode White_Space characters are on the BMP and thus supported by this API.
141:    *
142:    * <p><b>Note:</b> as the Unicode definition evolves, we will modify this matcher to keep it up to
143:    * date.
144:    *
145:    * @since 19.0 (since 1.0 as constant {@code WHITESPACE})
146:    */
147:   public static CharMatcher whitespace() {
148:     return Whitespace.INSTANCE;
149:   }
150: 
151:   /**
152:    * Determines whether a character is a breaking whitespace (that is, a whitespace which can be
153:    * interpreted as a break between words for formatting purposes). See {@link #whitespace()} for a
154:    * discussion of that term.
155:    *
620:     int pos = indexIn(string);
621:     if (pos == -1) {
622:       return string;
623:     }
624: 
625:     char[] chars = string.toCharArray();
626:     int spread = 1;
627: 
628:     // This unusual loop comes from extensive benchmarking
629:     OUT:
630:     while (true) {
631:       pos++;
632:       while (true) {
633:         if (pos == chars.length) {
634:           break OUT;
635:         }
636:         if (matches(chars[pos])) {
637:           break;
638:         }
639:         chars[pos - spread] = chars[pos];
640:         pos++;
641:       }
642:       spread++;
643:     }
644:     return new String(chars, 0, pos - spread);
645:   }
646: 
647:   /**
648:    * Returns a string containing all matching BMP characters of a character sequence, in order. For
649:    * example:
650:    *
1232:   /** Implementation of {@link #whitespace()}. */
1233:   @VisibleForTesting
1234:   static final class Whitespace extends NamedFastMatcher {
1235: 
1236:     // TABLE is a precomputed hashset of whitespace characters. MULTIPLIER serves as a hash function
1237:     // whose key property is that it maps 25 characters into the 32-slot table without collision.
1238:     // Basically this is an opportunistic fast implementation as opposed to "good code". For most
1239:     // other use-cases, the reduction in readability isn't worth it.
1240:     static final String TABLE =
1241:         "\u2002\u3000\r\u0085\u200A\u2005\u2000\u3000"
1242:             + "\u2029\u000B\u3000\u2008\u2003\u205F\u3000\u1680"
1243:             + "\u0009\u0020\u2006\u2001\u202F\u00A0\u000C\u2009"
1244:             + "\u3000\u2004\u3000\u3000\u2028\n\u2007\u3000";
1245:     static final int MULTIPLIER = 1682554634;
1246:     static final int SHIFT = Integer.numberOfLeadingZeros(TABLE.length() - 1);
1247: 
1248:     static final CharMatcher INSTANCE = new Whitespace();
1249: 
1250:     Whitespace() {
1251:       super("CharMatcher.whitespace()");
1252:     }
1253: 
1254:     @Override
1255:     public boolean matches(char c) {
1256:       return TABLE.charAt((MULTIPLIER * c) >>> SHIFT) == c;
1257:     }
1258: 
1259:     @GwtIncompatible // used only from other GwtIncompatible code
1260:     @Override
1261:     void setBits(BitSet table) {
1262:       for (int i = 0; i < TABLE.length(); i++) {
1263:         table.set(TABLE.charAt(i));
1264:       }
1265:     }
1266:   }
SOURCE guava/src/com/google/common/base/Strings.java SHA 8868c096cfdabbe38170b6e395369c315cfb72a1
30:  * Static utility methods pertaining to {@code String} or {@code CharSequence} instances.
31:  *
32:  * @author Kevin Bourrillion
33:  * @since 3.0
34:  */
35: @GwtCompatible
36: public final class Strings {
37:   private Strings() {}
38: 
39:   /**
40:    * Returns the given string if it is non-null; the empty string otherwise.
41:    *
42:    * @param string the string to test and possibly return
43:    * @return {@code string} itself if it is non-null; {@code ""} if it is null
44:    */
45:   public static String nullToEmpty(@Nullable String string) {
46:     return Platform.nullToEmpty(string);
47:   }
48: 
49:   /**
50:    * Returns the given string if it is nonempty; {@code null} otherwise.
51:    *
52:    * @param string the string to test and possibly return
53:    * @return {@code string} itself if it is nonempty; {@code null} if it is empty or null
54:    */
55:   public static @Nullable String emptyToNull(@Nullable String string) {
56:     return Platform.emptyToNull(string);
57:   }
58: 
59:   /**
60:    * Returns {@code true} if the given string is null or is the empty string.
61:    *
62:    * <p>Consider normalizing your string references with {@link #nullToEmpty}. If you do, you can
63:    * use {@link String#isEmpty()} instead of this method, and you won't need special null-safe forms
64:    * of methods like {@link String#toUpperCase} either. Or, if you'd like to normalize "in the other
65:    * direction," converting empty strings to {@code null}, you can use {@link #emptyToNull}.
66:    *
67:    * @param string a string reference to check
68:    * @return {@code true} if the string is null or is the empty string
69:    */
70:   public static boolean isNullOrEmpty(@Nullable String string) {
71:     return Platform.stringIsNullOrEmpty(string);
72:   }
SOURCE guava/src/com/google/common/base/Platform.java SHA 8868c096cfdabbe38170b6e395369c315cfb72a1
90:   static CommonPattern compilePattern(String pattern) {
91:     Preconditions.checkNotNull(pattern);
92:     return patternCompiler.compile(pattern);
93:   }
94: 
95:   static boolean patternCompilerIsPcreLike() {
96:     return patternCompiler.isPcreLike();
97:   }
98: 
99:   private static PatternCompiler loadPatternCompiler() {
100:     return new JdkPatternCompiler();
101:   }
102: 
103:   private static final class JdkPatternCompiler implements PatternCompiler {
104:     @Override
105:     public CommonPattern compile(String pattern) {
106:       return new JdkPattern(Pattern.compile(pattern));
107:     }
108: 
109:     @Override
110:     public boolean isPcreLike() {
111:       return true;
112:     }
113:   }
114: }
SOURCE src/main/java/org/apache/commons/lang3/StringUtils.java SHA 598dfc163b8b410fb3bb8794521206ec8dcec82a
3480:         for (int i = 0; i < sz; i++) {
3481:             if (!CharUtils.isAsciiPrintable(cs.charAt(i))) {
3482:                 return false;
3483:             }
3484:         }
3485:         return true;
3486:     }
3487: 
3488:     /**
3489:      * Tests if a CharSequence is empty ({@code "")}, null, or contains only whitespace as defined by {@link Character#isWhitespace(char)}.
3490:      *
3491:      * <pre>
3492:      * StringUtils.isBlank(null)      = true
3493:      * StringUtils.isBlank("")        = true
3494:      * StringUtils.isBlank(" ")       = true
3495:      * StringUtils.isBlank("bob")     = false
3496:      * StringUtils.isBlank("  bob  ") = false
3497:      * </pre>
3498:      *
3499:      * @param cs the CharSequence to check, may be null.
3500:      * @return {@code true} if the CharSequence is null, empty or whitespace only.
3501:      * @since 2.0
3502:      * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
3503:      */
3504:     public static boolean isBlank(final CharSequence cs) {
3505:         final int strLen = length(cs);
3506:         if (strLen == 0) {
3507:             return true;
3508:         }
3509:         for (int i = 0; i < strLen; i++) {
3510:             if (!Character.isWhitespace(cs.charAt(i))) {
3511:                 return false;
3512:             }
3513:         }
3514:         return true;
3515:     }
3516: 
3517:     /**
3518:      * Tests if a CharSequence is empty ("") or null.
3519:      *
3520:      * <pre>
3521:      * StringUtils.isEmpty(null)      = true
3522:      * StringUtils.isEmpty("")        = true
3523:      * StringUtils.isEmpty(" ")       = false
3524:      * StringUtils.isEmpty("bob")     = false
3525:      * StringUtils.isEmpty("  bob  ") = false
3526:      * </pre>
3527:      *
3528:      * <p>
3529:      * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
3530:      * </p>
3531:      *
3532:      * @param cs the CharSequence to check, may be null.
3533:      * @return {@code true} if the CharSequence is empty or null.
3534:      * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
3535:      */
3536:     public static boolean isEmpty(final CharSequence cs) {
3537:         return cs == null || cs.length() == 0;
3538:     }

FOLLOWUP: actual delegated Strings platform methods and matchesAllOf entry
guava/src/com/google/common/base/Platform.java
50:      */
51:     return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get()));
52:   }
53: 
54:   static String formatCompact4Digits(double value) {
55:     return String.format(Locale.ROOT, "%.4g", value);
56:   }
57: 
58:   static boolean stringIsNullOrEmpty(@Nullable String string) {
59:     return string == null || string.isEmpty();
60:   }
61: 
62:   /**
63:    * Returns the string if it is not null, or an empty string otherwise.
64:    *
65:    * @param string the string to test and possibly return
66:    * @return {@code string} if it is not null; {@code ""} otherwise
67:    */
68:   static String nullToEmpty(@Nullable String string) {
69:     return (string == null) ? "" : string;
70:   }
71: 
72:   /**
73:    * Returns the string if it is not empty, or a null string otherwise.
74:    *
75:    * @param string the string to test and possibly return
76:    * @return {@code string} if it is not empty; {@code null} otherwise
77:    */
78:   static @Nullable String emptyToNull(@Nullable String string) {
79:     return stringIsNullOrEmpty(string) ? null : string;
80:   }
guava/src/com/google/common/base/CharMatcher.java
493:    * @return {@code true} if this matcher matches at least one character in the sequence
494:    * @since 8.0
495:    */
496:   public boolean matchesAnyOf(CharSequence sequence) {
497:     return !matchesNoneOf(sequence);
498:   }
499: 
500:   /**
501:    * Returns {@code true} if a character sequence contains only matching BMP characters.
502:    *
503:    * <p>The default implementation iterates over the sequence, invoking {@link #matches} for each
504:    * character, until this returns {@code false} or the end is reached.
505:    *
506:    * @param sequence the character sequence to examine, possibly empty
507:    * @return {@code true} if this matcher matches every character in the sequence, including when
508:    *     the sequence is empty
509:    */
510:   public boolean matchesAllOf(CharSequence sequence) {
511:     for (int i = sequence.length() - 1; i >= 0; i--) {
512:       if (!matches(sequence.charAt(i))) {
513:         return false;
514:       }
515:     }
516:     return true;
517:   }
518: 
519:   /**
520:    * Returns {@code true} if a character sequence contains no matching BMP characters. Equivalent to
