[Top][All Lists]

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

url-util.el patch: url-hexify/unhex-region

From: Markus Triska
Subject: url-util.el patch: url-hexify/unhex-region
Date: Sun, 12 Nov 2006 18:04:56 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.90 (gnu/linux)

2006-11-12  Markus Triska  <address@hidden>

        * url/url-util.el (url-hexify-string): add optional argument for
        upper case hex digits
        (url-hexify-region, url-unhex-region): new functions analogous to
        url-hexify-string and url-unhex-string, operating on the region

Index: url-util.el
RCS file: /sources/emacs/emacs/lisp/url/url-util.el,v
retrieving revision 1.14
diff -u -p -u -r1.14 url-util.el
--- url-util.el 31 Jul 2006 21:36:42 -0000      1.14
+++ url-util.el 12 Nov 2006 16:47:56 -0000
@@ -342,6 +342,16 @@ forbidden in URL encoding."
     (setq tmp (concat tmp str))
+(defun url-unhex-region (start end)
+  "Like `url-unhex-string', operating on the region."
+  (interactive "r")
+  (save-excursion
+    (let ((str (buffer-substring start end)))
+      (delete-region start end)
+      (goto-char start)
+      (insert (url-unhex-string str)))))
 (defconst url-unreserved-chars
     ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y 
@@ -352,12 +362,13 @@ forbidden in URL encoding."
 This is taken from RFC 2396.")
-(defun url-hexify-string (string)
+(defun url-hexify-string (string &optional upcase)
   "Return a new string that is STRING URI-encoded.
 First, STRING is converted to utf-8, if necessary.  Then, for each
 character in the utf-8 string, those found in `url-unreserved-chars'
 are left as-is, all others are represented as a three-character
-string: \"%\" followed by two lowercase hex digits."
+string: \"%\" followed by two hex digits in upper case if UPCASE is
+non-nil, and lower case otherwise."
   ;; To go faster and avoid a lot of consing, we could do:
   ;; (defconst url-hexify-table
@@ -369,14 +380,26 @@ string: \"%\" followed by two lowercase 
   ;;     map))
   ;; (mapconcat (curry 'aref url-hexify-table) ...)
-  (mapconcat (lambda (byte)
-               (if (memq byte url-unreserved-chars)
-                   (char-to-string byte)
-                 (format "%%%02x" byte)))
-             (if (multibyte-string-p string)
-                 (encode-coding-string string 'utf-8)
-               string)
-             ""))
+  (let ((convert (if upcase 'upcase 'identity)))
+    (mapconcat (lambda (byte)
+                (if (memq byte url-unreserved-chars)
+                    (char-to-string byte)
+                  (funcall convert (format "%%%02x" byte))))
+              (if (multibyte-string-p string)
+                  (encode-coding-string string 'utf-8)
+                string)
+              "")))
+(defun url-hexify-region (start end arg)
+  "Like `url-hexify-string', operating on the region. Prefix
+ARG means use upper case hex digits."
+  (interactive "r\nP")
+  (save-excursion
+    (let ((str (buffer-substring start end)))
+      (delete-region start end)
+      (goto-char start)
+      (insert (url-hexify-string str arg)))))
 (defun url-file-extension (fname &optional x)

reply via email to

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