PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/drush/composer/vendor/symfony/finder/Glob.php

https://bitbucket.org/kbasarab/kbasarab_vim
PHP | 104 lines | 56 code | 8 blank | 40 comment | 19 complexity | 9f1012cf3c688ec846f1c9f354529ea6 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder;
  11. /**
  12. * Glob matches globbing patterns against text.
  13. *
  14. * if match_glob("foo.*", "foo.bar") echo "matched\n";
  15. *
  16. * // prints foo.bar and foo.baz
  17. * $regex = glob_to_regex("foo.*");
  18. * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
  19. * {
  20. * if (/$regex/) echo "matched: $car\n";
  21. * }
  22. *
  23. * Glob implements glob(3) style matching that can be used to match
  24. * against text, rather than fetching names from a filesystem.
  25. *
  26. * Based on the Perl Text::Glob module.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com> PHP port
  29. * @author Richard Clamp <richardc@unixbeard.net> Perl version
  30. * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
  31. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  32. */
  33. class Glob
  34. {
  35. /**
  36. * Returns a regexp which is the equivalent of the glob pattern.
  37. *
  38. * @param string $glob The glob pattern
  39. * @param bool $strictLeadingDot
  40. * @param bool $strictWildcardSlash
  41. * @param string $delimiter Optional delimiter
  42. *
  43. * @return string regex The regexp
  44. */
  45. public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
  46. {
  47. $firstByte = true;
  48. $escaping = false;
  49. $inCurlies = 0;
  50. $regex = '';
  51. $sizeGlob = strlen($glob);
  52. for ($i = 0; $i < $sizeGlob; ++$i) {
  53. $car = $glob[$i];
  54. if ($firstByte) {
  55. if ($strictLeadingDot && '.' !== $car) {
  56. $regex .= '(?=[^\.])';
  57. }
  58. $firstByte = false;
  59. }
  60. if ('/' === $car) {
  61. $firstByte = true;
  62. }
  63. if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
  64. $regex .= "\\$car";
  65. } elseif ('*' === $car) {
  66. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  67. } elseif ('?' === $car) {
  68. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  69. } elseif ('{' === $car) {
  70. $regex .= $escaping ? '\\{' : '(';
  71. if (!$escaping) {
  72. ++$inCurlies;
  73. }
  74. } elseif ('}' === $car && $inCurlies) {
  75. $regex .= $escaping ? '}' : ')';
  76. if (!$escaping) {
  77. --$inCurlies;
  78. }
  79. } elseif (',' === $car && $inCurlies) {
  80. $regex .= $escaping ? ',' : '|';
  81. } elseif ('\\' === $car) {
  82. if ($escaping) {
  83. $regex .= '\\\\';
  84. $escaping = false;
  85. } else {
  86. $escaping = true;
  87. }
  88. continue;
  89. } else {
  90. $regex .= $car;
  91. }
  92. $escaping = false;
  93. }
  94. return $delimiter.'^'.$regex.'$'.$delimiter;
  95. }
  96. }