diff --git a/pkgs/racket-doc/scribblings/reference/paths.scrbl b/pkgs/racket-doc/scribblings/reference/paths.scrbl index a2f2134e32..6ae38c4663 100644 --- a/pkgs/racket-doc/scribblings/reference/paths.scrbl +++ b/pkgs/racket-doc/scribblings/reference/paths.scrbl @@ -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 diff --git a/pkgs/racket-test-core/tests/racket/pathlib.rktl b/pkgs/racket-test-core/tests/racket/pathlib.rktl index 14b7226a20..563ea863b2 100644 --- a/pkgs/racket-test-core/tests/racket/pathlib.rktl +++ b/pkgs/racket-test-core/tests/racket/pathlib.rktl @@ -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") diff --git a/racket/collects/racket/path.rkt b/racket/collects/racket/path.rkt index 54becc09b4..8281f6a0d1 100644 --- a/racket/collects/racket/path.rkt +++ b/racket/collects/racket/path.rkt @@ -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])))