Christophe Weblog Wiki Code Publications Music
much better `squeeze-unhex-and-decode-utf8-string'
authorChristophe Rhodes <csr21@cantab.net>
Sat, 25 Oct 2014 21:28:33 +0000 (22:28 +0100)
committerChristophe Rhodes <csr21@cantab.net>
Sat, 25 Oct 2014 21:28:33 +0000 (22:28 +0100)
It turns out that `url-unhex-string' uses the multiple-concat strategy
that I just removed from `squeeze-update-state', which ends up scaling
quadratically in the size of the input.  This is bad, particularly
when we are working (as I am) with large output string such as
resulting from an albums query.

squeeze.el

index 977813b28fc24c118fc0dafd600d7c7901250723..25491596d5633e9b41ab235b8b1135376b9011b5 100644 (file)
     (define-key map (kbd "TAB") 'completion-at-point)
     map))
 
+(defun squeeze-unhex-string (string)
+  (with-temp-buffer
+    (let ((case-fold-search t)
+          (start 0))
+      (while (string-match "%[0-9a-f][0-9a-f]" string start)
+        (let* ((s (match-beginning 0))
+               (ch1 (url-unhex (elt string (+ s 1))))
+               (code (+ (* 16 ch1)
+                        (url-unhex (elt string (+ s 2))))))
+          (insert (substring string start s)
+                  (byte-to-string code))
+          (setq start (match-end 0))))
+      (insert (substring string start)))
+    (buffer-string)))
+
 (defun squeeze-unhex-and-decode-utf8-string (string)
-  (decode-coding-string (url-unhex-string string) 'utf-8))
+  (decode-coding-string (squeeze-unhex-string string) 'utf-8))
 
 (define-derived-mode squeeze-mode comint-mode "Squeeze"
   "Major mode for interacting with the Squeezebox Server CLI.\\<squeeze-mode-map>"