From: "Eregon (Benoit Daloze) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
Subject: [ruby-core:119526] [Ruby master Feature#20792] String#forcible_encoding?
Date: Mon, 14 Oct 2024 19:24:43 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-110141.20241014192443.10206@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20792.20241009154452.10206@ruby-lang.org>
Issue #20792 has been updated by Eregon (Benoit Daloze).
> I think the advantage right now is that it doesn't require a mutable string to check.
`with_encoding` would always be the same as `.dup.force_encoding` (except slightly more efficient).
It doesn't mutate the receiver.
For the description use case you could then use `valid_encoding?` like in https://bugs.ruby-lang.org/issues/20792#note-3
That might compute the code range (if not already computed), but that's needed to know if the encoding is valid anyway, and interestingly it will remember this coderange for further operations on that new String.
Using `forcible_encoding?` it can't remember the code range and so it would have to be recomputed on `force_encoding` or the next operation needing it.
----------------------------------------
Feature #20792: String#forcible_encoding?
https://bugs.ruby-lang.org/issues/20792#change-110141
* 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/
next prev parent reply other threads:[~2024-10-14 19:25 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-09 15:44 [ruby-core:119495] " 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 ` Eregon (Benoit Daloze) via ruby-core [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-110141.20241014192443.10206@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).