Bah, speed up list-to-string by a few factors...
[clinton/parenscript.git] / src / utils.lisp
1 (in-package :js)
2
3 (defun list-join (list elt)
4 (let (res)
5 (dolist (i list)
6 (push i res)
7 (push elt res))
8 (pop res)
9 (nreverse res)))
10
11 (defun list-to-string (list)
12 (with-output-to-string (str)
13 (dolist (el list)
14 (write-string el str))))
15
16 (defun append-to-last (form elt)
17 (cond ((stringp form)
18 (concatenate 'string form elt))
19 ((consp form)
20 (let ((last (last form)))
21 (if (stringp (car last))
22 (rplaca last (concatenate 'string (car last) elt))
23 (append-to-last (car last) elt))
24 form))
25 (t (error "unsupported form ~S" form))))
26
27 (defun prepend-to-first (form elt)
28 (cond ((stringp form)
29 (concatenate 'string elt form))
30 ((consp form)
31 (let ((first (first form)))
32 (if (stringp first)
33 (rplaca form (concatenate 'string elt first))
34 (prepend-to-first first elt))
35 form))
36 (t (error "unsupported form ~S" form))))
37
38 (defun string-join (strings elt)
39 (list-to-string (list-join strings elt)))
40
41 (defun val-to-string (val)
42 (cond ((stringp val) val)
43 ((symbolp val) (string-downcase (symbol-name val)))
44 (t (princ-to-string val))))
45
46 (defun string-split (string separators)
47 (do ((len (length string))
48 (i 0 (1+ i))
49 (last 0)
50 res)
51 ((= i len)
52 (nreverse (if (> i last)
53 (cons (subseq string last i) res)
54 res)))
55 (when (member (char string i) separators)
56 (push (subseq string last i) res)
57 (setf last (1+ i)))))
58
59