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

Method simplifyPath

output/java_guava/1.4.17/Files.java:741–781  ·  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

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