-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.lisp
53 lines (42 loc) · 2.09 KB
/
misc.lisp
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
(in-package #:org.shirakumo.markless)
(defmethod count-words ((string string) &optional (method :whitespace))
(count-words (parse string T) :whitespace))
(defmethod count-words ((pathname pathname) &optional (method :whitespace))
(count-words (parse pathname T) method))
(defmethod count-words ((component components:component) &optional (method :whitespace))
(count-words-by method component))
(defgeneric count-words-by (method thing)
(:method-combination +))
(defmethod count-words-by + (method (component components:component))
0)
(defmethod count-words-by + (method (component components:parent-component))
(loop for child across (components:children component)
sum (count-words-by method child)))
(defmethod count-words-by + (method (component components:text-component))
(count-words-by method (components:text component)))
(defmethod count-words-by :around (method (component components:instruction))
0)
(defmethod count-words-by :around (method (component components:comment))
0)
(defmethod count-words-by + ((method (eql :whitespace)) (string string))
(let ((count 0) (word-started-p NIL))
(flet ((commit ()
(when word-started-p
(incf count)
(setf word-started-p NIL))))
(loop for i from 0 below (length string)
for c = (char string i)
do (cond (#+sb-unicode (not (find (sb-unicode:general-category c) '(:Ll :Lo :Lm :Lt :Lu)))
#-sb-unicode (or (find c ".!?-_,;:<>(){}[]&")
(find c '(#\Space #\Tab #\Linefeed #\Return)))
(commit))
(T
(setf word-started-p T)))
finally (commit)))
count))
(defmethod count-words-by + ((method (eql :character)) (string string))
(loop for i from 0 below (length string)
for c = (char string i)
count #+sb-unicode (find (sb-unicode:general-category c) '(:Ll :Lo :Lm :Lt :Lu))
#-sb-unicode (not (or (find c ".!?-_,;:<>(){}[]&0123456789")
(find c '(#\Space #\Tab #\Linefeed #\Return))))))