Sanitizes file path - turns all \ into / and assures it is UTF-8 encoded.
@param string $path
@param string $pathEncoding
@return string
508312
static public function sanitizePath(string $path,
                                        string $pathEncoding = null): string {
        if ($pathEncoding === null) {
            self::detectPathEncoding();
            $pathEncoding = self::$pathEncoding;
        }
        $path = iconv($pathEncoding, 'utf-8', $path);
        $path = str_replace('\\', '/', $path);
        return $path;
    }
766045
private function utf_decode_path($path) {
        $fullpath = $path;
        if (iconv('UTF-8', 'UTF-8', $fullpath) == $fullpath) {
            $this->_error_log("filename is utf-8. Needs conversion...");
            $fullpath = utf8_decode($fullpath);
        }
        return $fullpath;
    }
886088
public static String sanitizePath(String path, String substitute) {
    Preconditions.checkArgument(substitute.replaceAll(HDFS_ILLEGAL_TOKEN_REGEX, "").equals(substitute),
        "substitute contains illegal characters: " + substitute);

    return path.replaceAll(HDFS_ILLEGAL_TOKEN_REGEX, substitute);
  }
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);
    }
804766
def normalize_path(path)
      if(path.size > 256)
        path = "\\\\?\\" + Pathname.new(path).realpath.to_s
      end
#      require 'rchardet'
#      require 'iconv'
#      cd = CharDet.detect(path)
#      encoding = cd['encoding']
#      converter = Iconv.new("UTF-16LE", encoding)
#      converter.iconv(path)
      # path.encode!("UTF-16LE")
    end
684801
public static String normalizePath(String path) {
    String normalizedPath = path.replaceAll("//+", "/").replaceFirst("(.+)/$", "$1");
    PathUtils.validatePath(normalizedPath);
    return normalizedPath;
  }
1173086
private function normalizePath($path)
    {
        $path = rtrim($path, '/');

        if (empty($path) or '/' !== substr($path, 0, 1)) {
            $path = '/';
        }

        return $path;
    }
413313
protected function _filterPath($path)
    {
        $path = preg_replace_callback(
            '/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/',
            [$this, '_urlEncodeChar'],
            $path
        );

        if (empty($path)) {
            // No path
            return $path;
        }

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

        // Ensure only one leading slash, to prevent XSS attempts.
        return '/' . ltrim($path, '/');
    }
630301
private function filterPath(string $path): string
    {
        $path = \preg_replace_callback(
            '/(?:[^' . self::CHAR_UNRESERVED . ')(:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/u',
            [$this, 'urlEncodeChar'],
            $path
        );

        if ('' === $path) {
            // No path
            return $path;
        }

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

        // Ensure only one leading slash, to prevent XSS attempts.
        return '/' . \ltrim($path, '/');
    }
270773
private static String escapeSpecialAsciiAndNonAscii(String path) {
    int len = path.length(), ch;
    StringBuilder buffer = new StringBuilder(len * 3);
    // change C:/blah to /C:/blah
    if (len >= 2 && path.charAt(1) == ':') {
      ch = Character.toUpperCase(path.charAt(0));
      if (ch >= 'A' && ch <= 'Z') {
        buffer.append('/');
      }
    }
    // for each character in the path
    int i = 0;
    for (i = 0; i < len; i++) {
      ch = path.charAt(i);
      // if it's not an ASCII character, break here, and use UTF-8 encoding
      if (ch >= 128) {
        break;
      }
      if (gNeedEscaping[ch]) {
        buffer.append('%');
        buffer.append(gAfterEscaping1[ch]);
        buffer.append(gAfterEscaping2[ch]);
      } else {
        buffer.append((char) ch);
      }
    }
    // we saw some non-ascii character
    if (i < len) {
      // get UTF-8 bytes for the remaining sub-string
      byte[] bytes = null;
      byte b;
      bytes = path.substring(i).getBytes(StandardCharsets.UTF_8);
      len = bytes.length;
      // for each byte
      for (i = 0; i < len; i++) {
        b = bytes[i];
        // for non-ascii character: make it positive, then escape
        if (b < 0) {
          ch = b + 256;
          buffer.append('%');
          buffer.append(gHexChs[ch >> 4]);
          buffer.append(gHexChs[ch & 0xf]);
        } else if (gNeedEscaping[b]) {
          buffer.append('%');
          buffer.append(gAfterEscaping1[b]);
          buffer.append(gAfterEscaping2[b]);
        } else {
          buffer.append((char) b);
        }
      }
    }
    return buffer.toString();
  }
570978
function (path, minify, encode) {
        minify = minify || false;
        encode = encode || 'utf-8';

        if (this.isFileSync(path)) {
            var raw = fs.readFileSync(path, encode);
            if (minify) {
                raw = jsonminify(raw);
            }

            try {
                return JSON.parse(raw);
            } catch (e1) {
                try {
                    jsonlint.parse(raw);
                } catch (e2) {
                    throw e2;
                }
            }
        }
        return {};
    }
1252372
private function filterPath(string $path): string
    {
        if ($path == '') {
            return $path;
        }

        $result = preg_replace_callback(
            '/(?:[^'.self::UNRESERVED.self::SUB_DELIMS.':@%~\/]+|%(?![A-Za-f0-9]{2}))/',
            function (array $match) {
                return rawurlencode($match[0]);
            },
            $path
        );

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

        return '/'.ltrim($result, '/');
    }
1059127
private function filterPath(?string $path): string
    {
        if (null === $path) {
            throw new SyntaxError('The path can not be null');
        }

        if ('' === $path || ',' === $path) {
            return 'text/plain;charset=us-ascii,';
        }

        if (1 === preg_match(self::REGEXP_DATAPATH, $path)) {
            $path = substr($path, 0, -1).'charset=us-ascii,';
        }

        if (1 === preg_match(self::REGEXP_NON_ASCII_PATTERN, $path) && false === strpos($path, ',')) {
            throw new SyntaxError(sprintf('The path `%s` is invalid according to RFC2937', $path));
        }

        return $path;
    }
791895
static function sanify_path($path) {
            $path = rtrim(str_replace('\\', '/', $path), '/').'/';
            do {
                $old_path = $path;
                $path = str_replace('//', '/', str_replace('/./', '/', $path));
            } while ($path != $old_path);
            do {
                $path = preg_replace('_/([^/]+?)/\\.\\./_', '/', $path, -1, $count);
            } while ($count > 0);
            $path = rtrim($path, '/');

            return $path;
        }
1252945
protected function filterPath($path)
    {
        $path = preg_replace_callback(
            '/(?:[^' . self::CHARACTERS_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/',
            function ($match) {
                return rawurlencode($match[0]);
            },
            $path
        );

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

        if (substr($path, 0, 1) !== '/') {
            return $path;
        } else {
            return '/' . ltrim($path, '/');
        }
    }
1218847
private function getCdnURI($path)
    {
        $cleancdn =  explode('/', $path);
        unset($cleancdn[0]);
        unset($cleancdn[1]);
        reset($cleancdn);
        return implode($cleancdn, '/');
    }
842690
private String sanitize(String path) {
        String sanitized = path.replaceAll( "\\\\", "/" );
        
        if ( sanitized.matches( "^[a-z]:.*" ) ) {
            sanitized = sanitized.substring(0,1).toUpperCase() + sanitized.substring(1);
        }
        return sanitized;
    }