Skip to content

Commit 6061ccf

Browse files
committed
style: applied formatting
1 parent 2a1e668 commit 6061ccf

10 files changed

Lines changed: 715 additions & 148 deletions

File tree

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ KEncode provides three standalone entry points:
2727

2828
1. **ByteEncoding** text codecs: Base62, Base36, Base64, and Base85 encoders
2929
for raw binary data.
30-
2. **PackedFormat**: A binary format serializer that supports nested objects,
31-
lists, and maps. It uses bitsets for booleans and nullability to minimize
30+
2. **PackedFormat**: A binary format serializer that supports nested objects,
31+
lists, and maps. It uses bitsets for booleans and nullability to minimize
3232
overhead.
3333
3. **EncodedFormat**: A string format serializer that wraps the above to produce
3434
small deterministic string identifiers.
@@ -41,7 +41,7 @@ dependencies {
4141
}
4242
```
4343

44-
For PackedFormat and EncodedFormat you also need to load the
44+
For PackedFormat and EncodedFormat you also need to load the
4545
`kotlinx.serialization` plugin and core library.
4646

4747
### Full serialization example
@@ -89,16 +89,16 @@ payloads for Kotlin classes by moving structural metadata into a compact header.
8989
* VarInts: Int/Long fields can be optimized using @VarUInt or @VarInt (ZigZag)
9090
annotations.
9191
* Full Graph Support: Handles nested objects, lists, maps, and polymorphism
92-
recursively. While this is supported it will not produce as compact
93-
representations as flat structures that can pack all metadata into the same
92+
recursively. While this is supported it will not produce as compact
93+
representations as flat structures that can pack all metadata into the same
9494
header.
9595

9696
### Field layout
9797

9898
For a standard class, the encoding follows this structure:
9999

100100
1. Bitmask Header: A variable length bitset containing bits for all booleans and
101-
nullable indicators. A class with 10 booleans and 5 nullable fields uses 2
101+
nullable indicators. A class with 10 booleans and 5 nullable fields uses 2
102102
bytes for the header (the boolean variables are inlined to the header).
103103
2. Payload bytes: Fields are encoded in declaration order:
104104
* Primitives: Encoded densely (VarInt for Int/Long, fixed for others).
@@ -120,8 +120,8 @@ composing three layers:
120120

121121
```kotlin
122122
val format = EncodedFormat(
123-
binaryFormat = ProtoBuf,
124-
checksum = Crc16,
123+
binaryFormat = ProtoBuf,
124+
checksum = Crc16,
125125
codec = Base36
126126
)
127127

@@ -160,5 +160,6 @@ val encrypted = cipher.doFinal(binary)
160160
// 3. Encode to Text
161161
val token = Base62.encode(encrypted)
162162
```
163+
163164
See [Examples](https://github.com/Eignex/kencode/blob/main/src/test/kotlin/com/eignex/kencode/Examples.kt)
164165
for a BouncyCastle demo.

src/main/kotlin/com/eignex/kencode/EncodedFormat.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ open class EncodedFormat(
6262
override fun <T> encodeToString(
6363
serializer: SerializationStrategy<T>, value: T
6464
): String {
65-
val bytes = configuration.binaryFormat.encodeToByteArray(serializer, value)
65+
val bytes =
66+
configuration.binaryFormat.encodeToByteArray(serializer, value)
6667
val checked = if (configuration.checksum != null) {
6768
bytes + configuration.checksum.digest(bytes)
6869
} else bytes
@@ -81,7 +82,8 @@ open class EncodedFormat(
8182
val input = configuration.codec.decode(string)
8283
val bytes = if (configuration.checksum != null) {
8384
require(input.size >= configuration.checksum.size)
84-
val bytes = input.sliceArray(0..<input.size - configuration.checksum.size)
85+
val bytes =
86+
input.sliceArray(0..<input.size - configuration.checksum.size)
8587
val actual =
8688
input.sliceArray(input.size - configuration.checksum.size..<input.size)
8789
val expected = configuration.checksum.digest(bytes)
@@ -90,7 +92,10 @@ open class EncodedFormat(
9092
}
9193
bytes
9294
} else input
93-
return configuration.binaryFormat.decodeFromByteArray(deserializer, bytes)
95+
return configuration.binaryFormat.decodeFromByteArray(
96+
deserializer,
97+
bytes
98+
)
9499
}
95100
}
96101

@@ -138,7 +143,11 @@ fun EncodedFormat(
138143
}
139144
builder.builderAction()
140145

141-
val newConfig = EncodedConfiguration(builder.codec, builder.checksum, builder.binaryFormat)
146+
val newConfig = EncodedConfiguration(
147+
builder.codec,
148+
builder.checksum,
149+
builder.binaryFormat
150+
)
142151

143152
return EncodedFormat(newConfig)
144153
}

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

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)