Additional fix of String.getRootName() for network names, relevant test fixes

This commit is contained in:
Mikhail Glukhikh
2015-03-23 14:55:27 +03:00
parent 5b636eef7b
commit 88733d34eb
2 changed files with 15 additions and 7 deletions
@@ -6,12 +6,11 @@ import java.util.NoSuchElementException
private fun String.getRootName(): String {
// Note: separators should be already replaced to system ones
var first = indexOf(File.separatorChar, 0)
println("$this.getRootName: first=$first")
if (first == 0) {
if (length() > 1) {
if (length() > 1 && this[1] == File.separatorChar) {
// Network names like //my.host/home/something ? => //my.host/home/ should be root
// We have to consider here also /my.host/home/something because on Unix
// File converts // just to /
// NB: does not work in Unix because //my.host/home is converted into /my.host/home there
// 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)
+12 -3
View File
@@ -624,7 +624,11 @@ class FilesTest {
checkFileElements(File("C:\\"), File("C:\\".separatorsToSystem()), listOf())
checkFileElements(File("C:/"), File("C:/"), listOf())
checkFileElements(File("C:"), File("C:"), listOf())
checkFileElements(File("//host.ru/home/mike"), File("//host.ru/home"), listOf("mike"))
if (File.separatorChar == '\\') {
// Check only in Windows
checkFileElements(File("\\\\host.ru\\home\\mike"), File("\\\\host.ru\\home"), listOf("mike"))
checkFileElements(File("//host.ru/home/mike"), File("//host.ru/home"), listOf("mike"))
}
checkFileElements(File(""), null, listOf(""))
checkFileElements(File("."), null, listOf("."))
checkFileElements(File(".."), null, listOf(".."))
@@ -646,9 +650,14 @@ class FilesTest {
}
test fun subPath() {
assertEquals(File("mike"), File("//my.host.net/home/mike/temp").subPath(0, 1))
assertEquals(File("mike"), File("\\\\my.host.net\\home\\mike\\temp").subPath(0, 1))
if (File.separatorChar == '\\') {
// Check only in Windows
assertEquals(File("mike"), File("//my.host.net/home/mike/temp").subPath(0, 1))
assertEquals(File("mike"), File("\\\\my.host.net\\home\\mike\\temp").subPath(0, 1))
}
assertEquals(File("bar/gav"), File("/foo/bar/gav/hi").subPath(1, 3))
assertEquals(File("foo"), File("/foo/bar/gav/hi").subPath(0, 1))
assertEquals(File("gav/hi"), File("/foo/bar/gav/hi").subPath(2, 4))
}
test fun normalize() {