Preg.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /*
  3. * This file is part of composer/pcre.
  4. *
  5. * (c) Composer <https://github.com/composer>
  6. *
  7. * For the full copyright and license information, please view
  8. * the LICENSE file that was distributed with this source code.
  9. */
  10. namespace Composer\Pcre;
  11. class Preg
  12. {
  13. /** @internal */
  14. public const ARRAY_MSG = '$subject as an array is not supported. You can use \'foreach\' instead.';
  15. /** @internal */
  16. public const INVALID_TYPE_MSG = '$subject must be a string, %s given.';
  17. /**
  18. * @param non-empty-string $pattern
  19. * @param array<mixed> $matches Set by method
  20. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  21. * @return 0|1
  22. *
  23. * @param-out array<int|string, string|null> $matches
  24. */
  25. public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
  26. {
  27. self::checkOffsetCapture($flags, 'matchWithOffsets');
  28. $result = preg_match($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL, $offset);
  29. if ($result === false) {
  30. throw PcreException::fromFunction('preg_match', $pattern);
  31. }
  32. return $result;
  33. }
  34. /**
  35. * Variant of `match()` which outputs non-null matches (or throws)
  36. *
  37. * @param non-empty-string $pattern
  38. * @param array<mixed> $matches Set by method
  39. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  40. * @return 0|1
  41. * @throws UnexpectedNullMatchException
  42. *
  43. * @param-out array<int|string, string> $matches
  44. */
  45. public static function matchStrictGroups(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
  46. {
  47. $result = self::match($pattern, $subject, $matchesInternal, $flags, $offset);
  48. $matches = self::enforceNonNullMatches($pattern, $matchesInternal, 'match');
  49. return $result;
  50. }
  51. /**
  52. * Runs preg_match with PREG_OFFSET_CAPTURE
  53. *
  54. * @param non-empty-string $pattern
  55. * @param array<mixed> $matches Set by method
  56. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_OFFSET_CAPTURE are always set, no other flags are supported
  57. * @return 0|1
  58. *
  59. * @param-out array<int|string, array{string|null, int<-1, max>}> $matches
  60. */
  61. public static function matchWithOffsets(string $pattern, string $subject, ?array &$matches, int $flags = 0, int $offset = 0): int
  62. {
  63. $result = preg_match($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL | PREG_OFFSET_CAPTURE, $offset);
  64. if ($result === false) {
  65. throw PcreException::fromFunction('preg_match', $pattern);
  66. }
  67. return $result;
  68. }
  69. /**
  70. * @param non-empty-string $pattern
  71. * @param array<mixed> $matches Set by method
  72. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  73. * @return 0|positive-int
  74. *
  75. * @param-out array<int|string, list<string|null>> $matches
  76. */
  77. public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
  78. {
  79. self::checkOffsetCapture($flags, 'matchAllWithOffsets');
  80. self::checkSetOrder($flags);
  81. $result = preg_match_all($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL, $offset);
  82. if (!is_int($result)) { // PHP < 8 may return null, 8+ returns int|false
  83. throw PcreException::fromFunction('preg_match_all', $pattern);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * Variant of `match()` which outputs non-null matches (or throws)
  89. *
  90. * @param non-empty-string $pattern
  91. * @param array<mixed> $matches Set by method
  92. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  93. * @return 0|positive-int
  94. * @throws UnexpectedNullMatchException
  95. *
  96. * @param-out array<int|string, list<string>> $matches
  97. */
  98. public static function matchAllStrictGroups(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
  99. {
  100. $result = self::matchAll($pattern, $subject, $matchesInternal, $flags, $offset);
  101. $matches = self::enforceNonNullMatchAll($pattern, $matchesInternal, 'matchAll');
  102. return $result;
  103. }
  104. /**
  105. * Runs preg_match_all with PREG_OFFSET_CAPTURE
  106. *
  107. * @param non-empty-string $pattern
  108. * @param array<mixed> $matches Set by method
  109. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
  110. * @return 0|positive-int
  111. *
  112. * @param-out array<int|string, list<array{string|null, int<-1, max>}>> $matches
  113. */
  114. public static function matchAllWithOffsets(string $pattern, string $subject, ?array &$matches, int $flags = 0, int $offset = 0): int
  115. {
  116. self::checkSetOrder($flags);
  117. $result = preg_match_all($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL | PREG_OFFSET_CAPTURE, $offset);
  118. if (!is_int($result)) { // PHP < 8 may return null, 8+ returns int|false
  119. throw PcreException::fromFunction('preg_match_all', $pattern);
  120. }
  121. return $result;
  122. }
  123. /**
  124. * @param string|string[] $pattern
  125. * @param string|string[] $replacement
  126. * @param string $subject
  127. * @param int $count Set by method
  128. *
  129. * @param-out int<0, max> $count
  130. */
  131. public static function replace($pattern, $replacement, $subject, int $limit = -1, ?int &$count = null): string
  132. {
  133. if (!is_scalar($subject)) {
  134. if (is_array($subject)) {
  135. throw new \InvalidArgumentException(static::ARRAY_MSG);
  136. }
  137. throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
  138. }
  139. $result = preg_replace($pattern, $replacement, $subject, $limit, $count);
  140. if ($result === null) {
  141. throw PcreException::fromFunction('preg_replace', $pattern);
  142. }
  143. return $result;
  144. }
  145. /**
  146. * @param string|string[] $pattern
  147. * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string|null, int<-1, max>}>): string) : callable(array<int|string, string|null>): string) $replacement
  148. * @param string $subject
  149. * @param int $count Set by method
  150. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  151. *
  152. * @param-out int<0, max> $count
  153. */
  154. public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, ?int &$count = null, int $flags = 0): string
  155. {
  156. if (!is_scalar($subject)) {
  157. if (is_array($subject)) {
  158. throw new \InvalidArgumentException(static::ARRAY_MSG);
  159. }
  160. throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
  161. }
  162. $result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
  163. if ($result === null) {
  164. throw PcreException::fromFunction('preg_replace_callback', $pattern);
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Variant of `replaceCallback()` which outputs non-null matches (or throws)
  170. *
  171. * @param string $pattern
  172. * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string, int<0, max>}>): string) : callable(array<int|string, string>): string) $replacement
  173. * @param string $subject
  174. * @param int $count Set by method
  175. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  176. *
  177. * @param-out int<0, max> $count
  178. */
  179. public static function replaceCallbackStrictGroups(string $pattern, callable $replacement, $subject, int $limit = -1, ?int &$count = null, int $flags = 0): string
  180. {
  181. return self::replaceCallback($pattern, function (array $matches) use ($pattern, $replacement) {
  182. return $replacement(self::enforceNonNullMatches($pattern, $matches, 'replaceCallback'));
  183. }, $subject, $limit, $count, $flags);
  184. }
  185. /**
  186. * @param ($flags is PREG_OFFSET_CAPTURE ? (array<string, callable(array<int|string, array{string|null, int<-1, max>}>): string>) : array<string, callable(array<int|string, string|null>): string>) $pattern
  187. * @param string $subject
  188. * @param int $count Set by method
  189. * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
  190. *
  191. * @param-out int<0, max> $count
  192. */
  193. public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, ?int &$count = null, int $flags = 0): string
  194. {
  195. if (!is_scalar($subject)) {
  196. if (is_array($subject)) {
  197. throw new \InvalidArgumentException(static::ARRAY_MSG);
  198. }
  199. throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
  200. }
  201. $result = preg_replace_callback_array($pattern, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
  202. if ($result === null) {
  203. $pattern = array_keys($pattern);
  204. throw PcreException::fromFunction('preg_replace_callback_array', $pattern);
  205. }
  206. return $result;
  207. }
  208. /**
  209. * @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
  210. * @return list<string>
  211. */
  212. public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
  213. {
  214. if (($flags & PREG_SPLIT_OFFSET_CAPTURE) !== 0) {
  215. throw new \InvalidArgumentException('PREG_SPLIT_OFFSET_CAPTURE is not supported as it changes the type of $matches, use splitWithOffsets() instead');
  216. }
  217. $result = preg_split($pattern, $subject, $limit, $flags);
  218. if ($result === false) {
  219. throw PcreException::fromFunction('preg_split', $pattern);
  220. }
  221. return $result;
  222. }
  223. /**
  224. * @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
  225. * @return list<array{string, int}>
  226. * @phpstan-return list<array{string, int<0, max>}>
  227. */
  228. public static function splitWithOffsets(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
  229. {
  230. $result = preg_split($pattern, $subject, $limit, $flags | PREG_SPLIT_OFFSET_CAPTURE);
  231. if ($result === false) {
  232. throw PcreException::fromFunction('preg_split', $pattern);
  233. }
  234. return $result;
  235. }
  236. /**
  237. * @template T of string|\Stringable
  238. * @param string $pattern
  239. * @param array<T> $array
  240. * @param int-mask<PREG_GREP_INVERT> $flags PREG_GREP_INVERT
  241. * @return array<T>
  242. */
  243. public static function grep(string $pattern, array $array, int $flags = 0): array
  244. {
  245. $result = preg_grep($pattern, $array, $flags);
  246. if ($result === false) {
  247. throw PcreException::fromFunction('preg_grep', $pattern);
  248. }
  249. return $result;
  250. }
  251. /**
  252. * Variant of match() which returns a bool instead of int
  253. *
  254. * @param non-empty-string $pattern
  255. * @param array<mixed> $matches Set by method
  256. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  257. *
  258. * @param-out array<int|string, string|null> $matches
  259. */
  260. public static function isMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
  261. {
  262. return (bool) static::match($pattern, $subject, $matches, $flags, $offset);
  263. }
  264. /**
  265. * Variant of `isMatch()` which outputs non-null matches (or throws)
  266. *
  267. * @param non-empty-string $pattern
  268. * @param array<mixed> $matches Set by method
  269. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  270. * @throws UnexpectedNullMatchException
  271. *
  272. * @param-out array<int|string, string> $matches
  273. */
  274. public static function isMatchStrictGroups(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
  275. {
  276. return (bool) self::matchStrictGroups($pattern, $subject, $matches, $flags, $offset);
  277. }
  278. /**
  279. * Variant of matchAll() which returns a bool instead of int
  280. *
  281. * @param non-empty-string $pattern
  282. * @param array<mixed> $matches Set by method
  283. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  284. *
  285. * @param-out array<int|string, list<string|null>> $matches
  286. */
  287. public static function isMatchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
  288. {
  289. return (bool) static::matchAll($pattern, $subject, $matches, $flags, $offset);
  290. }
  291. /**
  292. * Variant of `isMatchAll()` which outputs non-null matches (or throws)
  293. *
  294. * @param non-empty-string $pattern
  295. * @param array<mixed> $matches Set by method
  296. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  297. *
  298. * @param-out array<int|string, list<string>> $matches
  299. */
  300. public static function isMatchAllStrictGroups(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
  301. {
  302. return (bool) self::matchAllStrictGroups($pattern, $subject, $matches, $flags, $offset);
  303. }
  304. /**
  305. * Variant of matchWithOffsets() which returns a bool instead of int
  306. *
  307. * Runs preg_match with PREG_OFFSET_CAPTURE
  308. *
  309. * @param non-empty-string $pattern
  310. * @param array<mixed> $matches Set by method
  311. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  312. *
  313. * @param-out array<int|string, array{string|null, int<-1, max>}> $matches
  314. */
  315. public static function isMatchWithOffsets(string $pattern, string $subject, ?array &$matches, int $flags = 0, int $offset = 0): bool
  316. {
  317. return (bool) static::matchWithOffsets($pattern, $subject, $matches, $flags, $offset);
  318. }
  319. /**
  320. * Variant of matchAllWithOffsets() which returns a bool instead of int
  321. *
  322. * Runs preg_match_all with PREG_OFFSET_CAPTURE
  323. *
  324. * @param non-empty-string $pattern
  325. * @param array<mixed> $matches Set by method
  326. * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
  327. *
  328. * @param-out array<int|string, list<array{string|null, int<-1, max>}>> $matches
  329. */
  330. public static function isMatchAllWithOffsets(string $pattern, string $subject, ?array &$matches, int $flags = 0, int $offset = 0): bool
  331. {
  332. return (bool) static::matchAllWithOffsets($pattern, $subject, $matches, $flags, $offset);
  333. }
  334. private static function checkOffsetCapture(int $flags, string $useFunctionName): void
  335. {
  336. if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
  337. throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use ' . $useFunctionName . '() instead');
  338. }
  339. }
  340. private static function checkSetOrder(int $flags): void
  341. {
  342. if (($flags & PREG_SET_ORDER) !== 0) {
  343. throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the type of $matches');
  344. }
  345. }
  346. /**
  347. * @param array<int|string, string|null|array{string|null, int}> $matches
  348. * @return array<int|string, string>
  349. * @throws UnexpectedNullMatchException
  350. */
  351. private static function enforceNonNullMatches(string $pattern, array $matches, string $variantMethod)
  352. {
  353. foreach ($matches as $group => $match) {
  354. if (is_string($match) || (is_array($match) && is_string($match[0]))) {
  355. continue;
  356. }
  357. throw new UnexpectedNullMatchException('Pattern "'.$pattern.'" had an unexpected unmatched group "'.$group.'", make sure the pattern always matches or use '.$variantMethod.'() instead.');
  358. }
  359. /** @var array<string> */
  360. return $matches;
  361. }
  362. /**
  363. * @param array<int|string, list<string|null>> $matches
  364. * @return array<int|string, list<string>>
  365. * @throws UnexpectedNullMatchException
  366. */
  367. private static function enforceNonNullMatchAll(string $pattern, array $matches, string $variantMethod)
  368. {
  369. foreach ($matches as $group => $groupMatches) {
  370. foreach ($groupMatches as $match) {
  371. if (null === $match) {
  372. throw new UnexpectedNullMatchException('Pattern "'.$pattern.'" had an unexpected unmatched group "'.$group.'", make sure the pattern always matches or use '.$variantMethod.'() instead.');
  373. }
  374. }
  375. }
  376. /** @var array<int|string, list<string>> */
  377. return $matches;
  378. }
  379. }