mraible commented on code in PR #357:
URL: https://github.com/apache/roller/pull/357#discussion_r4637222710


##########
app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java:
##########
@@ -440,9 +479,93 @@ public static void populateSpamRules(
             } else {
                 stringRules.add(token);
             }
-        }        
+        }
     }
-        
+
+    /**
+     * The literal text, case-insensitive, with a word boundary on each end
+     * that is a word character; {@code \b} next to "(" or "/" would need a
+     * word character beside it and so would rarely match.
+     */
+    private static Pattern wordPattern(String word) {
+        StringBuilder regex = new StringBuilder();
+        if (isWordChar(word.charAt(0))) {
+            regex.append("\\b");
+        }
+        regex.append(Pattern.quote(word));
+        if (isWordChar(word.charAt(word.length() - 1))) {
+            regex.append("\\b");
+        }
+        return Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE);
+    }
+
+    private static boolean isWordChar(char c) {
+        return c == '_' || (c < 128 && Character.isLetterOrDigit(c));

Review Comment:
   Plain-word rules now compile with `CASE_INSENSITIVE | UNICODE_CASE`, and the 
`\b` edges are replaced with `(?<![\p{L}\p{M}\p{N}_])` / 
`(?![\p{L}\p{M}\p{N}_])` lookarounds, added only on an edge whose first or last 
code point is a word character by that same definition, so `(foo)` and 
`example.com/` still match. The pattern is still just a quoted literal plus 
fixed one-character lookarounds; the new 
`BannedwordslistTest.testWeblogRulesAsUnicodeWords` checks that `café` matches 
"CAFÉ" but not "caféine" (and the same for a leading `élan`).



-- 
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