[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 40a361fbd6: Bindat (src, strz): Operate on vectors too
From: |
Stefan Monnier |
Subject: |
master 40a361fbd6: Bindat (src, strz): Operate on vectors too |
Date: |
Fri, 21 Oct 2022 11:26:52 -0400 (EDT) |
branch: master
commit 40a361fbd600cba00f97e853112534626f10c654
Author: Nacho Barrientos <nacho.barrientos@cern.ch>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
Bindat (src, strz): Operate on vectors too
Copyright-paperwork-exempt: yes
* lisp/emacs-lisp/bindat.el (bindat--unpack-str, bindat--unpack-strz):
Fix the non-string case.
* test/lisp/emacs-lisp/bindat-tests.el (bindat-test--strz-array-unpack)
(bindat-test--str-simple-array-unpack,bindat-test--str-combined-array-unpack):
New tests.
---
lisp/emacs-lisp/bindat.el | 6 ++++--
test/lisp/emacs-lisp/bindat-tests.el | 19 ++++++++++++++++++-
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el
index 0ecac3d52a..82d3c5309f 100644
--- a/lisp/emacs-lisp/bindat.el
+++ b/lisp/emacs-lisp/bindat.el
@@ -163,7 +163,9 @@
(let ((s (substring bindat-raw bindat-idx (+ bindat-idx len))))
(setq bindat-idx (+ bindat-idx len))
(if (stringp s) s
- (apply #'unibyte-string s))))
+ ;; FIXME: There should be a more efficient way to do this.
+ ;; Should `apply' accept vectors in addition to lists?
+ (apply #'unibyte-string (append s nil)))))
(defun bindat--unpack-strz (&optional len)
(let ((i 0) s)
@@ -172,7 +174,7 @@
(setq s (substring bindat-raw bindat-idx (+ bindat-idx i)))
(setq bindat-idx (+ bindat-idx (or len (1+ i))))
(if (stringp s) s
- (apply #'unibyte-string s))))
+ (apply #'unibyte-string (append s nil)))))
(defun bindat--unpack-bits (len)
(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 0c03c51e2e..2abf714852 100644
--- a/test/lisp/emacs-lisp/bindat-tests.el
+++ b/test/lisp/emacs-lisp/bindat-tests.el
@@ -252,7 +252,24 @@
(should (equal (bindat-unpack spec "abc\0") "abc"))
;; Missing null terminator.
(should-error (bindat-unpack spec ""))
- (should-error (bindat-unpack spec "a"))))
+ (should-error (bindat-unpack spec "a")))
+
+ (ert-deftest bindat-test--strz-array-unpack ()
+ (should (equal (bindat-unpack spec [#x61 #x62 #x63 #x00]) "abc"))))
+
+(let ((spec (bindat-type str 3)))
+ (ert-deftest bindat-test--str-simple-array-unpack ()
+ (should (equal (bindat-unpack spec [#x61 #x62 #x63]) "abc"))))
+
+(let ((spec (bindat-type
+ (first u8)
+ (string str 3)
+ (last uint 16))))
+ (ert-deftest bindat-test--str-combined-array-unpack ()
+ (let ((unpacked (bindat-unpack spec [#xff #x63 #x62 #x61 #xff #xff])))
+ (should (equal (bindat-get-field unpacked 'string) "cba"))
+ (should (equal (bindat-get-field unpacked 'first) (- (expt 2 8) 1)))
+ (should (equal (bindat-get-field unpacked 'last) (- (expt 2 16) 1))))))
(let ((spec '((x strz 2))))
(ert-deftest bindat-test--strz-legacy-fixedlen-len ()
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master 40a361fbd6: Bindat (src, strz): Operate on vectors too,
Stefan Monnier <=