Skip to content

Commit b6d18d9

Browse files
committed
add kdoc
1 parent 939f2bb commit b6d18d9

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,58 @@ package com.eignex.kencode
55
import kotlinx.serialization.ExperimentalSerializationApi
66
import kotlinx.serialization.SerialInfo
77

8+
/**
9+
* Marks an `Int` or `Long` property to be encoded using **signed varint** encoding.
10+
*
11+
* Behavior:
12+
* - Values are ZigZag–transformed, so small negative numbers encode compactly.
13+
* - Serialized using a variable-length LEB128-style varint.
14+
* - Applies only to `Int` and `Long` fields inside `PackedFormat` structures.
15+
*
16+
* Decoding:
17+
* - Automatically applies ZigZag decode.
18+
*
19+
* Usage:
20+
* ```
21+
* @Serializable
22+
* data class Example(
23+
* @VarInt val delta: Int, // small negatives become very compact
24+
* @VarInt val offset: Long
25+
* )
26+
* ```
27+
*
28+
* Has no effect for primitive types outside `PackedFormat`.
29+
*/
830
@SerialInfo
931
@Target(AnnotationTarget.PROPERTY)
1032
@Retention(AnnotationRetention.RUNTIME)
1133
annotation class VarInt
1234

35+
/**
36+
* Marks an `Int` or `Long` property to be encoded using **unsigned varint** encoding.
37+
*
38+
* Behavior:
39+
* - No ZigZag transform; values are treated as non-negative.
40+
* - Serialized using a variable-length LEB128-style varint.
41+
* - Applies only to `Int` and `Long` fields inside `PackedFormat` structures.
42+
*
43+
* Recommended for:
44+
* - IDs
45+
* - version counters
46+
* - monotonic sequence numbers
47+
* - any value that is always `>= 0`
48+
*
49+
* Usage:
50+
* ```
51+
* @Serializable
52+
* data class Example(
53+
* @VarUInt val id: Long, // compact for small and medium positive values
54+
* @VarUInt val count: Int
55+
* )
56+
* ```
57+
*
58+
* Has no effect for primitive types outside `PackedFormat`.
59+
*/
1360
@SerialInfo
1461
@Target(AnnotationTarget.PROPERTY)
1562
@Retention(AnnotationRetention.RUNTIME)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ open class BaseRadix(
8787
val bytesCount = blockIndex + 1
8888
ceil((bytesCount * 8) / logBase).toInt()
8989
}.also { arr ->
90-
// Ensure strictly increasing encoded length for each additional input byte,
91-
// to avoid ambiguity when decoding.
90+
// Ensure strictly increasing encoded length for each additional input
91+
// byte, // to avoid ambiguity when decoding.
9292
for (i in 1 until arr.size) {
9393
val prev = arr[i - 1]
9494
while (arr[i] <= prev) {
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
package com.eignex.kencode
22

33
/**
4-
* Abstraction for byte→string and string→byte encoders.
4+
* Abstraction for bidirectional byte–text encodings.
5+
*
6+
* A `ByteEncoding` converts raw binary data into an ASCII-safe string
7+
* representation, and back. Implementations include:
8+
*
9+
* - `Base36` / `Base62` (`BaseRadix`-based)
10+
* - `Base64` / `Base64UrlSafe`
11+
* - `Base85`
12+
*
13+
* Contract:
14+
* - `encode` must produce a deterministic, reversible representation.
15+
* - `decode` must reject invalid input via `IllegalArgumentException`.
16+
* - All implementations are pure and thread-safe.
17+
*
18+
* Usage:
19+
* ```
20+
* val encoded = Base62.encode(bytes)
21+
* val decoded = Base62.decode(encoded)
22+
* ```
523
*/
624
interface ByteEncoding {
725

26+
/**
27+
* Encode the given byte range `[offset, offset + length)` into a text
28+
* representation. Throws [IllegalArgumentException] on invalid offset or
29+
* length.
30+
*/
831
fun encode(
932
input: ByteArray,
1033
offset: Int = 0,
1134
length: Int = input.size - offset
1235
): String
1336

37+
/**
38+
* Decode an encoded string back into the original bytes. Throws
39+
* [IllegalArgumentException] the input contains characters not in the
40+
* encoding alphabet or the input is structurally invalid for the encoding.
41+
*/
1442
fun decode(input: CharSequence): ByteArray
1543
}

0 commit comments

Comments
 (0)