summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXinglu Chen <public@yoctocell.xyz>2021-03-03 14:55:15 +0100
committerAlexandru-Sergiu <brown121407@posteo.ro>2021-03-03 21:58:49 +0200
commit965e86688df29ba68ba3c68f16d33fa5cc5f1f3a (patch)
tree88cc2168bca95e1b301073bc58c7a5aafafae43b
parentda553c5fcb9c3865a5e3a7506a02bb9314bb572d (diff)
Add replace-extension.
* f.scm (no-extension): New procedure. (replace-extension): Likewise.
-rw-r--r--f.scm21
1 files changed, 20 insertions, 1 deletions
diff --git a/f.scm b/f.scm
index f699d91..ffac4df 100644
--- a/f.scm
+++ b/f.scm
@@ -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)))