unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Byte swapping and bindat
       [not found] <87pmjglllc.fsf.ref@yahoo.com>
@ 2022-06-10 13:00 ` Po Lu
  2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 7+ messages in thread
From: Po Lu @ 2022-06-10 13:00 UTC (permalink / raw)
  To: help-gnu-emacs

I was looking into replacing some of the very complicated Motif binary
message parsing code in x-dnd.el with bindat, but there doesn't seem to
be a way to make bindat do byte-swapping automatically, which makes it a
lot less attractive.

Is there a way to specify a bindat type with fields whose endianess
depends on an external parameter (or another field of the type?)

Defining each type twice for both byte orders seems very ugly.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-10 13:00 ` Byte swapping and bindat Po Lu
@ 2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-10 15:53     ` Eli Zaretskii
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-06-10 13:32 UTC (permalink / raw)
  To: help-gnu-emacs

> Is there a way to specify a bindat type with fields whose endianess
> depends on an external parameter (or another field of the type?)

I'm so glad you asked:

    (let* ((threshold 32)
           (type (bindat-type (kind   uint 8)
                              (length sint 32 (> kind threshold)))))
      (list (bindat-unpack type "\x10\x00\x00\x01\x01")
            (bindat-unpack type "\x80\x00\x00\x01\x01")))
    =>
    (((kind . 16)  (length . 257))
     ((kind . 128) (length . 16842752)))

That's one of the benefits of the new Bindat :-)


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-06-10 15:53     ` Eli Zaretskii
  2022-06-10 17:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-11  0:54     ` Po Lu
  2 siblings, 0 replies; 7+ messages in thread
From: Eli Zaretskii @ 2022-06-10 15:53 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 10 Jun 2022 09:32:31 -0400
> From:  Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
> 
>     (let* ((threshold 32)
>            (type (bindat-type (kind   uint 8)
>                               (length sint 32 (> kind threshold)))))
>       (list (bindat-unpack type "\x10\x00\x00\x01\x01")
>             (bindat-unpack type "\x80\x00\x00\x01\x01")))
>     =>
>     (((kind . 16)  (length . 257))
>      ((kind . 128) (length . 16842752)))

It would be nice to have sint documented in the ELisp manual...



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-10 15:53     ` Eli Zaretskii
@ 2022-06-10 17:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-11  0:54     ` Po Lu
  2 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-06-10 17:03 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor [2022-06-10 09:32:31] wrote:
>> Is there a way to specify a bindat type with fields whose endianess
>> depends on an external parameter (or another field of the type?)
>
> I'm so glad you asked:
>
>     (let* ((threshold 32)
>            (type (bindat-type (kind   uint 8)
>                               (length sint 32 (> kind threshold)))))
>       (list (bindat-unpack type "\x10\x00\x00\x01\x01")
>             (bindat-unpack type "\x80\x00\x00\x01\x01")))
>     =>
>     (((kind . 16)  (length . 257))
>      ((kind . 128) (length . 16842752)))
>
> That's one of the benefits of the new Bindat :-)

Oh, and if you want to do that for `uint` (which currently doesn't take
a "reverse" argument (patch welcome)), you can do:

    (let* ((threshold 32)
           (type (bindat-type (kind   uint 8)
                              (length if (> kind threshold)
                                         (uintr 32) (uint 32)))))
      (list (bindat-unpack type "\x10\x00\x00\x01\x01")
            (bindat-unpack type "\x80\x00\x00\x01\x01")))
    =>
    (((kind . 16)  (length . 257))
     ((kind . 128) (length . 16842752)))


-- Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-06-10 15:53     ` Eli Zaretskii
  2022-06-10 17:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-06-11  0:54     ` Po Lu
  2022-06-11  1:08       ` Stefan Monnier
  2 siblings, 1 reply; 7+ messages in thread
From: Po Lu @ 2022-06-11  0:54 UTC (permalink / raw)
  To: Stefan Monnier via Users list for the GNU Emacs text editor
  Cc: Stefan Monnier

Stefan Monnier via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

>> Is there a way to specify a bindat type with fields whose endianess
>> depends on an external parameter (or another field of the type?)
>
> I'm so glad you asked:
>
>     (let* ((threshold 32)
>            (type (bindat-type (kind   uint 8)
>                               (length sint 32 (> kind threshold)))))
>       (list (bindat-unpack type "\x10\x00\x00\x01\x01")
>             (bindat-unpack type "\x80\x00\x00\x01\x01")))
>     =>
>     (((kind . 16)  (length . 257))
>      ((kind . 128) (length . 16842752)))
>
> That's one of the benefits of the new Bindat :-)

Thanks.  Where is that documented?

(And what about an unsigned integer? Is it just `uint', which according
to the doc is a big-endian number?)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-11  0:54     ` Po Lu
@ 2022-06-11  1:08       ` Stefan Monnier
  2022-06-11  1:15         ` Po Lu
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2022-06-11  1:08 UTC (permalink / raw)
  To: Po Lu; +Cc: Stefan Monnier via Users list for the GNU Emacs text editor

>> That's one of the benefits of the new Bindat :-)
> Thanks.  Where is that documented?

Not sure what "that" refers to.
If you mean "sint" it's documented in `C-h f bindate-type RET`.

If you mean the fact that the `r` argument can be an arbitrary ELisp
expression, it's not explicitly documented, because it holds for
basically every part of `bindate-type` specs.

> (And what about an unsigned integer? Is it just `uint', which according
> to the doc is a big-endian number?)

I assume you've now seen my other post which answers this question.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Byte swapping and bindat
  2022-06-11  1:08       ` Stefan Monnier
@ 2022-06-11  1:15         ` Po Lu
  0 siblings, 0 replies; 7+ messages in thread
From: Po Lu @ 2022-06-11  1:15 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: Stefan Monnier via Users list for the GNU Emacs text editor

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Not sure what "that" refers to.
> If you mean "sint" it's documented in `C-h f bindate-type RET`.

Hmm, I must've missed that.

> I assume you've now seen my other post which answers this question.

Yes, thanks.



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-06-11  1:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <87pmjglllc.fsf.ref@yahoo.com>
2022-06-10 13:00 ` Byte swapping and bindat Po Lu
2022-06-10 13:32   ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-06-10 15:53     ` Eli Zaretskii
2022-06-10 17:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-06-11  0:54     ` Po Lu
2022-06-11  1:08       ` Stefan Monnier
2022-06-11  1:15         ` Po Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).