Groups | Search | Server Info | Login | Register
Groups > comp.lang.scheme > #6499
| From | "B. Pym" <Nobody447095@here-nor-there.org> |
|---|---|
| Newsgroups | comp.lang.lisp, comp.lang.scheme |
| Subject | Re: what is wrong with this loop? |
| Date | 2025-07-10 22:41 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <104pfjd$13cp2$1@dont-email.me> (permalink) |
Cross-posted to 2 groups.
Kenny Tilton wrote:
> > here are some test cases:
> >
> > (decode-http-encoded "aaaa") ;; this works
> > (decode-http-encoded "aaaa%62ccc" ) ;; this dies because start is 0
> > ;; and end is -3 (0 based index)
....
> (defun decode-http-encoded( x )
> (with-output-to-string(s)
> (loop for start = 0 then (+ pct 3)
> for pct = (position #\% x :start start)
> while pct
> do (princ (subseq x start pct) s )
> (let ((is (subseq x (1+ pct) (+ pct 3))))
> (princ (code-char (parse-integer is)) s))
The integer to be parsed is in hexadecimal, Kenny.
> finally (princ (subseq x start) s))
> s ))
Instead of CL (COBOL-Like), let's use a Lispy language.
Gauche Scheme:
The easy way.
(define (decode-http-encoded x)
(regexp-replace-all
#/%(..)/
x
(lambda (m) (integer->char (string->number (m 1) 16)))))
(decode-http-encoded "foo%20bar%20%41ct")
===>
"foo bar Act"
Harder.
(use gauche.sequence) ;; subseq size-of
(define (decode-http-encoded x)
(define (hex->c s) (integer->char (string->number s 16)))
(with-output-to-string
(lambda()
(do ((i 0 (+ i 1)))
((= i (size-of x)))
(let1 c (ref x i)
(when (equal? c #\%)
(set! c (hex->c (subseq x (+ i 1)(+ i 3))))
(inc! i 2))
(display c))))))
Back to comp.lang.scheme | Previous | Next — Next in thread | Find similar
Re: what is wrong with this loop? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-10 22:41 +0000
Re: what is wrong with this loop? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-11 19:13 +0000
Re: what is wrong with this loop? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-07-11 19:36 +0000
csiph-web