@@ -53,23 +53,25 @@ public String findMimeType(TruffleFile file) throws IOException {
53
53
}
54
54
}
55
55
56
- try (BufferedReader fileContent = file .newBufferedReader (StandardCharsets .UTF_8 )) {
56
+ try (BufferedReader fileContent = file .newBufferedReader (StandardCharsets .ISO_8859_1 )) {
57
57
final String firstLine = fileContent .readLine ();
58
58
if (firstLine != null && SHEBANG_REGEXP .matcher (firstLine ).matches ()) {
59
59
return RubyLanguage .getMimeType (false );
60
60
}
61
61
} catch (IOException | SecurityException e ) {
62
- // Reading random files as UTF-8 could cause all sorts of errors
62
+ // Reading random files could cause all sorts of errors
63
63
}
64
64
return null ;
65
65
}
66
66
67
67
@ Override
68
68
public Charset findEncoding (TruffleFile file ) {
69
- try (BufferedReader fileContent = file .newBufferedReader (StandardCharsets .UTF_8 )) {
69
+ // We use ISO-8859-1 because every byte is valid in that encoding and
70
+ // we only care about US-ASCII characters for magic encoding comments.
71
+ try (BufferedReader fileContent = file .newBufferedReader (StandardCharsets .ISO_8859_1 )) {
70
72
return findEncoding (fileContent ).getCharset ();
71
73
} catch (IOException | SecurityException e ) {
72
- // Reading random files as UTF-8 could cause all sorts of errors
74
+ // Reading random files could cause all sorts of errors
73
75
return Encodings .UTF_8 .jcoding .getCharset ();
74
76
}
75
77
}
@@ -86,8 +88,8 @@ public static Encoding findEncoding(BufferedReader reader) {
86
88
}
87
89
88
90
if (encodingCommentLine != null ) {
89
- var encodingComment = new TStringWithEncoding (TStringUtils . utf8TString ( encodingCommentLine ),
90
- Encodings .UTF_8 );
91
+ var encodingComment = new TStringWithEncoding (
92
+ TStringUtils . fromJavaString ( encodingCommentLine , Encodings .BINARY ), Encodings . BINARY );
91
93
Encoding [] encodingHolder = new Encoding [1 ];
92
94
RubyLexer .parseMagicComment (encodingComment , (name , value ) -> {
93
95
if (RubyLexer .isMagicEncodingComment (name )) {
0 commit comments