Function: url-unhex-string

url-unhex-string is an autoloaded and byte-compiled function defined in url-util.el.gz.

Signature

(url-unhex-string STR &optional ALLOW-NEWLINES)

Documentation

Decode %XX sequences in a percent-encoded URL.

If optional second argument ALLOW-NEWLINES is non-nil, then allow the decoding of carriage returns and line feeds in the string, which is normally forbidden in URL encoding.

The resulting string in general requires decoding using an appropriate coding-system; see decode-coding-string.

Source Code

;; Defined in /usr/src/emacs/lisp/url/url-util.el.gz
;; Fixme: Is this definition better, and does it ever matter?

;; (defun url-unhex-string (str &optional allow-newlines)
;;   "Remove %XX, embedded spaces, etc in a url.
;; If optional second argument ALLOW-NEWLINES is non-nil, then allow the
;; decoding of carriage returns and line feeds in the string, which is normally
;; forbidden in URL encoding."
;;   (setq str (or str ""))
;;   (setq str (replace-regexp-in-string "%[[:xdigit:]]\\{2\\}"
;;                                    (lambda (match)
;;                                      (string (string-to-number
;;                                               (substring match 1) 16)))
;;                                    str t t))
;;   (if allow-newlines
;;       (replace-regexp-in-string "[\n\r]" (lambda (match)
;;                                         (format "%%%.2X" (aref match 0)))
;;                              str t t)
;;     str))

;;;###autoload
(defun url-unhex-string (str &optional allow-newlines)
  "Decode %XX sequences in a percent-encoded URL.
If optional second argument ALLOW-NEWLINES is non-nil, then allow the
decoding of carriage returns and line feeds in the string, which is normally
forbidden in URL encoding.

The resulting string in general requires decoding using an
appropriate coding-system; see `decode-coding-string'."
  (setq str (or str ""))
  (let ((tmp "")
	(case-fold-search t))
    (while (string-match "%[0-9a-f][0-9a-f]" str)
      (let* ((start (match-beginning 0))
	     (ch1 (url-unhex (elt str (+ start 1))))
	     (code (+ (* 16 ch1)
		      (url-unhex (elt str (+ start 2))))))
	(setq tmp (concat
		   tmp (substring str 0 start)
		   (cond
		    (allow-newlines
		     (byte-to-string code))
		    ((or (= code ?\n) (= code ?\r))
		     " ")
		    (t (byte-to-string code))))
	      str (substring str (match-end 0)))))
    (concat tmp str)))