This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-jsieve.git
commit 6f67cb07fbd087c09be171fb32b9cd6c096a0744 Author: Benoit TELLIER <[email protected]> AuthorDate: Wed Nov 18 22:25:42 2026 +0100 [FIX] ComparatorUtils: walk the value by code point in the glob matcher :matches copied each value into an int[] of code points through String#codePoints().toArray(), for each key: the whole body text once per key for body :matches, through a SpinedBuffer and a second copy when the value is not Latin-1. This was half of the CPU and 60% of the bytes allocated by evaluateBodyMatches. The matcher now reads the String with codePointAt, advancing by Character#charCount. ? still matches a character, a match never splits a surrogate pair, and an unpaired surrogate is still a character of its own. The steps, hence the budget charged, are the same. --- .../apache/jsieve/comparators/ComparatorUtils.java | 22 +++++++++++++++------- .../jsieve/comparator/ComparatorUtilsTest.java | 11 ++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java b/core/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java index ab7c91e..f7d6ae4 100644 --- a/core/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java +++ b/core/src/main/java/org/apache/jsieve/comparators/ComparatorUtils.java @@ -157,27 +157,34 @@ public class ComparatorUtils { context.chargeComparison(linearCost(string, glob)); } final int[] pattern = compileGlob(glob); - final int[] target = string.codePoints().toArray(); + // The string is walked by code point, without copying it: ? matches + // a character and a match never splits a surrogate pair + final int length = string.length(); + // Position, in chars, in the string int t = 0; int p = 0; // Position in the pattern right after the last star seen, -1 if none int afterStar = -1; - // Position in the target the last star is currently matched up to + // Position, in chars, in the string the last star is currently + // matched up to int starMatchEnd = 0; // Steps of failed attempts not charged yet. The steps that are never // undone (stars, and characters matched before the last star or in // the last attempt) are at most the lengths charged up front. long uncharged = 0; boolean mismatch = false; - while (t < target.length) { + while (t < length) { if (p < pattern.length && pattern[p] == GLOB_STAR) { afterStar = ++p; starMatchEnd = t; - } else if (p < pattern.length - && (pattern[p] == GLOB_ANY || pattern[p] == target[t])) { + continue; + } + final int c = string.codePointAt(t); + if (p < pattern.length + && (pattern[p] == GLOB_ANY || pattern[p] == c)) { p++; - t++; + t += Character.charCount(c); } else if (afterStar >= 0) { // Mismatch: let the last star swallow one more character. // Earlier stars never need revisiting. @@ -190,7 +197,8 @@ public class ComparatorUtils { } } p = afterStar; - t = ++starMatchEnd; + starMatchEnd += Character.charCount(string.codePointAt(starMatchEnd)); + t = starMatchEnd; } else { mismatch = true; break; diff --git a/core/src/test/java/org/apache/jsieve/comparator/ComparatorUtilsTest.java b/core/src/test/java/org/apache/jsieve/comparator/ComparatorUtilsTest.java index ec13305..dc5c553 100644 --- a/core/src/test/java/org/apache/jsieve/comparator/ComparatorUtilsTest.java +++ b/core/src/test/java/org/apache/jsieve/comparator/ComparatorUtilsTest.java @@ -182,6 +182,15 @@ public class ComparatorUtilsTest { return builder.toString(); } + @Test + public void questionMarkShouldMatchAnUnpairedSurrogate() throws Exception { + assertMatch("a\uD83Db", "a?b"); + assertMatch("a\uDE00b", "a?b"); + assertNoMatch("a\uD83D\uDE00b", "a??b"); + assertMatch("\uDE00\uD83D\uDE00", "*?"); + assertMatch("\uDE00\uD83D\uDE00", "??"); + } + @Test public void supplementaryCharacterShouldNotBeAWildcard() throws Exception { // U+1002A must not be truncated into '*' @@ -293,7 +302,7 @@ public class ComparatorUtilsTest { } private static final String[] ALPHABET = {"a", "b", "A", "*", "?", "\\", ".", "\n", "\r", - "ß", "😀"}; + "ß", "😀", "\uD83D", "\uDE00"}; private static String randomString(Random random, int maxLength) { int length = random.nextInt(maxLength + 1); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
