emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] xml-escape-region


From: Daniel Colascione
Subject: Re: [PATCH] xml-escape-region
Date: Wed, 7 Oct 2009 22:13:27 -0400

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Oct 7, 2009, at 6:10 PM, Stefan Monnier wrote:

+;;;##autoload
+(defun xml-escape-region (beg end)
+  (interactive "*r")
+  (let ((escaped (xml-escape-string (buffer-substring beg end))))
+    (delete-region beg end)
+    (insert escaped)))

I'd rather not autoload such a function.

Do you mean that it should be loaded all the time, or that the user should have to explicitly load xml.el before using the function? If the latter, then that would make binding it to a key less convenient.

 But more importantly, this
implementation is very inefficient. xml-escape-string itself is rather
inefficient except for short strings; this is OK for its current uses,
but for xml-escape-region it's definitely not good (i.e. only usable for
small regions).

How's this? It's O(N) in the amount of text escaped.

(defun xml-escape-region (beg end)
"XML-escape text between BEG and END according to `xml-entity- alist`."
  (interactive "*r")

  (let ((search-re (mapconcat #'regexp-quote
                              (mapcar #'cdr xml-entity-alist)
                              "\\|"))
        (end (set-marker (make-marker) end)))

    (save-excursion
      (goto-char beg)
      (while (re-search-forward search-re end t)
        (replace-match (concat "&"
                               (car (rassoc (match-string 0)
                                            xml-entity-alist))
                               ";"))))))

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)

iEYEARECAAYFAkrNSscACgkQ17c2LVA10VvMXgCfcp9fGLBvgg2x/WvemODxe77H
o4kAoM/oz33aci08TnzboRm4GTY6zskD
=DnOn
-----END PGP SIGNATURE-----





reply via email to

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