Skip to content

Commit 1809605

Browse files
Add FileUtils.relativeSubPath
This function allows making a path relative. However, unlike the existing FileUtils.relativePath, this function only works to make a path relative to one of its parent paths (e.g. strip a prefix), which allows it to be a lot simpler.
1 parent 411cc3b commit 1809605

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

app/src/processing/app/helpers/FileUtils.java

+15
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ public static String relativePath(String origin, String target) {
161161
return relative + target.substring(origin.length() + 1);
162162
}
163163

164+
// Return target, relative to target. Differences with relativePath
165+
// above:
166+
// - Paths are not canonicalized and must be absolute
167+
// - Target must be below origin.
168+
public static String relativeSubPath(String origin, String target) {
169+
// Sanity check
170+
if (!target.startsWith(origin + File.separator))
171+
return null;
172+
return target.substring(origin.length() + 1);
173+
}
174+
175+
public static String relativeSubPath(File origin, File target) {
176+
return relativeSubPath(origin.getAbsolutePath(), target.getAbsolutePath());
177+
}
178+
164179
public static String getLinuxPathFrom(File file) {
165180
return BACKSLASH.matcher(file.getAbsolutePath()).replaceAll("/");
166181
}

0 commit comments

Comments
 (0)