Applies the specified URL mask and returns parameterized matches
@return $args array
@param $pattern string
@param $url string|NULL
194360
function mask($pattern,$url=NULL) {
		if (!$url)
			$url=$this->rel($this->hive['URI']);
		$case=$this->hive['CASELESS']?'i':'';
		preg_match('/^'.
			preg_replace('/@(\w+\b)/','(?P<\1>[^\/\?]+)',
			str_replace('\*','([^\?]+)',preg_quote($pattern,'/'))).
				'\/?(?:\?.*)?$/'.$case.'um',$url,$args);
		return $args;
	}
636675
public function mask($pattern, $placeholder = null, $name = null, $value = null) 
	{
		Argument::i()
			->test(1, 'string')
			->test(2, 'string', 'null')
			->test(3, 'string', 'null')
			->test(4, 'scalar', 'null');
			
		$field = Mask::i()->setPattern($pattern);
		
		if(!is_null($name)) {
			$field->setName($name);
		}
		
		if(!is_null($placeholder)){
			$field->setHolder($placeholder);
		}
		
		if(!is_null($value)) {
			$field->setValue($value);
		}
		
		return $field;
	}
414735
private function getMatchStringParameterIndices($matchString, $pattern)
    {
        $indices = [];

        foreach ($pattern as $part) {
            if (!$this->isTokenPatternPart($part)) {
                $offset = 0;

                foreach ($indices as $currentIndex) {
                    $prefix = $currentIndex['prefix'];

                    if ($prefix == $part || substr_count($prefix, $part) || substr_count($part, $prefix)) {
                        $offset = $currentIndex['index'];
                    }
                }

                $indices[] = [
                    'prefix' => $part,
                    'index' => strpos($matchString, $part, $offset) + strlen($part)
                ];
            }
        }

        return $indices;
    }
442408
private function matchMethod($callbacks, $url)
    {
        // match a callback based on url pattern
        $methodData = false;
        $matchedParameters = false;

        // match method
        foreach ($callbacks as $pattern => $data) {
            if (($matchedParameters = $this->doesPatternMatch($pattern, $data, $url)) !== false) {
                $methodData = $data;
                break;
            } else if ($data['resourceNaming'] === false) {
                $matchedParameters = $this->tryMatchingOptionalParams($pattern, $data, $url);
                if ($matchedParameters) {
                    $methodData = $data;
                    break;
                }
            }
        }

        return [
            'methodData'        => $methodData,
            'matchedParameters' => $matchedParameters
        ];
    }
