Skip to content

[GR-64899] Fix binary Regexp compilation to TRegex #3859

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

Merged
merged 3 commits into from
May 9, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Compatibility:

Performance:

* Use TRegex for binary Regexps with non-US-ASCII characters in the pattern like `/[\x80-\xff]/n` (#3858, @eregon).

Changes:

Expand Down
4 changes: 4 additions & 0 deletions spec/ruby/core/regexp/linear_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
Regexp.linear_time?('a').should == true
end

it "returns true if matching can be done in linear time for a binary Regexp" do
Regexp.linear_time?(/[\x80-\xff]/n).should == true
end

it "return false if matching can't be done in linear time" do
Regexp.linear_time?(/(a)\1/).should == false
Regexp.linear_time?("(a)\\1").should == false
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/truffleruby/core/regexp/TRegexCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
package org.truffleruby.core.regexp;

import java.nio.charset.UnsupportedCharsetException;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.InteropLibrary;
Expand Down Expand Up @@ -139,6 +137,11 @@ public static String toTRegexEncoding(RubyEncoding encoding) {

@TruffleBoundary
private static Object compileTRegex(RubyContext context, RubyRegexp regexp, boolean atStart, RubyEncoding enc) {
String tRegexEncoding = TRegexCache.toTRegexEncoding(enc);
if (tRegexEncoding == null) {
return null;
}

String processedRegexpSource;
RubyEncoding[] fixedEnc = new RubyEncoding[]{ null };
final TStringBuilder tstringBuilder;
Expand All @@ -155,21 +158,16 @@ private static Object compileTRegex(RubyContext context, RubyRegexp regexp, bool
var tstring = tstringBuilder.toTString();
try {
processedRegexpSource = TStringUtils.toJavaStringOrThrow(tstring, tstringBuilder.getRubyEncoding());
} catch (CannotConvertBinaryRubyStringToJavaString | UnsupportedCharsetException e) {
// Some strings cannot be converted to Java strings, e.g. strings with the
// BINARY encoding containing characters higher than 127.
// Also, some charsets might not be supported on the JVM and therefore
// a conversion to j.l.String might be impossible.
return null;
} catch (CannotConvertBinaryRubyStringToJavaString e) {
// A BINARY regexp with non-US-ASCII bytes, pass it as "raw bytes" instead.
// TRegex knows how to interpret those bytes correctly as we pass the encoding name as well.
var latin1string = tstring.forceEncodingUncached(Encodings.BINARY.tencoding,
Encodings.ISO_8859_1.tencoding);
processedRegexpSource = TStringUtils.toJavaStringOrThrow(latin1string, Encodings.ISO_8859_1);
}

String flags = optionsToFlags(regexp.options, atStart);

String tRegexEncoding = TRegexCache.toTRegexEncoding(enc);
if (tRegexEncoding == null) {
return null;
}

String ignoreAtomicGroups = context.getOptions().TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS
? ",IgnoreAtomicGroups=true"
: "";
Expand Down
Loading