fork(1) download
  1. ; your code goes here
  2. (defun match (pattern string)
  3. (let ((i 0)
  4. (len (length string))
  5. star)
  6. (loop
  7. for c across pattern
  8. do
  9. (when (= i len)
  10. (return-from match))
  11. (case c
  12. (#\* (setf star t))
  13. (#\? (incf i))
  14. (otherwise (if star
  15. (loop while (and (< i len) (not (eql c (elt string i))))
  16. do (incf i)
  17. finally (incf i) (setf star nil))
  18. (if (eql c (elt string i))
  19. (incf i)
  20. (return-from match))))))
  21. (= i len)))
  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~]~%" (eql r x) s r))
  30.  
Success #stdin #stdout 0.01s 10536KB
stdin
Standard input is empty
stdout
OK "abcd": expected false
OK "aabcd": expected true
OK "a?bcd": expected true
OK "aa?bxcxd": expected true
OK "aa?bxcd": expected true