Skip to content

Commit fbc644f

Browse files
feat(core): implement toString with radix
1 parent 64b5bbe commit fbc644f

File tree

10 files changed

+30
-12
lines changed

10 files changed

+30
-12
lines changed

kbigint/src/androidMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ actual class KBigInt private constructor(private var value: BigInteger) : Compar
105105

106106
actual override fun equals(other: Any?) = other is KBigInt && value == other.value
107107

108-
actual override fun hashCode() = toString().hashCode()
108+
actual override fun hashCode(): Int = toString().hashCode()
109109

110-
actual override fun toString() = value.toString()
110+
actual fun toString(radix: Int): String = value.toString(radix)
111+
112+
actual override fun toString(): String = value.toString()
111113

112114
/**
113115
* Convert the value to an [Int].

kbigint/src/androidUnitTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ actual class KBigIntTest {
114114
@Test
115115
actual fun testToString() {
116116
assertEquals(OVER_MAX_LONG, string.toString())
117+
assertEquals(OVER_MAX_INT.toString(2), long.toString(2))
117118
}
118119

119120
@Test

kbigint/src/commonMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,8 @@ expect class KBigInt : Comparable<KBigInt> {
8989

9090
override fun hashCode(): Int
9191

92+
/** Convert the value to a [String] with the given [radix]. */
93+
fun toString(radix: Int): String
94+
9295
override fun toString(): String
9396
}

kbigint/src/jsMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ actual class KBigInt private constructor(@JsExternalArgument private var value:
149149

150150
actual override fun equals(other: Any?) = other is KBigInt && value == other.value
151151

152-
actual override fun hashCode() = toString().hashCode()
152+
actual override fun hashCode(): Int = toString().hashCode()
153+
154+
@JsName("toRadixString")
155+
actual fun toString(radix: Int) = value.toString(radix)
156+
157+
actual override fun toString() = value.toString()
153158

154159
/**
155160
* Convert the value to an [Int].
@@ -161,7 +166,5 @@ actual class KBigInt private constructor(@JsExternalArgument private var value:
161166
/** Convert the value to a [Double]. */
162167
fun toDouble() = toString().toDouble()
163168

164-
actual override fun toString() = value.toString()
165-
166169
actual fun toByteArray() = KBigIntUtils.toByteArray(value)
167170
}

kbigint/src/jsMain/kotlin/io/github/observeroftime/kbigint/KBigIntUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.observeroftime.kbigint
22

3-
internal external class BigInt
3+
internal external class BigInt {
4+
fun toString(radix: Int): String
5+
}
46

57
internal external fun BigInt(n: dynamic): BigInt
68

kbigint/src/jsTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ actual class KBigIntTest {
117117
@Test
118118
actual fun testToString() {
119119
assertEquals(OVER_MAX_LONG, string.toString())
120+
assertEquals(OVER_MAX_INT.toString(2), long.toString(2))
120121
}
121122

122123
@Test

kbigint/src/jvmMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ actual class KBigInt private constructor(private var value: BigInteger) : Compar
100100

101101
actual override fun equals(other: Any?) = other is KBigInt && value == other.value
102102

103-
actual override fun hashCode() = toString().hashCode()
103+
actual override fun hashCode(): Int = toString().hashCode()
104104

105-
actual override fun toString() = value.toString()
105+
actual fun toString(radix: Int): String = value.toString(radix)
106+
107+
actual override fun toString(): String = value.toString()
106108

107109
/**
108110
* Convert the value to an [Int].

kbigint/src/jvmTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ actual class KBigIntTest {
114114
@Test
115115
actual fun testToString() {
116116
assertEquals(OVER_MAX_LONG, string.toString())
117+
assertEquals(OVER_MAX_INT.toString(2), long.toString(2))
117118
}
118119

119120
@Test

kbigint/src/nativeMain/kotlin/io/github/observeroftime/kbigint/KBigInt.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,21 @@ actual class KBigInt private constructor(private var value: mp_int) : Comparable
375375

376376
actual override fun equals(other: Any?) = other is KBigInt && mp_cmp(value.ptr, other.value.ptr) == 0
377377

378-
actual override fun hashCode() = toString().hashCode()
378+
actual override fun hashCode(): Int = toString().hashCode()
379379

380-
actual override fun toString(): String {
380+
actual fun toString(radix: Int): String {
381381
val arena = Arena()
382382
val size = arena.alloc<IntVar>()
383-
mp_radix_size(value.ptr, 10, size.ptr).check()
383+
mp_radix_size(value.ptr, radix, size.ptr).check()
384384
val result = arena.allocArray<ByteVar>(size.value)
385-
mp_to_radix(value.ptr, result, size.value.toULong(), null, 10).check()
385+
mp_to_radix(value.ptr, result, size.value.toULong(), null, radix).check()
386386
val string = result.toKString()
387387
arena.clear()
388388
return string
389389
}
390390

391+
actual override fun toString() = toString(10)
392+
391393
/** Convert the value to an [Int]. */
392394
fun toInt() = mp_get_i32(value.ptr)
393395

kbigint/src/nativeTest/kotlin/io/github/observeroftime/kbigint/KBigIntTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ actual class KBigIntTest {
135135
@Test
136136
actual fun testToString() {
137137
assertEquals(OVER_MAX_LONG, string.toString())
138+
assertEquals(OVER_MAX_INT.toString(2), long.toString(2))
138139
}
139140

140141
@Test

0 commit comments

Comments
 (0)