Bug #566

String encoding error messages are inconsistent

Added by Michael Selig over 6 years ago. Updated almost 4 years ago.

[ruby-core:18600]
Status:Closed
Priority:Normal
Assignee:Yui NARUSE
ruby -v: Backport:

Description

=begin
Please compare:
"abc".encode("UTF-16BE") << "abc"
==> EncodingCompatibilityError: incompatible character encodings: UTF-16BE and US-ASCII
and:
"abc".encode("UTF-16BE") =~ /abc/
==> ArgumentError: incompatible encoding regexp match (US-ASCII regexp with UTF-16BE string)

also handling of broken (illegal) string encodings is not consistent:
"abc".force_encoding("UTF-16BE") =~ /abc/
==> ArgumentError: broken UTF-16BE string
and:
"abc".force_encoding("UTF-16BE") == "abc"
==> false (no error)
and:
"abc".encode("UTF-16BE").count("b".force_encoding("UTF-16BE"))
==> ArgumentError: invalid byte sequence in UTF-16BE
=end

History

#1 Updated by Yukihiro Matsumoto over 6 years ago

=begin
Hi,

In message "Re: [Bug #566] String encoding error messages are inconsistent"
on Mon, 15 Sep 2008 15:50:17 +0900, Michael Selig redmine@ruby-lang.org writes:

|Please compare:
|"abc".encode("UTF-16BE") << "abc"
|==> EncodingCompatibilityError: incompatible character encodings: UTF-16BE and US-ASCII
|and:
|"abc".encode("UTF-16BE") =~ /abc/
|==> ArgumentError: incompatible encoding regexp match (US-ASCII regexp with UTF-16BE string)
|
|also handling of broken (illegal) string encodings is not consistent:
|"abc".force_encoding("UTF-16BE") =~ /abc/
|==> ArgumentError: broken UTF-16BE string
|and:
|"abc".force_encoding("UTF-16BE") == "abc"
|==> false (no error)
|and:
|"abc".encode("UTF-16BE").count("b".force_encoding("UTF-16BE"))
|==> ArgumentError: invalid byte sequence in UTF-16BE

I am not sure what you mean by "inconsistent". What are your ideal
messages (or behavior) for each case?

                        matz.

=end

#2 Updated by Koichi Sasada over 6 years ago

  • Assignee set to Yui NARUSE

=begin

=end

#3 Updated by Yuki Sonoda over 6 years ago

  • Target version set to 1.9.1 Release Candidate
  • Category set to M17N

=begin

=end

#4 Updated by Yukihiro Matsumoto about 6 years ago

=begin
Hi,

Sorry for being late.

In message "Re: Re: [Bug #566] String encoding error messages are inconsistent"
on Tue, 16 Sep 2008 07:38:13 +0900, "Michael Selig" michael.selig@fs.com.au writes:

|I would expect these to both be "EncodingCompatibilityError"

OK, I will.

|> |also handling of broken (illegal) string encodings is not consistent:
|> |"abc".force_encoding("UTF-16BE") =~ /abc/
|> |==> ArgumentError: broken UTF-16BE string
|> |and:
|> |"abc".force_encoding("UTF-16BE") == "abc"
|> |==> false (no error)
|> |and:
|> |"abc".encode("UTF-16BE").count("b".force_encoding("UTF-16BE"))
|> |==> ArgumentError: invalid byte sequence in UTF-16BE
|
|I guess in this group there are 2 issues:
|1) (This is minor) I would expect both error messages to have the same

|text - I think the "invalid byte sequence in XXX" is the better.
|2) It seems inconsistent to me that the 1st & 2nd expressions look almost

|the same as each other (a regexp match & a string compare) yet only the

|regexp match raises an error.

(1) I changed messages more consistent.
(2) "=~" and "==" are different operation. The former requires more
precondition than the latter, e.g. type of both operands should be
strings or regexps, and they should be encoding compatible. On
the other hands, the latter . They should give either true or
false, and should not raise any exception.

                        matz.

=end

#5 Updated by Yukihiro Matsumoto about 6 years ago

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

=begin
Applied in changeset r20626.
=end

Also available in: Atom PDF