[plt-scheme] Filename-extension bug in hidden dirs?
On Thu, 25 May 2006, Paulo J. Matos wrote:
> Hi all,
>
> Contrary to what's supposed (plt-scheme 301.14):
>> (filename-extension "/home/pmatos/.ooffice2/profile/")
> #"ooffice2/profile/"
>
> Since it is a directory it should return #f. Probably it is getting
> messed up by the . which means hidden dirs in linux. Probably a bug,
> right?
Yeah; I think this should be using split-path to avoid weirdness. So
rather than:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; name can be any string; we just look for a dot
(define filename-extension
(lambda (name)
(unless (path-string? name)
(raise-type-error 'filename-extension "path or string" name))
(let ([name (if (path? name)
(path->bytes name)
name)])
(let ([m (regexp-match #rx#"[.]([^.]+)$" name)])
(and m
(cadr m))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Something like:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module fixed-filename-extension mzscheme
(provide filename-extension)
(define filename-extension
(lambda (name)
(unless (path-string? name)
(raise-type-error 'filename-extension "path or string" name))
(let-values ([(base name must-be-dir?)
(split-path name)])
(and (not must-be-dir?)
(let ([m (regexp-match #rx#"[.]([^.]+)$"
(path->bytes name))])
(and m
(cadr m))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
will behave better:
;;;;;;
> (filename-extension "foo.scm")
#"scm"
> (filename-extension "/home/pmatos/.ooffice2/profile/")
#f
;;;;;;
I'll send this off to bugs.plt-scheme.org. Best of wishes!