regular-expressions.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.possibleElisions = exports.simpleRegularExpression = exports.ADDRESS_BOUNDARY = exports.padGroup = exports.groupPossibilities = void 0;
  27. const v6 = __importStar(require("./constants"));
  28. const sprintf_js_1 = require("sprintf-js");
  29. function groupPossibilities(possibilities) {
  30. return (0, sprintf_js_1.sprintf)('(%s)', possibilities.join('|'));
  31. }
  32. exports.groupPossibilities = groupPossibilities;
  33. function padGroup(group) {
  34. if (group.length < 4) {
  35. return (0, sprintf_js_1.sprintf)('0{0,%d}%s', 4 - group.length, group);
  36. }
  37. return group;
  38. }
  39. exports.padGroup = padGroup;
  40. exports.ADDRESS_BOUNDARY = '[^A-Fa-f0-9:]';
  41. function simpleRegularExpression(groups) {
  42. const zeroIndexes = [];
  43. groups.forEach((group, i) => {
  44. const groupInteger = parseInt(group, 16);
  45. if (groupInteger === 0) {
  46. zeroIndexes.push(i);
  47. }
  48. });
  49. // You can technically elide a single 0, this creates the regular expressions
  50. // to match that eventuality
  51. const possibilities = zeroIndexes.map((zeroIndex) => groups
  52. .map((group, i) => {
  53. if (i === zeroIndex) {
  54. const elision = i === 0 || i === v6.GROUPS - 1 ? ':' : '';
  55. return groupPossibilities([padGroup(group), elision]);
  56. }
  57. return padGroup(group);
  58. })
  59. .join(':'));
  60. // The simplest case
  61. possibilities.push(groups.map(padGroup).join(':'));
  62. return groupPossibilities(possibilities);
  63. }
  64. exports.simpleRegularExpression = simpleRegularExpression;
  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((0, sprintf_js_1.sprintf)('%s(:0{1,4}){1,%d}', left, elidedGroups - 1));
  83. // 5. elision from the right side
  84. possibilities.push((0, sprintf_js_1.sprintf)('(0{1,4}:){1,%d}%s', elidedGroups - 1, right));
  85. // 6. no elision
  86. possibilities.push((0, sprintf_js_1.sprintf)('(0{1,4}:){%d}0{1,4}', elidedGroups - 1));
  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, sprintf_js_1.sprintf)('(0{1,4}:){%d}:(0{1,4}:){%d}0{1,4}', position, elidedGroups - position - groups - 1));
  91. }
  92. }
  93. return groupPossibilities(possibilities);
  94. }
  95. exports.possibleElisions = possibleElisions;
  96. //# sourceMappingURL=regular-expressions.js.map