GNU bug report logs - #79990
[PATCH v1] Add binary format specifications '%b' and '%B'

Previous Next

Package: emacs;

Reported by: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>

Date: Thu, 11 Dec 2025 19:44:02 UTC

Severity: normal

Tags: patch

To reply to this bug, email your comments to 79990 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#79990; Package emacs. (Thu, 11 Dec 2025 19:44:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 11 Dec 2025 19:44:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH v1] Add binary format specifications '%b' and '%B'
Date: Thu, 11 Dec 2025 14:42:44 -0500
[Message part 1 (text/plain, inline)]
Tags: patch

Tags: patch

Hey everyone,

For quick things in elisp I often use a small helper function to
format binary digits.  C23 now recognizes ‘%b’ and ‘%B’ for the
‘printf’ functions, and some Lisps have similar directives (e.g. ‘~b’
and ‘~B’ in CL, Guile Scheme), so it would be nice to have this in
‘format’.  Attached is a first draft of a patch achieving this.

[v1-0001-Add-binary-format-specifications-b-and-B.patch (text/patch, attachment)]
[Message part 3 (text/plain, inline)]
Thanks,

-- 
Jacob S. Gordon
jacob.as.gordon <at> gmail.com
Please avoid sending me HTML emails and MS Office documents.
https://useplaintext.email/#etiquette

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79990; Package emacs. (Fri, 12 Dec 2025 08:05:01 GMT) Full text and rfc822 format available.

Message #8 received at 79990 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>,
 Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 79990 <at> debbugs.gnu.org
Subject: Re: bug#79990: [PATCH v1] Add binary format specifications '%b' and
 '%B'
Date: Fri, 12 Dec 2025 10:04:04 +0200
> From: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>
> Date: Thu, 11 Dec 2025 14:42:44 -0500
> 
> For quick things in elisp I often use a small helper function to
> format binary digits.  C23 now recognizes ‘%b’ and ‘%B’ for the
> ‘printf’ functions, and some Lisps have similar directives (e.g. ‘~b’
> and ‘~B’ in CL, Guile Scheme), so it would be nice to have this in
> ‘format’.  Attached is a first draft of a patch achieving this.

Thanks.

If this relies on C23-compliant libc implementation of printf (as I
think it does), then I don't think we want to have a feature in Emacs
which can be used on a small subset of platforms.  We need instead
either to provide our own implementation of that or maybe use Gnulib
printf.

Adding Paul for discussing the possible implementations and the need.

> +@item %b
> +@item %B
> +@cindex integer to binary
> +Replace the specification with the base-two or binary representation of
> +an integer.  Negative integers are formatted in a platform-dependent
> +way.

Don't we want the negative values to be formatted the same on all
platforms?  And if we leave this platform-dependent, then how will
tests you add work on the platforms where the result is different from
what the tests assume?

>       @samp{%b} uses lower case and @samp{%B} uses upper case in the
> +alternate display form prefixes @samp{0b} and @samp{0B}, respectively.
> +The object can also be a floating-point number that is formatted as an
> +integer, dropping any fraction.

Does this support bignums?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79990; Package emacs. (Fri, 12 Dec 2025 16:25:02 GMT) Full text and rfc822 format available.

Message #11 received at 79990 <at> debbugs.gnu.org (full text, mbox):

From: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>, Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 79990 <at> debbugs.gnu.org
Subject: Re: bug#79990: [PATCH v1] Add binary format specifications '%b' and
 '%B'
Date: Fri, 12 Dec 2025 11:23:59 -0500
On 2025-12-12 03:04, Eli Zaretskii wrote:
> If this relies on C23-compliant libc implementation of printf (as I
> think it does), then I don't think we want to have a feature in
> Emacs which can be used on a small subset of platforms.

Oh you’re right, the fixnum path does call out to sprintf.  I agree
this should be withheld unless it can be made sufficiently portable.

>> +an integer.  Negative integers are formatted in a platform-dependent
>> +way.
>
> Don't we want the negative values to be formatted the same on all
> platforms?  And if we leave this platform-dependent, then how will
> tests you add work on the platforms where the result is different from
> what the tests assume?

