Canonicalize a given path, removing segments containing "." and consuming segments for "..".

If no segment could be consumed for "..", retains the segment.
548932
static String canonicalizePath(String path) {
    String[] parts = path.split(MODULE_SLASH);
    String[] buffer = new String[parts.length];
    int position = 0;
    int available = 0;

    boolean absolutePath = (parts.length > 1 && parts[0].isEmpty());
    if (absolutePath) {
      // If the path starts with "/" (so the left side, index zero, is empty), then the path will
      // always remain absolute. Make the first segment unavailable to touch.
      --available;
    }

    for (String part : parts) {
      if (part.equals(".")) {
        continue;
      }

      if (part.equals("..")) {
        if (available > 0) {
          // Consume the previous segment.
          --position;
          --available;
          buffer[position] = null;
        } else if (!absolutePath) {
          // If this is a relative path, retain "..", as it can't be consumed on the left.
          buffer[position] = part;
          ++position;
        }
        continue;
      }

      buffer[position] = part;
      ++position;
      ++available;
    }

    if (absolutePath && position == 1) {
      return MODULE_SLASH;  // special-case single absolute segment as joining [""] doesn't work
    }
    return MODULE_JOINER.join(Arrays.copyOf(buffer, position));
  }
129536
private static String normalize( String pathName )
    {
        // replaces multiple '/' by one unique '/'
        pathName = pathName.replaceAll( "/+", "/" );

        // removes a '/' if it finishes a path and is not root
        pathName = pathName.replaceAll( "(.+)/$", "$1" );

        if ( !pathName.startsWith( "/" ) ) {
            pathName = "/" + pathName;
        }
        return pathName;
    }
23991
protected String normalizePath(String path) {

        if (path.equals("/")) {
            return path;
        } else {
            return CmsFileUtil.removeTrailingSeparator(path);
        }

    }
1260272
def remove_alias(path):
    
    orm.delete(p for p in model.PathAlias if p.path == path)
    orm.commit()
1324590
def split(path)
      parts = []
      last_index = 0
      while index = path.index("/", last_index)
        if index == last_index
          parts << ""
        else
          parts << path[last_index...index]
        end
        last_index = index + 1
      end
      # and also get that last segment
      parts << path[last_index..-1]
      # it should begin with a blank segment from the leading "/"; kill that
      parts.shift
      parts
    end
382049
def consume_path_sep(self, i):
        """"""

        try:
            if self.bslash_abort:
                count = -1
                c = '\\'
                while c == '\\':
                    count += 1
                    c = next(i)
                i.rewind(1)
                # Rewind one more if we have an odd number (escape): \\\*
                if count > 0 and count % 2:
                    i.rewind(1)
            else:
                c = '/'
                while c == '/':
                    c = next(i)
                i.rewind(1)
        except StopIteration:
            pass
1046078
private static Path speculativeFastParsePath(String path) {
        String s = ConfigImplUtil.unicodeTrim(path);
        if (s.isEmpty())
            return null;
        if (hasUnsafeChars(s))
            return null;
        if (s.startsWith(".") || s.endsWith(".") || s.contains(".."))
            return null; // let the full parser throw the error

        PathBuilder pb = new PathBuilder();
        appendPathString(pb, s);
        return pb.result();
    }
313436
private static Path normalizePath(Path path) {
		URI uri = path.toUri();
		if (uri.getScheme() == null) {
			try {
				uri = new URI("file", uri.getHost(), uri.getPath(), uri.getFragment());
				path = new Path(uri.toString());
			} catch (URISyntaxException e) {
				throw new IllegalArgumentException("path is invalid", e);
			}
		}
		return path;
	}
