Regarding the proposed Binray Literals Deprecation

Dave P. dave287091 at gmail.com
Sat Sep 10 21:13:57 UTC 2022


On Saturday, 10 September 2022 at 20:12:05 UTC, mw wrote:
> On Saturday, 10 September 2022 at 19:56:14 UTC, Walter Bright 
> wrote:
>> On 9/10/2022 11:44 AM, mw wrote:
>>> Second, why not provide the above "trivial parser" into std 
>>> lib (so nobody need to reinvent the wheel)
>>
>> Indeed, why not! Want to give it a go?
>
>
> Not me.
>
> I'm fine with: (e.g. uint32)
>
> 0b0000_0110_0110_0110_0110_0110_0110_0110
>
> I don't see any advantage of:
>
> `"...._.XX._.XX._.XX._.XX._.XX._.XX._.XX.”`
>
> over the binary literals, and it worth the effort.
>
> And I don't think the latter is more readable than the former.
>
>
> What I'm saying is that: if you insist on removing binary (I 
> hope you not), then [why not provide ...] the migration tool.

```D
ulong parse(const char[] data){
     ulong result = 0;
     foreach(ch; data){
         switch(ch){
             case '.':
                 result <<= 1;
                 break;
             case 'x': case 'X':
                 result <<= 1;
                 result |= 1;
                 break;
             case ' ': case '\t': case '\n': case '\r':
             case '_':
                 continue;
             default:
                 throw new Exception("oops");
         }
     }
     return result;
}

static assert("...".parse == 0b000);
static assert("..x".parse == 0b001);
static assert(".x.".parse == 0b010);
static assert(".xx".parse == 0b011);
static assert("x..".parse == 0b100);
static assert("x.x".parse == 0b101);
static assert("xx.".parse == 0b110);
static assert("xxx".parse == 0b111);
static assert("
         xxx
         x.x
         xxx
         ".parse == 0b111_101_111);
static assert("x.x.__x__.x.x".parse == 0b1010__1__0101);
private bool does_throw(const char[] data){
     bool caught = false;
     try { const _ = data.parse; }
     catch (Exception e){ caught = true; }
     return caught;
}
static assert(does_throw("x0x"));
static assert(does_throw("1010"));
static assert(does_throw("1010"));
```


More information about the Digitalmars-d mailing list