~brown121407/f.scm

965e86688df29ba68ba3c68f16d33fa5cc5f1f3a — Xinglu Chen 1 year, 3 months ago da553c5
Add replace-extension.

* f.scm (no-extension): New procedure.
(replace-extension): Likewise.
1 files changed, 20 insertions(+), 1 deletions(-)

M f.scm
M f.scm => f.scm +20 -1
@@ 17,6 17,7 @@
  #:use-module ((ice-9 binary-ports) #:prefix i9:)
  #:use-module ((ice-9 textual-ports) #:prefix i9:)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 regex)
  #:use-module ((f ports) #:prefix p:)
  #:use-module ((f re-exports) #:prefix re:)
  #:export (read-bytes


@@ 31,7 32,9 @@
            mkdir
            delete
            traverse
            copy)
            copy
            no-extension
            replace-extension)
  #:re-export (chown
               chmod
               (rename-file . move)


@@ 169,3 172,19 @@ appending a #t at the end."))))))
                (throw 'f.scm "Can't copy: directory not empty. Try with making the call recursive
by appending a #t at the end."))))
      (copy-file src dest)))

(define (no-extension file)
  "Return FILE path without extension."
  (let ((without-ext (string-match "(.+)\\..*" (basename file))))
    (if without-ext
        (regexp-substitute #f without-ext 1)
        file)))

(define (replace-extension file ext)
  "Replace file extension in FILE with EXT.
EXT can include or exclude the beginning \".\"."
  (let ((ext (cond
              ((string-null? ext) "")
              ((string-match "^\\..+" ext) ext)
              (else (string-append "." ext)))))
    (string-append (no-extension file) ext)))