Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions okio/src/commonMain/kotlin/okio/internal/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ private fun Path.rootLength(): Int {
return 1
}

// Look for a root like `C:\`.
if (bytes.size > 2 && bytes[1] == ':'.code.toByte() && bytes[2] == '\\'.code.toByte()) {
// Look for a root like `C:\` or `C:/`.
if (bytes.size > 2 && bytes[1] == ':'.code.toByte() &&
(bytes[2] == '\\'.code.toByte() || bytes[2] == '/'.code.toByte())
) {
val c = bytes[0].toInt().toChar()
if (c !in 'a'..'z' && c !in 'A'..'Z') return -1
return 3
Expand All @@ -113,7 +115,6 @@ internal inline fun Path.commonIsRelative(): Boolean {

@Suppress("NOTHING_TO_INLINE")
internal inline fun Path.commonVolumeLetter(): Char? {
if (bytes.indexOf(SLASH) != -1) return null
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was preventing forward slashes from being accepted

if (bytes.size < 2) return null
if (bytes[1] != ':'.code.toByte()) return null
val c = bytes[0].toInt().toChar()
Expand Down Expand Up @@ -396,7 +397,7 @@ private fun Byte.toSlash(): ByteString {
}

private fun Buffer.startsWithVolumeLetterAndColon(slash: ByteString): Boolean {
if (slash != BACKSLASH) return false
if (slash != BACKSLASH && slash != SLASH) return false
if (size < 2) return false
if (get(1) != ':'.code.toByte()) return false
val b = get(0).toInt().toChar()
Expand Down
28 changes: 28 additions & 0 deletions okio/src/commonTest/kotlin/okio/PathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ class PathTest {
assertFalse(path.isRoot)
}

@Test
fun windowsVolumeLetterWithForwardSlash() {
val path = "C:/".toPath()
assertEquals(path, path.normalized())
assertEquals("C:/".toPath(), path.root)
assertEquals(listOf(), path.segments)
assertEquals("C:/", path.toString())
assertNull(path.parent)
assertEquals('C', path.volumeLetter)
assertEquals("", path.name)
assertTrue(path.isAbsolute)
assertTrue(path.isRoot)
}

@Test
fun windowsAbsolutePathWithVolumeLetterAndForwardSlash() {
val path = "C:/Windows/notepad.exe".toPath()
assertEquals(path, path.normalized())
assertEquals("C:/".toPath(), path.root)
assertEquals(listOf("Windows", "notepad.exe"), path.segments)
assertEquals("C:/Windows/notepad.exe", path.toString())
assertEquals("C:/Windows".toPath(), path.parent)
assertEquals('C', path.volumeLetter)
assertEquals("notepad.exe", path.name)
assertTrue(path.isAbsolute)
assertFalse(path.isRoot)
}

@Test
fun windowsAbsolutePath() {
val path = "\\".toPath()
Expand Down