Skip to content

Commit 0194812

Browse files
committed
[GR-19220] Implement IO#set_encoding_by_bom (#2372)
PullRequest: truffleruby/2704
2 parents 83b73dc + fea3cca commit 0194812

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Compatibility:
3232
* Fix `ObjectSpace._id2ref` for Symbols and frozen String literals (#2358).
3333
* Implemented `Enumerator::Lazy#filter_map` (#2356).
3434
* Fix LLVM toolchain issue on macOS 10.13 (#2352, [oracle/graal#3383](https://github.com/oracle/graal/issues/3383)).
35+
* Implement `IO#set_encoding_by_bom` (#2372, pawandubey).
3536
* Implemented `Enumerator::Lazy#with_index` (#2356).
3637
* Implement `rb_backref_set`.
3738

spec/ruby/core/io/set_encoding_by_bom_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,11 @@
6767

6868
-> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding is set to UTF-8 already')
6969
end
70+
71+
it 'returns exception if encoding conversion is already set' do
72+
@io.set_encoding(Encoding::UTF_8, Encoding::UTF_16BE)
73+
74+
-> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding conversion is set')
75+
end
7076
end
7177
end

spec/tags/core/io/set_encoding_by_bom_tags.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/tags/truffle/methods_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ fails:Public methods on TracePoint should include parameters
5757
fails:Public methods on TracePoint should include raised_exception
5858
fails:Public methods on TracePoint should include return_value
5959
fails:Public methods on ENV.singleton_class should include freeze
60-
fails:Public methods on IO should include set_encoding_by_bom
6160
fails:Public methods on BasicSocket should include read_nonblock
6261
fails:Public methods on BasicSocket should include write_nonblock
6362
fails:Public methods on Socket should not include local_address

src/main/ruby/truffleruby/core/io.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,25 @@ def set_encoding(external, internal=nil, options=undefined)
21582158
self
21592159
end
21602160

2161+
def set_encoding_by_bom
2162+
unless binmode?
2163+
raise ArgumentError, 'ASCII incompatible encoding needs binmode'
2164+
end
2165+
2166+
if internal_encoding
2167+
raise ArgumentError, 'encoding conversion is set'
2168+
end
2169+
2170+
if external_encoding && external_encoding != Encoding::ASCII_8BIT
2171+
raise ArgumentError, "encoding is set to #{external_encoding} already"
2172+
end
2173+
2174+
external = strip_bom
2175+
if external
2176+
@external = Encoding.find(external)
2177+
end
2178+
end
2179+
21612180
private def strip_bom
21622181
mode = Truffle::POSIX.truffleposix_fstat_mode(Primitive.io_fd(self))
21632182
return unless Truffle::StatOperations.file?(mode)

test/mri/excludes/TestIO_M17N.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,3 @@
147147
exclude :test_bom_non_reading, "needs investigation"
148148
exclude :test_stdin, "needs investigation"
149149
exclude :"test_strip_bom:UTF-16LE", "needs investigation"
150-
exclude :test_strip_bom_no_conv, "needs investigation"
151-
exclude :"test_strip_bom:UTF-16BE", "needs investigation"
152-
exclude :"test_strip_bom:UTF-32BE", "needs investigation"
153-
exclude :"test_strip_bom:UTF-32LE", "needs investigation"
154-
exclude :"test_strip_bom:UTF-8", "needs investigation"
155-
exclude :test_strip_bom_no_bom, "needs investigation"

0 commit comments

Comments
 (0)