[ruby-core:117591] [Ruby master Misc#20434] Deprecate encoding-releated regular expression modifiers
From:
duerst via ruby-core <ruby-core@...>
Date:
2024-04-18 06:22:30 UTC
List:
ruby-core #117591
Issue #20434 has been updated by duerst (Martin D=FCrst).
I guess there might still be some use for the encoding-related modifiers in=
single-line scripts and the like. But I don't have an actual use case; I h=
ope whoever has such an use case comes forward.
The replacement code (`::Regexp.new(::String.new("\x81\x40", encoding: "Win=
dows-31J"))`) is quite lengthy. This makes it clear that while each regular=
expression has an encodings in the same way as each String has an encoding=
, regular expressions don't really allow to manipulate the encoding. String=
s have #force_encoding and #encode, so maybe adding one or both methods to =
Regexp would help. The example could then be written as `/\x81\x40/.force_e=
ncoding("Windows-31J")` or /\3000/.encode("Windows-31J").
----------------------------------------
Misc #20434: Deprecate encoding-releated regular expression modifiers
https://bugs.ruby-lang.org/issues/20434#change-107992
* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
This is a follow-up to @duerst's comment here: https://bugs.ruby-lang.org/i=
ssues/20406#note-6.
As noted in the other issue, there are many encodings that factor in to how=
a regular expression operates. This includes:
* The encoding of the file
* The encoding of the string parts within the regular expression
* The regular expression encoding modifiers
* The encoding of the string being matched
At the time the modifiers were introduced, I believe the modifiers may have=
been the only (??) encoding that factored in here. At this point, however,=
they can lead to quite a bit of confusion, as noted in the other ticket.
I would like to propose to deprecate the regular expression encoding modifi=
ers. Instead, we could suggest in a warning to instead create a regular exp=
ression with an encoded string. For example, when we find:
```ruby
/\x81\x40/s
```
we would instead suggest:
```ruby
::Regexp.new(::String.new("\x81\x40", encoding: "Windows-31J"))
```
or equivalent. As a migration path, we could do the following:
1. Emit a warning to change to the suggested expression
2. Change the compiler to compile to the suggested expression when those fl=
ags are found
3. Remove support for the flags
Step 2 may be unnecessary depending on how long of a timeline we would like=
to provide. To be clear, I'm not advocating for any particular timeline, a=
nd would be fine with this being multiple years/versions to give plenty of =
time for people to migrate. But I do think this would be a good change to e=
liminate confusion about the interaction between the four different encodin=
gs at play.
--=20
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- [email protected]
To unsubscribe send an email to [email protected]
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-c=
ore.ml.ruby-lang.org/