Skip to content

Commit

Permalink
Add Relativize (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk4j authored Feb 27, 2024
1 parent 73c9eee commit 944ee7a
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions JShim/Sun/NIO/FS/WindowsPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,37 +178,71 @@ public IEnumerable<Path> Iterator()

public Path Normalize()
{
throw new NotImplementedException();
return ToAbsolutePath();
}

public Path Relativize(Path other)
{
throw new NotImplementedException();
string[] s = ToString().Split(Separators);
string[] o = other.ToString().Split(Separators);

// Ignore the common subpaths
int max = Math.Max(s.Length, o.Length);
List<string> sd = new List<string>();
List<string> od = new List<string>();
for(int i = 0; i < max; i++)
{
if(i < s.Length && i < o.Length && s[i].Equals(o[i]))
continue;

if(i < s.Length)
sd.Add(s[i]);

if(i < o.Length)
od.Add(o[i]);
}

StringBuilder sb = new StringBuilder();
for(int i = 0; i < sd.Count; i++)
{
sb.Append(System.IO.Path.DirectorySeparatorChar);
sb.Append("..");
}

foreach(string op in od)
{
sb.Append(System.IO.Path.DirectorySeparatorChar);
sb.Append(op);
}

string path = sb.ToString();

return new WindowsPath(fs, WindowsPathType.Relative, root, path);
}

public Path Resolve(Path other)
{
throw new NotImplementedException();
return Resolve(other.ToString());
}

public Path Resolve(string other)
{
throw new NotImplementedException();
return null;
}

public Path ResolveSibling(Path other)
{
throw new NotImplementedException();
return ResolveSibling(other.ToString());
}

public Path ResolveSibling(string other)
{
throw new NotImplementedException();
return null;
}

public bool StartsWith(Path other)
{
throw new NotImplementedException();
return StartsWith(other.ToString());
}

public bool StartsWith(string other)
Expand Down

0 comments on commit 944ee7a

Please sign in to comment.