[PATCH] allow binary coded literals

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] allow binary coded literals

René Rebe
Hi all,

this simple two-liner allows for binary coded literal constants
in the lexxer.

  Signed-off-by: René Rebe <[hidden email]>

===================================================================
--- src/lobject.c
+++ src/lobject.c
@@ -93,6 +93,8 @@
   if (endptr == s) return 0;  /* conversion failed */
   if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */
     *result = cast_num(strtoul(s, &endptr, 16));
+  else if (*endptr == 'b' || *endptr == 'B')  /* maybe an binary constant? */
+    *result = cast_num(strtoul(endptr + 1, &endptr, 2));
   if (*endptr == '\0') return 1;  /* most common case */
   while (isspace(cast(unsigned char, *endptr))) endptr++;
   if (*endptr != '\0') return 0;  /* invalid trailing characters? */


--
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

Tony Finch
On Wed, 4 Feb 2009, Rene Rebe wrote:
>
> this simple two-liner allows for binary coded literal constants
> in the lexxer.

0b is not supported by many versions of strtol().

Tony.
-- 
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

Sam Roberts
On Thu, Feb 5, 2009 at 9:39 AM, Tony Finch <[hidden email]> wrote:
> On Wed, 4 Feb 2009, Rene Rebe wrote:
>>
>> this simple two-liner allows for binary coded literal constants
>> in the lexxer.
>
> 0b is not supported by many versions of strtol().

It appears the patch doesn't pass the "0b" prefix to strtol(), it
strips it, and requests base conversion using the last argument to
strtol().

I like it, its nice for bit fields, and it would be nice to have a %b
string.format specifier, too.

Don't know if it effects backwards compatibility. Because 0b used to
be illegal, it doesn't change the meaning of legal programs. Unless
code is relying on 0b being an invalid prefix...

Cheers,
Sam

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

RJP Computing
On Thu, Feb 5, 2009 at 3:04 PM, Sam Roberts <[hidden email]> wrote:
It appears the patch doesn't pass the "0b" prefix to strtol(), it
strips it, and requests base conversion using the last argument to
strtol().

I like it, its nice for bit fields, and it would be nice to have a %b
string.format specifier, too.

I would love to see this in Lua 5.2.
--
Regards,
Ryan
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

Joseph Stewart
Really non-standard, but should I mention the erlang format (maybe ada, too) for N-base constants again?


to re-cap:

2#1001 = binary
8#777 = octal
16#deadbeef = hexadecimal
36#hello = base 36

I have a stale 5.0 patch I can update if anyone cares.

-joe

On Thu, Feb 5, 2009 at 4:54 PM, RJP Computing <[hidden email]> wrote:
On Thu, Feb 5, 2009 at 3:04 PM, Sam Roberts <[hidden email]> wrote:
It appears the patch doesn't pass the "0b" prefix to strtol(), it
strips it, and requests base conversion using the last argument to
strtol().

I like it, its nice for bit fields, and it would be nice to have a %b
string.format specifier, too.

I would love to see this in Lua 5.2.
--
Regards,
Ryan

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

Luiz Henrique de Figueiredo
In reply to this post by RJP Computing
> > I like it, its nice for bit fields, and it would be nice to have a %b
> > string.format specifier, too.
> 
> I would love to see this in Lua 5.2.

sprintf does not support %b so it's unlikely that it'll be added...
(or does C99 sprintf support binary output?)

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] allow binary coded literals

Farmbuyer
On Thu, Feb 5, 2009 at 6:35 PM, Luiz Henrique de Figueiredo
<[hidden email]> wrote:
>> > I like it, its nice for bit fields, and it would be nice to have a %b
>> > string.format specifier, too.
>>
>> I would love to see this in Lua 5.2.
>
> sprintf does not support %b so it's unlikely that it'll be added...
> (or does C99 sprintf support binary output?)

No, %b is not supported by sprintf.  I think output is a different
problem than input, however; the 0b prefix for reading binary literals
(the original proposal) would still be a nice change.