@Override public boolean evaluate(CharSequence subject) { return StringUtil.isBlank(subject); } };
@Override public Iterator<Character> iterator() { return StringUtil.iterator(subject); } };
/** * @return A string consisting of <var>n</var> times the character sequence <var>chars</var> */ public static String repeat(int n, char[] chars) { if (n == 0) return ""; int len = chars.length; if (len == 0) return ""; if (len == 1) return StringUtil.repeat(n, chars[0]); char[] ca = new char[n * len]; for (int i = 0, o = 0; i < n; i++) { for (int j = 0; j < len; j++) ca[o++] = chars[j]; } return String.copyValueOf(ca); }
/** * Runtime-optimized reimplementation of {@link String#indexOf(String)} and {@link String#lastIndexOf(String)}. * <p> * This method returns an implementation that performs at least as well as the {@link String} methods by * analyzing the <var>needle</var> (the string to search for). * </p> * * @param needle The string to search for */ public static IndexOf indexOf(final CharSequence needle) { if (needle.length() < 16) { return StringUtil.naiveIndexOf(needle); } else { return StringUtil.boyerMooreHorspoolIndexOf(needle); } }
/** * Creates a highly optimized {@link IndexOf} object that searches for "multivalent" <var>needle</var>. * Multivalent means that a character in the haystack equals <em>any of</em> {@code needle[offset]}. * <p> * E.g. a case-insensitive search for {@code "abc"} in the {@code haystack} string could be implemented like * this: * </p> * <pre> * int idx = StringUtil.indexOf(new char[][] { { 'a', 'A' }, { 'b', 'B' }, { 'c', 'C' } }).indexOf(haystack); * </pre> */ public static IndexOf indexOf(char[][] needle) { int len = needle.length; UNIVALENT_NEEDLE: { char[] univalentNeedle = new char[len]; for (int i = 0; i < len; i++) { if (needle[i].length != 1) break UNIVALENT_NEEDLE; univalentNeedle[i] = needle[i][0]; } return StringUtil.indexOf(univalentNeedle); } return ( len < 3 ? StringUtil.naiveIndexOf(needle) : StringUtil.boyerMooreHorspoolIndexOf(needle) ); }
for (int i = needle.length - 1; i >= 0; i--) { char[] n = StringUtil.removeDuplicates(needle[i]); if (univalent) return StringUtil.boyerMooreHorspoolIndexOf(StringUtil.mirror(needle));
/** * The often-needed "equal" method that handles {@code null} references. * <p> * Redeemed by {@code java.util.Objects.equals(Object, Object)} which is available since Java 1.7. * </p> * * @return Both arguments are {@code null}, or are {@link CharSequence}s with equal contents, or are equal */ public static <T> boolean equals(@Nullable T o1, @Nullable T o2) { if (o1 == null) return o2 == null; if (o2 == null) return false; if (o1 instanceof CharSequence && o2 instanceof CharSequence) { return StringUtil.equals((CharSequence) o1, (CharSequence) o2); } return o1.equals(o2); }
thrownText = StringUtil.lessTrailingLineSeparators(thrownText);
/** * Checks whether the next element equals (ignoring case) the <var>expected</var> string, but does not consume it. * * @see StringUtil#equalsIgnoreCase(String, String) */ public boolean peekIgnoreCase(String expected) { return StringUtil.equalsIgnoreCase(expected, this.delegate.peek()); }
/** * Runtime-optimized reimplementation of {@link String#indexOf(String)} and {@link String#lastIndexOf(String)}. * <p> * This method returns an implementation that performs at least as well as the {@link String} methods by * analyzing the <var>needle</var> (the string to search for). * </p> * * @param needle The string to search for */ public static IndexOf indexOf(char[] needle) { return StringUtil.indexOf(CharSequences.from(needle)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static boolean compare( @Nullable Object lhsv, Predicate<? super Integer> condition, @Nullable Object rhsv ) throws EvaluationException { if (lhsv == null || rhsv == null) return false; if (lhsv instanceof Comparable && rhsv instanceof Comparable) { return condition.evaluate(((Comparable) lhsv).compareTo(rhsv)); } if (lhsv instanceof CharSequence && rhsv instanceof CharSequence) { return condition.evaluate(StringUtil.compareTo((CharSequence) lhsv, (CharSequence) rhsv)); } throw new EvaluationException( "Operands '" + lhsv.getClass().getName() + "' and '" + rhsv.getClass().getName() + "' cannot be lexicographically compared" ); }
/** * <pre> * class-or-interface-type := identifier { '.' identifier } * </pre> */ private Class<?> parseClassOrInterfaceType() throws ParseException { final String identifier = this.read(IDENTIFIER); // Imported class. Class<?> clasS = this.loadImportedClass(identifier); if (clasS != null) return clasS; // Qualified class name. final List<String> identifiers = new ArrayList<String>(); identifiers.add(identifier); for (;;) { clasS = this.loadClass(StringUtil.join(identifiers, ".")); if (clasS != null) return clasS; this.read("."); identifiers.add(this.read(TokenType.IDENTIFIER)); } }
/** * The often-needed "equal" method that handles {@code null} references. * <p> * Redeemed by {@code java.util.Objects.equals(Object, Object)} which is available since Java 1.7. * </p> * * @return Both arguments are {@code null}, or are {@link CharSequence}s with equal contents, or are equal */ public static <T> boolean equals(@Nullable T o1, @Nullable T o2) { if (o1 == null) return o2 == null; if (o2 == null) return false; if (o1 instanceof CharSequence && o2 instanceof CharSequence) { return StringUtil.equals((CharSequence) o1, (CharSequence) o2); } return o1.equals(o2); }
thrownText = StringUtil.lessTrailingLineSeparators(thrownText);
/** * Verifies that the next element equals (ignoring case) the <var>expected</var> string, and consumes it. * * @throws NoSuchElementException This peekerator has no more elements * @throws NoSuchElementException The next element does equal the <var>expected</var> string * @see StringUtil#equalsIgnoreCase(String, String) */ public void nextEqualsIgnoreCase(String expected) { String actual = this.delegate.next(); if (!StringUtil.equalsIgnoreCase(actual, expected)) { throw new NoSuchElementException("Expected '" + expected + "' instead of '" + actual + "'"); } }
@Override public boolean evaluate(CharSequence subject) { return StringUtil.isBlank(subject); } };
@Override public Iterator<Character> iterator() { return StringUtil.iterator(subject); } };
/** * Verifies that the next element equals (ignoring case) any of the the <var>expected</var> string, and consumes it. * * @return The index of the first string that equals the next element * @throws NoSuchElementException This peekerator has no more elements * @throws NoSuchElementException The next element does equal any of the <var>expected</var> * strings * @see StringUtil#equalsIgnoreCase(String, String) */ public int nextEqualsIgnoreCase(String... expected) { String actual = this.delegate.next(); for (int i = 0; i < expected.length; i++) { if (StringUtil.equalsIgnoreCase(actual, expected[i])) return i; } throw new NoSuchElementException( "Expected one of " + Arrays.toString(expected) + " instead of \"" + actual + "\"" ); }
@Override public CharSequence transform(CharSequence in) { int delta = leftMarginWidth + measure - in.length(); if (delta <= 0) return in; return ( in.subSequence(0, leftMarginWidth) + StringUtil.repeat(delta, ' ') + in.subSequence(leftMarginWidth, in.length()) ); } };