find-first-char

Name

find-first-char -- Find the first occurance of a character in a string

Synopsis

(find-first-char string skipchars findchars #!optional (pos 0))

Description

Finds first character in string that is in findchars, skipping all occurances of characters in skipchars. Search begins at pos. If no such characters are found, returns -1.

If skipchars is empty, skip anything not in findchars If skipchars is #f, skip nothing If findchars is empty, the first character not in skipchars is matched It is an error if findchars is not a string. It is an error if findchars is empty and skipchars is not a non-empty string.

Author

Norman Walsh, <ndw@nwalsh.com>

Source Code

(define (find-first-char string skipchars findchars #!optional (pos 0))
  ;; Find the first occurance of a character in a string
  (let ((skiplist (if (string? skipchars)
		      (string->list skipchars)
		      '()))
	(findlist (string->list findchars)))
    (if (and (null? skiplist) (null? findlist))
	;; this is an error
	-2
	(if (or (>= pos (string-length string)) (< pos 0))
	    -1
	    (let ((ch (string-ref string pos)))
	      (if (null? skiplist) 
		  ;; try to find first
		  (if (member ch findlist)
		      pos
		      (if (string? skipchars)
			  (find-first-char string 
					   skipchars findchars (+ 1 pos))
			  -1))
		  ;; try to skip first
		  (if (member ch skiplist)
		      (find-first-char string skipchars findchars (+ 1 pos))
		      (if (or (member ch findlist) (null? findlist))
			  pos
			  -1))))))))