915837
public static function tokenizePattern($pattern, $isHost = false, array $requirements = [], array $defaults = [])
    {
        // left pad pattern with separator
        if (!$isHost && false === self::isSeparator(substr($pattern, 0, 1))) {
            $pattern = '/'.$pattern;
        }

        list ($path, ) = $splt = array_pad(preg_split('#\{\w+\??\}#', $pattern), 1, '/');

        if (2 > count($splt)) {
            return [new Text($path)];
        }

        $separator = $isHost ? '.' : '/';
        $matchDel = join('|', array_map('preg_quote', str_split(Ps::SEPARATORS), str_split(
            str_repeat(Ps::EXP_DELIM, strlen(Ps::SEPARATORS))
        )));

        $expr = Ps::EXP_DELIM.sprintf(
            self::VAR_REGEXP,
            $matchDel,
            preg_quote(self::L_DELIM, Ps::EXP_DELIM),
            preg_quote(self::R_DELIM, Ps::EXP_DELIM)
        ).Ps::EXP_DELIM.'x';

        preg_match_all($expr, $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

        $pos    = 0;
        $plen   = strlen($pattern);
        $nlen   = count($matches) - 1;
        $tokens = [];

        foreach ($matches as $i => $match) {
            // right hand delimiter, not always present
            $rdel = (!isset($match['rdel']) || -1 === $match['rdel'][1]) ? null : $match['rdel'];
            $ldel = -1 === $match['ldel'][1] ? null : $match['ldel'];
            $var  = -1 === $match['var'][1] ? null : $match['var'];
            $opt  = 0 === strcmp('?', substr($match[0][0], -2, 1));

            $default    = isset($defaults[$var[0]]) ? $defaults[$var[0]] : null;
            $constraint = isset($requirements[$var[0]]) ? $requirements[$var[0]] : null;

            $offset     = $match[0][1];

            // add preceeding text
            if (0 !== strlen($str = substr($pattern, $pos, $offset - $pos))) {
                $text = new Text($str, $lt = self::lastToken($tokens));
                self::pushTokens($text, $lt, $tokens);
            }

            // add left hand delimiter
            if (null !== $ldel) {
                $delim = new Delimiter($ldel[0], $lt = self::lastToken($tokens));
                self::pushTokens($delim, $lt, $tokens);
            }

            $opt = null === $default ? $opt : true;

            // add variable
            $tVar = new Variable($var[0], !$opt, $constraint, $lt = self::lastToken($tokens), null, $separator);
            self::pushTokens($tVar, $lt, $tokens);

            // add right hand delimiter
            if (null !== $rdel) {
                $delim = new Delimiter($rdel[0], $lt = self::lastToken($tokens));
                self::pushTokens($delim, $lt, $tokens);
            }

            // update current position
            $pos = $offset + strlen($match[0][0]);

            // add trailing text.
            if ($nlen === $i && $plen !== $pos) {
                $tail = substr($pattern, -($plen - $pos));

                if (0 !== strlen($tail)) {
                    $t = new Text($tail, $lt = self::lastToken($tokens));
                    self::pushTokens($t, $lt, $tokens);
                }
            }
        }

        return $tokens;
    }
1367203
protected function setFragment($pattern, IUrl $url, $encode = false, $key = ':fragment') {
        $fragment = $url->getFragment();

        if ($fragment) {
            $fragment = s('#%s', $fragment);
        }

        $pattern = s($pattern, [$key => $fragment]);

        return $pattern;
    }
616157
public function match($url = null)
    {
        $match = parent::match($url);
        $regex = $this->getRegex($this->getMatchUrl());

        if ($regex !== ' ') {
            if (preg_match("@" . ltrim($regex) . "@si", $this->getRequestedUrl(), $matches) || true === $match) {
                unset($matches[0]);
                ParameterBag::setParameters($matches);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }
282560
private function uriMatchesPattern( $requestUrl, $pattern )
	{
		if ( $requestUrl == $pattern )
		{
			return true;
		}
		elseif ( @preg_match( $pattern, '' ) !== false )
		{
			return boolval( preg_match( $pattern, $requestUrl ) );
		}
		else
		{
			return false;
		}
	}
1167776
protected function doesUrlMatchToAtLeastOnePattern($url, array $patterns)
    {
        foreach ($patterns as $pattern) {
            $regex = '|' . $pattern . '|';
            $this->verbose(
                "Checking url '" . $url . "' with regex '" . $regex . "' -> ",
                OutputInterface::VERBOSITY_VERBOSE
            );
            if (preg_match($regex, $url)) {
                $this->verbose("MATCHED\n", OutputInterface::VERBOSITY_VERBOSE);
                return true;
            } else {
                $this->verbose("NOT matched\n", OutputInterface::VERBOSITY_VERBOSE);
            }
        }
        return false;
    }
152178
function ( mask ) {

        if ( mask && ( mask instanceof RegExp ) !== true ) {

            throw { type: 'getURLs', message: 'First argument must be RegExp' };

        }

        return urls.map( function ( value ) {

            return _getURLValue( value );

        } ).filter( function ( value, pos, self ) {

            var unique = self.indexOf( value ) === pos;

            if ( mask && unique ) {

                return mask.test( value );

            } else {

                return unique;

            }

        } );

    }
1294643
public static String getUrlMapping(String url, Map<String, String> urlMappings) {
        if (urlMappings != null) {
            for (String pattern : urlMappings.keySet()) {
                if (urlMatcher.match(pattern, url)) {
                    return urlMappings.get(pattern);
                }
            }
        }
        
        return null;
    }
987529
protected function setProtocol($pattern, IUrl $url, $encode = false, $key = ':scheme') {
        $scheme = $url->getProtocol();

        if ($scheme) {
            $scheme = s('%s://', $scheme);
        }

        $pattern = s($pattern, [$key => $scheme]);

        return $pattern;
    }
1115038
protected function _urlMatch($patterns)
    {
        if (empty($patterns)) {
            return false;
        }

        $url = urldecode($this->_View->request->url);
        $path = str_starts_with($url, '/') ? str_replace_once('/', '', $url) : $url;

        if (option('url_locale_prefix')) {
            $patterns = explode("\n", $patterns);
            $locales = array_keys(quickapps('languages'));
            $localesPattern = '(' . implode('|', array_map('preg_quote', $locales)) . ')';

            foreach ($patterns as &$p) {
                if (!preg_match("/^{$localesPattern}\//", $p)) {
                    $p = I18n::locale() . '/' . $p;
                    $p = str_replace('//', '/', $p);
                }
            }

            $patterns = implode("\n", $patterns);
        }

        // Convert path settings to a regular expression.
        // Therefore replace newlines with a logical or, /* with asterisks and  "/" with the front page.
        $toReplace = [
            '/(\r\n?|\n)/', // newlines
            '/\\\\\*/', // asterisks
            '/(^|\|)\/($|\|)/' // front '/'
        ];

        $replacements = [
            '|',
            '.*',
            '\1' . preg_quote($this->_View->Url->build('/'), '/') . '\2'
        ];

        $patternsQuoted = preg_quote($patterns, '/');
        $patterns = '/^(' . preg_replace($toReplace, $replacements, $patternsQuoted) . ')$/';

        return (bool)preg_match($patterns, $path);
    }
281768
protected static function _applyUrlFilters($url)
    {
        $request = static::getRequest(true);
        $e = null;
        foreach (static::$_urlFilters as $filter) {
            try {
                $url = $filter($url, $request);
            } catch (Exception $e) {
                // fall through
            } catch (Throwable $e) {
                // fall through
            }
            if ($e !== null) {
                if (is_array($filter)) {
                    $ref = new ReflectionMethod($filter[0], $filter[1]);
                } else {
                    $ref = new ReflectionFunction($filter);
                }
                $message = sprintf(
                    'URL filter defined in %s on line %s could not be applied. The filter failed with: %s',
                    $ref->getFileName(),
                    $ref->getStartLine(),
                    $e->getMessage()
                );
                throw new RuntimeException($message, $e->getCode(), $e);
            }
        }

        return $url;
    }
58814
protected function loadPattern($patternName, array $pattern)
    {
        $pattern['matcher'] = new RequestMatcher($pattern['path']);

        $this->patterns[$patternName] = $pattern;

        return $this;
    }
725669
private static function buildPattern($masks)
    {
        $pattern = array();
        $masks = is_array($masks) ? $masks : [$masks];
        foreach ($masks as $mask) {
            $mask = rtrim(strtr($mask, '\\', '/'), '/');
            $prefix = '';
            if ($mask === '') {
                continue;

            } elseif ($mask === '*') {
                return NULL;

            } elseif ($mask[0] === '/') { // absolute fixing
                $mask = ltrim($mask, '/');
                $prefix = '(?<=^/)';
            }
            $pattern[] = $prefix . strtr(preg_quote($mask, '#'),
                    array('\*\*' => '.*', '\*' => '[^/]*', '\?' => '[^/]', '\[\!' => '[^', '\[' => '[', '\]' => ']', '\-' => '-'));
        }

        return $pattern ? '#/(' . implode('|', $pattern) . ')\z#i' : NULL;
    }
294406
protected function setHost($pattern, IUrl $url, $encode = false, $key = ':host') {
        $host = $url->getHost();
        $pattern = s($pattern, [$key => $host]);

        return $pattern;
    }