Skip to content

Commit 3683a05

Browse files
committed
seeking java 1.7 compatibility
1 parent 0988c69 commit 3683a05

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

src/main/kotlin/unsigned/Long.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unsigned.*
2+
import unsigned.java_1_7.toUnsignedString
23
import java.math.BigInteger
34

45
/**

src/main/kotlin/unsigned/Ubyte.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package unsigned
22

3+
import unsigned.java_1_7.compareUnsigned
4+
import unsigned.java_1_7.divideUnsigned
5+
import unsigned.java_1_7.remainderUnsigned
36
import java.math.BigInteger
47
import kotlin.experimental.and
58
import kotlin.experimental.inv
@@ -21,7 +24,7 @@ data class Ubyte(var v: Byte = 0) : Number() {
2124
}
2225

2326
constructor(number: Number) : this(number.toByte())
24-
@JvmOverloads constructor(string: String, base: Int = 10) : this(Integer.parseUnsignedInt(string.filter { it != '_' && it != '\'' }, base).toShort())
27+
@JvmOverloads constructor(string: String, base: Int = 10) : this(Integer.parseUnsignedInt(string.filter { it != '_' && it != '\'' }, base).toByte())
2528

2629
override fun toByte() = v
2730
override fun toShort() = v.toUInt().toShort()
@@ -57,11 +60,11 @@ data class Ubyte(var v: Byte = 0) : Number() {
5760

5861
infix operator fun div(b: Ubyte) = Ubyte(toInt() / b.toInt())
5962
infix operator fun div(b: Byte) = Ubyte(toInt() / b.toUInt())
60-
infix operator fun div(b: Int) = Ubyte(Integer.divideUnsigned(toInt(), b))
63+
infix operator fun div(b: Int) = Ubyte(toInt().divideUnsigned(b))
6164

6265
infix operator fun rem(b: Ubyte) = Ubyte(toInt() % b.toInt())
6366
infix operator fun rem(b: Byte) = Ubyte(toInt() % b.toUInt())
64-
infix operator fun rem(b: Int) = Ubyte(Integer.remainderUnsigned(toInt(), b))
67+
infix operator fun rem(b: Int) = Ubyte(toInt().remainderUnsigned(b))
6568

6669
// TODO add counterparts with res
6770

@@ -87,7 +90,7 @@ data class Ubyte(var v: Byte = 0) : Number() {
8790

8891
fun inv() = Ubyte(v.inv())
8992

90-
operator fun compareTo(b: Ubyte) = Integer.compareUnsigned(toInt(), b.toInt())
91-
operator fun compareTo(b: Byte) = Integer.compareUnsigned(toInt(), b.toUInt())
92-
operator fun compareTo(b: Int) = Integer.compareUnsigned(toInt(), b)
93+
operator fun compareTo(b: Ubyte) = toInt().compareUnsigned(b.toInt())
94+
operator fun compareTo(b: Byte) = toInt().compareUnsigned(b.toUInt())
95+
operator fun compareTo(b: Int) = toInt().compareUnsigned(b)
9396
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package unsigned.java_1_7
2+
3+
import java.math.BigInteger
4+
5+
6+
// In lieu of tricky code, for now just use long arithmetic.
7+
fun Int.divideUnsigned(divisor: Int) = (toUnsignedLong() / divisor.toUnsignedLong()).toInt()
8+
9+
fun Int.toUnsignedLong() = toLong() and 0xffffffffL
10+
11+
// In lieu of tricky code, for now just use long arithmetic.
12+
fun Int.remainderUnsigned(divisor: Int) = (toUnsignedLong() % divisor.toUnsignedLong()).toInt()
13+
14+
fun Int.compareUnsigned(b: Int) = compare(this + Int.MIN_VALUE, b + Int.MIN_VALUE)
15+
16+
fun compare(x: Int, y: Int) = if (x < y) -1 else if (x == y) 0 else 1
17+
18+
@Throws(NumberFormatException::class)
19+
fun String.parseUnsignedInt(radix: Int): Int {
20+
21+
val len = length
22+
if (len > 0) {
23+
val firstChar = this[0]
24+
if (firstChar == '-')
25+
throw NumberFormatException(String.format("Illegal leading minus sign " + "on unsigned string %s.", this))
26+
else {
27+
if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
28+
radix == 10 && len <= 9) // Integer.MAX_VALUE in base 10 is 10 digits
29+
return this.toInt(radix)
30+
else {
31+
val ell = this.toLong(radix)
32+
if (ell and BigInteger("0xffff_ffff_0000_0000").toLong() == 0L)
33+
return ell.toInt()
34+
else
35+
throw NumberFormatException(String.format("String value %s exceeds " + "range of unsigned int.", this))
36+
}
37+
}
38+
} else throw NumberFormatException("For input string: $this")
39+
}

src/main/kotlin/unsigned/java_1_7.kt renamed to src/main/kotlin/unsigned/java_1_7/long.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package unsigned
1+
package unsigned.java_1_7
22

33
import java.math.BigInteger
44

55

6-
fun Long.toUnsignedString(radix: Int = 10): String =
7-
if (this >= 0)
8-
toString(radix)
9-
else
10-
when (radix) {
6+
fun Long.toUnsignedString(radix: Int = 10): String {
7+
return when {
8+
this >= 0 -> toString(radix)
9+
else -> when (radix) {
1110
2 -> toBinaryString()
1211

1312
4 -> toUnsignedString0(2)
@@ -34,6 +33,8 @@ fun Long.toUnsignedString(radix: Int = 10): String =
3433

3534
else -> toUnsignedBigInteger().toString(radix)
3635
}
36+
}
37+
}
3738

3839
fun Long.toString(radix: Int): String {
3940
var i = this

0 commit comments

Comments
 (0)