Skip to content

Commit 6277b08

Browse files
committed
Return null in RubyFileTypeDetector#findEncoding if no magic encoding comment
* Like before and to pass the unit tests.
1 parent 3a844cf commit 6277b08

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

src/main/java/org/truffleruby/RubyFileTypeDetector.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import java.util.Locale;
1717
import java.util.regex.Pattern;
1818

19-
import org.jcodings.Encoding;
2019
import org.truffleruby.core.encoding.Encodings;
20+
import org.truffleruby.core.encoding.RubyEncoding;
2121
import org.truffleruby.core.encoding.TStringUtils;
2222
import org.truffleruby.core.string.TStringWithEncoding;
2323
import org.truffleruby.parser.lexer.RubyLexer;
@@ -68,14 +68,17 @@ public Charset findEncoding(TruffleFile file) {
6868
// We use ISO-8859-1 because every byte is valid in that encoding and
6969
// we only care about US-ASCII characters for magic encoding comments.
7070
try (BufferedReader fileContent = file.newBufferedReader(StandardCharsets.ISO_8859_1)) {
71-
return findEncoding(fileContent).getCharset();
71+
var encoding = findEncoding(fileContent);
72+
if (encoding != null) {
73+
return encoding.jcoding.getCharset();
74+
}
7275
} catch (IOException | SecurityException e) {
7376
// Reading random files could cause all sorts of errors
74-
return StandardCharsets.UTF_8;
7577
}
78+
return null; // no magic encoding comment
7679
}
7780

78-
public static Encoding findEncoding(BufferedReader reader) {
81+
public static RubyEncoding findEncoding(BufferedReader reader) {
7982
try {
8083
final String firstLine = reader.readLine();
8184
if (firstLine != null) {
@@ -91,15 +94,14 @@ public static Encoding findEncoding(BufferedReader reader) {
9194
TStringUtils.fromJavaString(encodingCommentLine, Encodings.BINARY), Encodings.BINARY);
9295
var encoding = RubyLexer.parseMagicEncodingComment(encodingComment);
9396
if (encoding != null) {
94-
return encoding.jcoding;
97+
return encoding;
9598
}
9699
}
97100
}
98101
} catch (IOException e) {
99102
// Use the default encoding if reading failed somehow
100103
}
101104

102-
// The default encoding
103-
return Encodings.UTF_8.jcoding;
105+
return null;
104106
}
105107
}

src/main/java/org/truffleruby/parser/RubySource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ public RubySource(Source source, String sourcePath, TStringWithEncoding code, bo
6363
// The sourcePath might not exist, so we cannot reread from the filesystem.
6464
// So we look for the magic encoding comment and if not found use UTF-8.
6565
var sourceString = source.getCharacters().toString();
66-
var jcoding = RubyFileTypeDetector.findEncoding(new BufferedReader(new StringReader(sourceString)));
67-
var encoding = Encodings.getBuiltInEncoding(jcoding);
66+
var encoding = RubyFileTypeDetector.findEncoding(new BufferedReader(new StringReader(sourceString)));
67+
if (encoding == null) {
68+
encoding = Encodings.UTF_8;
69+
}
6870
code = new TStringWithEncoding(TStringUtils.fromJavaString(sourceString, encoding), encoding);
6971
}
7072
assert checkMagicEncoding(code);

src/test-internal/java/org/truffleruby/test/internal/RubyFileTypeDetectorTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,17 @@ public void testIndirect() throws IOException {
8484
@Test
8585
public void testEncoding() {
8686
final RubyFileTypeDetector fileTypeDetector = new RubyFileTypeDetector();
87-
testWithAST("", new Consumer<RubyRootNode>() {
88-
@Override
89-
public void accept(RubyRootNode rootNode) {
90-
TruffleLanguage.Env env = RubyLanguage.getCurrentContext().getEnv();
91-
try {
92-
for (TestCase testCase : getTestCases()) {
93-
if (testCase.hasRubyMimeType) {
94-
TruffleFile file = env.getPublicTruffleFile(testCase.path.toString());
95-
assertEquals(testCase.encoding, fileTypeDetector.findEncoding(file));
96-
}
87+
testWithAST("", rootNode -> {
88+
TruffleLanguage.Env env = RubyLanguage.getCurrentContext().getEnv();
89+
try {
90+
for (TestCase testCase : getTestCases()) {
91+
if (testCase.hasRubyMimeType) {
92+
TruffleFile file = env.getPublicTruffleFile(testCase.path.toString());
93+
assertEquals(testCase.encoding, fileTypeDetector.findEncoding(file));
9794
}
98-
} catch (IOException ioe) {
99-
throw new RuntimeException(ioe);
10095
}
96+
} catch (IOException ioe) {
97+
throw new RuntimeException(ioe);
10198
}
10299
});
103100
}

0 commit comments

Comments
 (0)