Project

General

Profile

Bug #9314

String#encode method raises TypeError when called with Encoding::UTF_16

Added by krists.ozols (Krists Ozols) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:59354]

Description

It works with encodings like Encoding::UTF_8..

"test_string".encode(Encoding::UTF_8) # works
"test_string".encode(Encoding::UTF_8.to_s) # works
"test_string".encode(Encoding::UTF_16) # does not work. Error: `encode': no implicit conversion of Encoding into String (TypeError) from ...
"test_string".encode(Encoding::UTF_16.to_s) # works

it did not raise any errors in previous ruby versions.

Associated revisions

Revision 5db5677c
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision 44462
Added by nobu (Nobuyoshi Nakada) over 5 years ago

encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Revision b3761323
Added by naruse (Yui NARUSE) over 5 years ago

merge revision(s) 44462,44477: [Backport #9314]

    * encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask
      encoding index and ignore dummy flags.  [ruby-core:59354] [Bug #9314]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@45083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

History

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Rejected

There is no real encoding, "UTF-16".
UTF-{16,32} encodings need endianness.

Updated by krists.ozols (Krists Ozols) over 5 years ago

nobu (Nobuyoshi Nakada) wrote:

There is no real encoding, "UTF-16".
UTF-{16,32} encodings need endianness.

In that case shouldn't it raise error when called with "UTF-16" string as it does with something random(like "text".encode("not-real-encoding"))?

Updated by krists.ozols (Krists Ozols) over 5 years ago

More examples:

>> "A".encode(Encoding::UTF_16.to_s).bytes
=> [254, 255, 0, 65]
>> "A".encode(Encoding::UTF_16BE).bytes
=> [0, 65]
>> "A".encode(Encoding::UTF_16LE).bytes
=> [65, 0]

There is even a side-effect. After using UTF_16BE suddenly UTF_16 works:

$ irb
>> "some_text".encode(Encoding::UTF_16).bytes
TypeError: no implicit conversion of Encoding into String
    from (irb):1:in `encode'
    from (irb):1
    from /Users/krists/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
>> "some_text".encode(Encoding::UTF_16BE).bytes
=> [0, 115, 0, 111, 0, 109, 0, 101, 0, 95, 0, 116, 0, 101, 0, 120, 0, 116]
>> "some_text".encode(Encoding::UTF_16).bytes
=> [254, 255, 0, 115, 0, 111, 0, 109, 0, 101, 0, 95, 0, 116, 0, 101, 0, 120, 0, 116]

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Rejected to Open
#5

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r44462.
Krists, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


encoding.c: mask dummy flags

  • encoding.c (must_encindex, rb_enc_from_index, rb_obj_encoding): mask encoding index and ignore dummy flags. [ruby-core:59354] [Bug #9314]

Updated by usa (Usaku NAKAMURA) over 5 years ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN to 1.9.3: DONTNEED, 2.0.0: DONTNEED, 2.1: REQUIRED

Updated by naruse (Yui NARUSE) over 5 years ago

  • Backport changed from 1.9.3: DONTNEED, 2.0.0: DONTNEED, 2.1: REQUIRED to 1.9.3: DONTNEED, 2.0.0: DONTNEED, 2.1: DONE

r45083.

Also available in: Atom PDF