replace split function - cl-yag - Common Lisp Yet Another website Generator | |
git clone git://bitreich.org/cl-yag/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit 25582ad800216c04f8f575ccb0e0a099a7897535 | |
parent f586103e1a32e6e2b5b3891275218c0e7400bc0a | |
Author: Solene Rapenne <[email protected]> | |
Date: Tue, 28 Nov 2017 07:21:33 +0100 | |
replace split function | |
Diffstat: | |
M generator.lisp | 27 +++++++++++++++------------ | |
1 file changed, 15 insertions(+), 12 deletions(-) | |
--- | |
diff --git a/generator.lisp b/generator.lisp | |
@@ -15,17 +15,20 @@ | |
while pos))) | |
;; common-lisp don't have a split string function natively | |
-;; thanks https://gist.github.com/siguremon/1174988 | |
-(defun split-str-1 (string &optional (separator " ") (r nil)) | |
- (let ((n (position separator string | |
- :from-end t | |
- :test #'(lambda (x y) | |
- (find y x :test #'string=))))) | |
- (if n | |
- (split-str-1 (subseq string 0 n) separator (cons (subseq string (1+ n)… | |
- (cons string r)))) | |
-(defun split-str (string &optional (separator " ")) | |
- (split-str-1 string separator)) | |
+(defun split-str(text &optional (separator #\Space)) | |
+ "this function split a string with separator and return a list" | |
+ (let ((text (concatenate 'string text (string separator)))) | |
+ (loop for char across text | |
+ counting char into count | |
+ when (char= char separator) | |
+ collect | |
+ ;; we look at the position of the left separator from right to left | |
+ (let ((left-separator-position (position separator text :from-end t … | |
+ (subseq text | |
+ ;; if we can't find a separator at the left of the current… | |
+ ;; the string | |
+ (if left-separator-position (+ 1 left-separator-position) … | |
+ (- count 1)))))) | |
;; we have to remove the quotes | |
;; when using collect in a loop | |
@@ -225,4 +228,4 @@ | |
(create-gopher-hole))) | |
(generate-site) | |
- | |
+(quit) |