utils.lisp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (in-package :cl-user)
  2. (defpackage #:assboard.utils
  3. (:use :cl)
  4. (:export
  5. #:+project-path+
  6. #:starts-with
  7. #:replace-all))
  8. (in-package :assboard.utils)
  9. (defmacro aget (key alist)
  10. `(cdr (assoc ,key ,alist :test #'equal)))
  11. (defmethod yason:encode ((symbol symbol) &optional (stream *standard-output*))
  12. (yason:encode (s-sql:to-sql-name symbol) stream))
  13. (defparameter +project-path+
  14. (asdf:component-pathname (asdf:find-system '#:assboard)))
  15. (defun starts-with (with-what str)
  16. (let ((len (min (length str) (length with-what))))
  17. (equal (subseq str 0 len)
  18. with-what)))
  19. (defun replace-all (string part replacement &key (test #'char=))
  20. "Returns a new string in which all the occurences of the part
  21. is replaced with replacement."
  22. (with-output-to-string (out)
  23. (loop with part-length = (length part)
  24. for old-pos = 0 then (+ pos part-length)
  25. for pos = (search part string
  26. :start2 old-pos
  27. :test test)
  28. do (write-string string out
  29. :start old-pos
  30. :end (or pos (length string)))
  31. when pos do (write-string replacement out)
  32. while pos)))
  33. (defun vec-to-hash (vec key-fn)
  34. (loop for val across vec
  35. with result = (make-hash-table :test #'equal :size (length vec))
  36. do (setf (gethash (funcall key-fn val) result) val)
  37. finally (return result)))