DSSSL Library Reference: Part of the Modular DocBook Stylesheet distribution
Prev |  | Next

# split

## Name

split -- Splits string at whitespace and returns the resulting list of tokens

## Synopsis

    (split str #!optional (whitespace '(#\space)))

## Description

Given a string containing delimited tokens, return a list of the tokens in string form.

`str`

  The string to split.

`whitespace`

  A list of characters that should be treated as whitespace.

## Author

David Megginson, <dmeggins@uottawa.ca>

## Source Code

    (define (split str #!optional (whitespace '(#\space)))
      ;; Splits string at whitespace and returns the resulting list of tokens
      (let loop ((characters (string->list str)) ; Top-level recursive loop.
    	     (current-word '())
    	     (tokens '()))

        ; If there are no characters left,
        ; then we're done!
        (cond ((null? characters)
    	   ; Is there a token in progress?
    	   (if (null? current-word)
    	       (reverse tokens)
    	       (reverse (cons (list->string (reverse current-word))
    			      tokens))))
    	  ; If there are characters left,
    	  ; then keep going.
    	  (#t
    	   (let ((c (car characters))
    		 (rest (cdr characters)))
    	     ; Are we reading a space?
    	     (cond ((member c whitespace)
    		    (if (null? current-word)
    			(loop rest '() tokens)
    			(loop rest
    			      '()
    			      (cons (list->string (reverse current-word))
    				    tokens))))
    		   ; We are reading a non-space
    		   (#t
    		    (loop rest (cons c current-word) tokens))))))))

Prev | Home | Next
string-with-space | Up | strip