IBue commented on code in PR #1327:
URL: https://github.com/apache/commons-lang/pull/1327#discussion_r1873862840


##########
src/main/java/org/apache/commons/lang3/StringUtils.java:
##########
@@ -2888,17 +2876,20 @@ public static int indexOfAnyBut(final CharSequence seq, 
final CharSequence searc
         if (isEmpty(seq) || isEmpty(searchChars)) {
             return INDEX_NOT_FOUND;
         }
-        final int strLen = seq.length();
-        for (int i = 0; i < strLen; i++) {
-            final char ch = seq.charAt(i);
-            final boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 
0) >= 0;
-            if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
-                final char ch2 = seq.charAt(i + 1);
-                if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) 
< 0) {
-                    return i;
-                }
-            } else if (!chFound) {
-                return i;
+        final Set<Integer> seqSetCodePoints = 
seq.codePoints().boxed().collect(Collectors.toSet()); // JDK >=10: 
Collectors::toUnmodifiableSet
+        final Set<Integer> searchSetCodePoints = 
searchChars.codePoints().boxed()
+                .collect(Collectors.toSet()); // JDK >=10: 
Collectors::toUnmodifiableSet
+        final Set<Integer> complSetCodePoints = 
seqSetCodePoints.stream().filter(((Predicate<Integer>) 
searchSetCodePoints::contains).negate()) // JDK >=11: 
Predicate.not(searchSetCodePoints::contains)
+                .collect(Collectors.toSet()); // JDK >=10: 
Collectors::toUnmodifiableSet

Review Comment:
   > 
   > ```java
   > Set<Integer> searchCodePoints = 
searchChars.codePoints().boxed().collect(Collectors.toSet());
   > int idx = seq.codePoints()
   >         .takeWhile(c -> !searchCodePoints.contains(c))
   >         .reduce(0, (count, codePoint) -> count + 
Character.charCount(codePoint));
   > return idx < seq.length() ? idx : INDEX_NOT_FOUND;
   > ```
   
   This is a really clever stream-native solution (though 
`!searchCodePoints.contains…` is the `StringUtils::indexOfAny()` variant)
   that appears to have almost no memory footprint penalty and is even 2-3Ă— 
faster than this pull-request.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to