Commit c504f823c206d6e5a378df5c06ca17c9432391eb

Thomas de Grivel 2017-06-21T11:39:20

untabify

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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
diff --git a/pstr.lisp b/pstr.lisp
index eb4b7aa..9c168a7 100644
--- a/pstr.lisp
+++ b/pstr.lisp
@@ -26,8 +26,8 @@
   (let ((g!pstr (gensym "PSTR-")))
     `(let ((,g!pstr ,x))
        (let ((,str (pstr-str ,g!pstr))
-	     (,pos (pstr-pos ,g!pstr)))
-	 ,@body))))
+             (,pos (pstr-pos ,g!pstr)))
+         ,@body))))
 
 (defun pstr-copy (x)
   (declare (type pstr x))
@@ -35,7 +35,7 @@
 
 (defun pstr-eat-n (x n)
   (declare (type pstr x)
-	   (type positive-fixnum n))
+           (type positive-fixnum n))
   (when (< (length (pstr-str x)) (+ (pstr-pos x) n))
     (error "pstr-eat-n past end of string"))
   (incf (pstr-pos x) n)
@@ -45,7 +45,7 @@
   (with-pstr (s p) x
     (let ((i (+ p n)))
       (when (< i (length s))
-	(char s i)))))
+        (char s i)))))
 
 (defun pstr-eat-char (x char)
   (when (char= char (pstr-peek x))
@@ -55,22 +55,22 @@
 (defun pstr-eat-string (x string)
   (with-pstr (s p) x
     (let* ((len (length string))
-	   (e (+ p len)))
+           (e (+ p len)))
       (when (and (<= e (length s))
-		 (string= s string :start1 p :end1 e))
-	(pstr-eat-n x len)
-	string))))
+                 (string= s string :start1 p :end1 e))
+        (pstr-eat-n x len)
+        string))))
 
 (define-constant +hexdigits+ "0123456789ABCDEFabcdef"
   :test 'string=)
 
 (defun pstr-eat-pct-encoded (x)
   (when (and (char= #\% (pstr-peek x))
-	     (find (pstr-peek x 1) +hexdigits+)
-	     (find (pstr-peek x 2) +hexdigits+))
+             (find (pstr-peek x 1) +hexdigits+)
+             (find (pstr-peek x 2) +hexdigits+))
     (with-pstr (s p) x
       (prog1 (parse-integer s :start p :end (+ 2 p) :radix 16)
-	(pstr-eat-n x 3)))))
+        (pstr-eat-n x 3)))))
 
 (defun pstr-eat-if (n fn x)
   (when-let ((r (funcall fn (pstr-peek x))))
diff --git a/rol-uri.asd b/rol-uri.asd
index 0464ff2..6107253 100644
--- a/rol-uri.asd
+++ b/rol-uri.asd
@@ -29,9 +29,9 @@
   :version "0.1"
   :description "String and URI library"
   :depends-on ("alexandria"
-	       "babel"
-	       "cl-unicode"
-	       "str")
+               "babel"
+               "cl-unicode"
+               "str")
   :components
   ((:file "package")
    (:file "uri" :depends-on ("package"))
diff --git a/uri-template.lisp b/uri-template.lisp
index 5db835c..9b30c56 100644
--- a/uri-template.lisp
+++ b/uri-template.lisp
@@ -54,10 +54,10 @@
 
 (defclass uri-template ()
   ((parts :type list
-	  :initarg :parts
-	  :accessor uri-template-parts)
+          :initarg :parts
+          :accessor uri-template-parts)
    (expand-function :type function
-		    :reader uri-template-expand-function)))
+                    :reader uri-template-expand-function)))
 
 (defun uri-template-p (thing)
   "
@@ -74,8 +74,8 @@ at least one URI Template operator. Otherwise NIL is returned.
   (let ((pkg (find-package :RoL-uri.vars)))
     (when pkg
       (do-symbols (sym pkg)
-	(unexport sym pkg)
-	(unintern sym pkg)))
+        (unexport sym pkg)
+        (unintern sym pkg)))
     (defpackage :RoL-uri.vars
       (:nicknames :L>uri.vars :lowh.triangle.uri.vars)))
   "All uri variable names are interned in this package.")
@@ -93,21 +93,21 @@ at least one URI Template operator. Otherwise NIL is returned.
 (deftype uri-var ()
   "The type of all uri variables."
   `(and symbol
-	(satisfies uri-var-p)))
+        (satisfies uri-var-p)))
 
 (defmacro uri-let (bindings &body body)
   "Create dynamic bindings for URI variables and lexical bindings to
 access them in BODY."
   (let ((bindings (mapcar (lambda (b) (if (consp b) b (list b nil)))
-			  bindings)))
+                          bindings)))
     `(let ,(mapcar (lambda (b) `(,(uri-var (car b)) ,(cadr b)))
