From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Eure Newsgroups: gmane.emacs.bugs Subject: bug#8706: 24.0.50; [PATCH] Function to build a URL query-string Date: Wed, 25 May 2011 09:32:15 -0700 Message-ID: <91C69C7A-1980-4AF4-9B4B-4AFDB16D2C0B@simplegeo.com> References: <87d3j932n7.fsf@lifelogs.com> <87zkmaopfs.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1306352985 27099 80.91.229.12 (25 May 2011 19:49:45 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 25 May 2011 19:49:45 +0000 (UTC) To: tzz@lifelogs.com, 8706@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed May 25 21:49:41 2011 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QPK5I-0006Gj-GH for geb-bug-gnu-emacs@m.gmane.org; Wed, 25 May 2011 21:49:36 +0200 Original-Received: from localhost ([::1]:60432 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPK5I-0005cM-2j for geb-bug-gnu-emacs@m.gmane.org; Wed, 25 May 2011 15:49:36 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:51572) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPK59-0005Pr-OX for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 15:49:33 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPK55-0000Ue-Nz for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 15:49:27 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:43576) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPK55-0000Rl-HE for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 15:49:23 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1QPIWw-00086E-SD; Wed, 25 May 2011 14:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ian Eure Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 25 May 2011 18:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 8706 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: Ted Zlatanov , bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.130634696731029 (code B ref -1); Wed, 25 May 2011 18:10:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 25 May 2011 18:09:27 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QPIWM-00084O-Md for submit@debbugs.gnu.org; Wed, 25 May 2011 14:09:27 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QPIWC-00083D-Is for submit@debbugs.gnu.org; Wed, 25 May 2011 14:09:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPH0Q-0007kD-Kd for submit@debbugs.gnu.org; Wed, 25 May 2011 12:32:23 -0400 Original-Received: from lists.gnu.org ([140.186.70.17]:59148) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPH0Q-0007k9-J5 for submit@debbugs.gnu.org; Wed, 25 May 2011 12:32:22 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:43055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPH0P-0005uF-EL for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 12:32:22 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPH0O-0007jk-74 for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 12:32:21 -0400 Original-Received: from mail-px0-f171.google.com ([209.85.212.171]:56002) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPH0N-0007jd-Uf for bug-gnu-emacs@gnu.org; Wed, 25 May 2011 12:32:20 -0400 Original-Received: by pxi7 with SMTP id 7so5257171pxi.30 for ; Wed, 25 May 2011 09:32:18 -0700 (PDT) Original-Received: by 10.68.0.227 with SMTP id 3mr3435632pbh.284.1306341138356; Wed, 25 May 2011 09:32:18 -0700 (PDT) Original-Received: from [10.0.1.153] (c-76-104-208-94.hsd1.wa.comcast.net [76.104.208.94]) by mx.google.com with ESMTPS id t9sm5766793pbo.3.2011.05.25.09.32.16 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 25 May 2011 09:32:17 -0700 (PDT) In-Reply-To: <87zkmaopfs.fsf@lifelogs.com> X-Mailer: Apple Mail (2.1084) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Wed, 25 May 2011 14:10:02 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:46698 Archived-At: On May 25, 2011, at 6:55 AM, Ted Zlatanov wrote: > Ian, did you see my followup? >=20 I didn't; I'm not on the list, so I only get direct replies.=09 > On Mon, 23 May 2011 09:36:12 -0500 Ted Zlatanov = wrote:=20 >=20 > TZ> On Fri, 20 May 2011 11:37:45 -0700 Ian Eure = wrote:=20 > IE> This patch adds a url-build-query-string method, which performs = the > IE> opposite job of url-parse-query-string. I find myself needing this > IE> method in many projects, and having it in url-util.el seems > IE> beneficial. >=20 > TZ> I think this is useful. >=20 > IE> + (c-concat-separated > IE> + (mapcar (lambda (pair) (apply 'format "%s=3D%s" pair)) query) = "&")) >=20 > TZ> I would use `mapconcat' (it's what `c-concat-separated' uses under = the > TZ> hood anyhow). >=20 Done. > TZ> Also `format' errors out if it doesn't have enough arguments and = it's > TZ> legitimate to build a URL query like "url?x;y;z" where x, y, and z = don't > TZ> have values. So I would change the lambda to (untested): >=20 > TZ> (lambda (pair) > TZ> (if (nth 1 pair) > TZ> (apply 'format "%s=3D%s" pair) > TZ> (format "%s" (car-safe pair)))) >=20 > TZ> This also handles the case where `pair' is nil by design or by = accident. >=20 You can do this by passing a sequence such as '((x "")), which results = in "x=3D". I think this is fine, since this mirrors what you're actually = doing =97 sending an empty string. I think if we wanted to accept lots = of different formats, we'd need to do something like: 1. Two-element sequences should work as they do now. 2. One-element sequences should get an empty string appended. 3. Invalid sequences ignored. I'm not sure it makes sense to support #2, since it seems somewhat = opaque and you can do the same thing with an empty string in scenario = #1. #3 I'm not sure how to do without using remove-if from cl-seq. I don't know why someone would pass in nil in place of a k/v pair, and = it seems better to raise an error about that rather than silently = accepting it, since it seems likely to be an error in the calling code. I'm happy to discuss further if you think there's a good reason to = change it, but I feel like this iteration is acceptable. > TZ> Finally, the key and the value should be URL-encoded. Do you = assume > TZ> that will be done before the function is called? >=20 An oversight on my part; an updated patch is attached. Thank you for the = feedback. --- url-util.el.orig 2011-05-25 09:09:03.000000000 -0700 +++ url-util.el 2011-05-25 09:09:47.000000000 -0700 @@ -281,6 +281,25 @@ (setq retval (cons (list key val) retval))))) retval)) =20 +;;;###autoload +(defun url-build-query-string (query) + "Build a query-string. + +Given a QUERY in the form: +'((key1 val1) + (key2 val2)) + +This will return a string `key1=3Dval1&key2=3Dval2'. Keys may be = strings +or symbols; if they are symbols, the string name will be used." + + (mapconcat + (lambda (pair) + (apply 'format "%s=3D%s" + (mapcar + (lambda (sym) + (url-hexify-string (if (symbolp sym) (symbol-name sym) = sym))) + pair))) query "&")) + (defun url-unhex (x) (if (> x ?9) (if (>=3D x ?a)=