Re: what is wrong with this loop?

Liste des GroupesRevenir à cl scheme 
Sujet : Re: what is wrong with this loop?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 10. Jul 2025, 23:41:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <104pfjd$13cp2$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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))))))

Date Sujet#  Auteur
10 Jul23:41 * Re: what is wrong with this loop?3B. Pym
11 Jul20:13 `* Re: what is wrong with this loop?2B. Pym
11 Jul20:36  `- Re: what is wrong with this loop?1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal