Skip to content

Commit 3f2b819

Browse files
authored
Merge pull request #80 from touchlab/psh/handle-file-url-prefix
Handle file:// url prefix when fixing the databasePath
2 parents 6a131ef + f59c6b1 commit 3f2b819

File tree

4 files changed

+40
-21
lines changed
  • sqliter-driver/src

4 files changed

+40
-21
lines changed

sqliter-driver/src/appleMain/kotlin/co/touchlab/sqliter/internal/File.kt

+7-9
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ internal actual class File(dirPath:String?=null, name:String){
7777
val newPath:CharArray = origPath.toCharArray()
7878
val length = newPath.size
7979
var newLength = 0
80-
for (i in 0 until length) {
80+
val initialIndex = if (origPath.startsWith("file://", true)) 7 else 0
81+
for (i in initialIndex until length) {
8182
val ch = newPath[i]
8283
if (ch == '/') {
8384
if (!lastWasSlash) {
@@ -93,15 +94,12 @@ internal actual class File(dirPath:String?=null, name:String){
9394
if (lastWasSlash && newLength > 1) {
9495
newLength--
9596
}
97+
9698
// Reuse the original string if possible.
97-
return if (newLength != length) {
98-
val sb = StringBuilder(newLength)
99-
sb.append(newPath)
100-
sb.toString()
101-
}
102-
else {
103-
origPath
104-
}
99+
return if (newLength != length) buildString(newLength) {
100+
append(newPath)
101+
setLength(newLength)
102+
} else origPath
105103
}
106104

107105
private fun join(prefix: String, suffix: String): String {

sqliter-driver/src/linuxX64Main/kotlin/co/touchlab/sqliter/internal/File.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ internal actual class File(dirPath: String? = null, name: String) {
9494
val newPath: CharArray = origPath.toCharArray()
9595
val length = newPath.size
9696
var newLength = 0
97-
for (i in 0 until length) {
97+
val initialIndex = if (origPath.startsWith("file://", true)) 7 else 0
98+
for (i in initialIndex until length) {
9899
val ch = newPath[i]
99100
if (ch == '/') {
100101
if (!lastWasSlash) {
@@ -110,12 +111,12 @@ internal actual class File(dirPath: String? = null, name: String) {
110111
if (lastWasSlash && newLength > 1) {
111112
newLength--
112113
}
114+
113115
// Reuse the original string if possible.
114-
return if (newLength != length) {
115-
newPath.concatToString()
116-
} else {
117-
origPath
118-
}
116+
return if (newLength != length) buildString(newLength) {
117+
append(newPath)
118+
setLength(newLength)
119+
} else origPath
119120
}
120121

121122
private fun join(prefix: String, suffix: String): String {

sqliter-driver/src/mingwMain/kotlin/co/touchlab/sqliter/internal/File.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ internal actual class File(dirPath:String? = null, name:String) {
7878
val newPath:CharArray = origPath.toCharArray()
7979
val length = newPath.size
8080
var newLength = 0
81-
for (ch in newPath) {
81+
val initialIndex = if (origPath.startsWith("file://", true)) 7 else 0
82+
for (i in initialIndex until length) {
83+
val ch = newPath[i]
8284
if (ch == separatorChar) {
8385
if (!lastWasSlash) {
8486
newPath[newLength++] = separatorChar
@@ -94,12 +96,12 @@ internal actual class File(dirPath:String? = null, name:String) {
9496
if (lastWasSlash && newLength > 1) {
9597
newLength--
9698
}
99+
97100
// Reuse the original string if possible.
98-
return if (newLength != length) {
99-
newPath.concatToString()
100-
} else {
101-
origPath
102-
}
101+
return if (newLength != length) buildString(newLength) {
102+
append(newPath)
103+
setLength(newLength)
104+
} else origPath
103105
}
104106

105107
/**

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/DatabaseConfigurationTest.kt

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ class DatabaseConfigurationTest : BaseDatabaseTest(){
2626
assertTrue(dbPathString.endsWith(TEST_DB_NAME))
2727
}
2828

29+
@Test
30+
fun databasePathRemovesExtraSlashes() {
31+
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, "//tmp//")
32+
assertEquals("/tmp/$TEST_DB_NAME", dbPathString)
33+
}
34+
35+
@Test
36+
fun databasePathRemovesFileUrlPrefix() {
37+
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, "file:///tmp/")
38+
assertEquals("/tmp/$TEST_DB_NAME", dbPathString)
39+
}
40+
41+
@Test
42+
fun databasePathRemovesFileUrlPrefixInCaps() {
43+
val dbPathString = DatabaseFileContext.databasePath(TEST_DB_NAME, "FILE:///tmp/")
44+
assertEquals("/tmp/$TEST_DB_NAME", dbPathString)
45+
}
46+
2947
@Test
3048
fun memoryOnlyTest(){
3149
val conf = DatabaseConfiguration(

0 commit comments

Comments
 (0)