[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Binary notation for integers: 0b[10]+
- From: Ico Doornekamp <lua@...>
- Date: Mon, 01 Dec 2014 15:33:05 +0100
With proper integer support and binary operators in place in Lua 5.3, I would
like to propose something like the following to allow integers to be expressed
in binary format in Lua code as 0b[10]+:
--- lua-5.3.0-beta/src/lobject.c 2014-10-17 18:28:21.000000000 +0200
+++ lua-5.3.0-beta-ico/src/lobject.c 2014-12-01 15:13:29.557580628 +0100
@@ -280,6 +280,14 @@ static const char *l_str2int (const char
empty = 0;
}
}
+ else if (s[0] == '0' &&
+ (s[1] == 'b' || s[1] == 'B')) { /* bin? */
+ s += 2; /* skip '0b' */
+ for (; *s == '0' || *s == '1'; s++) {
+ a = a * 2 + luaO_hexavalue(*s);
+ empty = 0;
+ }
+ }
else { /* decimal */
for (; lisdigit(cast_uchar(*s)); s++) {
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
Examples:
Lua 5.3.0 (beta) Copyright (C) 1994-2014 Lua.org, PUC-Rio
> 0b10000000
128
> 0b01
1
> 0b11111111111111111111111111111111111
34359738367
As the above patch is small and unintrusive, would inclusion in Lua 5.3 be
feasible and/or usable?
By the way, I *love* the new hexadecimal 'P' binary exponent!
--
:wq
^X^Cy^K^X^C^C^C^C