Skip to content

Commit a08930c

Browse files
author
Sachin Shah
committed
Override toString() to return unsigned value
1 parent d399d35 commit a08930c

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

src/main/kotlin/unsigned/Ubyte.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ data class Ubyte(var v: Byte = 0) : Number() {
9494
operator fun compareTo(b: Ubyte) = toInt() compareUnsigned b.toInt()
9595
operator fun compareTo(b: Byte) = toInt() compareUnsigned b.toUInt()
9696
operator fun compareTo(b: Int) = toInt() compareUnsigned b
97+
98+
override fun toString() = toInt().toString()
9799
}

src/main/kotlin/unsigned/Uint.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,7 @@ data class Uint(var v: Int = 0) : Number() {
7777
operator fun compareTo(b: Uint) = v compareUnsigned b.toInt()
7878
operator fun compareTo(b: Int) = v compareUnsigned b
7979

80+
override fun toString() = toLong().toString()
81+
8082
// TODO long?
8183
}

src/main/kotlin/unsigned/Ulong.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ data class Ulong(var v: Long = 0) : Number(), Comparable<Ulong> {
7373

7474
// TODO others
7575

76+
override fun toString() = toBigInt().toString()
77+
7678
operator fun rangeTo(b: Ulong) = UlongRange(this, b)
7779

7880
class UlongRange(override val start: Ulong, override val endInclusive: Ulong) : ClosedRange<Ulong>, Iterable<Ulong> {

src/main/kotlin/unsigned/Ushort.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ data class Ushort(var v: Short = 0) : Number() {
9191
operator fun compareTo(b: Ushort) = toInt() compareUnsigned b.toInt()
9292
operator fun compareTo(b: Short) = toInt() compareUnsigned b.toUInt()
9393
operator fun compareTo(b: Int) = toInt() compareUnsigned b
94+
95+
override fun toString() = toInt().toString()
9496
}

src/test/kotlin/unsigned/test.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,5 +567,16 @@ class Unsigned : StringSpec() {
567567
((65_500 ucmp Ushort(65_499)) > 0) shouldBe true
568568
(65_500 ucmp Ushort(65_500)) shouldBe 0
569569
}
570+
571+
"string format" {
572+
Ubyte(0xff).v.toString() shouldBe "-1"
573+
Ubyte(0xff).toString() shouldBe "255"
574+
Ushort(0xffff).v.toString() shouldBe "-1"
575+
Ushort(0xffff).toString() shouldBe "65535"
576+
Uint(0xffff_ffff).v.toString() shouldBe "-1"
577+
Uint(0xffff_ffff).toString() shouldBe "4294967295"
578+
Ulong(Ulong.MAX_VALUE).v.toString() shouldBe "-1"
579+
Ulong(Ulong.MAX_VALUE).toString() shouldBe "18446744073709551615"
580+
}
570581
}
571582
}

0 commit comments

Comments
 (0)