unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Colin Woodbury" <colin@fosskers.ca>
To: "Stefan Monnier" <monnier@iro.umontreal.ca>
Cc: "Basil L. Contovounesios" <contovob@tcd.ie>, emacs-devel@gnu.org
Subject: Re: [PATCH] lisp/files.el: Add `file-name-set-extension`
Date: Tue, 25 May 2021 14:21:07 -0700	[thread overview]
Message-ID: <3780a7f9-19f4-4216-baa9-ce00b3dbace9@www.fastmail.com> (raw)
In-Reply-To: <jwveedur2cl.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 2012 bytes --]

That regex was borrowed directly from `string-trim`, to which I added a `.`. You're right that the main motivation was to allow the caller to pass either `.foo` or `foo` as the extension and have either case work (many languages do this). Leaving the other characters in the regex seemed like a sanitary thing to do, just in case they pass something bogus. Although as the code is, there's nothing preventing them from still giving bogus characters before the file, or after the extension, making the resulting filepath still meaningless. 

So we could consider:

1. Simplifying the regex to just include dots (and perhaps whitespace) (i.e. trust the caller), or;
2. Expanding the logic to sanitize both ends of both arguments, (i.e. don't trust the caller), or;
3. Adding error-throwing logic if malformed files or extensions are given (i.e. warn the user).

I'm happy to alter the patch for any of those scenarios, whichever is preferable. Thanks!

On Tue, 25 May 2021, at 13:25, Stefan Monnier wrote:
> > +  (when (and filename extension)
> > +    (let* ((patt "[ \t\n\r.]+") ; Inspired by `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)))))
> 
> Why do you trim [ \t\n\r.]+ from the end of the filename and the
> beginning of the extension?
> 
> I can see why you'd want to remove a single "." at the beginning of
> `extension`, so as to allow extension to come with or without a leading
> ".", but the rest seems rather surprising because such characters in my
> experience almost never show up in such a circumstance (which means
> that if they do, it's either on purpose and we should preserve it, or
> it's an error "upstream" and we should try and help expose the error
> rather than silently try to cover it).
> 
> 
>         Stefan
> 
> 

[-- Attachment #2: Type: text/html, Size: 2968 bytes --]

  reply	other threads:[~2021-05-25 21:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3780a7f9-19f4-4216-baa9-ce00b3dbace9@www.fastmail.com \
    --to=colin@fosskers.ca \
    --cc=contovob@tcd.ie \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
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).