kotlin.text.StringNumberConversions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-stdlib-common Show documentation
Show all versions of kotlin-stdlib-common Show documentation
Kotlin Common Standard Library (legacy, use kotlin-stdlib instead)
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
package kotlin.text
/**
* Parses the string as a signed [Byte] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toByteOrNull(): Byte? = toByteOrNull(radix = 10)
/**
* Parses the string as a signed [Byte] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toByteOrNull(radix: Int): Byte? {
val int = this.toIntOrNull(radix) ?: return null
if (int < Byte.MIN_VALUE || int > Byte.MAX_VALUE) return null
return int.toByte()
}
/**
* Parses the string as a [Short] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toShortOrNull(): Short? = toShortOrNull(radix = 10)
/**
* Parses the string as a [Short] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toShortOrNull(radix: Int): Short? {
val int = this.toIntOrNull(radix) ?: return null
if (int < Short.MIN_VALUE || int > Short.MAX_VALUE) return null
return int.toShort()
}
/**
* Parses the string as an [Int] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toIntOrNull(): Int? = toIntOrNull(radix = 10)
/**
* Parses the string as an [Int] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toIntOrNull(radix: Int): Int? {
checkRadix(radix)
val length = this.length
if (length == 0) return null
val start: Int
val isNegative: Boolean
val limit: Int
val firstChar = this[0]
if (firstChar < '0') { // Possible leading sign
if (length == 1) return null // non-digit (possible sign) only, no digits after
start = 1
if (firstChar == '-') {
isNegative = true
limit = Int.MIN_VALUE
} else if (firstChar == '+') {
isNegative = false
limit = -Int.MAX_VALUE
} else
return null
} else {
start = 0
isNegative = false
limit = -Int.MAX_VALUE
}
val limitForMaxRadix = (-Int.MAX_VALUE) / 36
var limitBeforeMul = limitForMaxRadix
var result = 0
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null
if (result < limitBeforeMul) {
if (limitBeforeMul == limitForMaxRadix) {
limitBeforeMul = limit / radix
if (result < limitBeforeMul) {
return null
}
} else {
return null
}
}
result *= radix
if (result < limit + digit) return null
result -= digit
}
return if (isNegative) result else -result
}
/**
* Parses the string as a [Long] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.1")
public fun String.toLongOrNull(): Long? = toLongOrNull(radix = 10)
/**
* Parses the string as a [Long] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.1")
public fun String.toLongOrNull(radix: Int): Long? {
checkRadix(radix)
val length = this.length
if (length == 0) return null
val start: Int
val isNegative: Boolean
val limit: Long
val firstChar = this[0]
if (firstChar < '0') { // Possible leading sign
if (length == 1) return null // non-digit (possible sign) only, no digits after
start = 1
if (firstChar == '-') {
isNegative = true
limit = Long.MIN_VALUE
} else if (firstChar == '+') {
isNegative = false
limit = -Long.MAX_VALUE
} else
return null
} else {
start = 0
isNegative = false
limit = -Long.MAX_VALUE
}
val limitForMaxRadix = (-Long.MAX_VALUE) / 36
var limitBeforeMul = limitForMaxRadix
var result = 0L
for (i in start until length) {
val digit = digitOf(this[i], radix)
if (digit < 0) return null
if (result < limitBeforeMul) {
if (limitBeforeMul == limitForMaxRadix) {
limitBeforeMul = limit / radix
if (result < limitBeforeMul) {
return null
}
} else {
return null
}
}
result *= radix
if (result < limit + digit) return null
result -= digit
}
return if (isNegative) result else -result
}
internal fun numberFormatError(input: String): Nothing = throw NumberFormatException("Invalid number format: '$input'")