enkimute / ganja.js

:triangular_ruler: Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) -
MIT License
1.52k stars 107 forks source link

Hex RegEx misses hex values #138

Open kungfooman opened 2 years ago

kungfooman commented 2 years ago

There is this RegEx:

https://github.com/enkimute/ganja.js/blob/5aca551cffd3f7c6b77c5fd31a2b31d09b8833a0/ganja.js#L1606

Broken down between | with example match for illustration:

'123.123E-123'.match(/^\d+[.]{0,1}\d*[E][+-]{0,1}\d*/g); // ✔️
'.123E-123'   .match(/^\.\d+[E][+-]{0,1}\d*/g         ); // ✔️
'0x123'       .match(/^0x\d+/g                        ); // ✔️
'0xABC'       .match(/^0x\d+/g                        ); // ❌
'123.123'     .match(/^\d+[.]{0,1}\d*/g               ); // ✔️
'.123'        .match(/^\.\d+/g                        ); // ✔️

The result is a token stream containing too many tokens:

image

I propose this RegEx, adding capital x via [xX], a-f and A-F:

'0xaAbB'.match(/^0[xX][\da-fA-F]+/g); // ✔️
'0XaAbB'.match(/^0[xX][\da-fA-F]+/g); // ✔️
'0x1234'.match(/^0[xX][\da-fA-F]+/g); // ✔️
'0X1234'.match(/^0[xX][\da-fA-F]+/g); // ✔️