Skip to content

Commit 3d8b795

Browse files
committed
🐛 improve key extraction and decoding logic for better clarity and performance
1 parent 0b432ac commit 3d8b795

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ out/
1616
!**/src/main/**/out/
1717
!**/src/test/**/out/
1818

19+
## Gradle ###
20+
local.properties
21+
1922
### Kotlin ###
2023
.kotlin
2124

@@ -42,4 +45,4 @@ bin/
4245
.vscode/
4346

4447
### Mac OS ###
45-
.DS_Store
48+
.DS_Store

qs-kotlin/src/main/kotlin/io/github/techouse/qskotlin/internal/Decoder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ internal object Decoder {
113113
key = options.getDecoder(part, charset).toString()
114114
value = if (options.strictNullHandling) null else ""
115115
} else {
116-
key = options.getDecoder(part.substring(0, pos), charset).toString()
116+
key = options.getDecoder(part.take(pos), charset).toString()
117117
value =
118118
Utils.apply<Any?>(
119119
parseListValue(
@@ -210,7 +210,7 @@ internal object Decoder {
210210
} else cleanRoot
211211

212212
val index: Int? =
213-
if (decodedRoot.isNotEmpty() && decodedRoot.all(Char::isDigit))
213+
if (decodedRoot.isNotEmpty() && decodedRoot.toIntOrNull() != null)
214214
decodedRoot.toInt()
215215
else null
216216

@@ -312,7 +312,7 @@ internal object Decoder {
312312
val segments = ArrayList<String>(key.count { it == '[' } + 1)
313313

314314
val first = key.indexOf('[')
315-
val parent = if (first >= 0) key.substring(0, first) else key
315+
val parent = if (first >= 0) key.take(first) else key
316316
if (parent.isNotEmpty()) segments.add(parent)
317317

318318
var open = first

qs-kotlin/src/test/kotlin/io/github/techouse/qskotlin/unit/DecodeSpec.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,9 @@ class DecodeSpec :
523523
decode("a[0]=b", DecodeOptions(listLimit = 0)) shouldBe mapOf("a" to listOf("b"))
524524

525525
decode("a[-1]=b", DecodeOptions(listLimit = -1)) shouldBe
526-
mapOf("a" to mapOf("-1" to "b"))
526+
mapOf("a" to mapOf(-1 to "b"))
527527
decode("a[-1]=b", DecodeOptions(listLimit = 0)) shouldBe
528-
mapOf("a" to mapOf("-1" to "b"))
528+
mapOf("a" to mapOf(-1 to "b"))
529529

530530
decode("a[0]=b&a[1]=c", DecodeOptions(listLimit = -1)) shouldBe
531531
mapOf("a" to mapOf(0 to "b", 1 to "c"))

0 commit comments

Comments
 (0)