From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Colin Woodbury" Newsgroups: gmane.emacs.devel Subject: [PATCH] lisp/files.el: Add `file-name-set-extension` Date: Tue, 25 May 2021 08:50:10 -0700 Message-ID: <6ff4b7d7-03ac-48d1-8d49-de66431d4e5b@www.fastmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary=0a88d12bd5224bd1ba1f3ee11ab7aecf Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39809"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Cyrus-JMAP/3.5.0-alpha0-448-gae190416c7-fm-20210505.004-gae190416 To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue May 25 19:06:53 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1llaW4-000ABK-Vl for ged-emacs-devel@m.gmane-mx.org; Tue, 25 May 2021 19:06:53 +0200 Original-Received: from localhost ([::1]:33152 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llaW3-00013U-Qk for ged-emacs-devel@m.gmane-mx.org; Tue, 25 May 2021 13:06:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42404) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llZM2-0006vQ-RH for emacs-devel@gnu.org; Tue, 25 May 2021 11:52:26 -0400 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:58031) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llZLz-0007uc-2d for emacs-devel@gnu.org; Tue, 25 May 2021 11:52:26 -0400 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id DBFC72D2 for ; Tue, 25 May 2021 11:52:19 -0400 (EDT) Original-Received: from imap1 ([10.202.2.51]) by compute1.internal (MEProxy); Tue, 25 May 2021 11:52:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fosskers.ca; h= mime-version:message-id:date:from:to:subject:content-type; s= fm1; bh=ih+w1J80Tc4nYUjwgE27dk5bo4sp7CWJQehXiQ/PVoc=; b=ooKjFWJe zFP+316daE3vWncZQI/W9YI1Ple88QKnFUAfk5q48sztVwl2nX/1eTs3apnRr9Nz RN5ReJVFlorgUibNTGt+MATaxZo+kPdZ4Nc71DyRpaQvhi3w++fw7Q1oNS5bdiw7 b8bwZLUnPpYcl24Na+k7cSfS41dCmsoUTm9qIYyxSn2QTkS/QcPOajfhHNHFCNAm VLFaZmKuAfcweV4cYp0wMhnrhqDPljGB8NrG8Z16+IlHvvFR9BokopDF8XjkdapH 0ZfmUTxHYyUYS3+/bbuOwV8KvU8D4uqUQMGZXwLziH5FP4rmk2RldbAJIgLU75kj 1ksfxBN+aplg+A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=ih+w1J80Tc4nYUjwgE27dk5bo4sp7 CWJQehXiQ/PVoc=; b=jZThfb2LqccZkVJ7y+IlZl/V4DOZk9XUG1AtfPUpop7x+ v0aTnmkoXoREjZ+FestTjGhonvhtWnUXUqbeYLt/8V10RTlEQ8piMDgZxgEQFFhb 1EZMOfZP/ZHCT1xxDQ90lIX6V1dZv8JEN5K+ppdCSUnto7/btcyeXrz+OxhmrBzW yRg4/miROYXkLH3up2PDgb9ya7ZRQzk6RTnfu2i4fuTjMzoyp3fnohFf1qU6EEQk mgtzwECM5sgwMVA9EX2cde1UDZcrvzY67rR19oFFzlHeN1d/YKVN1o23L47Jt9av zehg4PZWhW9QNju2VCobNRe0RFvCPZ9HvTIPTtwew== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdekuddgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsehmtderre erredtnecuhfhrohhmpedfveholhhinhcuhghoohgusghurhihfdcuoegtohhlihhnsehf ohhsshhkvghrshdrtggrqeenucggtffrrghtthgvrhhnpeeltdfgudelieefiefhheekve dvgfeitdehjeehleelveehheekueevledthfegudenucevlhhushhtvghrufhiiigvpedt necurfgrrhgrmhepmhgrihhlfhhrohhmpegtohhlihhnsehfohhsshhkvghrshdrtggr X-ME-Proxy: Original-Received: by mailuser.nyi.internal (Postfix, from userid 501) id 474B61300064; Tue, 25 May 2021 11:52:19 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Received-SPF: none client-ip=64.147.123.19; envelope-from=colin@fosskers.ca; helo=wout3-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:269864 Archived-At: --0a88d12bd5224bd1ba1f3ee11ab7aecf Content-Type: multipart/alternative; boundary=d96c521804e14c89a3e5c5eb2af73afb --d96c521804e14c89a3e5c5eb2af73afb Content-Type: text/plain This patch adds a safe way to set a filename's extension. It sanitizes the input so that both these cases do the expected thing: ELISP> (file-name-set-extension "jack.scss" "css") "jack.css" ELISP> (file-name-set-extension "jack.scss" ".css") "jack.css" Note that if we're trying to be safe with errors, nils, and empty strings, it's not sufficient to just `(concat (file-name-sans-extension file) "." extension)`. Cheers! --d96c521804e14c89a3e5c5eb2af73afb Content-Type: text/html
This patch adds a safe way to set a filename's extension. It sanitizes the input so that both these cases do the expected thing:

ELISP> (file-name-set-extension "jack.scss" "css")
"jack.css"

ELISP> (file-name-set-extension "jack.scss" ".css")
"jack.css"

Note that if we're trying to be safe with errors, nils, and empty strings, it's not sufficient to just `(concat (file-name-sans-extension file) "." extension)`.

Cheers!
--d96c521804e14c89a3e5c5eb2af73afb-- --0a88d12bd5224bd1ba1f3ee11ab7aecf Content-Disposition: attachment;filename="file-name-set-extension.patch" Content-Type: text/x-patch; name="file-name-set-extension.patch" Content-Transfer-Encoding: BASE64 ZGlmZiAtLWdpdCBhL2xpc3AvZmlsZXMuZWwgYi9saXNwL2ZpbGVzLmVsCmluZGV4IDYyZTE3 MDJmZGYuLmY4YWVmYTc5MzAgMTAwNjQ0Ci0tLSBhL2xpc3AvZmlsZXMuZWwKKysrIGIvbGlz cC9maWxlcy5lbApAQCAtNDg4OSw2ICs0ODg5LDIwIEBAIGV4dGVuc2lvbiwgdGhlIHZhbHVl IGlzIFwiXCIuIgogICAgICAgICAoaWYgcGVyaW9kCiAgICAgICAgICAgICAiIikpKSkpCiAK KyhkZWZ1biBmaWxlLW5hbWUtc2V0LWV4dGVuc2lvbiAoZmlsZW5hbWUgZXh0ZW5zaW9uKQor ICAiQ2hhbmdlIHRoZSBleHRl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