Skip to content

Commit f8df6c7

Browse files
committed
refactor: simplify PackedUtils by making functions top-level, remove internal object usage
1 parent 792ce91 commit f8df6c7

File tree

6 files changed

+239
-242
lines changed

6 files changed

+239
-242
lines changed

src/commonMain/kotlin/com/eignex/kencode/PackedContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal class HeaderContext {
126126
fun load(input: ByteArray, offset: Int, count: Int): Int {
127127
if (count == 0) return 0
128128
val numBytes = (count + 7) / 8
129-
val flags = PackedUtils.unpackFlags(input, offset, numBytes)
129+
val flags = unpackFlags(input, offset, numBytes)
130130
repeat(count) { bits.add(if (it < flags.size) flags[it] else false) }
131131
return numBytes
132132
}

src/commonMain/kotlin/com/eignex/kencode/PackedDecoder.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class PackedDecoder internal constructor(
9898
} else {
9999
val numBytes = (totalFlags + 7) / 8
100100
val allFlags =
101-
PackedUtils.unpackFlags(input, position, numBytes)
101+
unpackFlags(input, position, numBytes)
102102
position += numBytes
103103
booleanValues =
104104
BooleanArray(bitmask.boolCount) { i -> allFlags[i] }
@@ -120,13 +120,13 @@ class PackedDecoder internal constructor(
120120
if (isNullableCollection) {
121121
val numBytes = (size + 7) / 8
122122
collectionNullBitmap =
123-
PackedUtils.unpackFlags(input, position, numBytes)
123+
unpackFlags(input, position, numBytes)
124124
position += numBytes
125125
}
126126
if (isBooleanCollection) {
127127
val numBytes = (size + 7) / 8
128128
collectionBoolBitmap =
129-
PackedUtils.unpackFlags(input, position, numBytes)
129+
unpackFlags(input, position, numBytes)
130130
position += numBytes
131131
}
132132
}
@@ -174,7 +174,7 @@ class PackedDecoder internal constructor(
174174
decodeIntElement(currentDescriptor, currentIndex)
175175
} else {
176176
when (config.defaultEncoding) {
177-
IntPacking.SIGNED -> PackedUtils.zigZagDecodeInt(readVarInt())
177+
IntPacking.SIGNED -> zigZagDecodeInt(readVarInt())
178178
IntPacking.DEFAULT -> readVarInt()
179179
IntPacking.FIXED -> readIntPos()
180180
}
@@ -185,7 +185,7 @@ class PackedDecoder internal constructor(
185185
decodeLongElement(currentDescriptor, currentIndex)
186186
} else {
187187
when (config.defaultEncoding) {
188-
IntPacking.SIGNED -> PackedUtils.zigZagDecodeLong(readVarLong())
188+
IntPacking.SIGNED -> zigZagDecodeLong(readVarLong())
189189
IntPacking.DEFAULT -> readVarLong()
190190
IntPacking.FIXED -> readLongPos()
191191
}
@@ -273,7 +273,7 @@ class PackedDecoder internal constructor(
273273
config
274274
)
275275
) {
276-
IntPacking.SIGNED -> PackedUtils.zigZagDecodeInt(readVarInt())
276+
IntPacking.SIGNED -> zigZagDecodeInt(readVarInt())
277277
IntPacking.DEFAULT -> readVarInt()
278278
IntPacking.FIXED -> readIntPos()
279279
}
@@ -288,7 +288,7 @@ class PackedDecoder internal constructor(
288288
config
289289
)
290290
) {
291-
IntPacking.SIGNED -> PackedUtils.zigZagDecodeLong(readVarLong())
291+
IntPacking.SIGNED -> zigZagDecodeLong(readVarLong())
292292
IntPacking.DEFAULT -> readVarLong()
293293
IntPacking.FIXED -> readLongPos()
294294
}
@@ -433,13 +433,13 @@ class PackedDecoder internal constructor(
433433
}
434434

435435
private fun readShortPos(): Short =
436-
PackedUtils.readShort(input, position).also { position += 2 }
436+
readShort(input, position).also { position += 2 }
437437

438438
private fun readIntPos(): Int =
439-
PackedUtils.readInt(input, position).also { position += 4 }
439+
readInt(input, position).also { position += 4 }
440440

441441
private fun readLongPos(): Long =
442-
PackedUtils.readLong(input, position).also { position += 8 }
442+
readLong(input, position).also { position += 8 }
443443

444444
private fun readUtf8Char(): Char {
445445
require(position < input.size) { "Unexpected EOF while decoding UTF-8 char" }

src/commonMain/kotlin/com/eignex/kencode/PackedEncoder.kt

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PackedEncoder internal constructor(
7373
collectionSize: Int
7474
): CompositeEncoder {
7575
val target = if (inStructure) dataBuffer else output
76-
PackedUtils.writeVarInt(collectionSize, target)
76+
writeVarInt(collectionSize, target)
7777
val childEncoder = PackedEncoder(target, config, serializersModule)
7878
childEncoder.initializeStructure(descriptor)
7979
return childEncoder
@@ -135,27 +135,27 @@ class PackedEncoder internal constructor(
135135
}
136136

137137
override fun encodeShort(value: Short) {
138-
PackedUtils.writeShort(value, getBuffer())
138+
writeShort(value, getBuffer())
139139
}
140140

141141
override fun encodeInt(value: Int) {
142142
if (inStructure && !isCollection) {
143143
encodeIntElement(currentDescriptor, currentIndex, value)
144144
} else {
145145
when (config.defaultEncoding) {
146-
IntPacking.SIGNED -> PackedUtils.writeVarInt(
147-
PackedUtils.zigZagEncodeInt(
146+
IntPacking.SIGNED -> writeVarInt(
147+
zigZagEncodeInt(
148148
value
149149
),
150150
getBuffer()
151151
)
152152

153-
IntPacking.DEFAULT -> PackedUtils.writeVarInt(
153+
IntPacking.DEFAULT -> writeVarInt(
154154
value,
155155
getBuffer()
156156
)
157157

158-
IntPacking.FIXED -> PackedUtils.writeInt(value, getBuffer())
158+
IntPacking.FIXED -> writeInt(value, getBuffer())
159159
}
160160
}
161161
}
@@ -165,29 +165,29 @@ class PackedEncoder internal constructor(
165165
encodeLongElement(currentDescriptor, currentIndex, value)
166166
} else {
167167
when (config.defaultEncoding) {
168-
IntPacking.SIGNED -> PackedUtils.writeVarLong(
169-
PackedUtils.zigZagEncodeLong(
168+
IntPacking.SIGNED -> writeVarLong(
169+
zigZagEncodeLong(
170170
value
171171
),
172172
getBuffer()
173173
)
174174

175-
IntPacking.DEFAULT -> PackedUtils.writeVarLong(
175+
IntPacking.DEFAULT -> writeVarLong(
176176
value,
177177
getBuffer()
178178
)
179179

180-
IntPacking.FIXED -> PackedUtils.writeLong(value, getBuffer())
180+
IntPacking.FIXED -> writeLong(value, getBuffer())
181181
}
182182
}
183183
}
184184

185185
override fun encodeFloat(value: Float) {
186-
PackedUtils.writeInt(value.toBits(), getBuffer())
186+
writeInt(value.toBits(), getBuffer())
187187
}
188188

189189
override fun encodeDouble(value: Double) {
190-
PackedUtils.writeLong(value.toBits(), getBuffer())
190+
writeLong(value.toBits(), getBuffer())
191191
}
192192

193193
override fun encodeChar(value: Char) {
@@ -197,13 +197,13 @@ class PackedEncoder internal constructor(
197197
override fun encodeString(value: String) {
198198
val bytes = value.encodeToByteArray()
199199
val buf = getBuffer()
200-
PackedUtils.writeVarInt(bytes.size, buf)
200+
writeVarInt(bytes.size, buf)
201201
buf.write(bytes)
202202
}
203203

204204
@ExperimentalSerializationApi
205205
override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int) {
206-
PackedUtils.writeVarInt(index, getBuffer())
206+
writeVarInt(index, getBuffer())
207207
}
208208

209209
@ExperimentalSerializationApi
@@ -212,12 +212,12 @@ class PackedEncoder internal constructor(
212212
if (isNullableCollection) {
213213
collectionBitmapValues.add(true)
214214
} else {
215-
PackedUtils.writeVarLong(1L, dataBuffer)
215+
writeVarLong(1L, dataBuffer)
216216
}
217217
} else if (inStructure) {
218218
error("encodeNull should not be used inside Classes; use bitmask")
219219
} else {
220-
PackedUtils.writeVarLong(1L, output)
220+
writeVarLong(1L, output)
221221
}
222222
}
223223

@@ -227,10 +227,10 @@ class PackedEncoder internal constructor(
227227
if (isNullableCollection) {
228228
collectionBitmapValues.add(false)
229229
} else {
230-
PackedUtils.writeVarLong(0L, dataBuffer)
230+
writeVarLong(0L, dataBuffer)
231231
}
232232
} else if (!inStructure) {
233-
PackedUtils.writeVarLong(0L, output)
233+
writeVarLong(0L, output)
234234
}
235235
}
236236

@@ -296,15 +296,15 @@ class PackedEncoder internal constructor(
296296
config
297297
)
298298
) {
299-
IntPacking.SIGNED -> PackedUtils.writeVarInt(
300-
PackedUtils.zigZagEncodeInt(
299+
IntPacking.SIGNED -> writeVarInt(
300+
zigZagEncodeInt(
301301
value
302302
),
303303
dataBuffer
304304
)
305305

306-
IntPacking.DEFAULT -> PackedUtils.writeVarInt(value, dataBuffer)
307-
IntPacking.FIXED -> PackedUtils.writeInt(value, dataBuffer)
306+
IntPacking.DEFAULT -> writeVarInt(value, dataBuffer)
307+
IntPacking.FIXED -> writeInt(value, dataBuffer)
308308
}
309309
}
310310

@@ -319,15 +319,15 @@ class PackedEncoder internal constructor(
319319
config
320320
)
321321
) {
322-
IntPacking.SIGNED -> PackedUtils.writeVarLong(
323-
PackedUtils.zigZagEncodeLong(
322+
IntPacking.SIGNED -> writeVarLong(
323+
zigZagEncodeLong(
324324
value
325325
),
326326
dataBuffer
327327
)
328328

329-
IntPacking.DEFAULT -> PackedUtils.writeVarLong(value, dataBuffer)
330-
IntPacking.FIXED -> PackedUtils.writeLong(value, dataBuffer)
329+
IntPacking.DEFAULT -> writeVarLong(value, dataBuffer)
330+
IntPacking.FIXED -> writeLong(value, dataBuffer)
331331
}
332332
}
333333

@@ -344,7 +344,7 @@ class PackedEncoder internal constructor(
344344
index: Int,
345345
value: Short
346346
) {
347-
PackedUtils.writeShort(value, dataBuffer)
347+
writeShort(value, dataBuffer)
348348
}
349349

350350
override fun encodeCharElement(
@@ -360,15 +360,15 @@ class PackedEncoder internal constructor(
360360
index: Int,
361361
value: Float
362362
) {
363-
PackedUtils.writeInt(value.toBits(), dataBuffer)
363+
writeInt(value.toBits(), dataBuffer)
364364
}
365365

366366
override fun encodeDoubleElement(
367367
descriptor: SerialDescriptor,
368368
index: Int,
369369
value: Double
370370
) {
371-
PackedUtils.writeLong(value.toBits(), dataBuffer)
371+
writeLong(value.toBits(), dataBuffer)
372372
}
373373

374374
override fun encodeStringElement(
@@ -377,7 +377,7 @@ class PackedEncoder internal constructor(
377377
value: String
378378
) {
379379
val bytes = value.encodeToByteArray()
380-
PackedUtils.writeVarInt(bytes.size, dataBuffer)
380+
writeVarInt(bytes.size, dataBuffer)
381381
dataBuffer.write(bytes)
382382
}
383383

0 commit comments

Comments
 (0)