code critique
- From: Daniel Leidisch <news@xxxxxxxxxxxx>
- Date: Wed, 30 May 2007 11:15:15 +0200
For educational purposes (resp. fun) I wrote some code for the cgi
GET method. It works like this:
(get-parameter 'username) => "John"
Since I'm learning Lisp on my own, I would highly appreciate any tips
concerning my code. What could be done better, what is already ok?
Regards,
dhl
(defun convert-hex-encoded-chars (string)
"Converts %HEX encoded chars found in string."
(if (cl-ppcre:scan "%.." string)
(cl-ppcre:register-groups-bind
(before match after) ("([^%]*)(%..)(.*)" string)
(concatenate 'string before
(string (code-char
(parse-integer (subseq match 1) :radix 16)))
(convert-hex-encoded-chars after)))
string))
(defun split-query-string (query-string)
"Splits a query-string and returns a list of variable=value assignments."
(cl-ppcre:split "&" (substitute #\Space #\+
(convert-hex-encoded-chars query-string))))
(defun get-parameters (&optional
(query-string
(osicat:environment-variable "QUERY_STRING")))
"Returns an alist of (variable . value) pairs for a cgi query-string, which
may be given as an optional parameter. Otherwise, the environment-variable
QUERY_STRING is used."
(loop for variable in (split-query-string query-string)
collect (cl-ppcre:register-groups-bind
(key value) ("(.*)=(.*)" variable)
(cons (intern (string-upcase key))
value))))
(defparameter *parameters* (get-parameters)
"Alist of QUERY_STRING variables.")
(defun get-parameter (key)
"Returns the value of a QUERY_STRING variable named key."
(cdr (assoc key *parameters*)))
.
- Follow-Ups:
- Re: code critique
- From: Richard M Kreuter
- Re: code critique
- Prev by Date: Re: n accuracy
- Next by Date: Re: n accuracy
- Previous by thread: n accuracy
- Next by thread: Re: code critique
- Index(es):
Relevant Pages
|