Skip to content

Commit e75c7a7

Browse files
committed
Avoid index parsing when insertLast is used
1 parent c020d3f commit e75c7a7

File tree

1 file changed

+16
-8
lines changed
  • src/commonMain/kotlin/io/github/optimumcode/json/pointer

1 file changed

+16
-8
lines changed

src/commonMain/kotlin/io/github/optimumcode/json/pointer/JsonPointer.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public sealed class JsonPointer(
7373
PointerParent(
7474
parent,
7575
node.propertyName,
76+
node.index,
7677
)
7778
node = node.next
7879
}
@@ -168,6 +169,7 @@ public sealed class JsonPointer(
168169
private class PointerParent(
169170
val parent: PointerParent?,
170171
val segment: String,
172+
val index: Int? = null,
171173
)
172174

173175
private fun buildPath(
@@ -179,10 +181,18 @@ public sealed class JsonPointer(
179181
while (parentValue != null) {
180182
curr =
181183
parentValue.run {
182-
SegmentPointer(
183-
segment,
184-
curr,
185-
)
184+
if (index == null) {
185+
SegmentPointer(
186+
segment,
187+
curr,
188+
)
189+
} else {
190+
SegmentPointer(
191+
segment,
192+
curr,
193+
index,
194+
)
195+
}
186196
}
187197
parentValue = parentValue.parent
188198
}
@@ -269,12 +279,10 @@ private fun StringBuilder.appendEscaped(ch: Char) {
269279
internal object EmptyPointer : JsonPointer()
270280

271281
internal class SegmentPointer(
272-
segment: String,
282+
val propertyName: String,
273283
override val next: JsonPointer = EmptyPointer,
284+
val index: Int = parseIndex(propertyName),
274285
) : JsonPointer(next) {
275-
val propertyName: String = segment
276-
val index: Int = parseIndex(segment)
277-
278286
companion object {
279287
private const val NO_INDEX: Int = -1
280288
private const val LONG_LENGTH_THRESHOLD = 10

0 commit comments

Comments
 (0)