Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-2833: Handle x flag in java regex #2876

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading