aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2022-10-21 18:29:05 +0300
committerEli Zaretskii2022-10-21 18:29:05 +0300
commit9bc1999b6f2d92e06a2c3d95dfde0b64ee1c6daf (patch)
tree7f49338c8f17decd469df33eff8a13930155d421
parent4bb4b2a921ddc901802863005427fefbfa70807e (diff)
parent40a361fbd600cba00f97e853112534626f10c654 (diff)
downloademacs-9bc1999b6f2d92e06a2c3d95dfde0b64ee1c6daf.tar.gz
emacs-9bc1999b6f2d92e06a2c3d95dfde0b64ee1c6daf.zip
Merge branch 'master' of git.savannah.gnu.org:/srv/git/emacs
-rw-r--r--lisp/emacs-lisp/bindat.el6
-rw-r--r--test/lisp/emacs-lisp/bindat-tests.el19
2 files changed, 22 insertions, 3 deletions
diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el
index 0ecac3d52aa..82d3c5309f8 100644
--- a/lisp/emacs-lisp/bindat.el
+++ b/lisp/emacs-lisp/bindat.el
@@ -163,7 +163,9 @@
163 (let ((s (substring bindat-raw bindat-idx (+ bindat-idx len)))) 163 (let ((s (substring bindat-raw bindat-idx (+ bindat-idx len))))
164 (setq bindat-idx (+ bindat-idx len)) 164 (setq bindat-idx (+ bindat-idx len))
165 (if (stringp s) s 165 (if (stringp s) s
166 (apply #'unibyte-string s)))) 166 ;; FIXME: There should be a more efficient way to do this.
167 ;; Should `apply' accept vectors in addition to lists?
168 (apply #'unibyte-string (append s nil)))))
167 169
168(defun bindat--unpack-strz (&optional len) 170(defun bindat--unpack-strz (&optional len)
169 (let ((i 0) s) 171 (let ((i 0) s)
@@ -172,7 +174,7 @@
172 (setq s (substring bindat-raw bindat-idx (+ bindat-idx i))) 174 (setq s (substring bindat-raw bindat-idx (+ bindat-idx i)))
173 (setq bindat-idx (+ bindat-idx (or len (1+ i)))) 175 (setq bindat-idx (+ bindat-idx (or len (1+ i))))
174 (if (stringp s) s 176 (if (stringp s) s
175 (apply #'unibyte-string s)))) 177 (apply #'unibyte-string (append s nil)))))
176 178
177(defun bindat--unpack-bits (len) 179(defun bindat--unpack-bits (len)
178 (let ((bits nil) (bnum (1- (* 8 len))) j m) 180 (let ((bits nil) (bnum (1- (* 8 len))) j m)
diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el
index 0c03c51e2ef..2abf714852f 100644
--- a/test/lisp/emacs-lisp/bindat-tests.el
+++ b/test/lisp/emacs-lisp/bindat-tests.el
@@ -252,7 +252,24 @@
252 (should (equal (bindat-unpack spec "abc\0") "abc")) 252 (should (equal (bindat-unpack spec "abc\0") "abc"))
253 ;; Missing null terminator. 253 ;; Missing null terminator.
254 (should-error (bindat-unpack spec "")) 254 (should-error (bindat-unpack spec ""))
255 (should-error (bindat-unpack spec "a")))) 255 (should-error (bindat-unpack spec "a")))
256
257 (ert-deftest bindat-test--strz-array-unpack ()
258 (should (equal (bindat-unpack spec [#x61 #x62 #x63 #x00]) "abc"))))
259
260(let ((spec (bindat-type str 3)))
261 (ert-deftest bindat-test--str-simple-array-unpack ()
262 (should (equal (bindat-unpack spec [#x61 #x62 #x63]) "abc"))))
263
264(let ((spec (bindat-type
265 (first u8)
266 (string str 3)
267 (last uint 16))))
268 (ert-deftest bindat-test--str-combined-array-unpack ()
269 (let ((unpacked (bindat-unpack spec [#xff #x63 #x62 #x61 #xff #xff])))
270 (should (equal (bindat-get-field unpacked 'string) "cba"))
271 (should (equal (bindat-get-field unpacked 'first) (- (expt 2 8) 1)))
272 (should (equal (bindat-get-field unpacked 'last) (- (expt 2 16) 1))))))
256 273
257(let ((spec '((x strz 2)))) 274(let ((spec '((x strz 2))))
258 (ert-deftest bindat-test--strz-legacy-fixedlen-len () 275 (ert-deftest bindat-test--strz-legacy-fixedlen-len ()