Forum: Ruby-dev [ruby-trunk - Bug #10382] s = '\x80'; /#{s}/ raises an exception in US-ASCII script

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4c9779ffff66b09982330c24aca79ab7?d=identicon&s=25 Nobuyoshi N. (nobu)
on 2014-12-29 10:39
(Received via mailing list)
Issue #10382 has been updated by Nobuyoshi Nakada.

Related to Bug #10670: char-class matching same character with different
encodings raises exception added

Bug #10382: s = '\x80'; /#{s}/ raises an exception in US-ASCII script

* Author: Akira Tanaka
* Status: Closed
* Priority: Normal
* Assignee:
* Category:
* Target version:
* ruby -v: ruby 2.2.0dev (2014-10-14 trunk 47915) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN

% cat z1.rb
# -*- encoding: US-ASCII -*-
r = /#{'\x80'}/
p [r, r.encoding]
% ./miniruby -v z1.rb
ruby 2.2.0dev (2014-10-14 trunk 47915) [x86_64-linux]
[/\x80/, #<Encoding:ASCII-8BIT>]

% cat z2.rb
# -*- encoding: US-ASCII -*-
s = '\x80'
r = /#{s}/
p [r, r.encoding]
% ./miniruby -v z2.rb
ruby 2.2.0dev (2014-10-14 trunk 47915) [x86_64-linux]
z2.rb:3:in `<main>': invalid multibyte escape (ArgumentError)

後者のスクリプトは前者のスクリプトの文字列 '\x80' を変数に代入してから


それだけなら US-ASCII なので \x80 は invalid で例外という後者が正しいのですが、
US-ASCII のスクリプト内で \x80 などを使うと自動的に ASCII-8BIT になるという仕様 ruby-dev:33348

(静的な正規表現に関しての実装は reg_fragment_setenc_gen だと思います。)
This topic is locked and can not be replied to.