Skip to content

Commit

Permalink
GH-2833: Handle x flag in java regex
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Dec 2, 2024
1 parent e016cd8 commit 323eaed
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -132,7 +125,6 @@ public boolean match(String s) {
Matcher m = regexPattern.matcher(s);
return m.find();
}

}

public static class RegexXerces extends RegexEngine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +21,7 @@

/**
* {@literal @xerces.internal}
*
*
* @version $Id: REUtil.java 828015 2009-10-21 13:56:13Z knoaman $
*/
public final class REUtil {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 323eaed

Please sign in to comment.