File: wildcard.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (40 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (4)
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
; Wildcard Pattern matching algorithm
; * matches any substring (zero or more characters)
; ? matches any character
; ~c matches c

(defun match (pattern list)
       (labels ((match1 (pattern suspect)
		      (cond ((null pattern) (null suspect))
			    ((null suspect) (equal pattern '(:mult)))
			    ((eq (first pattern) :single)
			     (match1 (cdr pattern) (cdr suspect)))
			    ((eq (first pattern) :mult)
			     (if (null (rest pattern))
				 t 
				 (do ((p (rest pattern))
				      (l suspect (cdr l)))
				     ((or (null l) (match1 p l)) 
				      (not (null l))))))
			    ((eq (first pattern) (first suspect))
			     (match1 (rest pattern) (rest suspect)))
			    (t nil)))
	      (explode (list) 
		       (cond ((null list) nil)
			     ((eq (first list) #\*) 
			      (cons :mult (explode (rest list))))
			     ((eq (first list) #\?) 
			      (cons :single (explode (rest list))))
			     ((eq (first list) #\~) 
			      (cons (second list)
				    (explode (rest (rest list)))))
			     (t (cons (first list) (explode (rest list)))))))
	     (let ((pat (explode (coerce pattern 'cons))))
		  (mapcan #'(lambda (x) (when (match1 pat (coerce x 'cons))
					      (list x)))
			  list))))

(setq l (sort (apply #'nconc (map 'cons 
			    #'(lambda (x) (mapcar #'string x)) 
			    *obarray*))
	      #'string<))