File: split.lisp

package info (click to toggle)
mcvs 1.0.13-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 668 kB
  • ctags: 648
  • sloc: lisp: 5,091; ansic: 223; sh: 190; makefile: 58
file content (44 lines) | stat: -rw-r--r-- 1,740 bytes parent folder | download | duplicates (2)
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
;;; This source file is part of the Meta-CVS program, 
;;; which is distributed under the GNU license.
;;; Copyright 2002 Kaz Kylheku

(provide "split")

(defun split-fields (in-string delim-char-bag)
"Split string into rigid fields based on delimiter characters. 
Each individual delimiter character separates two fields.
Example:  (split-fields \":c:#a\" \":#\") ==> (\"\" \"c\" \"\" \"a\")"
  (let (list (tok-start 0))
    (if (= (length delim-char-bag) 1)
      (let ((delim (aref delim-char-bag 0)))
	(dotimes (index (length in-string)) 
	  (when (char= delim (char in-string index))
	    (push (subseq in-string tok-start index) list)
	    (setf tok-start (1+ index)))))
      (dotimes (index (length in-string)) 
	(when (find (char in-string index) delim-char-bag :test #'char=)
	    (push (subseq in-string tok-start index) list)
	    (setf tok-start (1+ index)))))
    (push (subseq in-string tok-start) list)
    (nreverse list)))

(defun split-words (in-string delim-char-string)
"Munge sequences of delimiter characters. The pieces
in between are returned as a list of strings.
Example:  (split-words \" a b cde f \"  \" \") ==> (\"a\" \"b\" \"cde\" \"f\")"
  (let (list (token "") (state :parsing-delim))
    (dotimes (i (length in-string))
      (let ((ch (aref in-string i)))
        (if (not (find ch delim-char-string))
          (progn (setf token (format nil "~a~a" token ch))
                 (setf state :parsing-token))
          (when (eq state :parsing-token)
            (push token list)
            (setf token "")
            (setf state :parsing-delim)))))
    (when (not (equal token ""))
      (push token list))
    (nreverse list)))

#| (time (dotimes (x 1000) (split-fields "aaa:bb:ccc:ddd:eee" ":"))) |#