1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

* lisp/emacs-lisp/bindat.el: Minor refactoring

(bindat--unpack-str, bindat--unpack-strz, bindat--unpack-bits):
New functions, extracted from `bindat--unpack-item`.
(bindat--unpack-item): Use them.
(bindat--align): New function.
(bindat--unpack-group, bindat--length-group, bindat--pack-group): Use it.
(bindat-get-field): Allow integers to index both lists (as returned by
`repeat`) and vectors (as returned by `vec`).
(bindat--pack-str, bindat--pack-bits): New functions, extracted from
`bindat--pack-item`.
(bindat--pack-item): Use them.

* test/lisp/emacs-lisp/bindat-tests.el (struct-bindat): Place the fields
in the order in which they appear in the structs.
This commit is contained in:
Stefan Monnier 2021-03-05 13:31:16 -05:00
parent d582356a7f
commit 03ada27cb8
2 changed files with 83 additions and 82 deletions

View file

@ -1,4 +1,4 @@
;;; bindat-tests.el --- tests for bindat.el -*- lexical-binding: t; coding: utf-8; -*-
;;; bindat-tests.el --- tests for bindat.el -*- lexical-binding: t -*-
;; Copyright (C) 2019-2021 Free Software Foundation, Inc.
@ -23,14 +23,14 @@
(require 'bindat)
(require 'cl-lib)
(defvar header-bindat-spec
(defconst header-bindat-spec
(bindat-spec
(dest-ip ip)
(src-ip ip)
(dest-port u16)
(src-port u16)))
(defvar data-bindat-spec
(defconst data-bindat-spec
(bindat-spec
(type u8)
(opcode u8)
@ -39,7 +39,7 @@
(data vec (length))
(align 4)))
(defvar packet-bindat-spec
(defconst packet-bindat-spec
(bindat-spec
(header struct header-bindat-spec)
(items u8)
@ -47,23 +47,23 @@
(item repeat (items)
(struct data-bindat-spec))))
(defvar struct-bindat
(defconst struct-bindat
'((header
(dest-ip . [192 168 1 100])
(src-ip . [192 168 1 101])
(dest-port . 284)
(src-port . 5408))
(items . 2)
(item ((data . [1 2 3 4 5])
(id . "ABCDEF")
(length . 5)
(item ((type . 2)
(opcode . 3)
(type . 2))
((data . [6 7 8 9 10 11 12])
(id . "BCDEFG")
(length . 7)
(length . 5)
(id . "ABCDEF")
(data . [1 2 3 4 5]))
((type . 1)
(opcode . 4)
(type . 1)))))
(length . 7)
(id . "BCDEFG")
(data . [6 7 8 9 10 11 12])))))
(ert-deftest bindat-test-pack ()
(should (equal