fork download
  1. (defun match (pattern string)
  2. (let ((i 0)
  3. (len (length string))
  4. star)
  5. (loop
  6. for c across pattern
  7. while (< i len)
  8. do
  9. (case c
  10. (#\* (setf star t))
  11. (#\? (incf i))
  12. (otherwise (if star
  13. (loop while (and (< i len) (not (eql c (elt string i))))
  14. do (incf i)
  15. finally (incf i) (setf star nil))
  16. (if (eql c (elt string i))
  17. (incf i)
  18. (return))))))
  19. (and (= i len) (>= len (length pattern)))))
  20.  
  21. (format t "~a ==> ~a ~%" "abcd" (match "a*?b*c*d" "abcd"))
  22.  
  23. (loop :for (s r) :on '("abcd" nil
  24. "aabcd" t
  25. "a?bcd" t
  26. "aa?bxcxd" t
  27. "aa?bxcd" t) :by #'cddr
  28. :for x := (match "a*?b*c*d" s) :do
  29. (format t "~:[FAIL~;OK~] ~:S: expected ~:[false~;true~]~%" (eq r x) s r))
Success #stdin #stdout 0.01s 10536KB
stdin
Standard input is empty
stdout
abcd ==> NIL 
OK "abcd": expected false
FAIL "aabcd": expected true
FAIL "a?bcd": expected true
OK "aa?bxcxd": expected true
FAIL "aa?bxcd": expected true