Skip to content

Commit

Permalink
Fix resolution and relativization when in root or empty context
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
  • Loading branch information
TheSilkMiner committed May 21, 2022
1 parent a8c8436 commit 7e4a377
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public Path getParent() {
}
final int[] components = this.components();
final int parentEnd = components[components.length - 2];
final String pathPortion = this.path.substring(0, parentEnd);
final String pathPortion = parentEnd == -1? this.root : this.path.substring(0, parentEnd); // If this is child of root, then there's a single component: parent is root or empty
return new TrundlePath(this.fs, this.pathType, this.root, pathPortion);
}

Expand Down Expand Up @@ -303,8 +303,7 @@ public Path resolve(@NotNull final Path other) {
if (path.isEmptyPath()) {
return this;
}
final String thisPath = this.isRoot()? "" : this.path;
final String resolvedPath = thisPath + '/' + path.path;
final String resolvedPath = this.isRoot()? path.path : this.path + '/' + path.path;
return new TrundlePath(this.fs, this.pathType, this.root, resolvedPath);
}

Expand Down Expand Up @@ -341,7 +340,7 @@ public Path relativize(@NotNull final Path other) {
}

if (divergenceIndex == -1) { // The two paths never diverged, so one is a parent of the other
if (theirComponents > ourComponents) { // They are a parent
if (theirComponents < ourComponents) { // They are a parent
final String upwardsWalker = elide("../".repeat(theirComponents - ourComponents));
return new TrundlePath(this.fs, TrundlePathType.RELATIVE, REL_ROOT, upwardsWalker);
}
Expand Down Expand Up @@ -409,7 +408,7 @@ public boolean equals(final Object obj) {

@Override
public String toString() {
return this.isAbsolute()? ABS_ROOT + this.path : this.path;
return this.isAbsolute()? ABS_ROOT + (this.isRoot()? "" : this.path) : this.path;
}

@Override
Expand Down Expand Up @@ -517,7 +516,7 @@ private int[] components() {
}

final String path = this.path;
int components = 0;
int components = this.isEmptyPath()? 0 : 1; // By default, every non-empty path has at least one component
for (final char c : path.toCharArray()) {
if (c == '/') {
++components;
Expand Down

0 comments on commit 7e4a377

Please sign in to comment.