You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.3 KiB

2 years ago
  1. 'use strict';
  2. const path = require('path');
  3. const WIN_SLASH = '\\\\/';
  4. const WIN_NO_SLASH = `[^${WIN_SLASH}]`;
  5. /**
  6. * Posix glob regex
  7. */
  8. const DOT_LITERAL = '\\.';
  9. const PLUS_LITERAL = '\\+';
  10. const QMARK_LITERAL = '\\?';
  11. const SLASH_LITERAL = '\\/';
  12. const ONE_CHAR = '(?=.)';
  13. const QMARK = '[^/]';
  14. const END_ANCHOR = `(?:${SLASH_LITERAL}|$)`;
  15. const START_ANCHOR = `(?:^|${SLASH_LITERAL})`;
  16. const DOTS_SLASH = `${DOT_LITERAL}{1,2}${END_ANCHOR}`;
  17. const NO_DOT = `(?!${DOT_LITERAL})`;
  18. const NO_DOTS = `(?!${START_ANCHOR}${DOTS_SLASH})`;
  19. const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
  20. const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
  21. const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
  22. const STAR = `${QMARK}*?`;
  23. const POSIX_CHARS = {
  24. DOT_LITERAL,
  25. PLUS_LITERAL,
  26. QMARK_LITERAL,
  27. SLASH_LITERAL,
  28. ONE_CHAR,
  29. QMARK,
  30. END_ANCHOR,
  31. DOTS_SLASH,
  32. NO_DOT,
  33. NO_DOTS,
  34. NO_DOT_SLASH,
  35. NO_DOTS_SLASH,
  36. QMARK_NO_DOT,
  37. STAR,
  38. START_ANCHOR
  39. };
  40. /**
  41. * Windows glob regex
  42. */
  43. const WINDOWS_CHARS = {
  44. ...POSIX_CHARS,
  45. SLASH_LITERAL: `[${WIN_SLASH}]`,
  46. QMARK: WIN_NO_SLASH,
  47. STAR: `${WIN_NO_SLASH}*?`,
  48. DOTS_SLASH: `${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$)`,
  49. NO_DOT: `(?!${DOT_LITERAL})`,
  50. NO_DOTS: `(?!(?:^|[${WIN_SLASH}])${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  51. NO_DOT_SLASH: `(?!${DOT_LITERAL}{0,1}(?:[${WIN_SLASH}]|$))`,
  52. NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  53. QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
  54. START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
  55. END_ANCHOR: `(?:[${WIN_SLASH}]|$)`
  56. };
  57. /**
  58. * POSIX Bracket Regex
  59. */
  60. const POSIX_REGEX_SOURCE = {
  61. alnum: 'a-zA-Z0-9',
  62. alpha: 'a-zA-Z',
  63. ascii: '\\x00-\\x7F',
  64. blank: ' \\t',
  65. cntrl: '\\x00-\\x1F\\x7F',
  66. digit: '0-9',
  67. graph: '\\x21-\\x7E',
  68. lower: 'a-z',
  69. print: '\\x20-\\x7E ',
  70. punct: '\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
  71. space: ' \\t\\r\\n\\v\\f',
  72. upper: 'A-Z',
  73. word: 'A-Za-z0-9_',
  74. xdigit: 'A-Fa-f0-9'
  75. };
  76. module.exports = {
  77. MAX_LENGTH: 1024 * 64,
  78. POSIX_REGEX_SOURCE,
  79. // regular expressions
  80. REGEX_BACKSLASH: /\\(?![*+?^${}(|)[\]])/g,
  81. REGEX_NON_SPECIAL_CHARS: /^[^@![\].,$*+?^{}()|\\/]+/,
  82. REGEX_SPECIAL_CHARS: /[-*+?.^${}(|)[\]]/,
  83. REGEX_SPECIAL_CHARS_BACKREF: /(\\?)((\W)(\3*))/g,
  84. REGEX_SPECIAL_CHARS_GLOBAL: /([-*+?.^${}(|)[\]])/g,
  85. REGEX_REMOVE_BACKSLASH: /(?:\[.*?[^\\]\]|\\(?=.))/g,
  86. // Replace globs with equivalent patterns to reduce parsing time.
  87. REPLACEMENTS: {
  88. '***': '*',
  89. '**/**': '**',
  90. '**/**/**': '**'
  91. },
  92. // Digits
  93. CHAR_0: 48, /* 0 */
  94. CHAR_9: 57, /* 9 */
  95. // Alphabet chars.
  96. CHAR_UPPERCASE_A: 65, /* A */
  97. CHAR_LOWERCASE_A: 97, /* a */
  98. CHAR_UPPERCASE_Z: 90, /* Z */
  99. CHAR_LOWERCASE_Z: 122, /* z */
  100. CHAR_LEFT_PARENTHESES: 40, /* ( */
  101. CHAR_RIGHT_PARENTHESES: 41, /* ) */
  102. CHAR_ASTERISK: 42, /* * */
  103. // Non-alphabetic chars.
  104. CHAR_AMPERSAND: 38, /* & */
  105. CHAR_AT: 64, /* @ */
  106. CHAR_BACKWARD_SLASH: 92, /* \ */
  107. CHAR_CARRIAGE_RETURN: 13, /* \r */
  108. CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
  109. CHAR_COLON: 58, /* : */
  110. CHAR_COMMA: 44, /* , */
  111. CHAR_DOT: 46, /* . */
  112. CHAR_DOUBLE_QUOTE: 34, /* " */
  113. CHAR_EQUAL: 61, /* = */
  114. CHAR_EXCLAMATION_MARK: 33, /* ! */
  115. CHAR_FORM_FEED: 12, /* \f */
  116. CHAR_FORWARD_SLASH: 47, /* / */
  117. CHAR_GRAVE_ACCENT: 96, /* ` */
  118. CHAR_HASH: 35, /* # */
  119. CHAR_HYPHEN_MINUS: 45, /* - */
  120. CHAR_LEFT_ANGLE_BRACKET: 60, /* < */
  121. CHAR_LEFT_CURLY_BRACE: 123, /* { */
  122. CHAR_LEFT_SQUARE_BRACKET: 91, /* [ */
  123. CHAR_LINE_FEED: 10, /* \n */
  124. CHAR_NO_BREAK_SPACE: 160, /* \u00A0 */
  125. CHAR_PERCENT: 37, /* % */
  126. CHAR_PLUS: 43, /* + */
  127. CHAR_QUESTION_MARK: 63, /* ? */
  128. CHAR_RIGHT_ANGLE_BRACKET: 62, /* > */
  129. CHAR_RIGHT_CURLY_BRACE: 125, /* } */
  130. CHAR_RIGHT_SQUARE_BRACKET: 93, /* ] */
  131. CHAR_SEMICOLON: 59, /* ; */
  132. CHAR_SINGLE_QUOTE: 39, /* ' */
  133. CHAR_SPACE: 32, /* */
  134. CHAR_TAB: 9, /* \t */
  135. CHAR_UNDERSCORE: 95, /* _ */
  136. CHAR_VERTICAL_LINE: 124, /* | */
  137. CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */
  138. SEP: path.sep,
  139. /**
  140. * Create EXTGLOB_CHARS
  141. */
  142. extglobChars(chars) {
  143. return {
  144. '!': { type: 'negate', open: '(?:(?!(?:', close: `))${chars.STAR})` },
  145. '?': { type: 'qmark', open: '(?:', close: ')?' },
  146. '+': { type: 'plus', open: '(?:', close: ')+' },
  147. '*': { type: 'star', open: '(?:', close: ')*' },
  148. '@': { type: 'at', open: '(?:', close: ')' }
  149. };
  150. },
  151. /**
  152. * Create GLOB_CHARS
  153. */
  154. globChars(win32) {
  155. return win32 === true ? WINDOWS_CHARS : POSIX_CHARS;
  156. }
  157. };