@@ -62,14 +62,21 @@ class PackedDecoder(
6262 } else {
6363 val allFlags: BooleanArray
6464 if (totalFlags > 64 ) {
65- val (byteCount, bytesRead) = PackedUtils .decodeVarInt(input, position)
65+ val (byteCount, bytesRead) = PackedUtils .decodeVarInt(
66+ input,
67+ position
68+ )
6669 position + = bytesRead
6770 allFlags = PackedUtils .unpackFlags(input, position, byteCount)
6871 position + = byteCount
6972 } else {
70- val (flagsLong, bytesRead) = PackedUtils .decodeVarLong(input, position)
73+ val (flagsLong, bytesRead) = PackedUtils .decodeVarLong(
74+ input,
75+ position
76+ )
7177 position + = bytesRead
72- allFlags = PackedUtils .unpackFlagsFromLong(flagsLong, totalFlags)
78+ allFlags =
79+ PackedUtils .unpackFlagsFromLong(flagsLong, totalFlags)
7380 }
7481
7582 booleanValues = BooleanArray (booleanIndices.size) { i ->
@@ -122,15 +129,25 @@ class PackedDecoder(
122129 override fun decodeShort (): Short = readShortPos()
123130
124131 override fun decodeInt (): Int {
125- return if (inStructure && ! isCollection) decodeIntElement(currentDescriptor, currentIndex) else readIntPos()
132+ return if (inStructure && ! isCollection) decodeIntElement(
133+ currentDescriptor,
134+ currentIndex
135+ ) else readIntPos()
126136 }
127137
128138 override fun decodeLong (): Long {
129- return if (inStructure && ! isCollection) decodeLongElement(currentDescriptor, currentIndex) else readLongPos()
139+ return if (inStructure && ! isCollection) decodeLongElement(
140+ currentDescriptor,
141+ currentIndex
142+ ) else readLongPos()
130143 }
131144
132- override fun decodeFloat (): Float = java.lang.Float .intBitsToFloat(readIntPos())
133- override fun decodeDouble (): Double = java.lang.Double .longBitsToDouble(readLongPos())
145+ override fun decodeFloat (): Float =
146+ java.lang.Float .intBitsToFloat(readIntPos())
147+
148+ override fun decodeDouble (): Double =
149+ java.lang.Double .longBitsToDouble(readLongPos())
150+
134151 override fun decodeChar (): Char = readUtf8Char()
135152 override fun decodeString (): String = readStringInline()
136153
@@ -180,25 +197,38 @@ class PackedDecoder(
180197 for (i in booleanIndices.indices) if (booleanIndices[i] == index) return i
181198 return - 1
182199 }
200+
183201 private fun nullablePos (index : Int ): Int {
184202 for (i in nullableIndices.indices) if (nullableIndices[i] == index) return i
185203 return - 1
186204 }
187205
188- override fun decodeBooleanElement (descriptor : SerialDescriptor , index : Int ): Boolean {
206+ override fun decodeBooleanElement (
207+ descriptor : SerialDescriptor ,
208+ index : Int
209+ ): Boolean {
189210 val pos = booleanPos(index)
190211 if (pos == - 1 ) error(" Element $index is not a boolean" )
191212 return booleanValues[pos]
192213 }
193214
194- override fun decodeByteElement (descriptor : SerialDescriptor , index : Int ): Byte {
215+ override fun decodeByteElement (
216+ descriptor : SerialDescriptor ,
217+ index : Int
218+ ): Byte {
195219 require(position < input.size)
196220 return input[position++ ]
197221 }
198222
199- override fun decodeShortElement (descriptor : SerialDescriptor , index : Int ): Short = readShortPos()
223+ override fun decodeShortElement (
224+ descriptor : SerialDescriptor ,
225+ index : Int
226+ ): Short = readShortPos()
200227
201- override fun decodeIntElement (descriptor : SerialDescriptor , index : Int ): Int {
228+ override fun decodeIntElement (
229+ descriptor : SerialDescriptor ,
230+ index : Int
231+ ): Int {
202232 val anns = descriptor.getElementAnnotations(index)
203233 val hasFixedInt = anns.hasFixedInt()
204234 val hasVarInt = anns.hasVarInt()
@@ -225,7 +255,10 @@ class PackedDecoder(
225255 }
226256 }
227257
228- override fun decodeLongElement (descriptor : SerialDescriptor , index : Int ): Long {
258+ override fun decodeLongElement (
259+ descriptor : SerialDescriptor ,
260+ index : Int
261+ ): Long {
229262 val anns = descriptor.getElementAnnotations(index)
230263 val hasFixedInt = anns.hasFixedInt()
231264 val hasVarInt = anns.hasVarInt()
@@ -252,23 +285,42 @@ class PackedDecoder(
252285 }
253286 }
254287
255- override fun decodeFloatElement (descriptor : SerialDescriptor , index : Int ): Float =
288+ override fun decodeFloatElement (
289+ descriptor : SerialDescriptor ,
290+ index : Int
291+ ): Float =
256292 java.lang.Float .intBitsToFloat(readIntPos())
257293
258- override fun decodeDoubleElement (descriptor : SerialDescriptor , index : Int ): Double =
294+ override fun decodeDoubleElement (
295+ descriptor : SerialDescriptor ,
296+ index : Int
297+ ): Double =
259298 java.lang.Double .longBitsToDouble(readLongPos())
260299
261- override fun decodeCharElement (descriptor : SerialDescriptor , index : Int ): Char = readUtf8Char()
262- override fun decodeStringElement (descriptor : SerialDescriptor , index : Int ): String = readStringInline()
300+ override fun decodeCharElement (
301+ descriptor : SerialDescriptor ,
302+ index : Int
303+ ): Char = readUtf8Char()
304+
305+ override fun decodeStringElement (
306+ descriptor : SerialDescriptor ,
307+ index : Int
308+ ): String = readStringInline()
263309
264310 @ExperimentalSerializationApi
265- override fun decodeInlineElement (descriptor : SerialDescriptor , index : Int ): Decoder {
311+ override fun decodeInlineElement (
312+ descriptor : SerialDescriptor ,
313+ index : Int
314+ ): Decoder {
266315 currentIndex = index
267316 return this
268317 }
269318
270319 override fun <T > decodeSerializableElement (
271- descriptor : SerialDescriptor , index : Int , deserializer : DeserializationStrategy <T >, previousValue : T ?
320+ descriptor : SerialDescriptor ,
321+ index : Int ,
322+ deserializer : DeserializationStrategy <T >,
323+ previousValue : T ?
272324 ): T {
273325 currentIndex = index
274326
@@ -291,30 +343,53 @@ class PackedDecoder(
291343
292344 @ExperimentalSerializationApi
293345 override fun <T : Any > decodeNullableSerializableElement (
294- descriptor : SerialDescriptor , index : Int , deserializer : DeserializationStrategy <T ?>, previousValue : T ?
346+ descriptor : SerialDescriptor ,
347+ index : Int ,
348+ deserializer : DeserializationStrategy <T ?>,
349+ previousValue : T ?
295350 ): T ? {
296351 if (isCollection) {
297352 val notNull = decodeNotNullMark()
298353 return if (notNull) {
299- decodeSerializableElement(descriptor, index, deserializer as DeserializationStrategy <T >, previousValue)
354+ decodeSerializableElement(
355+ descriptor,
356+ index,
357+ deserializer as DeserializationStrategy <T >,
358+ previousValue
359+ )
300360 } else {
301361 decodeNull()
302362 }
303363 }
304364
305365 val pos = nullablePos(index)
306366 if (pos == - 1 ) {
307- return decodeSerializableElement(descriptor, index, deserializer as DeserializationStrategy <T >, previousValue)
367+ return decodeSerializableElement(
368+ descriptor,
369+ index,
370+ deserializer as DeserializationStrategy <T >,
371+ previousValue
372+ )
308373 }
309374
310375 if (nullValues[pos]) return null
311376
312- return decodeSerializableElement(descriptor, index, deserializer as DeserializationStrategy <T >, previousValue)
377+ return decodeSerializableElement(
378+ descriptor,
379+ index,
380+ deserializer as DeserializationStrategy <T >,
381+ previousValue
382+ )
313383 }
314384
315- private fun readShortPos (): Short = PackedUtils .readShort(input, position).also { position + = 2 }
316- private fun readIntPos (): Int = PackedUtils .readInt(input, position).also { position + = 4 }
317- private fun readLongPos (): Long = PackedUtils .readLong(input, position).also { position + = 8 }
385+ private fun readShortPos (): Short =
386+ PackedUtils .readShort(input, position).also { position + = 2 }
387+
388+ private fun readIntPos (): Int =
389+ PackedUtils .readInt(input, position).also { position + = 4 }
390+
391+ private fun readLongPos (): Long =
392+ PackedUtils .readLong(input, position).also { position + = 8 }
318393
319394 private fun readUtf8Char (): Char {
320395 require(position < input.size) { " Unexpected EOF while decoding UTF-8 char" }
@@ -327,6 +402,7 @@ class PackedDecoder(
327402 require((b1 and 0b1100_0000 ) == 0b1000_0000 ) { " Invalid UTF-8 continuation byte" }
328403 2 to (((b0 and 0x1F ) shl 6 ) or (b1 and 0x3F ))
329404 }
405+
330406 (b0 and 0b1111_0000 ) == 0b1110_0000 -> {
331407 require(position + 3 <= input.size)
332408 val b1 = input[position + 1 ].toInt() and 0xFF
@@ -335,6 +411,7 @@ class PackedDecoder(
335411 require((b2 and 0b1100_0000 ) == 0b1000_0000 ) { " Invalid UTF-8 continuation byte" }
336412 3 to (((b0 and 0x0F ) shl 12 ) or ((b1 and 0x3F ) shl 6 ) or (b2 and 0x3F ))
337413 }
414+
338415 else -> throw IllegalArgumentException (" Invalid UTF-8 start byte" )
339416 }
340417 position + = len
0 commit comments