Do not convert separatorsToSystem in file path extensions.

Do not require network host in network share root to contain dot.
File.root now is always non-null to be consistent with File.toComponents().root.
Introduce invariantSeparatorsPath property.
Deprecate relativeTo to make it return File later. Introduce toRelativeString instead.
Introduce relativeToOrNull and relativeToOrSelf extensions.
This commit is contained in:
Ilya Gorbunov
2015-12-10 01:26:01 +03:00
parent 52eb9e4276
commit 7c9b53c75c
4 changed files with 186 additions and 117 deletions
@@ -29,12 +29,11 @@ private fun String.getRootName(): String {
// So in Windows we'll have root of //my.host/home but in Unix just /
first = indexOf(File.separatorChar, 2)
if (first >= 0) {
val dot = indexOf('.', 2)
if (dot >= 0 && dot < first) {
first = indexOf(File.separatorChar, first + 1)
if (first >= 0)
return substring(0, first + 1)
}
first = indexOf(File.separatorChar, first + 1)
if (first >= 0)
return substring(0, first + 1)
else
return this
}
}
return substring(0, 1)
@@ -65,18 +64,23 @@ private fun String.getRootName(): String {
* @return string representing the root for this file, or empty string is this file name is relative.
*/
public val File.rootName: String
get() = separatorsToSystem().getRootName()
get() = path.getRootName()
/**
* Returns root component of this abstract name, like / from /home/user, or C:\ from C:\file.tmp,
* or //my.host/home for //my.host/home/user,
* or `null` if this name is relative, like bar/gav
* or //my.host/home for //my.host/home/user
*/
public val File.root: File?
get() {
val name = rootName
return if (name.length > 0) File(name) else null
}
public val File.root: File
get() = File(rootName)
/**
* Determines whether this file has a root or it represents a relative path.
*
* Returns `true` when this file has non-empty root.
*/
public val File.isRooted: Boolean
// TODO: Do not calculate root
get() = path.getRootName().isNotEmpty()
/**
* Represents the path to a file as a collection of directories.
@@ -91,12 +95,19 @@ public data class FilePathComponents
@Deprecated("This constructor will be removed soon. Use File.toComponents() extension to create an instance of FilePathComponents.")
constructor (rootName: String, fileList: List<File>): this(File(rootName), fileList)
@Deprecated("Use 'root' property or 'root.path' instead.", ReplaceWith("root.path"))
public val rootName: String get() = root.path
@Deprecated("Use 'segments' property instead.", ReplaceWith("segments"))
public val fileList: List<File> get() = segments
/**
* Returns a string representing the root for this file, or an empty string is this file name is relative.
*/
public val rootName: String get() = root.path
/**
* Returns `true` when the [root] is not empty.
*/
public val isRooted: Boolean get() = root.path.isNotEmpty()
/**
* Returns the number of elements in the path to the file.
*/
@@ -122,7 +133,7 @@ public data class FilePathComponents
* itself) and returns the resulting collection of components.
*/
public fun File.toComponents(): FilePathComponents {
val path = separatorsToSystem()
val path = path
val rootName = path.getRootName()
val subPath = path.substring(rootName.length)
// if: a special case when we have only root component
+60 -8
View File
@@ -69,7 +69,7 @@ public val File.extension: String
*
* @return the pathname with system separators.
*/
@Deprecated("Use File.separatorsToSystem instead", ReplaceWith("File(this).separatorsToSystem()", "java.io.File"))
@Deprecated("Use File.path instead", ReplaceWith("File(this).path", "java.io.File"))
public fun String.separatorsToSystem(): String {
val otherSep = if (File.separator == "/") "\\" else "/"
return replace(otherSep, File.separator)
@@ -101,10 +101,18 @@ public fun String.allSeparatorsToSystem(): String {
*
* @return the pathname with system separators.
*/
@Deprecated("File has already system separators.")
public fun File.separatorsToSystem(): String {
return toString().separatorsToSystem()
}
/**
* Returns [path] of this File using the invariant separator '/' to
* separate the names in the name sequence.
*/
public val File.invariantSeparatorsPath: String
get() = if (File.separatorChar != '/') path.replace(File.separatorChar, '/') else path
/**
* Returns file's name without an extension.
*/
@@ -118,16 +126,60 @@ public val File.nameWithoutExtension: String
*
* @return relative path from [base] to this.
*
* @throws IllegalArgumentException if child and parent have different roots.
* @throws IllegalArgumentException if this and base paths have different roots.
*/
@Deprecated("This function will change return type to File soon. Use toRelativeString instead.", ReplaceWith("toRelativeString(base)"))
public fun File.relativeTo(base: File): String
= relativeToOrNull(base) ?: throw IllegalArgumentException("this and base files have different roots: $this and $base")
= toRelativeString(base)
// TODO
//private fun File.relativeToOrPath(base: File): String
// = relativeToOrNull(base) ?: toString()
private fun File.relativeToOrNull(base: File): String? {
/**
* Calculates the relative path for this file from [base] file.
* Note that the [base] file is treated as a directory.
* If this file matches the [base] file, then an empty string will be returned.
*
* @return relative path from [base] to this.
*
* @throws IllegalArgumentException if this and base paths have different roots.
*/
public fun File.toRelativeString(base: File): String
= toRelativeStringOrNull(base) ?: throw IllegalArgumentException("this and base files have different roots: $this and $base")
/**
* Calculates the relative path for this file from [base] file.
* Note that the [base] file is treated as a directory.
* If this file matches the [base] file, then a [File] with empty path will be returned.
*
* @return File with relative path from [base] to this.
*
* @throws IllegalArgumentException if this and base paths have different roots.
*/
@Deprecated("This function will be renamed to relativeTo soon.")
public fun File.relativeToFile(base: File): File = File(this.relativeTo(base))
/**
* Calculates the relative path for this file from [base] file.
* Note that the [base] file is treated as a directory.
* If this file matches the [base] file, then a [File] with empty path will be returned.
*
* @return File with relative path from [base] to this, or `this` if this and base paths have different roots.
*/
public fun File.relativeToOrSelf(base: File): File
= toRelativeStringOrNull(base)?.let { File(it) } ?: this
/**
* Calculates the relative path for this file from [base] file.
* Note that the [base] file is treated as a directory.
* If this file matches the [base] file, then a [File] with empty path will be returned.
*
* @return File with relative path from [base] to this, or `null` if this and base paths have different roots.
*/
public fun File.relativeToOrNull(base: File): File?
= toRelativeStringOrNull(base)?.let { File(it) }
private fun File.toRelativeStringOrNull(base: File): String? {
// Check roots
val thisComponents = this.toComponents().normalize()
val baseComponents = base.toComponents().normalize()
@@ -411,7 +463,7 @@ private fun List<File>.normalize(): List<File> {
* @return concatenated this and [relative] paths, or just [relative] if it's absolute.
*/
public fun File.resolve(relative: File): File {
if (relative.root != null)
if (relative.isRooted)
return relative
val baseName = this.toString()
return if (baseName.isEmpty() || baseName.endsWith(File.separatorChar)) File(baseName + relative) else File(baseName + File.separatorChar + relative)