summaryrefslogtreecommitdiff
path: root/src/utilities.lisp
blob: 539ad28777463d7bc9d4bf9fc89b3577e597e901 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;;; utilities -- nuff said

(in-package :dnd)


(let ((host (uiop:hostname))
      (count 0))
  (defun nuid ()
    "Generates a Nearly Universal ID"
    (format nil "~36r"
            (sxhash
             (list
              (incf count)
              host
              (get-universal-time))))))

(defun hash-string (plaintext salt)
  "Hash plaintext using SALT"
  (flexi-streams:octets-to-string 
   (ironclad:digest-sequence
    :sha3
    (flexi-streams:string-to-octets (concatenate 'string salt plaintext)
                                    :external-format :utf-8))
   :external-format :latin1))

(defparameter +user-nick-chars+ "0123456789abcdefghijklmnopqrstuvwxyz-._")

(defun/t valid-nick-p (nick)
  :tests
  (eql ("??????") nil)
  (eql ("⚔") nil)
  (eql ("cool_beans") t)
  (eql ("COOOL_BEANS") t)
  (eql ("COOL beans") nil)
  :end 
  (unless (zerop (length nick))
    (loop :for char :across nick
          :always (find char +user-nick-chars+
                        :test #'char-equal))))

(defun/t asciip (thing)
  "T if THING is an ASCII character, NIL otherwise."
  :tests
  (eql (#\x) t)
  (eql (#\ö) nil)
  (eql (#\nul) t)
  (eql (#\return) t)
  (eql (nil) nil)
  (eql ("foo") nil)
  :end
  (and (characterp thing)
       (<= 0 (char-code thing) 127)))

(defun/t urlify (string &optional (sub #\-))
  "Canonical transformation for strings that makes them appropriate for urls."
  :tests
  (equal ("THIS IS COOL") "this-is-cool")
  (equal ("This      is cool") "this-is-cool")
  (equal ("Mc'this is κoöl   ") "mc-this-is-o-l")
  :end
  (str:join
   sub
   (str:split-omit-nulls
    #\space
    (substitute-if-not
     #\space
     (a:conjoin #'asciip #'alphanumericp)
     (string-downcase string)))))

(eval-when (:compile-toplevel :load-toplevel :execute)
 (defun starts-with-vowel-p (string)
   (find (elt string 0) "aeiou" :test #'char-equal)))