-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.lisp
131 lines (103 loc) · 4.32 KB
/
string.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(in-package :celwk)
#| http://www.lispworks.com/documentation/HyperSpec/Body/f_wr_stg.htm
MutableStrings:
(with-output-to-string (*standard-output*)
(write-string "books" nil :end 3)
(write-string "worms" *standard-output* :start 1))
=> "boo" + "orms" = "booorms"
write-string /write-line write-char
(with-output-to-string (*standard-output*)
(output "What"))
== (input "What")
|#
;; (=> (cmd "node -e 'console.log(JSON.stringify(process.env))'")
;; (string-trim '(#\Newline) ~)
;; (parse-json ~))
(defun trim (string)
(string-trim '(#\Newline #\Space #\Tab #\Return) string))
(defmacro read-output-string (&body outputs)
`(trim (with-output-to-string (*standard-output*)
,@outputs)))
(defun split-trim (string &optional (spliter "/"))
(delete "" (split spliter string) :test #'string=))
(defun to-simple-string (string)
(input "~a" string))
(defmacro insure-string (string)
`(unless (string? ,string)
(setf ,string (write-to-string ,string))))
(defmacro concats (&rest types)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@(mapcar (^(type)
`(defun ,(read-from-format "concat-~s" type) (&rest values)
(apply #'concatenate (cons ',type values))))
types)))
;; (mac (concats string list vector))
(concats string list vector)
(<=> concat-string concat)
(<=> concat-string string+)
(defun empty-string? (string)
(= (length string) 0))
(defun string-or-nil (value)
(when (string? value) value))
(defun string-join (string-list &optional (separator ""))
"The list must consist of Strings, return NIL if string-list is NIL"
(unless string-list
(return-from string-join nil))
(with-output-to-string (*standard-output*)
(loop (princ (pop string-list))
(unless string-list (return))
(princ separator))))
;; (let ((result (first string-list)))
;; (dolist (string (rest string-list) result)
;; (setf result (concat-string result separator string)))))
(defun string-repeat (string n)
"Create a string repeated n times"
(string-join (fill (make-list n) string)))
(defun string-of-code (code)
"Return lowercase string from the code
(read-from-string ...) useful as well"
(string-downcase (write-to-string code)))
;; write-to-string symbol => string
;; read-from-string string => symbol
;; (eval (read-from-string "(+ 1 2 3)")) => 6
;; (equal '(+ 1 2 3) (read-from-string "(+ 1 2 3)")) t !!!
(defmethod aesthetic (sth)
(input "~a" sth))
(defun wrapped-code (code &optional (seperator "=") (count 20))
(input "~A~%~A~%~2:*~A"
(string-repeat seperator count)
(string-of-code code)))
(defun join (string-list
&key (seperator " ") (out nil) (fn #'identity)
&aux (seperator (regex-replace-all "~" seperator "~~")))
"Join the string list"
(format out
(input "~~{~~A~~^~A~~}" seperator) (mapcar fn string-list)))
;; (@replace-all "([a-z])@1" "~@1~" "I need yooooo..")
;; (defmacro @replace-all (reg to str)
;; (flet ((recover (regular)
;; (let ((rreg (regex-replace-all "(?<!\\\\)@(?=\\d)" regular "\\")))
;; (regex-replace-all "\\\\@" rreg "@"))))
;; `(regex-replace-all ,(recover reg) ,str ,(recover to))))
(defmacro @replex (reg to str &key single)
"@1 @2... match (...) "
(flet ((recover (regular)
(regex-replace-all "\\\\@"
(regex-replace-all "(?<!\\\\)@(?=\\d)" regular "\\")
"@")))
(list (if single 'regex-replace 'regex-replace-all) (recover reg) str (recover to))))
(defun @reglax (reg to str &key single)
"@1 @2... match (...) regex-replace-all "
(flet ((recover (regular)
(regex-replace-all "\\\\@"
(regex-replace-all "(?<!\\\\)@(?=\\d)" regular "\\")
"@")))
(call (if single #'regex-replace #'regex-replace-all) (recover reg) str (recover to))))
;;(momerize-curry test (@reglax "([a-z0-9])\\1" "[\\1]"))
(defun replace-string (string sub to &key (all t) (from 0))
(let ((start (search sub string :start2 from)))
(if start
(setf string (concat-string (subseq string 0 start) to (subseq string (+ start (length sub))))))
(if (and start all)
(replace-string string sub to :all t :from (+ start (length to))) ;; In case of TO contains SUB
string)))