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

Use an explicit "UTF-8" character set argument when creating Strings from bytes. The platform default character set is guaranteed to be UTF-8. #173

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
7 changes: 6 additions & 1 deletion java/com/google/re2j/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.google.re2j;

import com.google.re2j.MatcherInput.Encoding;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
Expand Down Expand Up @@ -363,7 +364,11 @@ private boolean genMatch(int startByte, int anchor) {
String substring(int start, int end) {
// UTF_8 is matched in binary mode. So slice the bytes.
if (matcherInput.getEncoding() == Encoding.UTF_8) {
return new String(matcherInput.asBytes(), start, end - start);
try {
return new String(matcherInput.asBytes(), start, end - start, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // Not possible.
}
}

// This is fast for both StringBuilder and String.
Expand Down
Loading