-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsymbol-munger.lisp
308 lines (264 loc) · 10.9 KB
/
symbol-munger.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
(defpackage :symbol-munger
(:use :cl :cl-user :iter)
(:export :normalize-capitalization-and-spacing
:english->lisp-symbol
:english->lisp-name
:english->keyword
:english->camel-case
:english->studly-case
:english->underscores
:lisp->english
:lisp->keyword
:lisp->camel-case
:lisp->underscores
:lisp->studly-caps
:combine-symbols
:reintern
:qualified-symbol-string
:camel-case->english
:camel-case->lisp-name
:camel-case->lisp-symbol
:camel-case->keyword
:camel-case->underscores
:underscores->english
:underscores->lisp-name
:underscores->lisp-symbol
:underscores->keyword
:underscores->camel-case
:underscores->studly-caps
))
(in-package :symbol-munger)
(defgeneric %coerce-to-string (s)
(:documentation "This method can be specialized to help turn objects into
strings so they can be combined and normalized correctly")
(:method (s)
(typecase s
(symbol (symbol-name s))
(string s)
(float (format nil "~F" s))
(t (princ-to-string s)))))
(defmacro ensure-list! (place)
`(setf ,place (alexandria:ensure-list ,place)))
(defmacro ensure-flat-list! (place)
`(setf ,place (alexandria:flatten ,place)))
(defun qualified-symbol-string (sym)
(let ((*package* (find-package :keyword))
(*print-pretty* nil))
(format nil "~S" sym)))
(defun normalize-capitalization-and-spacing
(s &key (capitalize :each-word) (word-separators #\space)
word-separators-to-replace stream in-place)
"Will recapitalize a string and replace word-separators with a standard one
(in-place if desired and possible)
If s is a lisp tree, then each part will be %coerce-to-string'ed and treated
as a separate part of the phrase being normalized
Will write to a stream if given otherwise it.
Defaults to capitalizing each word but can be any of
{:each-word :first-word T (:all is an alias for T) nil :but-first-word (likeJavaScript) }
word-separators are used to distinguish new words for the purposes of capitalization
The first of these will be used to replace word-separators-to-replace (auto flattened)
word-separators-to-replace helps normalize word separators so that spaces or underscores
become the appropriate word-separator.
If this eql :capitals it assumes capital letters indicate a new word separation
(auto flattened)
returns a string (new or the one passed in if in-place) unless :stream is provided"
;; Check and enforce our assumptions
(ecase capitalize ((:each-word :first-word :but-first-word T :all nil) T))
(when (and in-place (member :capitals word-separators-to-replace))
(error "in-place replacement is not available for word separators which take no space (such as :capitals)"))
(ensure-flat-list! word-separators)
(ensure-flat-list! word-separators-to-replace)
(let ((str (or stream (unless in-place
(make-string-output-stream))))
(replacement-sep (let ((it (first word-separators)))
(typecase it
(string (if (= 1 (length it))
(elt it 0)
it))
(t it))))
(just-wrote-separator? nil))
(labels ((%write (c)
(etypecase c
(character (write-char c str))
(string (write-string c str))
(symbol (write-string (symbol-name c) str))))
(write-c (c)
(cond ((string= c replacement-sep)
(unless just-wrote-separator?
(setf just-wrote-separator? t)
(%write c)))
(t
(setf just-wrote-separator? nil)
(%write c)))))
(iter (for part in (alexandria:flatten s))
(for source-string = (%coerce-to-string part))
(for start-of-phrase? = (first-iteration-p))
(iter
(for c in-string source-string)
(for last-c previous c)
(for i from 0)
(for is-cap? = (eql c (char-upcase c)))
(setf start-of-phrase? (and start-of-phrase? (first-iteration-p)))
(for start-of-word? =
(or (first-iteration-p)
(and is-cap? (member :capitals word-separators-to-replace))
(and is-cap? (member :capitals word-separators))
;; the last char we wrote was some kind of separator
(member last-c word-separators-to-replace :test #'string-equal)
(member last-c word-separators :test #'string-equal)))
;; handle capital letters as word-separators
(when (and str ;; in-place will not work
replacement-sep ;; need to have a separator
(not start-of-phrase?) ;; dont start a string with a sep
;; put separators before new words
start-of-word?)
(write-c replacement-sep))
(for should-cap? =
(or (eq capitalize :all)
(eq capitalize T)
(and start-of-word?
(or (eq capitalize :each-word)
(if (first-iteration-p)
(eq capitalize :first-word)
(eq capitalize :but-first-word))))))
(for char = (cond
((member c word-separators-to-replace :test #'string-equal)
(or replacement-sep (next-iteration)))
(should-cap? (char-upcase c))
(T (char-downcase c))))
(when in-place (setf (elt source-string i) char))
(when str (write-c char)))))
(cond ((not stream) (get-output-stream-string str))
(in-place s))))
(defun english->lisp-name (phrase &key stream capitalize)
"Turns an english phrase into a string containing a common lisp style symbol-name"
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators (list #\-)
:word-separators-to-replace (list #\_ #\space #\newline #\tab)))
(defun english->lisp-symbol (phrase &optional (package *package*))
"Turns an english phrase into a common lisp symbol in the specified package"
(intern (english->lisp-name phrase :capitalize T) package))
(defun english->keyword (phrase)
"Turns an english phrase into a common lisp keyword"
(english->lisp-symbol phrase :keyword))
(defun english->camel-case (phrase &key stream
(capitalize :but-first-word))
"Turns an english phrase into a camelCasePhraseLikeThis "
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators nil
:word-separators-to-replace (list #\_ #\space)))
(defun english->studly-case (phrase &key stream)
"Turns an english phrase into a CamelCasePhraseLikeThis"
(english->camel-case phrase :stream stream :capitalize :each-word))
(defun english->underscores (phrase &key stream capitalize)
"Turns an english phrase into a a_phrase_like_this"
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\_
:word-separators-to-replace (list #\space)))
(defun lisp->english (phrase &key stream
(capitalize :each-word)
(word-separator #\space))
"Converts a common lisp symbol (or symbol-name) into an english phrase"
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators word-separator
:word-separators-to-replace (list #\-)))
(defun lisp->keyword (phrase)
(combine-symbols phrase :package :keyword))
(defun reintern (phrase &optional (package *package*))
;; never reintern nil
(when phrase
(combine-symbols phrase :package package)))
(defun combine-symbols (phrase &key (package *package*) (separator #\-))
;; never reintern nil
(when phrase
(intern
(normalize-capitalization-and-spacing
phrase
:capitalize T
;; these are flattened so if nil it will just use #\-
:word-separators separator
:word-separators-to-replace nil)
package)))
(defun lisp->camel-case (phrase &key stream)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize :but-first-word
:word-separators nil
:word-separators-to-replace (list #\-)))
(defun lisp->studly-caps (phrase &key stream)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize :each-word
:word-separators nil
:word-separators-to-replace (list #\-)))
(defun lisp->underscores (phrase &key stream capitalize)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\_
:word-separators-to-replace (list #\-)))
(defun camel-case->english (phrase &key stream
(capitalize :each-word))
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\space
:word-separators-to-replace (list :capitals #\_)))
(defun camel-case->lisp-name (phrase &key stream
(capitalize nil))
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\-
:word-separators-to-replace (list :capitals #\_)))
(defun camel-case->lisp-symbol (phrase &optional (package *package*))
(intern (camel-case->lisp-name phrase :capitalize T) package))
(defun camel-case->keyword (phrase)
(camel-case->lisp-symbol phrase :keyword))
(defun camel-case->underscores (phrase &key stream capitalize)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\_
:word-separators-to-replace (list :capitals #\space)))
;;;;;
(defun underscores->english (phrase &key stream
(capitalize :each-word))
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\space
:word-separators-to-replace (list #\_)))
(defun underscores->lisp-name (phrase &key stream
(capitalize nil))
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize capitalize
:word-separators #\-
:word-separators-to-replace (list #\_)))
(defun underscores->lisp-symbol (phrase &optional (package *package*))
"Turns a_phrase_with_underscores into a-phrase-with-underscores lisp symbol"
(intern (underscores->lisp-name phrase :capitalize T) package))
(defun underscores->keyword (phrase)
"Converts and underscores name to a common lisp keyword"
(underscores->lisp-symbol phrase :keyword))
(defun underscores->camel-case (phrase &key stream)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize :but-first-word
:word-separators nil
:word-separators-to-replace (list #\_)))
(defun underscores->studly-caps (phrase &key stream)
(normalize-capitalization-and-spacing
phrase
:stream stream :capitalize :each-word
:word-separators nil
:word-separators-to-replace (list #\_)))