• Re: what is wrong with this loop?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Thu Jul 10 22:41:50 2025
    From Newsgroup: comp.lang.lisp

    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))))))
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Jul 11 19:13:03 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    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))))))

    To make the processing easier, first convert the string
    to a list of characters.

    (define (decode-http-encoded x)
    (define (hex->c s) (integer->char (string->number s 16)))
    (let1 z (string->list x)
    (with-output-to-string
    (lambda()
    (while (pair? z)
    (let1 c (pop! z)
    (and (eqv? c #\%)
    (set! c (hex->c (string (pop! z) (pop! z)))))
    (display c)))))))


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Fri Jul 11 19:36:49 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    B. Pym wrote:

    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))))))

    To make the processing easier, first convert the string
    to a list of characters.

    (define (decode-http-encoded x)
    (define (hex->c s) (integer->char (string->number s 16)))
    (let1 z (string->list x)
    (with-output-to-string
    (lambda()
    (while (pair? z)
    (let1 c (pop! z)
    (and (eqv? c #\%)
    (set! c (hex->c (string (pop! z) (pop! z)))))
    (display c)))))))


    Let's define a safe version of pop! that doesn't
    raise an error if the list is empty.

    (define-syntax pop~
    (syntax-rules ()
    [ (pop~ List)
    (if (null? List)
    #f
    (pop! List)) ]))

    (define (decode-http-encoded x)
    (define (hex->c s) (integer->char (string->number s 16)))
    (let1 z (string->list x)
    (with-output-to-string
    (lambda()
    (while (pop~ z) => c
    (and (eqv? c #\%)
    (set! c (hex->c (string (pop! z) (pop! z)))))
    (display c))))))


    --- Synchronet 3.21a-Linux NewsLink 1.2