-		   bindings)
+                   bindings)
        (declare (special ,@(mapcar (lambda (b) (uri-var (car b)))
-				   bindings)))
+                                   bindings)))
        (symbol-macrolet ,(mapcar (lambda (b)
-				   `(,(car b) ,(uri-var (car b))))
-				 bindings)
-	 ,@body))))
+                                   `(,(car b) ,(uri-var (car b))))
+                                 bindings)
+         ,@body))))
 
 ;;  Read
 
@@ -115,40 +115,40 @@ access them in BODY."
   "Read a URI Template string as per RFC 6570 and return an instance of
 URI-TEMPLATE."
   (labels ((read/var (s)
-	     (cl-ppcre:register-groups-bind (name prefix explode)
-		 (#.(str "(" #1="(?:[0-9A-Za-z_]|%[0-9][0-9])"
-			 "(?:[.]?" #1# ")*)(?::(.*)|([*]))?$")
-		    s)
-	       (let ((sym (uri-var (string-upcase name))))
-		 (cond (prefix  `(prefix ,(parse-integer prefix) ,sym))
-		       (explode `(explode ,sym))
-		       (t       sym)))))
-	   (read/part (s)
-	     (cond ((char= #\{ (char s 0))
-		    (let ((start 1)
-			  (end (1- (length s))))
-		      (assert (char= #\} (char s end)))
-		      (let ((op (find (char s 1) +operator+)))
-			(when op (incf start))
-			(let ((vars (mapcar #'read/var
-					    (cl-ppcre:split "," s
-							    :start start
-							    :end end))))
-			  `(,op ,@vars)))))
-		   (t s))))
+             (cl-ppcre:register-groups-bind (name prefix explode)
+                 (#.(str "(" #1="(?:[0-9A-Za-z_]|%[0-9][0-9])"
+                         "(?:[.]?" #1# ")*)(?::(.*)|([*]))?$")
+                    s)
+               (let ((sym (uri-var (string-upcase name))))
+                 (cond (prefix  `(prefix ,(parse-integer prefix) ,sym))
+                       (explode `(explode ,sym))
+                       (t       sym)))))
+           (read/part (s)
+             (cond ((char= #\{ (char s 0))
+                    (let ((start 1)
+                          (end (1- (length s))))
+                      (assert (char= #\} (char s end)))
+                      (let ((op (find (char s 1) +operator+)))
+                        (when op (incf start))
+                        (let ((vars (mapcar #'read/var
+                                            (cl-ppcre:split "," s
+                                                            :start start
+                                                            :end end))))
+                          `(,op ,@vars)))))
+                   (t s))))
     (let (parts chars)
       (flet ((push-part ()
-	       (when chars
-		 (push (read/part (coerce (nreverse chars) 'string))
-		       parts)
-		 (setf chars nil))))
-	(dotimes (i (length string))
-	  (let ((c (char string i)))
-	    (cond
-	      ((char= #\{ c) (push-part) (push c chars))
-	      ((char= #\} c) (push c chars) (push-part))
-	      (t (push c chars)))))
-	(push-part))
+               (when chars
+                 (push (read/part (coerce (nreverse chars) 'string))
+                       parts)
+                 (setf chars nil))))
+        (dotimes (i (length string))
+          (let ((c (char string i)))
+            (cond
+              ((char= #\{ c) (push-part) (push c chars))
+              ((char= #\} c) (push c chars) (push-part))
+              (t (push c chars)))))
+        (push-part))
       (make-instance 'uri-template :parts (nreverse parts)))))
 
 ;;  Cache
@@ -161,8 +161,8 @@ URI-TEMPLATE."
 (defun uri-template (thing)
   (typecase thing
     (string (or (gethash thing *uri-templates*)
-		(setf (gethash thing *uri-templates*)
-		      (read-uri-template-from-string thing))))
+                (setf (gethash thing *uri-templates*)
+                      (read-uri-template-from-string thing))))
     (uri-template thing)))
 
 (defmethod make-load-form ((uri-template uri-template) &optional env)
@@ -181,24 +181,24 @@ URI-TEMPLATE."
 ;;  Eval
 
 (defun do-uri-template (template &key (parts #'list) (string #'identity)
-				      (op #'list) explode prefix
-				      (var #'identity))
+                                      (op #'list) explode prefix
+                                      (var #'identity))
   (labels ((do-var (x)
-	     (if (symbolp x)
-		 (funcall var x)
-		 (destructuring-bind (a b &optional c) x
-		   (ecase a
-		     ((explode) (if explode
+             (if (symbolp x)
+                 (funcall var x)
+                 (destructuring-bind (a b &optional c) x
+                   (ecase a
+                     ((explode) (if explode
                                     (let ((*separator* (explode-separator *op*)))
                                       (funcall explode (funcall var b)))
                                     (funcall var b)))
                      ((prefix) (if prefix
                                    (funcall prefix b (funcall var c))
                                    (funcall var c)))))))
-	   (do-part (part)
-	     (typecase part
-	       (string (when string (funcall string part)))
-	       (cons (destructuring-bind (op-char &rest vars) part
+           (do-part (part)
+             (typecase part
+               (string (when string (funcall string part)))
+               (cons (destructuring-bind (op-char &rest vars) part
                        (if op
                            (let ((*op* op-char)
                                  (*separator* (op-separator op-char)))
@@ -232,21 +232,21 @@ URI-TEMPLATE."
 
 (defmacro with-uri-vars (vars &body body)
   (let ((unbound-vars (gensym "UNBOUND-VARS-"))
-	(nils (gensym "NILS")))
+        (nils (gensym "NILS")))
     `(locally (declare (special ,@vars)) ;; (sb-ext:muffle-conditions warning)
        (let (,unbound-vars ,nils)
-	 (dolist (v ',vars)
-	   (when (not (boundp v))
-	     (push v ,unbound-vars)
-	     (push nil ,nils)))
-	 (progv unbound-vars nils
-	   ,@body)))))
+         (dolist (v ',vars)
+           (when (not (boundp v))
+             (push v ,unbound-vars)
+             (push nil ,nils)))
+         (progv unbound-vars nils
+           ,@body)))))
 
 (defun prefix (n s)
   (when s
     (if (<= (length s) n)
-	s
-	(subseq s 0 n))))
+        s
+        (subseq s 0 n))))
 
 (defgeneric expand-value (thing)
   (:documentation "Returns the expansion of THING as a string."))
@@ -265,47 +265,47 @@ URI-TEMPLATE."
 
 (defun expand-alist (alist separator assoc stream)
   (labels ((eat (l)
-	     (unless (endp l)
-	       (destructuring-bind (key . value) (first l)
-		 (let ((expanded (expand-value value)))
-		   (unless (emptyp expanded)
-		     (write-char separator stream)
-		     (write-string key stream)
-		     (write-char assoc stream)
-		     (write-string expanded stream))))
-	       (eat (rest l))))
-	   (eat-first (l)
-	     (unless (endp l)
-	       (destructuring-bind (key . value) (first l)
-		 (let ((expanded (expand-value value)))
-		   (cond ((emptyp expanded) (eat-first (rest l)))
-			 (:otherwise (write-string key stream)
-				     (write-char assoc stream)
-				     (write-string expanded stream)
-				     (eat (rest l)))))))))
+             (unless (endp l)
+               (destructuring-bind (key . value) (first l)
+                 (let ((expanded (expand-value value)))
+                   (unless (emptyp expanded)
+                     (write-char separator stream)
+                     (write-string key stream)
+                     (write-char assoc stream)
+                     (write-string expanded stream))))
+               (eat (rest l))))
+           (eat-first (l)
+             (unless (endp l)
+               (destructuring-bind (key . value) (first l)
+                 (let ((expanded (expand-value value)))
+                   (cond ((emptyp expanded) (eat-first (rest l)))
+                         (:otherwise (write-string key stream)
+                                     (write-char assoc stream)
+                                     (write-string expanded stream)
+                                     (eat (rest l)))))))))
     (eat-first alist)))
 
 (defun expand-list (list separator stream)
   (labels ((eat (l)
-	     (unless (endp l)
-	       (let ((expanded (expand-value (first l))))
-		 (unless (emptyp expanded)
-		   (write-char separator stream)
-		   (write-string expanded stream)))
-	       (eat (rest l))))
-	   (eat-first (l)
-	     (unless (endp l)
-	       (let ((expanded (expand-value (first l))))
-		 (cond ((emptyp expanded) (eat-first (rest l)))
-		       (:otherwise (write-string expanded stream)
-				   (eat (rest l))))))))
+             (unless (endp l)
+               (let ((expanded (expand-value (first l))))
+                 (unless (emptyp expanded)
+                   (write-char separator stream)
+                   (write-string expanded stream)))
+               (eat (rest l))))
+           (eat-first (l)
+             (unless (endp l)
+               (let ((expanded (expand-value (first l))))
+                 (cond ((emptyp expanded) (eat-first (rest l)))
+                       (:otherwise (write-string expanded stream)
+                                   (eat (rest l))))))))
     (eat-first list)))
 
 (defmethod expand-value ((x cons))
   (with-output-to-string (o)
     (if (consp (car x))
-	(expand-alist x *separator* #\= o)
-	(expand-list x *separator* o))))
+        (expand-alist x *separator* #\= o)
+        (expand-list x *separator* o))))
 
 (defun uri-template-expand-code (template &optional (stream-var 'stream))
   (declare (type symbol stream-var))
@@ -336,15 +336,15 @@ URI-TEMPLATE."
   (multiple-value-bind (code vars)
       (uri-template-expand-code template 'stream)
     (compile nil `(lambda (stream &key ,@vars &allow-other-keys)
-		    (declare (type stream stream)
-			     (special ,@vars))
-		    ,@code))))
+                    (declare (type stream stream)
+                             (special ,@vars))
+                    ,@code))))
 
 (defmethod slot-unbound (class
-			 (template uri-template)
-			 (slot (eql 'expand-function)))
+                         (template uri-template)
+                         (slot (eql 'expand-function)))
     (setf (slot-value template 'expand-function)
-	  (compile-uri-template-expand-function template)))
+          (compile-uri-template-expand-function template)))
 
 (defgeneric expand-uri (output uri &rest vars &key &allow-other-keys))
 
@@ -388,15 +388,15 @@ URI-TEMPLATE."
 
 (defun var-regex (op)
   (let ((char-regex
-	 (case-char op
-	   (nil '(:alternation
-		  (:property not-reserved-char-p)
-		  (:char-class #\@)
-		  (:sequence #\% :digit-class :digit-class)))
-	   ("+#./;?&" `(:alternation
-			(:property not-reserved-char-p)
-			(:sequence #\% :digit-class :digit-class)
-			(:char-class ,@(coerce +reserved+ 'list)))))))
+         (case-char op
+           (nil '(:alternation
+                  (:property not-reserved-char-p)
+                  (:char-class #\@)
+                  (:sequence #\% :digit-class :digit-class)))
+           ("+#./;?&" `(:alternation
+                        (:property not-reserved-char-p)
+                        (:sequence #\% :digit-class :digit-class)
+                        (:char-class ,@(coerce +reserved+ 'list)))))))
     `(:sequence
       #1=(:greedy-repetition 1 nil ,char-regex)
       (:non-greedy-repetition 0 nil (:sequence ,(op-separator op) #1#)))))
@@ -413,8 +413,8 @@ URI-TEMPLATE."
 (defun var-binding (op var)
   (if (consp var)
       (case (car var)
-	((explode) `(,(explode op) ,(second var)))
-	(:otherwise var))
+        ((explode) `(,(explode op) ,(second var)))
+        (:otherwise var))
       var))
 
 (defun uri-template-vars (template)
@@ -437,18 +437,18 @@ URI-TEMPLATE."
     (values
      (do-uri-template template
        :parts (lambda (&rest parts)
-		`(:sequence :start-anchor ,@parts :end-anchor))
+                `(:sequence :start-anchor ,@parts :end-anchor))
        :op (lambda (op &rest vars)
-	     `(:sequence
-	       ,@(when (op-prefix op) `(,(op-prefix op)))
-	       ,@(reduce (lambda (exp var)
-			   (push (var-binding op var) bindings)
-			   `((:named-register ,(var-name var)
-					      ,(var-regex op))
-			     ,@(when exp `((:greedy-repetition
-					    0 1 (:sequence ,(op-separator op)
-							   ,@exp))))))
-			 (reverse vars) :initial-value nil)))
+             `(:sequence
+               ,@(when (op-prefix op) `(,(op-prefix op)))
+               ,@(reduce (lambda (exp var)
+                           (push (var-binding op var) bindings)
+                           `((:named-register ,(var-name var)
+                                              ,(var-regex op))
+                             ,@(when exp `((:greedy-repetition
+                                            0 1 (:sequence ,(op-separator op)
+                                                           ,@exp))))))
+                         (reverse vars) :initial-value nil)))
        :explode (lambda (var) `(explode ,var))
        :prefix (lambda (n var) `(prefix ,n ,var))
        :var #'uri-var)
@@ -458,8 +458,8 @@ URI-TEMPLATE."
   (let ((string (gensym "STRING-")))
     (multiple-value-bind (regex vars) (uri-template-regex template)
       `(lambda (,string)
-	 (cl-ppcre:register-groups-bind ,vars
-	     (,(cl-ppcre:create-scanner regex) ,string)
+         (cl-ppcre:register-groups-bind ,vars
+             (,(cl-ppcre:create-scanner regex) ,string)
            (let ,(mapcar (lambda (var)
                            `(,(intern (var-name var))
                               ,(uri-var (var-name var))))
@@ -480,12 +480,12 @@ URI-TEMPLATE."
 
 (defmacro uri-template-bind (vars (template string) &body body)
   `(cl-ppcre:register-groups-bind ,vars ((uri-template-regex ,template)
-					 ,string)
+                                         ,string)
      ,@body))
 
 (defmacro uri-template-match ((template string) &body body)
   (multiple-value-bind (regex vars) (uri-template-regex template)
     `(cl-ppcre:register-groups-bind ,vars (,(cl-ppcre:create-scanner regex)
-					    ,string)
+                                            ,string)
        (with-uri-template-vars ,template
          ,@body))))
diff --git a/uri.lisp b/uri.lisp
index ec38b54..f45ea56 100644
--- a/uri.lisp
+++ b/uri.lisp
@@ -22,7 +22,7 @@
 
 (defun canonical-document-uri (uri)
   (or (when (string= "/" uri)
-	uri)
+        uri)
       (cl-ppcre:regex-replace
        "/$" (cl-ppcre:regex-replace "//" uri "/")
        "")))
@@ -34,8 +34,8 @@
 (define-constant +reserved+ (str +gen-delims+ +sub-delims+)
   :test 'string=)
 (define-constant +unreserved+ (str "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-				   "abcdefghijklmnopqrstuvwxyz"
-				   "0123456789-._~")
+                                   "abcdefghijklmnopqrstuvwxyz"
+                                   "0123456789-._~")
   :test 'string=)
 
 (defun unreserved-char-p (c)
@@ -50,15 +50,15 @@
 (defun %-encode-char (c &key stream (reserved +reserved+))
   (if (null stream)
       (with-output-to-string (s)
-	(%-encode-char c :stream s :reserved reserved))
+        (%-encode-char c :stream s :reserved reserved))
       (if (find c reserved)
-	  (let ((b (babel:string-to-octets
-		    (make-string 1 :initial-element c)
-		    :encoding :utf-8)))
-	    (dotimes (i (length b))
-	      (write-char #\% stream)
-	      (write (svref b i) :base 16 :case :upcase :stream stream)))
-	  (write-char c stream))))
+          (let ((b (babel:string-to-octets
+                    (make-string 1 :initial-element c)
+                    :encoding :utf-8)))
+            (dotimes (i (length b))
+              (write-char #\% stream)
+              (write (svref b i) :base 16 :case :upcase :stream stream)))
+          (write-char c stream))))
 
 (defun uri-char-p (c)
   (or (unreserved-char-p c)
@@ -66,54 +66,54 @@
 
 (defun hex-digit-p (c)
   (when (or (char<= #\0 c #\9)
-	    (char<= #\A c #\Z)
-	    (char<= #\a c #\z))
+            (char<= #\A c #\Z)
+            (char<= #\a c #\z))
     c))
 
 (defun %-encode-bytes (bytes &optional stream)
   (let ((*print-base* 16)
-	(len (length bytes)))
+        (len (length bytes)))
     (labels ((eat (i)
-	       (when (< i len)
-		 (write-char #\% stream)
-		 (write (the (unsigned-byte 8) (aref bytes i))
-			:base 16 :stream stream)
-		 (eat (1+ i)))))
+               (when (< i len)
+                 (write-char #\% stream)
+                 (write (the (unsigned-byte 8) (aref bytes i))
+                        :base 16 :stream stream)
+                 (eat (1+ i)))))
       (eat 0))))
 
 (defun %-encode (string &optional stream (allowed-char-p #'uri-char-p))
   (let ((len (length string)))
     (labels ((eat (i)
-	       (cond ((<= len i) nil)
-		     ((and (< (+ 2 i) len)
-			   (char= #\% (char string i))
-			   (hex-digit-p (char string (+ 1 i)))
-			   (hex-digit-p (char string (+ 2 i))))
-		      (write-string string stream :start i :end (+ 3 i))
-		      (eat (+ 3 i)))
-		     ((funcall allowed-char-p (char string i))
-		      (write-char (char string i) stream)
-		      (eat (1+ i)))
-		     (t
-		      (%-encode-bytes
-		       (babel:string-to-octets
-			(subseq string i (1+ i))
-			:encoding :utf-8)
-		       stream)
-		      (eat (1+ i))))))
+               (cond ((<= len i) nil)
+                     ((and (< (+ 2 i) len)
+                           (char= #\% (char string i))
+                           (hex-digit-p (char string (+ 1 i)))
+                           (hex-digit-p (char string (+ 2 i))))
+                      (write-string string stream :start i :end (+ 3 i))
+                      (eat (+ 3 i)))
+                     ((funcall allowed-char-p (char string i))
+                      (write-char (char string i) stream)
+                      (eat (1+ i)))
+                     (t
+                      (%-encode-bytes
+                       (babel:string-to-octets
+                        (subseq string i (1+ i))
+                        :encoding :utf-8)
+                       stream)
+                      (eat (1+ i))))))
       (if (null stream)
-	  (with-output-to-string (out)
-	    (setq stream out)
-	    (eat 0))
-	  (eat 0)))))
+          (with-output-to-string (out)
+            (setq stream out)
+            (eat 0))
+          (eat 0)))))
 
 #+test
 (%-encode "plop/%é%C3")
 
 (defun unaccent (c)
   (or (cl-ppcre:register-groups-bind (name)
-	  ("(.*) WITH .*" (cl-unicode:unicode-name c))
-	(cl-unicode:character-named name))
+          ("(.*) WITH .*" (cl-unicode:unicode-name c))
+        (cl-unicode:character-named name))
       c))
 
 (defun to-url (str)
@@ -122,20 +122,20 @@
    (with-output-to-string (out)
      (let ((len (length str)))
        (labels ((out (c)
-		  (write-char (unaccent (char-downcase c)) out))
-		(nohyphen (i)
-		  (when (< i len)
-		    (let ((c (char str i)))
-		      (if (alphanumericp c)
-			  (progn (out c)
-				 (hyphen (1+ i)))
-			  (nohyphen (1+ i))))))
-		(hyphen (i)
-		  (when (< i len)
-		    (let ((c (char str i)))
-		      (if (alphanumericp c)
-			  (progn (out c)
-				 (hyphen (1+ i)))
-			  (progn (write-char #\- out)
-				 (nohyphen (1+ i))))))))
-	 (nohyphen 0))))))
+                  (write-char (unaccent (char-downcase c)) out))
+                (nohyphen (i)
+                  (when (< i len)
+                    (let ((c (char str i)))
+                      (if (alphanumericp c)
+                          (progn (out c)
+                                 (hyphen (1+ i)))
+                          (nohyphen (1+ i))))))
+                (hyphen (i)
+                  (when (< i len)
+                    (let ((c (char str i)))
+                      (if (alphanumericp c)
+                          (progn (out c)
+                                 (hyphen (1+ i)))
+                          (progn (write-char #\- out)
+                                 (nohyphen (1+ i))))))))
+         (nohyphen 0))))))