Skip to content
This repository

JRUBY-6510 fix String.encode! #255

Merged
merged 1 commit into from over 1 year ago

2 participants

ryenus Charles Oliver Nutter
ryenus

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.

btw, I renamed the variable name in the other unbang encode method to align with the argument name in transcode.

ryenus

@headius here is the updated fix for JRUBY-6510 based on the latest master.

Charles Oliver Nutter headius merged commit 8a773ea into from September 28, 2012
Charles Oliver Nutter headius closed this September 28, 2012
ryenus ryenus referenced this pull request October 22, 2012
Closed

Fix 6510 #183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 08, 2012
ryenus fix JRUBY-6510, check type of the only 1 arg, also align arg name w/ …
…transcode
7033a99
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 also accept a hash as the 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
27  src/org/jruby/RubyString.java
@@ -7372,11 +7372,22 @@ public IRubyObject encode_bang(ThreadContext context) {
7372 7372
     }
7373 7373
 
7374 7374
     @JRubyMethod(name = "encode!", compat = RUBY1_9)
7375  
-    public IRubyObject encode_bang(ThreadContext context, IRubyObject enc) {
  7375
+    public IRubyObject encode_bang(ThreadContext context, IRubyObject arg) {
7376 7376
         Ruby runtime = context.runtime;
7377 7377
         modify19();
  7378
+        Encoding toEncoding;
  7379
+        IRubyObject options;
7378 7380
 
7379  
-        value = transcode(context, value, null, getEncoding(runtime, enc), runtime.getNil());
  7381
+        if (arg instanceof RubyHash) {
  7382
+            toEncoding = runtime.getDefaultInternalEncoding();
  7383
+            if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
  7384
+            options = arg;
  7385
+        } else {
  7386
+            toEncoding = getEncoding(runtime, arg);
  7387
+            options = runtime.getNil();
  7388
+        }
  7389
+
  7390
+        value = transcode(context, value, null, toEncoding, options);
7380 7391
 
7381 7392
         return this;
7382 7393
     }
@@ -7425,20 +7436,20 @@ public IRubyObject encode(ThreadContext context) {
7425 7436
     @JRubyMethod(name = "encode", compat = RUBY1_9)
7426 7437
     public IRubyObject encode(ThreadContext context, IRubyObject arg) {
7427 7438
         Ruby runtime = context.runtime;
7428  
-        Encoding forceEncoding;
  7439
+        Encoding toEncoding;
7429 7440
         IRubyObject options;
7430 7441
 
7431 7442
         if (arg instanceof RubyHash) {
7432  
-            forceEncoding = runtime.getDefaultInternalEncoding();
7433  
-            if (forceEncoding == null) forceEncoding = runtime.getEncodingService().getLocaleEncoding();
7434  
-            if (forceEncoding == null) return dup();
  7443
+            toEncoding = runtime.getDefaultInternalEncoding();
  7444
+            if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
  7445
+            if (toEncoding == null) return dup();
7435 7446
             options = arg;
7436 7447
         } else {
7437  
-            forceEncoding = getEncoding(runtime, arg);
  7448
+            toEncoding = getEncoding(runtime, arg);
7438 7449
             options = runtime.getNil();
7439 7450
         }
7440 7451
 
7441  
-        return runtime.newString(transcode(context, value, null, forceEncoding, options));
  7452
+        return runtime.newString(transcode(context, value, null, toEncoding, options));
7442 7453
     }
7443 7454
 
7444 7455
     @JRubyMethod(name = "encode", compat = RUBY1_9)
Commit_comment_tip

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.