Normalize relative directories in a path.

@param string $path

@return string
332506
public static function normalizePath($path)
    {
        if (isset(self::$normalizePath[$path])) {
            return self::$normalizePath[$path];
        }

        $path = \str_replace('\\', '/', $path);
        $path = static::removeFunkyWhiteSpace($path);

        $parts = [];

        foreach (\explode('/', $path) as $part) {
            switch ($part) {
                case '':
                case '.':
                    break;

                case '..':
                    if (empty($parts)) {
                        throw new \LogicException('Path is outside of the defined root, path: [' . $path . ']');
                    }
                    \array_pop($parts);
                    break;

                default:
                    $parts[] = $part;
                    break;
            }
        }

        return self::$normalizePath[$path] = implode('/', $parts);
    }
381145
protected function getRelativePath($path = null)
    {
        if (!isset($path)) {
            $path = $this->scanner->getInitialPath(true);
        }

        return gplcart_path_normalize(gplcart_path_relative($path));
    }
1198283
public static function normalizePath(string $path): string
    {
        if (substr($path, 0, 1) == DIRECTORY_SEPARATOR) {
            return $path;
        }

        return getcwd().DIRECTORY_SEPARATOR.$path;
    }
279738
private function normalizePath($path)
    {
        if (!is_string($path) && !method_exists($path, '__toString')) {
            throw new InvalidArgumentException('Uri path must be a string');
        }

        $path = $this->urlEncode($path);

        if (empty($path)) {
            return '';
        }

        if ($path[0] !== '/') {
            return $path;
        }

        // Ensure only one leading slash
        return '/' . ltrim($path, '/');
    }
1343618
protected function _getRelDir($path) {
        $filename = basename($path);
        $dir = substr($path, 0, strlen($path) - strlen($filename));
        return $dir;
    }
347461
public static function normalizeFullPath($path)
    {
        $to = Folder::correctSlashFor($path);
        $from = ($to == '/' ? '\\' : '/');

        return str_replace($from, $to, $path);
    }
996081
public static function normalizePath($path) {
    $parts = [];
    $path = str_replace('\\', '/', $path);
    $path = preg_replace('/\/+/', '/', $path);
    $segments = explode('/', $path);
    foreach ($segments as $segment) {
      if ($segment != '.') {
        $test = array_pop($parts);
        if (is_null($test)) {
          $parts[] = $segment;
        }
        elseif ($segment == '..') {
          if ($test == '..') {
            $parts[] = $test;
          }
          if ($test == '..' || $test == '') {
            $parts[] = $segment;
          }
        }
        else {
          $parts[] = $test;
          $parts[] = $segment;
        }
      }
    }
    return implode('/', $parts);
  }
