Generate toString implementation for unsigned types
This commit is contained in:
@@ -69,6 +69,8 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
|||||||
|
|
||||||
generateMemberConversions()
|
generateMemberConversions()
|
||||||
|
|
||||||
|
generateToStringHashCode()
|
||||||
|
|
||||||
out.println("}")
|
out.println("}")
|
||||||
out.println()
|
out.println()
|
||||||
|
|
||||||
@@ -203,6 +205,20 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun generateToStringHashCode() {
|
||||||
|
|
||||||
|
out.print(" public override fun toString(): String = ")
|
||||||
|
when (type) {
|
||||||
|
UnsignedType.UBYTE, UnsignedType.USHORT -> out.println("toInt().toString()")
|
||||||
|
UnsignedType.UINT -> out.println("toLong().toString()")
|
||||||
|
UnsignedType.ULONG -> out.println("ulongToString(data)")
|
||||||
|
else -> error(type)
|
||||||
|
}
|
||||||
|
|
||||||
|
out.println()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun maxByDomainCapacity(type1: UnsignedType, type2: UnsignedType): UnsignedType =
|
private fun maxByDomainCapacity(type1: UnsignedType, type2: UnsignedType): UnsignedType =
|
||||||
if (type1.ordinal > type2.ordinal) type1 else type2
|
if (type1.ordinal > type2.ordinal) type1 else type2
|
||||||
|
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ public inline class UByte internal constructor(private val data: Byte) : Compara
|
|||||||
public fun toUInt(): UInt = data.toUInt()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = data.toULong()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
|
public override fun toString(): String = toInt().toString()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUByte(): UByte = UByte(this)
|
public fun Byte.toUByte(): UByte = UByte(this)
|
||||||
|
|||||||
@@ -127,6 +127,8 @@ public inline class UInt internal constructor(private val data: Int) : Comparabl
|
|||||||
public fun toUInt(): UInt = this
|
public fun toUInt(): UInt = this
|
||||||
public fun toULong(): ULong = data.toULong()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
|
public override fun toString(): String = toLong().toString()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUInt(): UInt = UInt(this.toInt() and 0xFF)
|
public fun Byte.toUInt(): UInt = UInt(this.toInt() and 0xFF)
|
||||||
|
|||||||
@@ -127,6 +127,8 @@ public inline class ULong internal constructor(private val data: Long) : Compara
|
|||||||
public fun toUInt(): UInt = data.toUInt()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = this
|
public fun toULong(): ULong = this
|
||||||
|
|
||||||
|
public override fun toString(): String = ulongToString(data)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toULong(): ULong = ULong(this.toLong() and 0xFF)
|
public fun Byte.toULong(): ULong = ULong(this.toLong() and 0xFF)
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ public inline class UShort internal constructor(private val data: Short) : Compa
|
|||||||
public fun toUInt(): UInt = data.toUInt()
|
public fun toUInt(): UInt = data.toUInt()
|
||||||
public fun toULong(): ULong = data.toULong()
|
public fun toULong(): ULong = data.toULong()
|
||||||
|
|
||||||
|
public override fun toString(): String = toInt().toString()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Byte.toUShort(): UShort = UShort(this.toShort() and 0xFF)
|
public fun Byte.toUShort(): UShort = UShort(this.toShort() and 0xFF)
|
||||||
|
|||||||
@@ -51,4 +51,21 @@ internal fun ulongRemainder(v1: ULong, v2: ULong): ULong {
|
|||||||
val quotient = ((dividend ushr 1) / divisor) shl 1
|
val quotient = ((dividend ushr 1) / divisor) shl 1
|
||||||
val rem = dividend - quotient * divisor
|
val rem = dividend - quotient * divisor
|
||||||
return ULong(rem - if (ULong(rem) >= ULong(divisor)) divisor else 0)
|
return ULong(rem - if (ULong(rem) >= ULong(divisor)) divisor else 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
internal fun ulongToString(v: Long): String = ulongToString(v, 10)
|
||||||
|
|
||||||
|
internal fun ulongToString(v: Long, base: Int): String {
|
||||||
|
require(base == 10) // TODO: toString(base) support in common
|
||||||
|
if (v >= 0) return v.toString(/* base */)
|
||||||
|
|
||||||
|
var quotient = ((v ushr 1) / base) shl 1
|
||||||
|
var rem = v - quotient * base
|
||||||
|
if (rem >= base) {
|
||||||
|
rem -= base
|
||||||
|
quotient += 1
|
||||||
|
}
|
||||||
|
return quotient.toString(/* base */) + rem.toString(/* base */)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user