Skip to content

Commit 2467c05

Browse files
committed
up
1 parent a455438 commit 2467c05

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

.idea/modules/kotlin-unsigned.iml

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

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
55

66
buildscript {
77

8-
ext.kotlinVersion = '1.2.51'
8+
ext.kotlinVersion = '1.3-M1'
99

1010
repositories {
1111
jcenter() // shadow

src/main/kotlin/unsigned/Ubyte.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import kotlin.experimental.inv
1212
* Created by GBarbieri on 20.03.2017.
1313
*/
1414

15-
data class Ubyte(var v: Byte = 0) : Number() {
15+
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
16+
public inline class Ubyte(val v: Byte) : Number() {
1617

1718
companion object {
1819

1920
/** A constant holding the minimum value an <code>unsigned byte</code> can have, 0. */
2021
const val MIN_VALUE = 0x00
2122
/** A constant holding the maximum value an <code>unsigned byte</code> can have, 2<sup>8</sup>-1. */
22-
const val MAX_VALUE = 0xff
23+
const val MAX_VALUE: Int = 0xff
2324

2425
fun checkSigned(v: Number) = v.toInt() in MIN_VALUE..MAX_VALUE
2526
}

src/main/kotlin/unsigned/java_1_7/int.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ fun compare(a: Int, b: Int) = if (a < b) -1 else if (a == b) 0 else 1
1919
fun String.parseUnsignedInt(radix: Int): Int {
2020

2121
if (length > 0) {
22-
val firstChar = this[0]
23-
if (firstChar == '-')
24-
throw NumberFormatException(String.format("Illegal leading minus sign " + "on unsigned string %s.", this))
25-
else {
26-
if (length <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
27-
radix == 10 && length <= 9) // Integer.MAX_VALUE in base 10 is 10 digits
28-
return this.toInt(radix)
29-
else {
30-
val ell = this.toLong(radix)
31-
if (ell and BigInteger("ffffffff00000000", 16).toLong() == 0L)
32-
return ell.toInt()
33-
else
34-
throw NumberFormatException(String.format("String value %s exceeds " + "range of unsigned int.", this))
22+
return when (this[0]) {
23+
'-' -> throw NumberFormatException(String.format("Illegal leading minus sign " + "on unsigned string %s.", this))
24+
else -> when {
25+
length <= 5 || /* Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits */ radix == 10 && length <= 9 /* Integer.MAX_VALUE in base 10 is 10 digits*/ -> this.toInt(radix)
26+
else -> {
27+
val ell = this.toLong(radix)
28+
when {
29+
ell and BigInteger("ffffffff00000000", 16).toLong() == 0L -> ell.toInt()
30+
else -> throw NumberFormatException(String.format("String value %s exceeds " + "range of unsigned int.", this))
31+
}
32+
}
3533
}
3634
}
3735
} else throw NumberFormatException("For input string: $this")

src/main/kotlin/unsigned/unsigned.kt

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

3+
4+
import kotlin.UByte
5+
36
/**
47
* Created by GBarbieri on 06.10.2016.
58
*/
@@ -18,3 +21,8 @@ fun Char.toUint() = Uint(toInt())
1821
fun Char.toUlong() = Ulong(toLong())
1922
fun Char.toUshort() = Ushort(toShort())
2023

24+
25+
fun main(args: Array<String>) {
26+
27+
val a = Int
28+
}

0 commit comments

Comments
 (0)