Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filename-extension mishandles hidden files #1307

Closed
mbutterick opened this issue Apr 11, 2016 · 4 comments
Closed

filename-extension mishandles hidden files #1307

mbutterick opened this issue Apr 11, 2016 · 4 comments

Comments

@mbutterick
Copy link
Collaborator

A hidden file starts with a dot, but filename-extension will treat its name as an extension:

#lang racket
(filename-extension (string->path ".foo"))
> #"foo"

I would argue this is incorrect: that a filename extension is not merely a substring that starts with a dot, but a substring that starts with a dot and has some kind of filename in front of it (for which it serves as the extension).

Therefore, the fix would be to change the regexp pattern in the current filename-extension:

(define (filename-extension name)
  (let* ([name (file-name 'filename-extension name)]
         [name (and name (path->bytes name))])
    (cond [(and name (regexp-match #rx#"[.]([^.]+)$" name)) => cadr]
          [else #f])))

To have a dot pattern at the front, like so:

(define (filename-extension name)
  (let* ([name (file-name 'filename-extension name)]
         [name (and name (path->bytes name))])
    (cond [(and name (regexp-match #rx#".[.]([^.]+)$" name)) => cadr]
          [else #f])))

Because then:

#lang racket
(filename-extension (string->path ".foo"))
> #f
@mflatt
Copy link
Member

mflatt commented Apr 16, 2016

I agree that filename-extension should not treat a leading "." as an extension separator, but I worry about backward compatibility. There's also path-replace-suffix and path-add-suffix, which have the same problem and same compatibility issues.

(Aside: why "extension" versus "suffix"? The root of this mismatch is ancient history and reflected in the function name add-extension-suffix. Outside of Racket, he term "extension" seems clearly to be the preferred one.)

One possibility is to introduce path-replace-extension, path-add-extension, andpath-extension` that do the right thing, and then document the old functions as deprecated. How does that sound?

@mbutterick
Copy link
Collaborator Author

Very good, thank you.

BTW not to induce mission creep, but why do these functions treat the extension/suffix as a byte string? It seems peculiar when everything else is geared toward Unicode.

#lang racket
(path->string (string->path "foo.λ"))
(filename-extension (string->path "foo.λ"))
> "foo.λ"
> #"\316\273"

@mflatt
Copy link
Member

mflatt commented Apr 16, 2016

Unfortunately, paths are not composed of characters in general. On Unix variants, paths are composed of bytes. On Windows, they're composed of UTF-16 code units. Using a byte string accommodates both of those.

@mflatt mflatt closed this as completed in 4d9427a Apr 17, 2016
@mflatt
Copy link
Member

mflatt commented Apr 25, 2016

Unfortunately, the name path-extension created conflicts for the "grip" package. I've renamed it to path-get-extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants