-
Notifications
You must be signed in to change notification settings - Fork 14
/
strings.lisp
40 lines (35 loc) · 1.53 KB
/
strings.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
(in-package :academy)
(defun split-string-into-lines (string)
"Return a list containing the lines from STRING split on the end of each line"
(with-input-from-string (stream string)
(loop for line = (read-line stream nil)
while line
collect line)))
(defun split-string (delimiter string)
"Return a list containing the strings from STRING split on DELIMITER."
(loop with rtn
with acc
for char across string
if (char= delimiter char)
do (push (coerce (nreverse acc) 'string) rtn)
(setf acc nil)
else do (push char acc)
finally (return (nreverse (cons (coerce (nreverse acc) 'string) rtn)))))
(defun print-list-delimited (list &optional (stream *standard-output*) (printer #'prin1) (delimiter ","))
"Output LIST to STREAM as a comma separated listing."
(loop for els on list
do (funcall printer (car els) stream)
(unless (null (cdr els))
(princ delimiter stream))))
(defun string-starts-with (string prefix &key (test #'char=))
"Returns true if STRING starts with PREFIX."
(let ((mismatch (mismatch prefix string :test test)))
(or (not mismatch) (= mismatch (length prefix)))))
(defun string-ends-with (string suffix &key (test #'char=))
"Returns true if STRING ends with PREFIX."
(let ((mm 0))
(loop for end1 from (1- (length string)) downto 0
for end2 from (1- (length suffix)) downto 0
while (funcall test (aref string end1) (aref suffix end2))
do (incf mm))
(= mm (length suffix))))