MCPcopy Create free account
hub / github.com/antlr/codebuff / simplifyPath

Method simplifyPath

corpus/java/training/guava/io/Files.java:704–745  ·  view source on GitHub ↗

Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original. The following heuristics are used: empty string becomes . . stays as . fold out ./ fold out ../ when possible collapse multiple slashes delete trailing slash

(String pathname)

Source from the content-addressed store, hash-verified

702 * @since 11.0
703 */
704 public static String simplifyPath(String pathname) {
705 checkNotNull(pathname);
706 if (pathname.length() == 0) {
707 return ".";
708 }
709
710 // split the path apart
711 Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname);
712 List<String> path = new ArrayList<String>();
713
714 // resolve ., .., and //
715 for (String component : components) {
716 if (component.equals(".")) {
717 continue;
718 } else if (component.equals("..")) {
719 if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) {
720 path.remove(path.size() - 1);
721 } else {
722 path.add("..");
723 }
724 } else {
725 path.add(component);
726 }
727 }
728
729 // put it back together
730 String result = Joiner.on('/').join(path);
731 if (pathname.charAt(0) == '/') {
732 result = "/" + result;
733 }
734
735 while (result.startsWith("/../")) {
736 result = result.substring(3);
737 }
738 if (result.equals("/..")) {
739 result = "/";
740 } else if ("".equals(result)) {
741 result = ".";
742 }
743
744 return result;
745 }
746
747 /**
748 * Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for

Callers

nothing calls this directly

Calls 12

onMethod · 0.95
onMethod · 0.95
equalsMethod · 0.65
sizeMethod · 0.65
getMethod · 0.65
removeMethod · 0.65
addMethod · 0.65
checkNotNullMethod · 0.45
lengthMethod · 0.45
splitMethod · 0.45
omitEmptyStringsMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected