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

Method simplifyPath

output/java_guava/1.4.19/Files.java:742–782  ·  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

740
741
742 public static String simplifyPath(String pathname) {
743 checkNotNull(pathname);
744 if (pathname.length() == 0) {
745 return ".";
746 }
747
748 // split the path apart
749 Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname);
750 List<String> path = new ArrayList<String>();
751
752 // resolve ., .., and //
753 for (String component : components) {
754 if (component.equals(".")) {
755 continue;
756 } else if (component.equals("..")) {
757 if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) {
758 path.remove(path.size() - 1);
759 } else {
760 path.add("..");
761 }
762 } else {
763 path.add(component);
764 }
765 }
766
767 // put it back together
768 String result = Joiner.on('/').join(path);
769 if (pathname.charAt(0) == '/') {
770 result = "/" + result;
771 }
772
773 while (result.startsWith("/../")) {
774 result = result.substring(3);
775 }
776 if (result.equals("/..")) {
777 result = "/";
778 } else if ("".equals(result)) {
779 result = ".";
780 }
781 return result;
782 }
783
784 /**
785 * 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