-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.lisp
232 lines (170 loc) · 5.39 KB
/
string.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
(in-package :arc-compat.internal)
(in-readtable :common-lisp)
(in-suite arc-compat)
;;; String operations
;;;============================================================================
;;; This software is copyright (c) Paul Graham and Robert Morris. Permission
;;; to use it is granted under the Perl Foundations's Artistic License 2.0.
;;;
;;; Ported to Common Lisp by CHIBA Masaomi.
(def whitec (c)
"Predicate to test if a character is whitespace
(space, newline, tab, or return)."
(in c #\space #\newline #\tab #\return))
(tst whitec
(== (whitec #\tab)
t)
(== (whitec " ")
nil))
(def nonwhite (c)
"Predicate to test if a character is not whitespace (space, newline, tab,
or return)."
(no (whitec c)))
(tst nonwhite
(== (nonwhite #\tab)
nil)
(== (nonwhite #\a)
t))
(def letter (c) (cl:or (<= #\a c #\z) (<= #\A c #\Z)))
(def digit (c) (<= #\0 c #\9))
#|(def alphadig (c)
"Predicate to test if a character is alphabetic or a digit."
(or (letter c) (digit c)))|#
(defalias alphadig cl:alphanumericp
"Predicate to test if a character is alphabetic or a digit.")
(tst alphabetic
(== (alphadig #\A)
t )
(== (alphadig #\2)
t ))
(def punc (c)
"Predicate to detemine if c is a punctuation character in the set: .,;:!?"
(in c #\. #\, #\; #\: #\! #\?))
(tst punc
(== (punc #\.)
t )
(== (punc #\a)
nil )
(== (punc ".")
nil ))
(def downcase (x)
"Converts a string, character, or symbol to lower case. This only converts
ASCII; Unicode is unchanged."
(case (type x)
string (map #'cl:char-downcase x)
char (cl:char-downcase x)
sym (sym (map #'char-downcase (coerce x 'string)))
(err "Can't downcase" x)))
(tst downcase
(== (downcase "abcDEF123")
"abcdef123" )
(== (downcase #\A)
#\a )
(== (downcase '|abcDEF123|)
'|abcdef123| ))
(def upcase (x)
"Converts a string, character, or symbol to lower case. This only converts
ASCII; Unicode is unchanged."
(case (type x)
string (map #'cl:char-upcase x)
char (cl:char-upcase x)
sym (sym (map #'cl:char-upcase (coerce x 'string)))
(err "Can't upcase" x)))
(tst upcase
(== (upcase "abcDEF123")
"ABCDEF123" )
(== (upcase #\a)
#\A )
(== (upcase '|abcDEF123|)
'ABCDEF123 ))
(def ellipsize (str (o limit 80))
"If str is longer than the limit (default 80), truncate it and append
ellipses ('...')."
(cl:if (<= (len str) limit)
str
(+ (cut str 0 limit) "...")))
(tst ellipsize
(== (ellipsize "Too long" 6)
"Too lo..."))
(def rand-string (n)
"Generates a random string of alphanumerics of length n."
(let c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
(with (nc 62 s (newstring n) i 0 salt (get-internal-real-time))
(while (< i n)
(let x (mod (+ salt (cl:random 256)) 256)
(unless (> x 247)
(setf (elt s i) (elt c (mod x nc)))
(++ i))))
s)))
(tst rand-string
(== T (let str (rand-string 6)
(cl:and (is 6 (len str))
(all #'alphadig str)))))
(def string args
"Converts the args into a string. The args must be coerce-able to a string."
(apply #'+
(make-string 0)
(map (fn (_) (coerce _ 'string))
(rem nil args))))
(tst string
(== (string 2 'a '(#\b #\c))
"2Abc")
(== (string 2 'a #\a #\b)
"2Aab"))
(def recstring (test s (o start 0))
"Recursively steps through the string until f returns a non-nil value, and
returns that value. Returns nil otherwise. The values passed to f are integer
indices; the indices start at 0, or start if specified."
(let n (len s)
n ;ignore
(funcall
(afn (i)
(cl:and (< i (len s))
(cl:or (funcall test i)
(self (+ i 1)))))
start)))
(tst recstring
(== (let str "abcde"
(recstring
(fn (idx) (cl:if (is (cl:char str idx) #\c) (+ 10 idx)))
str ))
12 ))
(cl:defvar .bar*. " | ")
(define-symbol-macro bar* .bar*.)
(mac w/bars body
(w/uniq (out needbars)
`(let ,needbars nil
(do ,@(map (fn (e)
`(let ,out (tostring ,e)
(unless (is ,out "")
(cl:if ,needbars
(pr bar* ,out)
(do (set ,needbars t)
(pr ,out))))))
body)))))
(tst w/bars
(== (with-output-to-string (*standard-output*)
(w/bars (pr "a") 42 (pr "b") (pr "c") nil))
"a | b | c")
(let bar* " - "
(== (with-output-to-string (*standard-output*)
(w/bars (pr "a") 42 (pr "b") (pr "c") nil))
"a - b - c")))
(def headmatch (pat seq (o start 0))
(let p (len pat)
(funcall (afn (i)
(cl:or (is i p)
(cl:and (is (ref pat i) (ref seq (+ i start)))
(self (+ i 1)))))
0)))
(def subst (new old seq)
(let boundary (+ (- (len seq) (len old)) 1)
(tostring
(forlen i seq
(cl:if (cl:and (< i boundary) (headmatch old seq i))
(do (++ i (- (len old) 1))
(pr new))
(pr (ref seq i)))))))
;;; strings.arc library
;;;============================================================================
;;; eof