Backport #7954

"あ".byteslice(0,2).valid_encoding? should return false

Added by Toru Iwase about 1 year ago. Updated about 1 year ago.

[ruby-dev:47108]
Status:Closed
Priority:Normal
Assignee:Usaku NAKAMURA

Description

=begin
valid encodingな文字列に対するString#bytesliceがinvalid encodingなバイト列を生成してもvalid_encoding?がtrueになります。
これはfalseになるべきだと思います。

なお、1.9.3も同じ挙動です。

irb(main):001:0> RUBYDESCRIPTION
=> "ruby 2.0.0p0 (2013-02-24 revision 39474) [x86
64-linux]"
irb(main):002:0> "あ".encoding
=> #Encoding:UTF-8
irb(main):003:0> "あ".validencoding?
=> true
irb(main):004:0> "あ".byteslice(0,2)
=> "\xE3\x81"
irb(main):005:0> "あ".byteslice(0,2).valid
encoding?
=> true
irb(main):006:0> "\xE3\x81".encoding
=> #Encoding:UTF-8
irb(main):007:0> "\xE3\x81".valid_encoding?
=> false

ちなみに、invalidな文字列を正しい位置でbytesliceすると正しくvalidと判定されます。

irb(main):025:0> "あ\xE3".validencoding?
=> false
irb(main):026:0> "あ\xE3".byteslice(0,3).valid
encoding?
=> true
=end

Associated revisions

Revision 39978
Added by Usaku NAKAMURA about 1 year ago

merge revision(s) 39495: [Backport #7954]

* string.c (str_byte_substr): don't set coderange if it's not known.
  [Bug #7954] 

History

#1 Updated by Martin Dürst about 1 year ago

この場合の問題はそもぞも

"あ".byteslice(0,2).encoding
=> #Encoding:UTF-8

にあるかと思います。byteslice の戻り値の encoding は BINARY にすべきだと思います。

#2 Updated by Yui NARUSE about 1 year ago

  • Category set to M17N
  • Status changed from Open to Assigned
  • Assignee set to Yui NARUSE

duerst (Martin Dürst) wrote:

この場合の問題はそもぞも

"あ".byteslice(0,2).encoding
=> #Encoding:UTF-8

にあるかと思います。byteslice の戻り値の encoding は BINARY にすべきだと思います。

BINARY で受け取りたいならば、"あ".b.slice(0,2) とすればいいのであって、別のメソッドである意味がありません。

#3 Updated by Yui NARUSE about 1 year ago

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

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


  • string.c (strbytesubstr): don't set coderange if it's not known. [Bug #7954]

#4 Updated by Tomoyuki Chikanaga about 1 year ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport200
  • Category deleted (M17N)
  • Status changed from Closed to Assigned
  • Assignee changed from Yui NARUSE to Tomoyuki Chikanaga
  • Target version deleted (2.0.0)

#5 Updated by Tomoyuki Chikanaga about 1 year ago

  • Status changed from Assigned to Closed

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


merge revision(s) 39495: [Backport #7954]

* string.c (str_byte_substr): don't set coderange if it's not known.
  [Bug #7954] 

#6 Updated by Tomoyuki Chikanaga about 1 year ago

  • Project changed from Backport200 to Backport93
  • Status changed from Closed to Assigned
  • Assignee changed from Tomoyuki Chikanaga to Usaku NAKAMURA

1.9.3 でも同じ挙動ということなので Backport93 に移動しておきます。

#7 Updated by Usaku NAKAMURA about 1 year ago

  • Status changed from Assigned to Closed

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


merge revision(s) 39495: [Backport #7954]

* string.c (str_byte_substr): don't set coderange if it's not known.
  [Bug #7954] 

Also available in: Atom PDF