Re: URI Escape/Unescape Library?



Lars Brinkhoff <lars.spam@xxxxxxxxxx> writes:
>
> > I also replaced the loop and length with DOTIMES. The behaviour
> > appears to be the same.
>
> ...I believe you shouldn't rely on being able to modify the loop
> variable (i below). CLHS says:
>
> It is implementation-dependent whether dotimes establishes a new
> binding of var on each iteration or whether it establishes a
> binding for var once at the beginning and then assigns it on any
> subsequent iterations.

Doh! That's what I get for just writing a bit of test code instead of
reading the spec. Gotta get out of that habit.

> You could use with-input-from-string to read characters from text.

Nah--it doesn't really make sense conceptually to me to be reading
characters instead of iterating over the string. It's probably not all
that efficient, either, not that _that_ really matters. Here's the
(probable) final version.

(defun unencode (text)
"decode URL-escaped values: + is replaced with space; %nn with the
appropriate ASCII char"
(declare (string text))
(with-output-to-string (output)
(let ((len (length text)))
(loop with i = 0
with c do
(when (>= i len) (return))
(setf c (elt text i))
(cond ((equal c #\+) (setf c #\Space))
((equal c #\%)
(setf c (code-char
(read-from-string
(concatenate 'string "#x"
(subseq text (1+ i) (+ i 3))))))
(incf i 2)))
(write-char c output)))
output))

--
Robert Uhl <http://public.xdi.org/=ruhl>
Kristus is riesen! Weerelk, Hi is riesen!
.



Relevant Pages

  • Re: How is this conditional able to execute?
    ... > inside the loop, then each time the loop iterates then a NEW reference to ... > Note that the test of returndata is not a type. ... Here's some of my output starting with the first iteration: ... You can see returndata is the empty string above yet your IF is executed. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Get_Line problem (GNAT bug?)
    ... If the behavior is defined to support iteration ... A while loop, probably, or something in this area. ... I am using exceptions for parsing sources. ... But Get_Line already has a result, which is a string. ...
    (comp.lang.ada)
  • Re: Benchmarks: STLs string vs. C string
    ... that the catenation operator does not have to scan for the final null byte of the destination string, and can just copy the source string into the right position. ... In your test, the strcat loop has a quadratic complexity in the number of iterations because the "buffer" string grows by a fixded amount on each iteration, so the scan for the final null grows longer in proportion to the iteration number. ... If STL knows the length of "stlBuffer", each iteration of the STL loop takes the same time and the overall loop has linear complexity in the number of iterations. ...
    (comp.arch.embedded)
  • Re: Do While only does once
    ... I think it errors because you are not changing File in the loop, ... But as it got changed in iteration 1, ... > Dim OldName As String ...
    (microsoft.public.excel.programming)
  • Re: need help with looping
    ... >> the iteration to use DOTIMES or DOLIST. ... For complicated loops, LOOP may be a good idea, but how ...
    (comp.lang.lisp)

Loading