Skip to content

Commit 8816ce5

Browse files
committed
Merge branch 'm13'
# Conflicts: # .idea/modules/kotlin-unsigned.iml # build.gradle # src/main/kotlin/unsigned/unsigned.kt
2 parents 717ccde + 74618a6 commit 8816ce5

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

src/main/kotlin/unsigned/Ubyte.kt

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

15-
data class Ubyte(var v: Byte = 0) : Number() {
15+
public inline class Ubyte(val v: Byte) : Number() {
1616

1717
companion object {
1818

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

2424
fun checkSigned(v: Number) = v.toInt() in MIN_VALUE..MAX_VALUE
2525
}

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")

0 commit comments

Comments
 (0)