cidr-tools 12.1.0 → 12.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +51 -49
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -241,17 +241,19 @@ function subparts4(pStart, pEnd, output) {
241
241
  }
242
242
  }
243
243
  function subparts6(pStart, pEnd, output) {
244
+ const fullSize = pEnd - pStart + 1n;
245
+ const startLowBit = pStart & -pStart;
246
+ if ((fullSize & fullSize - 1n) === 0n && (startLowBit === 0n || startLowBit >= fullSize)) {
247
+ output.push(stringifyIp({
248
+ number: pStart,
249
+ version: 6
250
+ }) + prefixStrings[129 - bigintBitLength(fullSize)]);
251
+ return;
252
+ }
244
253
  let start = pStart;
245
254
  while (start <= pEnd) {
246
255
  const size = pEnd - start + 1n;
247
256
  const lowBit = start & -start;
248
- if ((size & size - 1n) === 0n && (lowBit === 0n || lowBit >= size)) {
249
- output.push(stringifyIp({
250
- number: start,
251
- version: 6
252
- }) + prefixStrings[129 - bigintBitLength(size)]);
253
- return;
254
- }
255
257
  const blockSize = lowBit !== 0n && lowBit <= size ? lowBit : biggestPowerOfTwo(size);
256
258
  output.push(stringifyIp({
257
259
  number: start,
@@ -547,60 +549,60 @@ function containsCidr(a, b) {
547
549
  }
548
550
  if (v4b.length > 0) {
549
551
  if (v4a.length === 0) return false;
550
- if (v4b.length === 1) {
551
- const ts = v4b[0].start, te = v4b[0].end;
552
- let found = false;
553
- for (const a of v4a) if (a.start <= ts && a.end >= te) {
554
- found = true;
552
+ let sorted = false;
553
+ for (const target of v4b) {
554
+ const ts = target.start, te = target.end;
555
+ let covered = false;
556
+ for (const iv of v4a) if (iv.start <= ts && iv.end >= te) {
557
+ covered = true;
555
558
  break;
556
559
  }
557
- if (!found) return false;
558
- } else {
559
- v4a.sort(cmpV4Start);
560
- const maxEnd = new Array(v4a.length);
561
- maxEnd[0] = v4a[0].end;
562
- for (let i = 1; i < v4a.length; i++) maxEnd[i] = Math.max(v4a[i].end, maxEnd[i - 1]);
563
- for (const target of v4b) {
564
- let lo = 0, hi = v4a.length - 1;
565
- let idx = -1;
566
- while (lo <= hi) {
567
- const mid = lo + hi >> 1;
568
- if (v4a[mid].start <= target.start) {
569
- idx = mid;
570
- lo = mid + 1;
571
- } else hi = mid - 1;
560
+ if (covered) continue;
561
+ if (!sorted) {
562
+ v4a.sort(cmpV4Start);
563
+ sorted = true;
564
+ }
565
+ let cur = ts;
566
+ for (const iv of v4a) {
567
+ if (iv.start > cur) break;
568
+ if (iv.end >= cur) {
569
+ if (iv.end >= te) {
570
+ covered = true;
571
+ break;
572
+ }
573
+ cur = iv.end + 1;
572
574
  }
573
- if (idx < 0 || maxEnd[idx] < target.end) return false;
574
575
  }
576
+ if (!covered) return false;
575
577
  }
576
578
  }
577
579
  if (v6b.length > 0) {
578
580
  if (v6a.length === 0) return false;
579
- if (v6b.length === 1) {
580
- const ts = v6b[0].start, te = v6b[0].end;
581
- let found = false;
582
- for (const a of v6a) if (a.start <= ts && a.end >= te) {
583
- found = true;
581
+ let sorted = false;
582
+ for (const target of v6b) {
583
+ const ts = target.start, te = target.end;
584
+ let covered = false;
585
+ for (const iv of v6a) if (iv.start <= ts && iv.end >= te) {
586
+ covered = true;
584
587
  break;
585
588
  }
586
- if (!found) return false;
587
- } else {
588
- v6a.sort(cmpV6Start);
589
- const maxEnd = new Array(v6a.length);
590
- maxEnd[0] = v6a[0].end;
591
- for (let i = 1; i < v6a.length; i++) maxEnd[i] = v6a[i].end > maxEnd[i - 1] ? v6a[i].end : maxEnd[i - 1];
592
- for (const target of v6b) {
593
- let lo = 0, hi = v6a.length - 1;
594
- let idx = -1;
595
- while (lo <= hi) {
596
- const mid = lo + hi >> 1;
597
- if (v6a[mid].start <= target.start) {
598
- idx = mid;
599
- lo = mid + 1;
600
- } else hi = mid - 1;
589
+ if (covered) continue;
590
+ if (!sorted) {
591
+ v6a.sort(cmpV6Start);
592
+ sorted = true;
593
+ }
594
+ let cur = ts;
595
+ for (const iv of v6a) {
596
+ if (iv.start > cur) break;
597
+ if (iv.end >= cur) {
598
+ if (iv.end >= te) {
599
+ covered = true;
600
+ break;
601
+ }
602
+ cur = iv.end + 1n;
601
603
  }
602
- if (idx < 0 || maxEnd[idx] < target.end) return false;
603
604
  }
605
+ if (!covered) return false;
604
606
  }
605
607
  }
606
608
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cidr-tools",
3
- "version": "12.1.0",
3
+ "version": "12.1.1",
4
4
  "author": "silverwind <me@silverwind.io>",
5
5
  "description": "Tools to work with IPv4 and IPv6 CIDR",
6
6
  "keywords": [