[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] bindat and multibyte string RAW-DATA
From: |
Thien-Thi Nguyen |
Subject: |
[PATCH] bindat and multibyte string RAW-DATA |
Date: |
Wed, 24 May 2006 01:26:38 +0200 |
i am having difficulty imagining how a multibyte string RAW-DATA to
`bindat-pack' and `bindat-unpack' could be anything less than latent
confusion and bugs cultivation. so i propose the change below. WDYT?
here is an example of the problem (silent weirdness):
;; unibyte ok
(bindat-unpack '((one u8) (two u8)) (string 40 41))
=> ((two . 41) (one . 40))
;; multibyte -- big "bytes"!
(bindat-unpack '((one u8) (two u8)) (string 53450 53451))
=> ((two . 53451) (one . 53450))
there was variability over "size of byte" a while back, but these
days the above behavior would probably be considered buggy.
apparently, there are other problems as well:
;; vector -- more big bytes
(bindat-unpack '((one u8) (two u8)) (vector 53450 53451))
=> ((two . 53451) (one . 53450))
however, the patch does not address this last problem. a check:
(when (vectorp raw-data)
(let ((i 0) (len (length raw-data)) value)
(while (< i len)
(setq value (aref raw-data i))
(unless (and (<= 0 value) (<= value 255))
(error "Array element out of range")))))
inserted into `bindat-unpack' would work, but it seems heavy...
thi
__________________________________________________________
*** bindat.el 23 May 2006 11:21:59 -0000 1.10
--- bindat.el 23 May 2006 23:11:32 -0000
***************
*** 345,352 ****
(defun bindat-unpack (spec raw-data &optional pos)
"Return structured data according to SPEC for binary data in RAW-DATA.
! RAW-DATA is a string or vector. Optional third arg POS specifies the
! starting offset in RAW-DATA."
(unless pos (setq pos 0))
(bindat--unpack-group spec))
--- 345,354 ----
(defun bindat-unpack (spec raw-data &optional pos)
"Return structured data according to SPEC for binary data in RAW-DATA.
! RAW-DATA is a unibyte string or vector. Optional third arg POS specifies
! the starting offset in RAW-DATA."
! (when (multibyte-string-p raw-data)
! (error "String is multibyte"))
(unless pos (setq pos 0))
(bindat--unpack-group spec))
***************
*** 581,590 ****
(defun bindat-pack (spec struct &optional raw-data pos)
"Return binary data packed according to SPEC for structured data STRUCT.
! Optional third arg RAW-DATA is a pre-allocated string or vector to pack into.
! Optional fourth arg POS is the starting offset into RAW-DATA.
! Note: The result is a multibyte string; use `string-make-unibyte' on it
! to make it unibyte if necessary."
(let ((no-return raw-data))
(unless pos (setq pos 0))
(unless raw-data
--- 583,592 ----
(defun bindat-pack (spec struct &optional raw-data pos)
"Return binary data packed according to SPEC for structured data STRUCT.
! Optional third arg RAW-DATA is a pre-allocated unibyte string or vector to
! pack into. Optional fourth arg POS is the starting offset into RAW-DATA."
! (when (multibyte-string-p raw-data)
! (error "Pre-allocated string is multibyte"))
(let ((no-return raw-data))
(unless pos (setq pos 0))
(unless raw-data
- [PATCH] bindat and multibyte string RAW-DATA,
Thien-Thi Nguyen <=