-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.lisp
55 lines (50 loc) · 1.63 KB
/
path.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(defpackage #:inga/path
(:use #:cl)
(:export #:merge-paths
#:get-variable-names
#:replace-variable-name))
(in-package #:inga/path)
(defun merge-paths (a b)
(cond
((and (uiop:string-suffix-p "/" a) (uiop:string-prefix-p "/" b))
(concatenate 'string a (subseq b 1)))
((and (not (uiop:string-suffix-p "/" a)) (not (uiop:string-prefix-p "/" b)))
(concatenate 'string a "/" b))
(t
(concatenate 'string a b))))
(defun get-variable-names (sequence)
(unless sequence (return-from get-variable-names))
(loop for c across sequence
with begin-token
with variable
with results
do
(alexandria:switch (c)
(#\{
(setf begin-token t))
(#\}
(setf results (append results (list variable)))
(setf variable nil)
(setf begin-token nil))
(t
(when begin-token
(setf variable (concatenate 'string variable (string c))))))
finally (return results)))
(defun replace-variable-name (path from to)
(loop for c across path
with begin-token
with variable
with result
do
(alexandria:switch (c)
(#\{
(setf begin-token t))
(#\}
(setf result (concatenate 'string result "{" (if (equal variable from) to variable) "}"))
(setf variable nil)
(setf begin-token nil))
(t
(if begin-token
(setf variable (concatenate 'string variable (string c)))
(setf result (concatenate 'string result (string c))))))
finally (return result)))