Skip to content

Commit

Permalink
[GR-19220] Implement IO#set_encoding_by_bom (#2372)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/2704
  • Loading branch information
eregon committed Jun 8, 2021
2 parents 83b73dc + fea3cca commit 0194812
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/io/set_encoding_by_bom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions spec/tags/core/io/set_encoding_by_bom_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/truffle/methods_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions test/mri/excludes/TestIO_M17N.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 0194812

Please sign in to comment.