rename relectly added path-extension to path-get-extension

The name `path-extension` created a conflict for an existing
registered package, so it should not have been added to
`racket/path`.

Also, `path-get-extension` was intended to work on a path
that is syntactically a directory, so fix and test that.
This commit is contained in:
Matthew Flatt 2016-04-24 20:16:31 -06:00
parent 18fd9949a6
commit 35ab9ffdb8
3 changed files with 21 additions and 19 deletions

View File

@ -635,7 +635,7 @@ syntactically a directory path (see @racket[split-path]), then the
result is @racket[#f].}
@defproc[(path-extension [path (or/c path-string? path-for-some-system?)])
@defproc[(path-get-extension [path (or/c path-string? path-for-some-system?)])
(or/c bytes? #f)]{
Returns a byte string that is the extension part of the filename in
@ -646,10 +646,10 @@ See @racket[path-replace-extension] for the definition of a filename
extension.
@examples[#:eval path-eval
(path-extension "x/y.rkt")
(path-extension "x/y")
(path-extension "x/y.tar.gz")
(path-extension "x/.racketrc")
(path-get-extension "x/y.rkt")
(path-get-extension "x/y")
(path-get-extension "x/y.tar.gz")
(path-get-extension "x/.racketrc")
]
@history[#:added "6.5.0.3"]}
@ -664,7 +664,7 @@ Determines whether the last element of @racket[path] ends with
If @racket[ext] is a @tech{byte string} with the shape of an extension
(i.e., starting with @litchar{.}), this check is equivalent to
checking whether @racket[(path-extension path)] produces @racket[ext].
checking whether @racket[(path-get-extension path)] produces @racket[ext].
@examples[#:eval path-eval
(path-has-extension? "x/y.rkt" #".rkt")
@ -680,7 +680,7 @@ checking whether @racket[(path-extension path)] produces @racket[ext].
@defproc[(filename-extension [path (or/c path-string? path-for-some-system?)])
(or/c bytes? #f)]{
@deprecated[#:what "function" @racket[path-extension]]
@deprecated[#:what "function" @racket[path-get-extension]]
Returns a byte string that is the extension part of the filename in
@racket[path] without the @litchar{.} separator. If @racket[path] is

View File

@ -52,11 +52,12 @@
;; ----------------------------------------
(rtest path-extension "a" #f)
(rtest path-extension "a.sls" #".sls")
(rtest path-extension (bytes->path #"b/a.sls" 'unix) #".sls")
(rtest path-extension (bytes->path #"b\\a.sls" 'windows) #".sls")
(rtest path-extension ".sls" #f)
(rtest path-get-extension "a" #f)
(rtest path-get-extension "a.sls" #".sls")
(rtest path-get-extension (bytes->path #"b/a.sls" 'unix) #".sls")
(rtest path-get-extension (bytes->path #"b\\a.sls" 'windows) #".sls")
(rtest path-get-extension ".sls" #f)
(rtest path-get-extension "a.sls/" #".sls")
(test #t path-has-extension? "a.sls" #".sls")
(test #t path-has-extension? "a.sls" ".sls")

View File

@ -4,7 +4,7 @@
simple-form-path
normalize-path
path-has-extension?
path-extension
path-get-extension
filename-extension
file-name-from-path
path-only
@ -146,15 +146,16 @@
(apply build-path (append (map (lambda (x) 'up) dir) file))]))
filename)))
(define (file-name who name)
(define (file-name who name dir-ok?)
(unless (or (path-string? name)
(path-for-some-system? name))
(raise-argument-error who "(or/c path-string? path-for-some-system?)" name))
(let-values ([(base file dir?) (split-path name)])
(and (not dir?) (path-for-some-system? file) file)))
(and (or dir-ok? (not dir?))
(path-for-some-system? file) file)))
(define (file-name-from-path name)
(file-name 'file-name-from-path name))
(file-name 'file-name-from-path name #f))
(define (path-only name)
(unless (or (path-string? name)
@ -180,15 +181,15 @@
(and (len . > . slen)
(bytes=? sfx (subbytes bs (- len slen))))))))
(define (path-extension name)
(let* ([name (file-name 'filename-extension name)]
(define (path-get-extension name)
(let* ([name (file-name 'path-get-extension name #t)]
[name (and name (path->bytes name))])
(cond [(and name (regexp-match #rx#"(?<=.)([.][^.]+)$" name)) => cadr]
[else #f])))
;; This old variant doesn't correctly handle filenames that start with ".":
(define (filename-extension name)
(let* ([name (file-name 'filename-extension name)]
(let* ([name (file-name 'filename-extension name #f)]
[name (and name (path->bytes name))])
(cond [(and name (regexp-match #rx#"[.]([^.]+)$" name)) => cadr]
[else #f])))