unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] lisp/files.el: Add `file-name-set-extension`
@ 2021-05-25 15:50 Colin Woodbury
  2021-05-25 17:48 ` Andreas Schwab
  2021-05-25 18:00 ` Basil L. Contovounesios
  0 siblings, 2 replies; 22+ messages in thread
From: Colin Woodbury @ 2021-05-25 15:50 UTC (permalink / raw)
  To: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 426 bytes --]

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!

[-- Attachment #1.2: Type: text/html, Size: 723 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: file-name-set-extension.patch --]
[-- Type: text/x-patch; name="file-name-set-extension.patch", Size: 1050 bytes --]

diff --git a/lisp/files.el b/lisp/files.el
index 62e1702fdf..f8aefa7930 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4889,6 +4889,20 @@ extension, the value is \"\"."
         (if period
             "")))))
 
+(defun file-name-set-extension (filename extension)
+  "Change the extension of a FILENAME to EXTENSION.
+Sanitizes the input to consolidate leading/trailing dots.
+
+Returns `nil' if either of the FILENAME or EXTENSION are `nil'
+before sanitizing, or empty afterwards."
+  (when (and filename extension)
+    (let* ((patt "[ \\t\\n\\r.]+") ; Borrowed from `string-trim'.
+           (filename (string-trim-right filename patt))
+           (extension (string-trim-left extension patt)))
+      (unless (or (string-empty-p filename)
+                  (string-empty-p extension))
+        (concat (file-name-sans-extension filename) "." extension)))))
+
 (defun file-name-base (&optional filename)
   "Return the base name of the FILENAME: no directory, no extension."
   (declare (advertised-calling-convention (filename) "27.1"))

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

end of thread, other threads:[~2021-06-19  9:16 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-25 15:50 [PATCH] lisp/files.el: Add `file-name-set-extension` Colin Woodbury
2021-05-25 17:48 ` Andreas Schwab
2021-05-25 19:42   ` Colin Woodbury
2021-05-25 18:00 ` Basil L. Contovounesios
2021-05-25 19:45   ` Colin Woodbury
2021-05-25 20:25     ` Stefan Monnier
2021-05-25 21:21       ` Colin Woodbury
2021-05-25 21:29         ` Stefan Monnier
2021-05-25 21:44           ` Colin Woodbury
2021-05-26  7:34             ` Andreas Schwab
2021-05-26 16:31               ` Colin Woodbury
2021-05-26 17:39                 ` Andreas Schwab
2021-05-31  1:16                   ` Colin Woodbury
2021-06-04 13:08                     ` Philipp
     [not found]                       ` <26B660D9-AC76-4AFC-9FFD-2F5D4DCA16C1@acm.org>
2021-06-09 20:54                         ` Colin Woodbury
2021-06-09 23:05                           ` Stefan Monnier
2021-06-10  0:42                             ` Colin Woodbury
2021-06-10  2:45                               ` Colin Woodbury
2021-06-10  7:46                           ` Michael Albinus
2021-06-10 14:40                             ` Colin Woodbury
2021-06-16  1:15                               ` Colin Woodbury
2021-06-19  9:16                                 ` Michael Albinus

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).