Skip to content

Commit 335438a

Browse files
committed
v3.2.1
- kotlin 1.3.31 - kotlintest 3.3.2 - gradle 5.4.1 - removed java 1.7 unsigned support (using jdk 1.8) - added corresponding MIN/MAX values for Utypes with the same type - added Utype hashCode(), toString(radix), toString(format)
1 parent 0e3cc3e commit 335438a

File tree

13 files changed

+157
-384
lines changed

13 files changed

+157
-384
lines changed

.idea/modules/kotlin-unsigned.iml

Lines changed: 28 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
plugins {
22
id 'maven'
3-
id "org.jetbrains.kotlin.jvm" version "1.3.21"
3+
id "org.jetbrains.kotlin.jvm" version "1.3.31"
44
}
55

66
ext{
77
moduleName = 'com.github.kotlin_graphics.kotlin_unsigned'
88
kotlin = 'org.jetbrains.kotlin:kotlin'
9-
kotlin_version = '1.3.21'
10-
kotlintest_version = '3.2.1'
9+
kotlin_version = '1.3.31'
10+
kotlintest_version = '3.3.2'
1111
}
1212

1313
dependencies {

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

src/main/kotlin/unsigned/Int.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,21 @@ infix fun Int.shl(b: Ulong) = this shl b.toInt()
102102
infix fun Int.udiv(b: Ulong) = (toULong() / b.toLong()).toInt()
103103
infix fun Int.urem(b: Ulong) = (toULong() % b.toLong()).toInt()
104104
infix fun Int.ucmp(b: Ulong) = toULong().compareTo(b.toLong())
105-
// no Int ushr Ulong
105+
// no Int ushr Ulong
106+
107+
108+
// binds jdk unsigned operation
109+
110+
infix fun Int.toUnsignedString(radix: Int): String = Integer.toUnsignedString(this, radix)
111+
/** no-radix version has a different codepath */
112+
val Int.toUnsignedString: String
113+
get() = Integer.toUnsignedString(this)
114+
115+
fun String.parseUnsignedInt(radix: Int = 10): Int = Integer.parseUnsignedInt(this, radix)
116+
117+
infix fun Int.compareUnsigned(b: Int): Int = Integer.compareUnsigned(this, b)
118+
119+
fun Int.toUnsignedLong(): Long = toLong() and 0xffffffffL
120+
121+
infix fun Int.divideUnsigned(divisor: Int) = Integer.divideUnsigned(this, divisor)
122+
infix fun Int.remainderUnsigned(divisor: Int) = Integer.remainderUnsigned(this, divisor)

src/main/kotlin/unsigned/Long.kt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package unsigned
22

33

4-
import unsigned.*
5-
import unsigned.java_1_7.toUnsignedString
64
import java.math.BigInteger
75

6+
87
// Created by GBarbieri on 31.01.2017.
98

109
fun Long.toUByte() = toByte()
@@ -17,7 +16,7 @@ fun Long.toUint() = Uint(this)
1716
fun Long.toUlong() = Ulong(this)
1817

1918
fun Long.toBigInt(): BigInteger = BigInteger.valueOf(this)
20-
fun Long.toUBigInt(): BigInteger = BigInteger(toUnsignedString())
19+
fun Long.toUBigInt(): BigInteger = BigInteger(toUnsignedString)
2120

2221
val Long.ub
2322
get() = toUbyte()
@@ -102,4 +101,32 @@ infix fun Long.shl(b: Ulong) = this shl b.toInt()
102101
infix fun Long.udiv(b: Ulong) = (toUBigInt() / b.toBigInt()).toLong()
103102
infix fun Long.urem(b: Ulong) = (toUBigInt() % b.toBigInt()).toLong()
104103
infix fun Long.ucmp(b: Ulong) = toUBigInt().compareTo(b.toBigInt())
105-
// no Int ushr Ulong
104+
// no Int ushr Ulong
105+
106+
107+
infix fun Long.toUnsignedString(radix: Int): String = java.lang.Long.toUnsignedString(this, radix)
108+
109+
fun Long.toUnsignedBigInteger(): BigInteger = when {
110+
this >= 0L -> BigInteger.valueOf(this)
111+
else -> {
112+
val upper = ushr(32).toInt()
113+
val lower = toInt()
114+
115+
// return (upper << 32) + lower
116+
BigInteger.valueOf(upper.toLong() and 0xffffffffL).shiftLeft(32) + BigInteger.valueOf(lower.toLong() and 0xffffffffL)
117+
}
118+
}
119+
120+
/** no-radix version has a different codepath */
121+
val Long.toUnsignedString: String
122+
get() = java.lang.Long.toUnsignedString(this)
123+
124+
fun String.parseUnsignedLong(radix: Int = 10): Long = java.lang.Long.parseUnsignedLong(this, radix)
125+
126+
infix fun Long.compareUnsigned(b: Long): Int = java.lang.Long.compareUnsigned(this, b)
127+
128+
infix fun Long.divideUnsigned(divisor: Long) = java.lang.Long.divideUnsigned(this, divisor)
129+
infix fun Long.remainderUnsigned(divisor: Long) = java.lang.Long.remainderUnsigned(this, divisor)
130+
131+
132+

src/main/kotlin/unsigned/Ubyte.kt

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

3-
import unsigned.java_1_7.compareUnsigned
4-
import unsigned.java_1_7.divideUnsigned
5-
import unsigned.java_1_7.parseUnsignedInt
6-
import unsigned.java_1_7.remainderUnsigned
73
import java.math.BigInteger
84
import kotlin.experimental.and
95
import kotlin.experimental.inv
@@ -12,15 +8,20 @@ import kotlin.experimental.inv
128
* Created by GBarbieri on 20.03.2017.
139
*/
1410

15-
class Ubyte(var v: Byte) : Number() {
11+
class Ubyte(var v: Byte) : Number(), Comparable<Ubyte> {
1612

1713
companion object {
1814

1915
/** A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
20-
const val MIN_VALUE = 0x00
16+
const val MIN_VALUE: Int = 0
2117
/** A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
2218
const val MAX_VALUE: Int = 0xff
2319

20+
/** A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
21+
val MIN = Ubyte(0)
22+
/** A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
23+
val MAX = Ubyte(0xff)
24+
2425
fun checkSigned(v: Number) = v.toInt() in MIN_VALUE..MAX_VALUE
2526
}
2627

@@ -61,11 +62,11 @@ class Ubyte(var v: Byte) : Number() {
6162

6263
infix operator fun div(b: Ubyte) = Ubyte(toInt() / b.toInt())
6364
infix operator fun div(b: Byte) = Ubyte(toInt() / b.toUInt())
64-
infix operator fun div(b: Int) = Ubyte(toInt() divideUnsigned b)
65+
infix operator fun div(b: Int) = Ubyte(toInt() udiv b)
6566

6667
infix operator fun rem(b: Ubyte) = Ubyte(toInt() % b.toInt())
6768
infix operator fun rem(b: Byte) = Ubyte(toInt() % b.toUInt())
68-
infix operator fun rem(b: Int) = Ubyte(toInt() remainderUnsigned b)
69+
infix operator fun rem(b: Int) = Ubyte(toInt() urem b)
6970

7071
// TODO add counterparts with res
7172

@@ -91,11 +92,13 @@ class Ubyte(var v: Byte) : Number() {
9192

9293
fun inv() = Ubyte(v.inv())
9394

94-
operator fun compareTo(b: Ubyte) = toInt() compareUnsigned b.toInt()
95-
operator fun compareTo(b: Byte) = toInt() compareUnsigned b.toUInt()
96-
operator fun compareTo(b: Int) = toInt() compareUnsigned b
97-
98-
override fun toString() = toInt().toString()
95+
override operator fun compareTo(other: Ubyte) = toInt() compareUnsigned other.toInt()
96+
operator fun compareTo(other: Byte) = toInt() compareUnsigned other.toUInt()
97+
operator fun compareTo(other: Int) = toInt() compareUnsigned other
9998

10099
override fun equals(other: Any?) = other is Ubyte && v == other.v
100+
override fun hashCode(): Int = v.hashCode()
101+
override fun toString() = toInt().toString()
102+
fun toString(radix: Int) = toInt().toString(radix)
103+
fun toString(format: String) = format.format(toInt())
101104
}

src/main/kotlin/unsigned/Uint.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package unsigned
22

3-
import unsigned.java_1_7.compareUnsigned
4-
import unsigned.java_1_7.divideUnsigned
5-
import unsigned.java_1_7.parseUnsignedInt
6-
import unsigned.java_1_7.remainderUnsigned
73
import java.math.BigInteger
84

95
/**
106
* Created by GBarbieri on 20.03.2017.
117
*/
128

13-
data class Uint(var v: Int = 0) : Number() {
9+
data class Uint(var v: Int = 0) : Number(), Comparable<Uint> {
1410

1511
companion object {
1612

1713
/** A constant holding the minimum value an <code>unsigned int</code> can have, 0. */
18-
const val MIN_VALUE = 0x00000000
14+
const val MIN_VALUE: Long = 0L
1915

2016
/** A constant holding the maximum value an <code>unsigned int</code> can have, 2<sup>32</sup>-1. */
21-
const val MAX_VALUE = 0xffffffffL
17+
const val MAX_VALUE: Long = 0xffffffffL
18+
19+
/** A constant holding the minimum value an <code>unsigned int</code> can have, 0. */
20+
val MIN = Uint(0)
21+
22+
/** A constant holding the maximum value an <code>unsigned int</code> can have, 2<sup>32</sup>-1. */
23+
val MAX = Uint(-1)
2224

2325
fun checkSigned(v: Number) = v.toLong() in MIN_VALUE..MAX_VALUE
2426
}
@@ -51,11 +53,11 @@ data class Uint(var v: Int = 0) : Number() {
5153
infix operator fun times(b: Uint) = Uint(v * b.v)
5254
infix operator fun times(b: Int) = Uint(v * b)
5355

54-
infix operator fun div(b: Uint) = Uint(v divideUnsigned b.toInt())
55-
infix operator fun div(b: Int) = Uint(v divideUnsigned b)
56+
infix operator fun div(b: Uint) = Uint(v udiv b.toInt())
57+
infix operator fun div(b: Int) = Uint(v udiv b)
5658

57-
infix operator fun rem(b: Uint) = Uint(v remainderUnsigned b.toInt())
58-
infix operator fun rem(b: Int) = Uint(v remainderUnsigned b)
59+
infix operator fun rem(b: Uint) = Uint(v urem b.toInt())
60+
infix operator fun rem(b: Int) = Uint(v urem b)
5961

6062
infix fun and(b: Uint) = Uint(v and b.toInt())
6163
infix fun and(b: Int) = Uint(v and b)
@@ -74,11 +76,14 @@ data class Uint(var v: Int = 0) : Number() {
7476

7577
fun inv() = Uint(v.inv())
7678

77-
operator fun compareTo(b: Uint) = v compareUnsigned b.toInt()
78-
operator fun compareTo(b: Int) = v compareUnsigned b
79+
override operator fun compareTo(other: Uint) = v compareUnsigned other.toInt()
80+
operator fun compareTo(other: Int) = v compareUnsigned other
7981

82+
override fun equals(other: Any?) = other is Uint && v == other.v
83+
override fun hashCode(): Int = v.hashCode()
8084
override fun toString() = toLong().toString()
85+
fun toString(radix: Int) = toLong().toString(radix)
86+
fun toString(format: String) = format.format(toLong())
8187

82-
override fun equals(other: Any?) = other is Uint && v == other.v
8388
// TODO long?
8489
}

src/main/kotlin/unsigned/Ulong.kt

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

3-
import unsigned.java_1_7.*
43
import java.math.BigInteger
54

65
/**
@@ -17,6 +16,12 @@ data class Ulong(var v: Long = 0) : Number(), Comparable<Ulong> {
1716
/** A constant holding the maximum value an <code>unsigned long</code> can have, 2<sup>64</sup>-1. */
1817
val MAX_VALUE = BigInteger("18446744073709551615")
1918

19+
/** A constant holding the minimum value an <code>unsigned long</code> can have, 0. */
20+
val MIN = Ulong(0L)
21+
22+
/** A constant holding the maximum value an <code>unsigned long</code> can have, 2<sup>64</sup>-1. */
23+
val MAX = Ulong(-1L)
24+
2025
fun checkSigned(v: Number) = BigInteger.valueOf(v.toLong()) in MIN_VALUE..MAX_VALUE
2126
}
2227

@@ -28,7 +33,7 @@ data class Ulong(var v: Long = 0) : Number(), Comparable<Ulong> {
2833
override fun toInt() = v.toInt()
2934
override fun toLong() = v
3035

31-
fun toBigInt(): BigInteger = BigInteger(v.toUnsignedString())
36+
fun toBigInt(): BigInteger = BigInteger(v.toUnsignedString)
3237

3338
override fun toDouble() = toBigInt().toDouble()
3439
override fun toFloat() = toBigInt().toFloat()
@@ -73,8 +78,11 @@ data class Ulong(var v: Long = 0) : Number(), Comparable<Ulong> {
7378

7479
// TODO others
7580

76-
override fun toString() = toBigInt().toString()
7781
override fun equals(other: Any?) = other is Ulong && v == other.v
82+
override fun hashCode(): Int = v.hashCode()
83+
override fun toString() = toBigInt().toString()
84+
fun toString(radix: Int) = toBigInt().toString(radix)
85+
fun toString(format: String) = format.format(toBigInt())
7886

7987
operator fun rangeTo(b: Ulong) = UlongRange(this, b)
8088

0 commit comments

Comments
 (0)