From 323eaed44d81fec0767441913c656da531f50f65 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Mon, 2 Dec 2024 20:31:13 +0000 Subject: [PATCH] GH-2833: Handle x flag in java regex --- .../apache/jena/ext/xerces_regex/REUtil.java | 2 +- .../org/apache/jena/sparql/expr/E_Regex.java | 6 ++--- .../apache/jena/sparql/expr/RegexEngine.java | 24 +++++++------------ .../ext/xerces/impl/xpath/regex/REUtil.java | 8 +++---- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/ext/xerces_regex/REUtil.java b/jena-arq/src/main/java/org/apache/jena/ext/xerces_regex/REUtil.java index 6ed482a0b4e..9a859959e2d 100644 --- a/jena-arq/src/main/java/org/apache/jena/ext/xerces_regex/REUtil.java +++ b/jena-arq/src/main/java/org/apache/jena/ext/xerces_regex/REUtil.java @@ -338,7 +338,7 @@ public static String quoteMeta(String literal) { StringBuilder buffer = null; for (int i = 0; i < len; i ++) { int ch = literal.charAt(i); - if (".*+?{[()|\\^$".indexOf(ch) >= 0) { + if (".*+?{}[]()|\\^$".indexOf(ch) >= 0) { if (buffer == null) { buffer = new StringBuilder(i+(len-i)*2); if (i > 0) buffer.append(literal.substring(0, i)); diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/expr/E_Regex.java b/jena-arq/src/main/java/org/apache/jena/sparql/expr/E_Regex.java index 62ae033d0f5..94f14e752fe 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/expr/E_Regex.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/expr/E_Regex.java @@ -91,9 +91,9 @@ public static RegexEngine makeRegexEngine(NodeValue vPattern, NodeValue vFlags) throw new ExprException("REGEX: Pattern is not a string: " + vPattern); if ( vFlags != null && !vFlags.isString() ) throw new ExprException("REGEX: Pattern flags are not a string: " + vFlags); - String s = (vFlags == null) ? null : vFlags.getString(); - checkFlags(s); - return makeRegexEngine(vPattern.getString(), s); + String flags = (vFlags == null) ? null : vFlags.getString(); + checkFlags(flags); + return makeRegexEngine(vPattern.getString(), flags); } private static void checkFlags(String flags) { diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/expr/RegexEngine.java b/jena-arq/src/main/java/org/apache/jena/sparql/expr/RegexEngine.java index 091bb6e3d0b..214a4425cf4 100644 --- a/jena-arq/src/main/java/org/apache/jena/sparql/expr/RegexEngine.java +++ b/jena-arq/src/main/java/org/apache/jena/sparql/expr/RegexEngine.java @@ -94,26 +94,19 @@ public static Pattern makePattern(String label, String patternStr, String flags) public static int makeMask(String modifiers) { if ( modifiers == null ) return 0; - int newMask = 0; for ( int i = 0 ; i < modifiers.length() ; i++ ) { switch (modifiers.charAt(i)) { - case 'i' : + case 'i' -> { newMask |= Pattern.UNICODE_CASE; newMask |= Pattern.CASE_INSENSITIVE; - break; - case 'm' : - newMask |= Pattern.MULTILINE; - break; - case 's' : - newMask |= Pattern.DOTALL; - break; - // Not supported by Java regex - // case 'x': newMask |= Pattern.; break; - case 'q' :; - break; - - default : + } + case 'm' -> newMask |= Pattern.MULTILINE; + case 's' -> newMask |= Pattern.DOTALL; + case 'x' -> newMask |= Pattern.COMMENTS; + // Handled separately. + case 'q' -> {} + default -> throw new ExprEvalException("Unsupported flag in regex modifiers: " + modifiers.charAt(i)); } } @@ -132,7 +125,6 @@ public boolean match(String s) { Matcher m = regexPattern.matcher(s); return m.find(); } - } public static class RegexXerces extends RegexEngine { diff --git a/jena-core/src/main/java/org/apache/jena/ext/xerces/impl/xpath/regex/REUtil.java b/jena-core/src/main/java/org/apache/jena/ext/xerces/impl/xpath/regex/REUtil.java index a1cffa1f1cb..04fa5403216 100644 --- a/jena-core/src/main/java/org/apache/jena/ext/xerces/impl/xpath/regex/REUtil.java +++ b/jena-core/src/main/java/org/apache/jena/ext/xerces/impl/xpath/regex/REUtil.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,7 +21,7 @@ /** * {@literal @xerces.internal} - * + * * @version $Id: REUtil.java 828015 2009-10-21 13:56:13Z knoaman $ */ public final class REUtil { @@ -338,7 +338,7 @@ public static String quoteMeta(String literal) { StringBuilder buffer = null; for (int i = 0; i < len; i ++) { int ch = literal.charAt(i); - if (".*+?{[()|\\^$".indexOf(ch) >= 0) { + if (".*+?{}[]()|\\^$".indexOf(ch) >= 0) { if (buffer == null) { buffer = new StringBuilder(i+(len-i)*2); if (i > 0) buffer.append(literal.substring(0, i));