From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 29EE51F4B8 for ; Wed, 9 Oct 2024 19:08:16 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=rBaEejIu; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=FaqyY/av; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1728500893; bh=54y0nbmmYibxTnJtx3KmMNrF8kbNlp5ezdogUhuEc4o=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=rBaEejIuNNIDubAQHb+Z6TwEJBHJ7GeAQ2lsoLyHquYJpEyMgU3cDp83UR5phHLEf f4956EW/4z/YniOUYGCN4k/Yll1duJj5mP9y2t2QJqmqGz1aqo0G4xd+3peQinfR4/ 3iSqPgkwQkrT+8VCtq28OGqaVMPbjkjc6Ghok75A= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 8F3B943FEE for ; Wed, 9 Oct 2024 19:08:13 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=FaqyY/av; dkim-atps=neutral Received: from s.wrqvtvvn.outbound-mail.sendgrid.net (s.wrqvtvvn.outbound-mail.sendgrid.net [149.72.120.130]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 4F99043FC5 for ; Wed, 9 Oct 2024 19:08:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=yYj7pbYQc0je/tkouBLPv7GHFWeI7veWPEO5Alm4IFU=; b=FaqyY/av1CSMz3VMMFXXcc8GLmR/FUdCZA14hT9LbUn/Oa/TTFIkze1SaWvuP2J0P/uj MNAM0sDSYAJynpJIKmGpO5pEUiENi90Vk5thLotZ33f7y9ayTOreBj6wDjZN7c/gGDEhAg oNriR/U58E5s2LygxUDRiOdcazNf1o2tZaVXvAWvtCdF1aoStlwCa+KSAzcpsJwMTbdOZj M8usKwLslggTYvxIKuFB1jIoR2nwgrFxyMyInc5Y1E4YCzPm6uzTyoTaz9ijd4LKSDt7Oi Q2fAPUwgDyjuZWqq74FpDOg3Isy+WLIfm1IErr98CDO5bDUvwa/zyi3mtM02hdDA== Received: by recvd-7cc7f7d978-dvzlp with SMTP id recvd-7cc7f7d978-dvzlp-1-6706D48F-2 2024-10-09 19:07:59.049211451 +0000 UTC m=+2337121.753553846 Received: from herokuapp.com (unknown) by geopod-ismtpd-13 (SG) with ESMTP id loLmYHNaSCid5y1u_s4NcA for ; Wed, 09 Oct 2024 19:07:59.021 +0000 (UTC) Date: Wed, 09 Oct 2024 19:07:59 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20792 X-Redmine-Issue-Author: kddnewton X-Redmine-Issue-Priority: Normal X-Redmine-Sender: austin X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 96134 X-SG-EID: =?us-ascii?Q?u001=2EVFUB+kV3Tq4e5lldJMdduawSriB1JogS5aa7Rh5fx7YNCBpuvSXdLEKR+?= =?us-ascii?Q?oHmCKNf40JhtjwdWHsIE3bMUFJ9+3OpdrxTb8tg?= =?us-ascii?Q?QoXGs38KhL8KZURsDSA+y4fflelIg5lKXQ05XZn?= =?us-ascii?Q?r9B33nQ+EX6c1G+AQhJmKXWsKfFrYZmABNmKzf=2F?= =?us-ascii?Q?FXEBjpGPKLV0HTXs1Azse71Usora4CtOuQ8HKv0?= =?us-ascii?Q?q6rpcsF5zlqZs2GJQ6MnCzEjljgEyJs0USUy7He?= =?us-ascii?Q?TaqyULgF+6=2FvotIkti3rPhVXSQ=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: YFJGFMN2X2SBPYOYXYBCVAMKAE4C3SHX X-Message-ID-Hash: YFJGFMN2X2SBPYOYXYBCVAMKAE4C3SHX X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.9 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:119499] [Ruby master Feature#20792] String#forcible_encoding? List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "austin (Austin Ziegler) via ruby-core" Cc: "austin (Austin Ziegler)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20792 has been updated by austin (Austin Ziegler). Eregon (Benoit Daloze) wrote in #note-3: > I think I discussed this with @byroot a couple times as well, what about `String#with_encoding(Encoding)`, which is like `force_encoding` but doesn't mutate the receiver. > Then this would be efficient in that it would scan the code range once: > ```ruby > utf8 = binary_str.with_encoding(Encoding::UTF_8) > if utf8.valid_encoding? > return utf8 > else > return binary_str > end > ``` > > And of course it would shorten `str.dup.force_encoding(Encoding::UTF_8)` to `str.with_encoding(Encoding::UTF_8)`. A variation of this would be something like: ```ruby if utf8 = binary_str.try_encoding(Encoding::UTF_8) return utf8 else return binary_str end ``` The implementation would be the equivalent of: ```ruby def try_encoding(encoding) target = self.dup.force_encoding(encoding) target.valid_encoding? ? target : nil end ``` ---------------------------------------- Feature #20792: String#forcible_encoding? https://bugs.ruby-lang.org/issues/20792#change-110113 * 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/