Project

General

Profile

Actions

Bug #9314

closed

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

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

Status:
Closed
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.

Updated by nobu (Nobuyoshi Nakada) over 10 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 10 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 10 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 10 years ago

  • Status changed from Rejected to Open
Actions #5

Updated by nobu (Nobuyoshi Nakada) over 10 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) about 10 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) about 10 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0