scratch – Blame information for rev 120

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