Replace File.relativePath from stdlib with File.descendantRelativeTo in fileUtils.
IO utils: cleanup java usages.
This commit is contained in:
@@ -31,4 +31,24 @@ fun File.withReplacedExtensionOrNull(oldExt: String, newExt: String): File? {
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the relative path to this file from [base] file.
|
||||
* Note that the [base] file is treated as a directory.
|
||||
*
|
||||
* If this file matches the [base] directory an empty path is returned.
|
||||
* If this file does not belong to the [base] directory, it is returned unchanged.
|
||||
*/
|
||||
public fun File.descendantRelativeTo(base: File): File {
|
||||
val prefix = base.canonicalPath
|
||||
val answer = this.canonicalPath
|
||||
return if (answer.startsWith(prefix)) {
|
||||
val prefixSize = prefix.length
|
||||
if (answer.length > prefixSize) {
|
||||
File(answer.substring(prefixSize + 1))
|
||||
} else File("")
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user