diff --git a/src/commonMain/kotlin/net.sergeych.bintools/DataSink.kt b/src/commonMain/kotlin/net.sergeych.bintools/DataSink.kt index 4816af2..4728c2d 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/DataSink.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/DataSink.kt @@ -23,10 +23,10 @@ interface DataSink { for(d in data) writeByte(d) } - fun writeVarUInt(value: UInt) { Varint.encodeUnsigned(value.toULong())} - fun writeVarInt(value: UInt) { Varint.encodeSigned(value.toLong())} - fun writeSmartUInt(value: UInt) { Smartint.encodeUnsigned(value.toULong())} - fun writeSmartInt(value: UInt) { Smartint.encodeSigned(value.toLong())} + fun writeVarUInt(value: UInt) { Varint.encodeUnsigned(value.toULong(), this)} + fun writeVarInt(value: UInt) { Varint.encodeSigned(value.toLong(), this)} + fun writeSmartUInt(value: UInt) { Smartint.encodeUnsigned(value.toULong(), this)} + fun writeSmartInt(value: UInt) { Smartint.encodeSigned(value.toLong(), this)} } inline fun DataSink.writeNumber(value: T) { diff --git a/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt b/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt index 29db24f..0dd5b09 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt @@ -112,6 +112,7 @@ fun ByteArray.toDumpLines(wide: Boolean = false): List { } val remainder = from % lineSize if (remainder > 0) { + if( remainder < lineSize/2) b.append(' ') var cnt = lineSize - remainder while (cnt-- > 0) b.append(' ') } diff --git a/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt b/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt index fddc8db..d613d80 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/BipackDecoder.kt @@ -60,7 +60,7 @@ class BipackDecoder(val input: DataSource, var elementsCount: Int = 0,val isColl var count = descriptor.elementsCount for (a in descriptor.annotations) { if (a is Extendable) - count = source.readSmartUInt().toInt() + count = source.readVarUInt().toInt() else if (a is Framed) { val code = CRC.crc32(descriptor.serialName.encodeToByteArray()) // if we fail to read CRC, it is IO error, so DataSource.EndOfData will be diff --git a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt index 4fd33b2..120e833 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/BipackEncoder.kt @@ -73,7 +73,7 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() { CRC.crc32(descriptor.serialName.encodeToByteArray()) ) } else if (a is Extendable) { - sink.writeSmartUInt(descriptor.elementsCount.toUInt()) + sink.writeVarUInt(descriptor.elementsCount.toUInt()) } } return BipackEncoder(sink) diff --git a/src/commonTest/kotlin/bipack/BipackEncoderTest.kt b/src/commonTest/kotlin/bipack/BipackEncoderTest.kt index 0ac2da3..e6cfe37 100644 --- a/src/commonTest/kotlin/bipack/BipackEncoderTest.kt +++ b/src/commonTest/kotlin/bipack/BipackEncoderTest.kt @@ -64,14 +64,12 @@ class BipackEncoderTest { @Test fun encodeExtendable() { val a = Foobar1(1, 2)//, "bum") - println(BipackEncoder.encode(a).toDump()) val b = BipackDecoder.decode(BipackEncoder.encode(a)) assertEquals(3, BipackEncoder.encode(a).size) assertEquals(a, b) - println(b) val c = BipackDecoder.decode(BipackEncoder.encode(a)) -// assertEquals(-1, c.other) -// assertEquals(a.bar, c.bar) + assertEquals(-1, c.other) + assertEquals(a.bar, c.bar) } @Test @@ -220,7 +218,7 @@ class BipackEncoderTest { // @Test @Test fun testMaps() { - val t1 = mapOf("foo" to "f1", "bar" to "b1", "bazz" to "b3")//f=17f/7f) + val t1 = mapOf("foo bar" to "f1", "bar" to "b1", "bazz" to "b3")//f=17f/7f) val d = BipackEncoder.encode(t1) println(d.toDump()) println(t1) @@ -229,6 +227,33 @@ class BipackEncoderTest { // assertEquals(t1, d.decodeFromBipack()) } + @Test + fun testMaps2() { +// val t1 = mapOf("foo" to Foobar1N(7)) + val t1 = mapOf("foo" to Foobar1N(7), "bar" to Foobar1N(14), "bazz" to Foobar1N(77))//f=17f/7f) + val d = BipackEncoder.encode(t1) + println(d.toDump()) + println(t1) + println(BipackDecoder.decode>(d)) +// println(d.decodeFromBipack>()) +// assertEquals(t1, d.decodeFromBipack()) + } + + @Test + fun testMaps3() { + println(BipackEncoder.encode(Foobar1(1)).toDump()) + println(BipackEncoder.encode(Foobar1(1)).decodeFromBipack()) +// val t1 = mapOf("foo" to Foobar1N(7)) + val t1 = mapOf("foo" to Foobar1(7), "bar" to Foobar1(14), "bazz" to Foobar1(77))//f=17f/7f) + val d = BipackEncoder.encode(t1) + println(d.toDump()) + println(t1) + println(BipackDecoder.decode>(d)) +// println(d.decodeFromBipack>()) +// assertEquals(t1, d.decodeFromBipack()) + } + + @Serializable // @CrcProtected // @Framed