ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:119495] [Ruby master Feature#20792] String#forcible_encoding?
@ 2024-10-09 15:44 kddnewton (Kevin Newton) via ruby-core
  2024-10-09 18:20 ` [ruby-core:119496] " Eregon (Benoit Daloze) via ruby-core
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: kddnewton (Kevin Newton) via ruby-core @ 2024-10-09 15:44 UTC (permalink / raw)
  To: ruby-core; +Cc: kddnewton (Kevin Newton)

Issue #20792 has been reported by kddnewton (Kevin Newton).

----------------------------------------
Feature #20792: String#forcible_encoding?
https://bugs.ruby-lang.org/issues/20792

* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
I would like to add a method to String called `forcible_encoding?(encoding)`. This would return true or false depending on whether the receiver can be forced into the given encoding without breaking the string. It would effectively be an alias for:

```ruby
def forcible_encoding?(enc)
  original = encoding
  result = force_encoding(enc).valid_encoding?
  force_encoding(original)
  result
end
```

I would like this method because there are extremely rare but possible circumstances where source files are marked as binary but contain UTF-8-encoded characters. In that case I would like to check if it's possible to cleanly force UTF-8 before actually doing it. The code I'm trying to replace is here: https://github.com/ruby/prism/blob/d6e9b8de36b4d18debfe36e4545116539964ceeb/lib/prism/parse_result.rb#L15-L30.

The pull request for the code is here: https://github.com/ruby/ruby/pull/11851.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-11-08 15:49 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-09 15:44 [ruby-core:119495] [Ruby master Feature#20792] String#forcible_encoding? kddnewton (Kevin Newton) via ruby-core
2024-10-09 18:20 ` [ruby-core:119496] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 18:23 ` [ruby-core:119497] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 18:53 ` [ruby-core:119498] " Eregon (Benoit Daloze) via ruby-core
2024-10-09 19:07 ` [ruby-core:119499] " austin (Austin Ziegler) via ruby-core
2024-10-09 22:34 ` [ruby-core:119500] " Eregon (Benoit Daloze) via ruby-core
2024-10-10  3:44 ` [ruby-core:119501] " nirvdrum (Kevin Menard) via ruby-core
2024-10-11  8:05 ` [ruby-core:119509] " nobu (Nobuyoshi Nakada) via ruby-core
2024-10-11 15:04 ` [ruby-core:119512] " byroot (Jean Boussier) via ruby-core
2024-10-14 17:41 ` [ruby-core:119525] " kddnewton (Kevin Newton) via ruby-core
2024-10-14 19:24 ` [ruby-core:119526] " Eregon (Benoit Daloze) via ruby-core
2024-10-21  8:50 ` [ruby-core:119538] " kddnewton (Kevin Newton) via ruby-core
2024-10-21 10:52 ` [ruby-core:119571] " Eregon (Benoit Daloze) via ruby-core
2024-11-06 18:21 ` [ruby-core:119779] [Ruby master Feature#20792] String#with_encoding(encoding) kddnewton (Kevin Newton) via ruby-core
2024-11-07 10:38 ` [ruby-core:119804] " mame (Yusuke Endoh) via ruby-core
2024-11-08 15:45 ` [ruby-core:119846] " kddnewton (Kevin Newton) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).