800944
@Restricted(NoExternalUse.class)
    public static String normalize(@Nonnull String path) {
        StringBuilder buf = new StringBuilder();
        // Check for prefix designating absolute path
        Matcher m = ABSOLUTE_PREFIX_PATTERN.matcher(path);
        if (m.find()) {
            buf.append(m.group(1));
            path = path.substring(m.end());
        }
        boolean isAbsolute = buf.length() > 0;
        // Split remaining path into tokens, trimming any duplicate or trailing separators
        List<String> tokens = new ArrayList<>();
        int s = 0, end = path.length();
        for (int i = 0; i < end; i++) {
            char c = path.charAt(i);
            if (c == '/' || c == '\\') {
                tokens.add(path.substring(s, i));
                s = i;
                // Skip any extra separator chars
                while (++i < end && ((c = path.charAt(i)) == '/' || c == '\\')) { }
                // Add token for separator unless we reached the end
                if (i < end) tokens.add(path.substring(s, s+1));
                s = i;
            }
        }
        if (s < end) tokens.add(path.substring(s));
        // Look through tokens for "." or ".."
        for (int i = 0; i < tokens.size();) {
            String token = tokens.get(i);
            if (token.equals(".")) {
                tokens.remove(i);
                if (tokens.size() > 0)
                    tokens.remove(i > 0 ? i - 1 : i);
            } else if (token.equals("..")) {
                if (i == 0) {
                    // If absolute path, just remove: /../something
                    // If relative path, not collapsible so leave as-is
                    tokens.remove(0);
                    if (tokens.size() > 0) token += tokens.remove(0);
                    if (!isAbsolute) buf.append(token);
                } else {
                    // Normalize: remove something/.. plus separator before/after
                    i -= 2;
                    for (int j = 0; j < 3; j++) tokens.remove(i);
                    if (i > 0) tokens.remove(i-1);
                    else if (tokens.size() > 0) tokens.remove(0);
                }
            } else
                i += 2;
        }
        // Recombine tokens
        for (String token : tokens) buf.append(token);
        if (buf.length() == 0) buf.append('.');
        return buf.toString();
    }
138392
function strip(path) {
    var cur;
    var next;
    var str = '';

    for (var i = 0; i < path.length; i++) {
        cur = path.charAt(i);

        if (i !== path.length - 1) {
            next = path.charAt(i + 1);
        }

        if (cur === '/' && (next === '/' || (next === '?' && i > 0))) {
            continue;
        }

        str += cur;
    }

    return (str);
}
536558
protected function removeDotSegments($path)
    {
        if ($path === null) {
            throw new \InvalidArgumentException('Path not set');
        }

        // §5.2.4.  Remove Dot Segments
        $input  = $path;
        $output = '';

        while ($input != '') {
            if (substr($input, 0, 3) == '../') {
                $input = (string) substr($input, 3);
            } elseif (substr($input, 0, 2) == './') {
                $input = (string) substr($input, 2);
            } elseif (substr($input, 0, 3) == '/./') {
                $input = substr($input, 2);
            } elseif ($input == '/.') {
                $input = '/';
            } elseif (substr($input, 0, 4) == '/../') {
                $input  = substr($input, 3);
                $pos    = strrpos($output, '/');
                if ($pos === false) {
                    $output = '';
                } else {
                    $output = substr($output, 0, $pos);
                }
            } elseif ($input == '/..') {
                $input  = '/';
                $pos    = strrpos($output, '/');
                if ($pos === false) {
                    $output = '';
                } else {
                    $output = substr($output, 0, $pos);
                }
            } elseif ($input == '.' || $input == '..') {
                $input = '';
            } else {
                $pos = strpos($input, '/', 1);
                if ($pos === false) {
                    $output    .= $input;
                    $input      = '';
                } else {
                    $output    .= substr($input, 0, $pos);
                    $input      = substr($input, $pos);
                }
            }
        }

        return $output;
    }
772163
public static String normalizePath(String path) {
    // remove double slashes & backslashes
    path = path.replace("//", "/");
    path = path.replace("\\", "/");

    // trim trailing slash from non-root path (ignoring windows drive)
    if (path.length() > 1 && path.endsWith("/")) {
      path = path.substring(0, path.length() - 1);
    }

    return path;
  }
1363052
function normalizePath(path = '') {

  // Remove the trailing slash if one has been given
  if(path.length > 1 && path.endsWith('/')) {
    path = path.slice(0, -1);
  }

  // Remove the first slash
  if(path.startsWith('/')) {
    path = path.slice(1);
  }

  // Remove the query if it exists
  if(path.includes('?')) {
    path = path.split('?')[0];
  }

  // Decode the url
  path = decodeURIComponent(path);

  return path;
}
846139
protected static String normalizePath (String path) {
    int pathLen;
    do {
      pathLen = path.length();
      path = path.replaceAll("[^/]+/\\.\\./", "");
    } while (path.length() != pathLen);
    return path;
  }
1005096
public static Path sanitizePath(Path path, String substitute) {
    return new Path(sanitizePath(path.toString(), substitute));
  }
390149
def normalize_path(path, encoding='utf-8'):
    '''
    '''
    if not path.startswith('/'):
        path = '/' + path
    path = percent_encode(flatten_path(path, flatten_slashes=True), encoding=encoding)
    return uppercase_percent_encoding(path)
976071
def resolve_frompath(pkgpath, relpath, level=0):
    """"""
    if level == 0:
        return relpath
    parts = pkgpath.split('.') + ['_']
    parts = parts[:-level] + (relpath.split('.') if relpath else [])
    return '.'.join(parts)