0.pre8.69
[sbcl/lichteblau.git] / contrib / sb-bsd-sockets / split.lisp
blobfec708c9a223bba0105142f0374ec05105848748
1 (in-package :sb-bsd-sockets)
3 ;;; This courtesy of Pierre Mai in comp.lang.lisp 08 Jan 1999 00:51:44 +0100
4 ;;; Message-ID: <87lnjebq0f.fsf@orion.dent.isdn.cs.tu-berlin.de>
6 (defun split (string &optional max (ws '(#\Space #\Tab)))
7 "Split `string' along whitespace as defined by the sequence `ws'.
8 The whitespace is elided from the result. The whole string will be
9 split, unless `max' is a non-negative integer, in which case the
10 string will be split into `max' tokens at most, the last one
11 containing the whole rest of the given `string', if any."
12 (flet ((is-ws (char) (find char ws)))
13 (loop for start = (position-if-not #'is-ws string)
14 then (position-if-not #'is-ws string :start index)
15 for index = (and start
16 (if (and max (= (1+ word-count) max))
17 nil
18 (position-if #'is-ws string :start start)))
19 while start
20 collect (subseq string start index)
21 count 1 into word-count
22 while index)))