Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

JRUBY-6510 fix String.encode! #255

Merged
merged 1 commit into from

2 participants

@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.

@headius headius merged commit 8a773ea into from
@ryenus ryenus referenced this pull request
Closed

Fix 6510 #183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 8, 2012
  1. @ryenus
This page is out of date. Refresh to see the latest.
View
16 spec/regression/JRUBY-6510_hash_treat_as_encoding_spec.rb
@@ -0,0 +1,16 @@
+# encoding: UTF-8
+require 'rspec'
+
+describe 'JRUBY-6510: String.encode!' do
+ it 'should also accept a hash as the only argument' do
+ enc_dft_in = Encoding.default_internal
+ begin
+ Encoding.default_internal = 'ISO-8859-1'
+ s = "äöü"
+ s.encode!({:invalid => :replace, :undef => :replace})
+ s.encoding.name.should == 'ISO-8859-1'
+ ensure
+ Encoding.default_internal = enc_dft_in unless enc_dft_in.nil?
+ end
+ end
+end
View
27 src/org/jruby/RubyString.java
@@ -7372,11 +7372,22 @@ public IRubyObject encode_bang(ThreadContext context) {
}
@JRubyMethod(name = "encode!", compat = RUBY1_9)
- public IRubyObject encode_bang(ThreadContext context, IRubyObject enc) {
+ public IRubyObject encode_bang(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.runtime;
modify19();
+ Encoding toEncoding;
+ IRubyObject options;
- value = transcode(context, value, null, getEncoding(runtime, enc), runtime.getNil());
+ if (arg instanceof RubyHash) {
+ toEncoding = runtime.getDefaultInternalEncoding();
+ if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
+ options = arg;
+ } else {
+ toEncoding = getEncoding(runtime, arg);
+ options = runtime.getNil();
+ }
+
+ value = transcode(context, value, null, toEncoding, options);
return this;
}
@@ -7425,20 +7436,20 @@ public IRubyObject encode(ThreadContext context) {
@JRubyMethod(name = "encode", compat = RUBY1_9)
public IRubyObject encode(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.runtime;
- Encoding forceEncoding;
+ Encoding toEncoding;
IRubyObject options;
if (arg instanceof RubyHash) {
- forceEncoding = runtime.getDefaultInternalEncoding();
- if (forceEncoding == null) forceEncoding = runtime.getEncodingService().getLocaleEncoding();
- if (forceEncoding == null) return dup();
+ toEncoding = runtime.getDefaultInternalEncoding();
+ if (toEncoding == null) toEncoding = runtime.getEncodingService().getLocaleEncoding();
+ if (toEncoding == null) return dup();
options = arg;
} else {
- forceEncoding = getEncoding(runtime, arg);
+ toEncoding = getEncoding(runtime, arg);
options = runtime.getNil();
}
- return runtime.newString(transcode(context, value, null, forceEncoding, options));
+ return runtime.newString(transcode(context, value, null, toEncoding, options));
}
@JRubyMethod(name = "encode", compat = RUBY1_9)
Something went wrong with that request. Please try again.