emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/osc 4b28a3c 4/8: Concatenate messages instead of using


From: Stefan Monnier
Subject: [elpa] externals/osc 4b28a3c 4/8: Concatenate messages instead of using a temp-buffer and buffer-string
Date: Tue, 1 Dec 2020 18:00:15 -0500 (EST)

branch: externals/osc
commit 4b28a3c6a2caf0f68bc46c49fa792ddcaa0ada37
Author: Mario Lang <mlang@blind.guru>
Commit: Mario Lang <mlang@blind.guru>

    Concatenate messages instead of using a temp-buffer and buffer-string
    
    * packages/osc/osc.el: (osc-float32, osc-int32, osc-string): New functions.
    * (osc-insert-float32, osc-insert-int32, osc-insert-string): Remove.
    * (osc-send-message): Adjust.
---
 osc.el | 59 ++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 32 insertions(+), 27 deletions(-)

diff --git a/osc.el b/osc.el
index 316b532..e88f135 100644
--- a/osc.el
+++ b/osc.el
@@ -51,10 +51,11 @@
 
 (require 'cl-lib)
 
-(defun osc-insert-string (string)
-  (insert string 0 (make-string (- 3 (% (length string) 4)) 0)))
+(defun osc-string (string)
+  (setq string (encode-coding-string string 'binary))
+  (concat string (make-string (1+ (- 3 (% (length string) 4))) 0)))
 
-(defun osc-insert-float32 (value)
+(defun osc-float32 (value)
   (let (s (e 0) f)
     (cond
      ((= value 0.0)
@@ -73,18 +74,17 @@
       (if (= e 0) (while (< (* f (expt 2.0 e)) 1.0) (setq e (1+ e))))
       (setq f (round (* (1- (* f (expt 2.0 e))) (expt 2 23)))
            e (+ (* -1 e) 127))))
-    (insert (+ (lsh s 7) (lsh (logand e #XFE) -1))
-           (+ (lsh (logand e #X01) 7) (lsh (logand f #X7F0000) -16))
-           (lsh (logand f #XFF00) -8)
-           (logand f #XFF))))
+    (unibyte-string (+ (lsh s 7) (lsh (logand e #XFE) -1))
+                   (+ (lsh (logand e #X01) 7) (lsh (logand f #X7F0000) -16))
+                   (lsh (logand f #XFF00) -8)
+                   (logand f #XFF))))
 
-(defun osc-insert-int32 (value)
+(defun osc-int32 (value)
   (let (bytes)
     (dotimes (i 4)
       (push (% value 256) bytes)
       (setq value (/ value 256)))
-    (dolist (byte bytes)
-      (insert byte))))
+    (apply 'unibyte-string bytes)))
 
 ;;;###autoload
 (defun osc-make-client (host port)
@@ -99,23 +99,28 @@
 ;;;###autoload
 (defun osc-send-message (client path &rest args)
   "Send an OSC message from CLIENT to the specified PATH with ARGS."
-  (with-temp-buffer
-    (set-buffer-multibyte nil)
-    (osc-insert-string path)
-    (osc-insert-string
-     (apply 'concat "," (mapcar (lambda (arg)
-                                 (cond
-                                  ((floatp arg) "f")
-                                  ((integerp arg) "i")
-                                  ((stringp arg) "s")
-                                  (t (error "Invalid argument: %S" arg))))
-                               args)))
-    (dolist (arg args)
-      (cond
-       ((floatp arg) (osc-insert-float32 arg))
-       ((integerp arg) (osc-insert-int32 arg))
-       ((stringp arg) (osc-insert-string arg))))
-    (process-send-string client (buffer-string))))
+  (process-send-string
+   client
+   (apply
+    'concat
+    (osc-string path)
+    (osc-string
+     (apply
+      'concat ","
+      (mapcar (lambda (arg)
+               (cond
+                ((floatp arg) "f")
+                ((integerp arg) "i")
+                ((stringp arg) "s")
+                (t (error "Invalid argument: %S" arg))))
+             args)))
+    (mapcar
+     (lambda (arg)
+       (cond
+       ((floatp arg) (osc-float32 arg))
+       ((integerp arg) (osc-int32 arg))
+       ((stringp arg) (osc-string arg))))
+     args))))
 
 (defun osc-read-string ()
   (let ((pos (point)) string)



reply via email to

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