diff --git a/CHANGELOG.md b/CHANGELOG.md index 79fd2b2bb591..ba96d37004dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Compatibility: * Fix `ObjectSpace._id2ref` for Symbols and frozen String literals (#2358). * Implemented `Enumerator::Lazy#filter_map` (#2356). * Fix LLVM toolchain issue on macOS 10.13 (#2352, [oracle/graal#3383](https://github.com/oracle/graal/issues/3383)). +* Implement `IO#set_encoding_by_bom` (#2372, pawandubey). * Implemented `Enumerator::Lazy#with_index` (#2356). * Implement `rb_backref_set`. diff --git a/spec/ruby/core/io/set_encoding_by_bom_spec.rb b/spec/ruby/core/io/set_encoding_by_bom_spec.rb index c551042beea3..7368ec7677b7 100644 --- a/spec/ruby/core/io/set_encoding_by_bom_spec.rb +++ b/spec/ruby/core/io/set_encoding_by_bom_spec.rb @@ -67,5 +67,11 @@ -> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding is set to UTF-8 already') end + + it 'returns exception if encoding conversion is already set' do + @io.set_encoding(Encoding::UTF_8, Encoding::UTF_16BE) + + -> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding conversion is set') + end end end diff --git a/spec/tags/core/io/set_encoding_by_bom_tags.txt b/spec/tags/core/io/set_encoding_by_bom_tags.txt deleted file mode 100644 index 301ccace30c7..000000000000 --- a/spec/tags/core/io/set_encoding_by_bom_tags.txt +++ /dev/null @@ -1,8 +0,0 @@ -fails:IO#set_encoding_by_bom returns the result encoding if found BOM UTF-8 sequence -fails:IO#set_encoding_by_bom returns the result encoding if found BOM UTF_16LE sequence -fails:IO#set_encoding_by_bom returns the result encoding if found BOM UTF_16BE sequence -fails:IO#set_encoding_by_bom returns nil if found BOM sequence not provided -fails:IO#set_encoding_by_bom returns exception if io not in binary mode -fails:IO#set_encoding_by_bom returns exception if encoding already set -fails:IO#set_encoding_by_bom returns the result encoding if found BOM UTF_32LE sequence -fails:IO#set_encoding_by_bom returns the result encoding if found BOM UTF_32BE sequence diff --git a/spec/tags/truffle/methods_tags.txt b/spec/tags/truffle/methods_tags.txt index 693b239b69f3..bd1b3f2e3bed 100644 --- a/spec/tags/truffle/methods_tags.txt +++ b/spec/tags/truffle/methods_tags.txt @@ -57,7 +57,6 @@ fails:Public methods on TracePoint should include parameters fails:Public methods on TracePoint should include raised_exception fails:Public methods on TracePoint should include return_value fails:Public methods on ENV.singleton_class should include freeze -fails:Public methods on IO should include set_encoding_by_bom fails:Public methods on BasicSocket should include read_nonblock fails:Public methods on BasicSocket should include write_nonblock fails:Public methods on Socket should not include local_address diff --git a/src/main/ruby/truffleruby/core/io.rb b/src/main/ruby/truffleruby/core/io.rb index f7efc581e808..29397b883246 100644 --- a/src/main/ruby/truffleruby/core/io.rb +++ b/src/main/ruby/truffleruby/core/io.rb @@ -2158,6 +2158,25 @@ def set_encoding(external, internal=nil, options=undefined) self end + def set_encoding_by_bom + unless binmode? + raise ArgumentError, 'ASCII incompatible encoding needs binmode' + end + + if internal_encoding + raise ArgumentError, 'encoding conversion is set' + end + + if external_encoding && external_encoding != Encoding::ASCII_8BIT + raise ArgumentError, "encoding is set to #{external_encoding} already" + end + + external = strip_bom + if external + @external = Encoding.find(external) + end + end + private def strip_bom mode = Truffle::POSIX.truffleposix_fstat_mode(Primitive.io_fd(self)) return unless Truffle::StatOperations.file?(mode) diff --git a/test/mri/excludes/TestIO_M17N.rb b/test/mri/excludes/TestIO_M17N.rb index 242e70aacf49..bf7664838cbb 100644 --- a/test/mri/excludes/TestIO_M17N.rb +++ b/test/mri/excludes/TestIO_M17N.rb @@ -147,9 +147,3 @@ exclude :test_bom_non_reading, "needs investigation" exclude :test_stdin, "needs investigation" exclude :"test_strip_bom:UTF-16LE", "needs investigation" -exclude :test_strip_bom_no_conv, "needs investigation" -exclude :"test_strip_bom:UTF-16BE", "needs investigation" -exclude :"test_strip_bom:UTF-32BE", "needs investigation" -exclude :"test_strip_bom:UTF-32LE", "needs investigation" -exclude :"test_strip_bom:UTF-8", "needs investigation" -exclude :test_strip_bom_no_bom, "needs investigation"