tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

parse-literal.js (916B)


      1 // Valid radices for the BigInt constructor.
      2 const radices = [
      3  {
      4    radix: 2,
      5    prefix: "0b",
      6  },
      7  {
      8    radix: 8,
      9    prefix: "0o",
     10  },
     11  {
     12    radix: 10,
     13    prefix: "",
     14  },
     15  {
     16    radix: 16,
     17    prefix: "0x",
     18  },
     19 ];
     20 
     21 // Leading zeroes are ignored.
     22 const zeroes = [
     23  "", "0", "00",
     24 ];
     25 
     26 // Generate BigInt literals with up to 200 digits to cover the case when the
     27 // result has more than one BigInt::Digit.
     28 function* literals(radix, prefix) {
     29  const digits = "0123456789abcdefghijklmnopqrstuvwxyz";
     30  assertEq(radix < digits.length, true);
     31 
     32  let n = 0n;
     33  let s = prefix;
     34  for (let i = 1; i <= 200; ++i) {
     35    let d = i % radix;
     36    n = n * BigInt(radix) + BigInt(d);
     37    s += digits[d];
     38 
     39    yield [s, n];
     40  }
     41 }
     42 
     43 for (let {radix, prefix} of radices) {
     44  for (let zero of zeroes) {
     45    for (let [s, n] of literals(radix, prefix + zero)) {
     46      assertEq(BigInt(s), n, `literal: "${s}"`);
     47    }
     48  }
     49 }