Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Created July 25, 2010 21:47
Show Gist options
  • Save paulosuzart/489922 to your computer and use it in GitHub Desktop.
Save paulosuzart/489922 to your computer and use it in GitHub Desktop.
(ns org.jtornadoweb.cljhttputils
(:gen-class
:name org.jtornadoweb.CljHttpUtils
:methods [[parseQueryString [String] java.util.HashMap]]
:main false))
(defn #^:static
-parseQueryString
"Return all parameters in a HashMap. Same as python cgi.pase_qs. If no parameters are
found , returns an empty Map of parameters (not null)."
[this uri]
(def mp (java.util.HashMap.))
(let [q (.split (.replaceAll uri "[\\?/]" "") "[&]")]
(if (not= (aget q 0) "")
(doseq [i q]
(let [pair (.split i "=")]
(.put mp (first pair) (second pair))))))
mp)
@paulosuzart
Copy link
Author

Thanks Alan. As you can see I'm new to clj :P I'll try to use a clojure map instead. One point that made me use a HashMap directly was mutability. Using a Ref for a simple task like this sounds a bit overwhelming. Let see!
The previous version uses a Ref to a Clj Map, would that code slower than this last version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment