fork 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. while (< i len)
  9. do
  10. ;; (format t "~a [~d]~a ~%" c i (elt string i))
  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))))))
  21. (and (= 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 "~a ==> ~f ~%" s x)
  30. (format t "~:[FAIL~;OK~] ~:S: expected ~:[false~;true~]~%" (eql r x) s r))
  31.  
Success #stdin #stdout 0s 10488KB
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