Skip to content
This repository

Fix 6510 #183

Closed
wants to merge 3 commits into from

2 participants

ryenus Charles Oliver Nutter
ryenus
ryenus commented

This is to fix issue JRUBY-6510

In the past, when there's only 1 argument to String#encode!, the argument was always treated as the target encoding to transcode to.

This is wrong, because, when the only argument is a hash, it should be treated as the option for transcode, not the target encoding.

Charles Oliver Nutter
Owner

This one got lost in the shuffle. Can you re-push a version that applies to master and collapse your commits into one?

ryenus ryenus closed this
ryenus

sure, I'll close this one and make a new PR

ryenus

Just to make the story complete, the patch is resubmitted with pull request #255: #255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16 spec/regression/JRUBY-6510_hash_treat_as_encoding_spec.rb
... ... @@ -0,0 +1,16 @@
  1 +# encoding: UTF-8
  2 +require 'rspec'
  3 +
  4 +describe 'JRUBY-6510: String.encode!' do
  5 + it 'should accept a hash as only argument' do
  6 + enc_dft_in = Encoding.default_internal
  7 + begin
  8 + Encoding.default_internal = 'ISO-8859-1'
  9 + s = "äöü"
  10 + s.encode!({:invalid => :replace, :undef => :replace})
  11 + s.encoding.name.should == 'ISO-8859-1'
  12 + ensure
  13 + Encoding.default_internal = enc_dft_in unless enc_dft_in.nil?
  14 + end
  15 + end
  16 +end
15 src/org/jruby/RubyString.java
@@ -7377,11 +7377,22 @@ public IRubyObject encode_bang(ThreadContext context) {
7377 7377 }
7378 7378
7379 7379 @JRubyMethod(name = "encode!", compat = RUBY1_9)
7380   - public IRubyObject encode_bang(ThreadContext context, IRubyObject enc) {
  7380 + public IRubyObject encode_bang(ThreadContext context, IRubyObject arg) {
7381 7381 Ruby runtime = context.getRuntime();
7382 7382 modify19();
  7383 + Encoding toEncoding;
  7384 + IRubyObject options;
  7385 +
  7386 + if (arg instanceof RubyHash) {
  7387 + toEncoding = runtime.getDefaultInternalEncoding();
  7388 + if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
  7389 + options = arg;
  7390 + } else {
  7391 + toEncoding = getEncoding(runtime, arg);
  7392 + options = runtime.getNil();
  7393 + }
7383 7394
7384   - value = transcode(context, value, null, getEncoding(runtime, enc), runtime.getNil());
  7395 + value = transcode(context, value, null, toEncoding, options);
7385 7396
7386 7397 return this;
7387 7398 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.