(defun decode-http-encoded( x )
"take a string with potential http charater encoding and return
orignal string, http encoding format '%xx' where % literal and x is 0-f"
(with-output-to-string(s)
(loop for index = 0 for follow = 0
while (setf index (position #\% x :start index))
do (princ (subseq x follow (- index follow)) s )
(princ (code-char (parse-integer (subseq x (1+ index) 2))) s)
(setf follow (+ 3 index) index (+ 3 index))
finally (princ (subseq x follow) s))
s ))
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)
now the only place this can happen is in '(- index follow). The
problem is that even if I wrap it in (abs (- index follow)) I still
get the negative number. I have done macroexpand and macroexpand-1
and do not see anyhing
Thanks
marc
ps all in all loop is pretty cool though
marc
Marc Spitzer wrote:
> (defun decode-http-encoded( x )
> "take a string with potential http charater encoding and return
> orignal string, http encoding format '%xx' where % literal and x is 0-f"
> (with-output-to-string(s)
> (loop for index = 0 for follow = 0
you are telling loop to set index and follow to zero at the start of
each iteration. so you are doomed. :)
> while (setf index (position #\% x :start index))
> do (princ (subseq x follow (- index follow)) s )
> (princ (code-char (parse-integer (subseq x (1+ index) 2))) s)
In (subseq x (1+ index) 2) you are hoping the "end" parameter is a
"length" parameter. Can you see where I am headed with this line of
argument? :)
> (setf follow (+ 3 index) index (+ 3 index))
These increments are stomped on when the next iteration begins, for
reasons stated. Don't scroll down if you do not want to see my attempt
(somewhat tested).
> finally (princ (subseq x follow) s))
> s ))
>
> 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)
>
> now the only place this can happen is in '(- index follow). The
> problem is that even if I wrap it in (abs (- index follow)) I still
> get the negative number. I have done macroexpand and macroexpand-1
> and do not see anyhing
>
> Thanks
>
> marc
>
> ps all in all loop is pretty cool though
>
> marc
--
http://tilton-technology.com
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
(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))
finally (princ (subseq x start) s))
s ))
Kenny Tilton <·······@nyc.rr.com> writes:
> Marc Spitzer wrote:
>
>> (defun decode-http-encoded( x )
>> "take a string with potential http charater encoding and return
>> orignal string, http encoding format '%xx' where % literal and x is
>> 0-f"
>> (with-output-to-string(s)
>> (loop for index = 0 for follow = 0
>
> you are telling loop to set index and follow to zero at the start of
> each iteration. so you are doomed. :)
>
Well at least this one is my fault, it was right there in the book.
I thought it would initialize once and then I could manage it.
>> while (setf index (position #\% x :start index))
>> do (princ (subseq x follow (- index follow)) s )
>> (princ (code-char (parse-integer (subseq x (1+ index) 2))) s)
>
> In (subseq x (1+ index) 2) you are hoping the "end" parameter is a
> "length" parameter. Can you see where I am headed with this line of
> argument? :)
yup, its all the books fault for not being clearer<sob sob>
>
>> (setf follow (+ 3 index) index (+ 3 index))
>
> These increments are stomped on when the next iteration begins, for
> reasons stated. Don't scroll down if you do not want to see my attempt
> (somewhat tested).
>
>> finally (princ (subseq x follow) s))
>> s ))
>> 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)
>> now the only place this can happen is in '(- index follow). The
>> problem is that even if I wrap it in (abs (- index follow)) I still
>> get the negative number. I have done macroexpand and macroexpand-1
>> and do not see anyhing
>> Thanks
>> marc
>> ps all in all loop is pretty cool though
>> marc
>
> --
> http://tilton-technology.com
>
> Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
>
> Your Project Here! http://alu.cliki.net/Industry%20Application
>
>
> (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))
> finally (princ (subseq x start) s))
> s ))
thanks
marc
Marc Spitzer <········@optonline.net> writes:
> Well at least this one is my fault, it was right there in the book.
> I thought it would initialize once and then I could manage it.
The loop keyword you're looking for is WITH!
--
(espen)
Espen Vestre <·····@*do-not-spam-me*.vestre.net> writes:
> Marc Spitzer <········@optonline.net> writes:
>
>> Well at least this one is my fault, it was right there in the book.
>> I thought it would initialize once and then I could manage it.
>
> The loop keyword you're looking for is WITH!
> --
> (espen)
Thanks,
marc