1373181
public function normalize($path) {
        $path = strtr(trim($path), '\\', '/');
        $scheme = "";
        $matches = array();
        if (preg_match('#([a-zA-Z]+:/)/(.+)#', $path, $matches)) {
            $scheme = $matches[1];
            $path = "/" . $matches[2];
        }
        $result = substr(
                array_reduce(
                        explode('/', $path)
                        , create_function('$a, $b', '
                            if($a === 0){
                                    if($b === "." 
                                        || $b === ".." 
                                        || substr($b,1,1) === ":" 
                                        || $b === ""){
                                        return preg_replace("/\/+/", "/", "$b/");
                                    } else {
                                        return preg_replace("/\/+/", "/", "./$b/");
                                    }
                            }
                            if($b === "" || $b === "."){
                                    return preg_replace("/\/+/", "/", "$a/");
                            }
                            if($b === ".." && substr($a, -4) === "/../"){
                                    return preg_replace("/\/+/", "/", "$a/$b/");
                            }
                            if($b === ".." && $a === "../"){
                                    return preg_replace("/\/+/", "/", "$a/$b/");
                            }
                            if($b === ".." && $a === "./"){
                                    return "../";
                            }
                            if($b === ".." && $a === "/"){
                                    return "//";
                            }
                            if($b === ".."){
                                    return preg_replace("/\/+/", "/", strtr(dirname($a), \'\\\\\', \'/\') . "/");
                            }
                            return preg_replace("/\/+/", "/", "$a/$b/");
                        ')
                        , 0)
                , 0, -1);
        return $scheme . ($result === "" ? "/" : $result);
    }
1333361
private function relativizePaths($path)
    {
        if (!$this->basePath) {
            return $path;
        }

        return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
    }
1332184
protected function normalizePath($path)
    {
        if (preg_match("/^id:.*|^rev:.*|^(ns:[0-9]+(\/.*)?)/", $path) === 1) {
            return $path;
        }

        $path = (trim($path, '/') === '') ? '' : '/'.$path;

        return str_replace('//', '/', $path);
    }
890372
private function normalizePath($path, $removeTrailingSeparator = false)
    {
        $newPath = str_replace('/', DIRECTORY_SEPARATOR, $path);
        $newPath = str_replace('\\', DIRECTORY_SEPARATOR, $newPath);

        $newPath = str_replace(':'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, '://', $newPath);

        if ($removeTrailingSeparator) {
            return rtrim($newPath, DIRECTORY_SEPARATOR);
        }

        return $newPath;
    }
440127
protected function relativizePath(string $path): string
    {
        $base_segments = $this->getSegments($this->base_uri->getPath());
        $target_segments = $this->getSegments($path);
        $target_basename = array_pop($target_segments);
        array_pop($base_segments);
        foreach ($base_segments as $offset => $segment) {
            if (!isset($target_segments[$offset]) || $segment !== $target_segments[$offset]) {
                break;
            }
            unset($base_segments[$offset], $target_segments[$offset]);
        }
        $target_segments[] = $target_basename;

        return $this->formatPath(
            str_repeat('../', count($base_segments)).implode('/', $target_segments)
        );
    }
986344
protected static function normalizePath(string $path): string
    {
        if (!static::isValidPath($path)) {
            $message = sprintf('Invalid URI path: %s', $path);
            throw new DomainException($message);
        }

        $path = static::removeDotSegments($path);

        return static::encodePath(static::decode($path, static::UNRESERVED_SET));
    }
1226340
public static function normalizePath($path)
    {
        // Resolve any streams
        /** @var UniformResourceLocator $locator */
        $locator = Grav::instance()['locator'];
        if ($locator->isStream($path)) {
            $path = $locator->findResource($path);
        }

        // Set root properly for any URLs
        $root = '';
        preg_match(self::ROOTURL_REGEX, $path, $matches);
        if ($matches) {
            $root = $matches[1];
            $path = $matches[2];
        }

        // Strip off leading / to ensure explode is accurate
        if (Utils::startsWith($path,'/')) {
            $root .= '/';
            $path = ltrim($path, '/');
        }

        // If there are any relative paths (..) handle those
        if (Utils::contains($path, '..')) {
            $segments = explode('/', trim($path, '/'));
            $ret = [];
            foreach ($segments as $segment) {
                if (($segment === '.') || $segment === '') {
                    continue;
                }
                if ($segment === '..') {
                    array_pop($ret);
                } else {
                    $ret[] = $segment;
                }
            }
            $path = implode('/', $ret);
        }

        // Stick everything back together
        $normalized = $root . $path;
        return $normalized;
    }
109151
private function normalizePath(): string
    {
        $path = $this->path;
        if (substr($path, -1) === '$') {
            $path = substr($path, 0, strlen($path) - 1);
        }

        if (substr($path, -1) === '?') {
            $path = substr($path, 0, strlen($path) - 1);
        }

        return $path;
    }
1030545
protected function _normalizePaths()
    {
        foreach ($this->_paths as &$path) {
            $ds = DIRECTORY_SEPARATOR;
            $path = $this->_normalizePath($path, $ds);
            $path = rtrim($path, $ds) . $ds;
        }
    }
1269423
protected static function normalizePath($path)
    {
        $pathAry = explode('/', $path);
        $i       = 1;

        do {
            if ('.' == $pathAry[$i]) {
                if ($i < count($pathAry) - 1) {
                    array_splice($pathAry, $i, 1);
                } else {
                    $pathAry[$i] = '';
                    $i++;
                }

            } elseif ('..' == $pathAry[$i]) {
                if (1 == $i) {
                    array_splice($pathAry, 1, 1);

                } elseif ('..' != $pathAry[$i - 1]) {
                    if ($i < count($pathAry) - 1) {
                        array_splice($pathAry, $i - 1, 2);
                        $i--;
                    } else {
                        array_splice($pathAry, $i - 1, 2, '');
                    }
                }

            } else {
                $i++;
            }
        } while ($i < count($pathAry));

        return implode('/', $pathAry);
    }