Project

General

Profile

Actions

Bug #18149

closed

Can't match against strings with special codes within

Added by chucke (Tiago Cardoso) over 2 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:105141]

Description

This can be summed up by the following example:

"\xFF".match /\\x/
#=> (irb):6:in `match': invalid byte sequence in UTF-8 (ArgumentError)

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Open to Rejected

That's because that is an invalid byte sequence, at least if the string's encoding is UTF-8. If the string's encoding is not UTF-8, it works fine:

$ ruby -e 'p "\xFF".encoding'
#<Encoding:ASCII-8BIT>
$ ruby -Ku -e 'p "\xFF".encoding'
#<Encoding:UTF-8>

$ ruby -e 'p "\xFF".match(/\\x/)' 
nil
$ ruby -Ku -e 'p "\xFF".match(/\\x/)'
-e:1:in `match': invalid byte sequence in UTF-8 (ArgumentError)
        from -e:1:in `match'
        from -e:1:in `<main>'

You can force the string encoding to binary to avoid the ArgumentError:

$ ruby -Ku -e 'p "\xFF".b.match(/\\x/)'
nil

So I don't think this is a bug, I believe the behavior is expected.

Updated by chucke (Tiago Cardoso) over 2 years ago

Thx for the reply. I think that's reasonable.

Actions

Also available in: Atom PDF

Like0
Like0Like0