[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: New library num-base-converters
From: |
Stefan Monnier |
Subject: |
Re: New library num-base-converters |
Date: |
Tue, 15 Aug 2017 11:24:05 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) |
>>> +(defun nbc-number-base-converter (num base-in base-out)
>>> + "Translate NUM, a string representing an integer, to a different base.
>>> +BASE-IN, an integer, is the basis of the input NUM.
>>> +BASE-OUT, an integer, is the basis to display NUM."
>> Having a string as both input and output is a bad API, I think.
> It's better API than not having API at all; it provides me the flexibility
> to handle input as '0xff', '#xff' or 'ff'. Not that bad.
What do you mean by "handle"? It only lets you convert the string to
a string in a different base, which is rarely what you need.
Instead you will generally want to extract the number it represents,
then modify it a bit than turn it back into a string in a given base.
>> All one needs is basically to add a `base` arg to number-to-string
>> (there's already one for string-to-number).
> Then we are again limited to 2 < b < 16 bases.
No. We can similarly extend string-to-number to accept bases > 16.
If you want to provide it without changing Emacs's core, you can make it
a package that exports <pkg>-string-to-number and
<pkg>-number-to-string, but the API should separate the conversion from
a string to a number and back, rather than force you to conversions
between strings, which force you to do things like "baseN-to-dec, then
string-to-number, do something, then number-to-string, then
dec-to-baseN", which is just silly (and inefficient to boot).
Stefan
Re: New library num-base-converters, Stefan Monnier, 2017/08/15