regular-expressions.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.ADDRESS_BOUNDARY = void 0;
  27. exports.groupPossibilities = groupPossibilities;
  28. exports.padGroup = padGroup;
  29. exports.simpleRegularExpression = simpleRegularExpression;
  30. exports.possibleElisions = possibleElisions;
  31. const v6 = __importStar(require("./constants"));
  32. function groupPossibilities(possibilities) {
  33. return `(${possibilities.join('|')})`;
  34. }
  35. function padGroup(group) {
  36. if (group.length < 4) {
  37. return `0{0,${4 - group.length}}${group}`;
  38. }
  39. return group;
  40. }
  41. exports.ADDRESS_BOUNDARY = '[^A-Fa-f0-9:]';
  42. function simpleRegularExpression(groups) {
  43. const zeroIndexes = [];
  44. groups.forEach((group, i) => {
  45. const groupInteger = parseInt(group, 16);
  46. if (groupInteger === 0) {
  47. zeroIndexes.push(i);
  48. }
  49. });
  50. // You can technically elide a single 0, this creates the regular expressions
  51. // to match that eventuality
  52. const possibilities = zeroIndexes.map((zeroIndex) => groups
  53. .map((group, i) => {
  54. if (i === zeroIndex) {
  55. const elision = i === 0 || i === v6.GROUPS - 1 ? ':' : '';
  56. return groupPossibilities([padGroup(group), elision]);
  57. }
  58. return padGroup(group);
  59. })
  60. .join(':'));
  61. // The simplest case
  62. possibilities.push(groups.map(padGroup).join(':'));
  63. return groupPossibilities(possibilities);
  64. }
  65. function possibleElisions(elidedGroups, moreLeft, moreRight) {
  66. const left = moreLeft ? '' : ':';
  67. const right = moreRight ? '' : ':';
  68. const possibilities = [];
  69. // 1. elision of everything (::)
  70. if (!moreLeft && !moreRight) {
  71. possibilities.push('::');
  72. }
  73. // 2. complete elision of the middle
  74. if (moreLeft && moreRight) {
  75. possibilities.push('');
  76. }
  77. if ((moreRight && !moreLeft) || (!moreRight && moreLeft)) {
  78. // 3. complete elision of one side
  79. possibilities.push(':');
  80. }
  81. // 4. elision from the left side
  82. possibilities.push(`${left}(:0{1,4}){1,${elidedGroups - 1}}`);
  83. // 5. elision from the right side
  84. possibilities.push(`(0{1,4}:){1,${elidedGroups - 1}}${right}`);
  85. // 6. no elision
  86. possibilities.push(`(0{1,4}:){${elidedGroups - 1}}0{1,4}`);
  87. // 7. elision (including sloppy elision) from the middle
  88. for (let groups = 1; groups < elidedGroups - 1; groups++) {
  89. for (let position = 1; position < elidedGroups - groups; position++) {
  90. possibilities.push(`(0{1,4}:){${position}}:(0{1,4}:){${elidedGroups - position - groups - 1}}0{1,4}`);
  91. }
  92. }
  93. return groupPossibilities(possibilities);
  94. }
  95. //# sourceMappingURL=regular-expressions.js.map