Compare root of FilePathComponents according to case-sensitivity rules of current file system.
This commit is contained in:
@@ -91,6 +91,11 @@ public data class FilePathComponents(public val rootName: String, public val fil
|
|||||||
*/
|
*/
|
||||||
public fun size(): Int = fileList.size
|
public fun size(): Int = fileList.size
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [File] representing root of the path (for example, `/` or `C:` or empty for relative paths).
|
||||||
|
*/
|
||||||
|
public val root: File = File(rootName)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sub-path of the path, starting with the directory at the specified [beginIndex] and up
|
* Returns a sub-path of the path, starting with the directory at the specified [beginIndex] and up
|
||||||
* to the specified [endIndex].
|
* to the specified [endIndex].
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ private fun File.relativeToOrNull(base: File): String? {
|
|||||||
// Check roots
|
// Check roots
|
||||||
val thisComponents = this.filePathComponents().normalize()
|
val thisComponents = this.filePathComponents().normalize()
|
||||||
val baseComponents = base.filePathComponents().normalize()
|
val baseComponents = base.filePathComponents().normalize()
|
||||||
if (thisComponents.rootName != baseComponents.rootName) {
|
if (thisComponents.root != baseComponents.root) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,7 +333,7 @@ public fun File.listFiles(filter: (file: File) -> Boolean): Array<File>? = listF
|
|||||||
public fun File.startsWith(other: File): Boolean {
|
public fun File.startsWith(other: File): Boolean {
|
||||||
val components = filePathComponents()
|
val components = filePathComponents()
|
||||||
val otherComponents = other.filePathComponents()
|
val otherComponents = other.filePathComponents()
|
||||||
if (components.rootName != otherComponents.rootName && otherComponents.rootName != "")
|
if (components.root != otherComponents.root && otherComponents.rootName != "")
|
||||||
return false
|
return false
|
||||||
return if (components.size() < otherComponents.size()) false
|
return if (components.size() < otherComponents.size()) false
|
||||||
else components.fileList.subList(0, otherComponents.size()).equals(otherComponents.fileList)
|
else components.fileList.subList(0, otherComponents.size()).equals(otherComponents.fileList)
|
||||||
@@ -360,7 +360,7 @@ public fun File.startsWith(other: String): Boolean = startsWith(File(other))
|
|||||||
public fun File.endsWith(other: File): Boolean {
|
public fun File.endsWith(other: File): Boolean {
|
||||||
val components = filePathComponents()
|
val components = filePathComponents()
|
||||||
val otherComponents = other.filePathComponents()
|
val otherComponents = other.filePathComponents()
|
||||||
if (components.rootName != otherComponents.rootName && otherComponents.rootName != "")
|
if (components.root != otherComponents.root && otherComponents.rootName != "")
|
||||||
return false
|
return false
|
||||||
val shift = components.size() - otherComponents.size()
|
val shift = components.size() - otherComponents.size()
|
||||||
return if (shift < 0) false
|
return if (shift < 0) false
|
||||||
@@ -436,8 +436,7 @@ public fun File.resolve(relative: String): File = resolve(File(relative))
|
|||||||
public fun File.resolveSibling(relative: File): File {
|
public fun File.resolveSibling(relative: File): File {
|
||||||
val components = filePathComponents()
|
val components = filePathComponents()
|
||||||
val parentSubPath = if (components.size() == 0) File("..") else components.subPath(0, components.size() - 1)
|
val parentSubPath = if (components.size() == 0) File("..") else components.subPath(0, components.size() - 1)
|
||||||
val rootName = components.rootName
|
return components.root.resolve(parentSubPath).resolve(relative)
|
||||||
return File(rootName).resolve(parentSubPath).resolve(relative)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import kotlin.test.*
|
|||||||
|
|
||||||
class FilesTest {
|
class FilesTest {
|
||||||
|
|
||||||
|
private val isCaseInsensitiveFileSystem = File("C:/") == File("c:/")
|
||||||
|
|
||||||
@test fun testPath() {
|
@test fun testPath() {
|
||||||
val fileSuf = System.currentTimeMillis().toString()
|
val fileSuf = System.currentTimeMillis().toString()
|
||||||
val file1 = createTempFile("temp", fileSuf)
|
val file1 = createTempFile("temp", fileSuf)
|
||||||
@@ -100,6 +102,10 @@ class FilesTest {
|
|||||||
assertEquals("..", file6.relativeTo(file7))
|
assertEquals("..", file6.relativeTo(file7))
|
||||||
assertEquals("Documents", file7.relativeTo(file6))
|
assertEquals("Documents", file7.relativeTo(file6))
|
||||||
|
|
||||||
|
if (isCaseInsensitiveFileSystem) {
|
||||||
|
assertEquals("bar", File("C:/bar").relativeTo(File("c:/")))
|
||||||
|
}
|
||||||
|
|
||||||
val file8 = File("""\\my.host\home/user/documents/vip""")
|
val file8 = File("""\\my.host\home/user/documents/vip""")
|
||||||
val file9 = File("""\\my.host\home/other/images/nice""")
|
val file9 = File("""\\my.host\home/other/images/nice""")
|
||||||
|
|
||||||
@@ -215,6 +221,9 @@ class FilesTest {
|
|||||||
assertTrue(File("C:\\Users\\Me\\Temp\\Game").startsWith("C:\\Users\\Me"))
|
assertTrue(File("C:\\Users\\Me\\Temp\\Game").startsWith("C:\\Users\\Me"))
|
||||||
assertFalse(File("C:\\Users\\Me\\Temp\\Game").startsWith("C:\\Users\\He"))
|
assertFalse(File("C:\\Users\\Me\\Temp\\Game").startsWith("C:\\Users\\He"))
|
||||||
assertTrue(File("C:\\Users\\Me").startsWith("C:\\"))
|
assertTrue(File("C:\\Users\\Me").startsWith("C:\\"))
|
||||||
|
if (isCaseInsensitiveFileSystem) {
|
||||||
|
assertTrue(File("C:\\Users\\Me").startsWith("c:\\"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun endsWith() {
|
@test fun endsWith() {
|
||||||
@@ -224,6 +233,9 @@ class FilesTest {
|
|||||||
assertTrue(File("/foo/bar/gav/bar").endsWith("/gav/bar"))
|
assertTrue(File("/foo/bar/gav/bar").endsWith("/gav/bar"))
|
||||||
assertFalse(File("/foo/bar/gav").endsWith("/bar"))
|
assertFalse(File("/foo/bar/gav").endsWith("/bar"))
|
||||||
assertFalse(File("foo/bar").endsWith("/bar"))
|
assertFalse(File("foo/bar").endsWith("/bar"))
|
||||||
|
if (isCaseInsensitiveFileSystem) {
|
||||||
|
assertTrue(File("/foo/bar").endsWith("Bar"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun subPath() {
|
@test fun subPath() {
|
||||||
|
|||||||
Reference in New Issue
Block a user