emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] /srv/bzr/emacs/emacs-24 r108233: add url-build-query-strin


From: Ted Zlatanov
Subject: [Emacs-diffs] /srv/bzr/emacs/emacs-24 r108233: add url-build-query-string and improve url-parse-query-string as per bug#8706
Date: Fri, 02 Nov 2012 01:48:22 -0000
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 108233
committer: Ted Zlatanov <address@hidden>
branch nick: quickfixes
timestamp: Tue 2012-05-15 04:47:38 -0400
message:
  add url-build-query-string and improve url-parse-query-string as per bug#8706
  
  * url/url-util.el (url-build-query-string): New function.
  (url-parse-query-string): Allow that '=' is not required and split
  URL parameters on ';', not just '&'.
modified:
  lisp/url/ChangeLog
  lisp/url/url-util.el
=== modified file 'lisp/url/ChangeLog'
--- a/lisp/url/ChangeLog        2012-05-14 07:56:04 +0000
+++ b/lisp/url/ChangeLog        2012-05-15 08:47:38 +0000
@@ -1,3 +1,9 @@
+2012-05-15  Ian Eure  <address@hidden>
+
+       * url-util.el (url-build-query-string): New function.
+       (url-parse-query-string): Allow that '=' is not required and split
+       URL parameters on ';', not just '&'.
+
 2012-05-14  Lars Magne Ingebrigtsen  <address@hidden>
 
        * url-expand.el (url-default-expander): Copy over the fullness of

=== modified file 'lisp/url/url-util.el'
--- a/lisp/url/url-util.el      2012-05-10 06:27:12 +0000
+++ b/lisp/url/url-util.el      2012-05-15 08:47:38 +0000
@@ -263,24 +263,65 @@
 ;;;###autoload
 (defun url-parse-query-string (query &optional downcase allow-newlines)
   (let (retval pairs cur key val)
-    (setq pairs (split-string query "&"))
+    (setq pairs (split-string query "[;&]"))
     (while pairs
       (setq cur (car pairs)
            pairs (cdr pairs))
-      (if (not (string-match "=" cur))
-         nil                           ; Grace
-       (setq key (url-unhex-string (substring cur 0 (match-beginning 0))
-                                   allow-newlines))
-       (setq val (url-unhex-string (substring cur (match-end 0) nil)
-                                   allow-newlines))
-       (if downcase
-           (setq key (downcase key)))
-       (setq cur (assoc key retval))
-       (if cur
-           (setcdr cur (cons val (cdr cur)))
-         (setq retval (cons (list key val) retval)))))
+      (unless (string-match "=" cur)
+        (setq cur (concat cur "=")))
+
+      (when (string-match "=" cur)
+        (setq key (url-unhex-string (substring cur 0 (match-beginning 0))
+                                    allow-newlines))
+        (setq val (url-unhex-string (substring cur (match-end 0) nil)
+                                    allow-newlines))
+        (if downcase
+            (setq key (downcase key)))
+        (setq cur (assoc key retval))
+        (if cur
+            (setcdr cur (cons val (cdr cur)))
+          (setq retval (cons (list key val) retval)))))
     retval))
 
+;;;###autoload
+(defun url-build-query-string (query &optional semicolons keep-empty)
+  "Build a query-string.
+
+Given a QUERY in the form:
+'((key1 val1)
+  (key2 val2)
+  (key3 val1 val2)
+  (key4)
+  (key5 ""))
+
+\(This is the same format as produced by `url-parse-query-string')
+
+This will return a string
+\"key1=val1&key2=val2&key3=val1&key3=val2&key4&key5\". Keys may
+be strings or symbols; if they are symbols, the symbol name will
+be used.
+
+When SEMICOLONS is given, the separator will be \";\".
+
+When KEEP-EMPTY is given, empty values will show as \"key=\"
+instead of just \"key\" as in the example above."
+  (mapconcat
+   (lambda (key-vals)
+     (let ((escaped
+            (mapcar (lambda (sym)
+                      (url-hexify-string (format "%s" sym))) key-vals)))
+       (mapconcat (lambda (val)
+                    (let ((vprint (format "%s" val))
+                          (eprint (format "%s" (car escaped))))
+                      (concat eprint
+                              (if (or keep-empty
+                                      (and val (not (zerop (length vprint)))))
+                                  "="
+                                "")
+                              vprint)))
+                  (or (cdr escaped) '("")) (if semicolons ";" "&"))))
+   query (if semicolons ";" "&")))
+
 (defun url-unhex (x)
   (if (> x ?9)
       (if (>= x ?a)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]