I took this warning from the %o and %x descriptions, which seems to be
associated with the ‘binary-as-unsigned’ option.  IIUC the default
behaviour is portable, so I think these statements could be sharpened
or combined into a paragraph about ‘binary-as-unsigned’.  Is this
correct Paul?

>>       @samp{%b} uses lower case and @samp{%B} uses upper case in the
>> +alternate display form prefixes @samp{0b} and @samp{0B}, respectively.
>> +The object can also be a floating-point number that is formatted as an
>> +integer, dropping any fraction.
> Does this support bignums?

Yes, the bignum path calls bignum_to_c_string -> gmp_lib::mpz_get_str
with base 2.

   (let ((x (1- (expt 2 128)))) (and (bignump x) (format "%#B" x)))

Best,

--
Jacob S. Gordon
jacob.as.gordon <at> gmail.com
Please avoid sending me HTML emails and MS Office documents.
https://useplaintext.email/#etiquette




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79990; Package emacs. (Fri, 12 Dec 2025 18:10:02 GMT) Full text and rfc822 format available.

Message #14 received at 79990 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Eli Zaretskii <eliz <at> gnu.org>, "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>
Cc: 79990 <at> debbugs.gnu.org
Subject: Re: bug#79990: [PATCH v1] Add binary format specifications '%b' and
 '%B'
Date: Fri, 12 Dec 2025 10:09:01 -0800
>>  				   + ((p[signedp] == '0'
>>  				       && (p[signedp + 1] == 'x'
>> -					   || p[signedp + 1] == 'X'))
>> +					   || p[signedp + 1] == 'X'
>> +					   || p[signedp + 1] == 'b'
>> +					   || p[signedp + 1] == 'B'))

This looks wrong, as "0b" and "0B" can be produced by hexadecimal 
formats like "%.2X". Better would be to replace the entire expression 
with (!float_conversion && p[signedp] == '0' && p[signedp + 1] == 
conversion), perhaps with a comment explaining that "== conversion".


> We need instead
> either to provide our own implementation of that or maybe use Gnulib
> printf.

Gnulib printf would bring in a lot of stuff: converting floating point 
numbers portably, doing multibyte conversion Gnulib style, etc. Although 
it would work, it's overkill.

Instead, I suggest changing styled_format's 'b' and 'B' processing to 
convert fixnums to bignums and then "goto bignum_arg". On nicer 
platforms where plain printf works (e.g., glibc 2.35+) this step can be 
omitted as an optimization, but that's icing on the cake and could be 
done in a later patch.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79990; Package emacs. (Fri, 12 Dec 2025 20:15:02 GMT) Full text and rfc822 format available.

Message #17 received at 79990 <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: "Jacob S. Gordon" <jacob.as.gordon <at> gmail.com>, Eli Zaretskii <eliz <at> gnu.org>
Cc: 79990 <at> debbugs.gnu.org
Subject: Re: bug#79990: [PATCH v1] Add binary format specifications '%b' and
 '%B'
Date: Fri, 12 Dec 2025 12:14:42 -0800
On 2025-12-12 08:23, Jacob S. Gordon wrote:
> On 2025-12-12 03:04, Eli Zaretskii wrote:
>>> +an integer.  Negative integers are formatted in a platform-dependent
>>> +way.
>>
>> Don't we want the negative values to be formatted the same on all
>> platforms?  And if we leave this platform-dependent, then how will
>> tests you add work on the platforms where the result is different from
>> what the tests assume?
> 
> I took this warning from the %o and %x descriptions, which seems to be
> associated with the ‘binary-as-unsigned’ option.  IIUC the default
> behaviour is portable, so I think these statements could be sharpened
> or combined into a paragraph about ‘binary-as-unsigned’.  Is this
> correct Paul?

Sounds right: %b, %o and %x are all in the same boat.

Come to think of it, how about if we remove binary-as-unsigned instead? 
In practice that experimental variable seems to cause more trouble than 
it hurts. The only response we got[1] to our request for why it's useful 
was actually more an illustration why it's harmful, as the code using it 
mishandled some negative numbers[2].

[1]: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32252#167
[2]: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32252#174




This bug report was last modified 4 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.