<commit_before><?php

namespace OmniApp;

/**
 * Route
 */
class Route
{
    /**
     * Register a route for path
     *
     * @static
     * @param $path
     * @param $runner
     */
    public static function on($path, $runner)
    {
        App::config('route.' . $path, $runner, true);
    }

    /**
     * Parse site path
     *
     * @static
     * @param $path
     * @return array
     * @throws \Exception
     */
    public static function parse($path)
    {
        $routes = (array)App::config('route');

        //$path = trim($path, '/');
        if ($path AND !preg_match('/^[\w\-~\/\.]{1,400}$/', $path)) $path = '404';

        foreach ($routes as $route => $controller) {
            if (!$route) continue;
            $complete = false;
            $params = array();

            if (!strpos($route, ':')) {
                if ($path === $route) {
                    $complete = $route;
                }
            } else {
                if (preg_match(self::toRegex($route), $path, $matches)) {
                    $complete = array_shift($matches);
                    $params = explode('/', trim(mb_substr($path, mb_strlen($complete)), '/'));
                    if ($params[0]) {
                        foreach ($matches as $match) array_unshift($params, $match);
                    } else $params = $matches;
                }
            }

            if ($complete) {
                return array($controller, $complete, $params);
            }
        }

        return self::notFound();
    }

    /**
     * Get or set not found route
     *
     * @param mixed $runner
     * @return mixed
     */
    public static function notFound($runner = null)
    {
        if ($runner) {
            App::config('route.404', $runner, true);
        }
        return App::config('route.404');
    }


    /**
     * Get or set error runner
     *
     * @param mixed $runner
     * @return mixed
     */
    public static function error($runner = null)
    {
        if ($runner) {
            App::config('route.error', $runner, true);
        }
        return App::config('route.error');
    }

    /**
     * To regex
     *
     * @param $string
     * @return string
     */
    public static function toRegex($string)
    {
        $regex = str_replace(array('/'), array('\/'), $string);
        $regex = '/^' . $regex . '$/';
        return $regex;
    }
}<commit_msg>Route support "/u/:id" and full regex and simple regex<commit_after><?php

namespace OmniApp;

/**
 * Route
 */
class Route
{
    /**
     * Register a route for path
     *
     * @static
     * @param $path
     * @param $runner
     */
    public static function on($path, $runner)
    {
        App::config()->route[$path] = $runner;
    }

    /**
     * Parse site path
     *
     * @static
     * @param $path
     * @return array
     * @throws \Exception
     */
    public static function parse($path)
    {
        $routes = (array)App::config('route');

        //$path = trim($path, '/');
        if ($path AND !preg_match('/^[\w\-~\/\.]{1,400}$/', $path)) $path = '404';

        foreach ($routes as $route => $controller) {
            if (!$route) continue;
            $complete = false;
            $params = array();

            // Regex or Param check
            if (!strpos($route, ':') && strpos($route, '^') === false) {
                if ($path === $route) {
                    $complete = $route;
                }
            } else {
                // Try match
                if (preg_match(self::toRegex($route), $path, $matches)) {
                    $complete = $route;
                    array_shift($matches);
                    $params = $matches;
                }
            }

            // When complete the return
            if ($complete) {
                return array($controller, $complete, $params);
            }
        }

        return self::notFound();
    }

    /**
     * Get or set not found route
     *
     * @param mixed $runner
     * @return mixed
     */
    public static function notFound($runner = null)
    {
        if ($runner) {
            App::config('route.404', $runner, true);
        }
        return App::config('route.404');
    }


    /**
     * Get or set error runner
     *
     * @param mixed $runner
     * @return mixed
     */
    public static function error($runner = null)
    {
        if ($runner) {
            App::config('route.error', $runner, true);
        }
        return App::config('route.error');
    }

    /**
     * To regex
     *
     * @param $regex
     * @return string
     */
    public static function toRegex($regex)
    {
        if ($regex[1] !== '^') {
            $regex = str_replace(array('/'), array('\\/'), $regex);
            if ($regex{0} == '^') {
                $regex = '/' . $regex . '/';
            } elseif (strpos($regex, ':')) {
                $regex = '/^' . preg_replace('/:([a-zA-Z0-9]+)/', '(?<$1>[a-zA-Z0-9\.\-\+_]+?)', $regex) . '$/';
            } else {
                $regex = '/^' . $regex . '$/';
            }
        }
        return $regex;
    }
}
{'fraction_non_alphanumeric': 0.17076983196561157, 'fraction_numerical': 0.006643220007815553, 'mean_word_length': 1.405545112781955, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2933392', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1482, 'n_words': 343}