Extracted string-parsing extensions and their tests into separate files.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:kotlin.jvm.JvmName("StringsKt")
|
||||
@file:kotlin.jvm.JvmVersion
|
||||
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
|
||||
|
||||
package kotlin.text
|
||||
|
||||
|
||||
/**
|
||||
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a signed [Byte] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Short] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toShort(): Short = java.lang.Short.parseShort(this)
|
||||
|
||||
/**
|
||||
* Parses the string as an [Int] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Long] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toLong(): Long = java.lang.Long.parseLong(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Float] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toFloat(): Float = java.lang.Float.parseFloat(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Double] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toDouble(): Double = java.lang.Double.parseDouble(this)
|
||||
|
||||
@@ -5,10 +5,9 @@
|
||||
|
||||
package kotlin.text
|
||||
|
||||
import java.io.StringReader
|
||||
import java.util.regex.Pattern
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
|
||||
/**
|
||||
@@ -366,62 +365,12 @@ public inline fun String.toLowerCase(locale: java.util.Locale): String = (this a
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toUpperCase(locale: java.util.Locale): String = (this as java.lang.String).toUpperCase(locale)
|
||||
|
||||
/**
|
||||
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a signed [Byte] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toByte(): Byte = java.lang.Byte.parseByte(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Short] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toShort(): Short = java.lang.Short.parseShort(this)
|
||||
|
||||
/**
|
||||
* Parses the string as an [Int] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Long] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toLong(): Long = java.lang.Long.parseLong(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Float] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toFloat(): Float = java.lang.Float.parseFloat(this)
|
||||
|
||||
/**
|
||||
* Parses the string as a [Double] number and returns the result.
|
||||
* @throws NumberFormatException if the string is not a valid representation of a number.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toDouble(): Double = java.lang.Double.parseDouble(this)
|
||||
|
||||
/**
|
||||
* Encodes the contents of this string using the specified character set and returns the resulting byte array.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray = (this as java.lang.String).getBytes(charset)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converts the string into a regular expression [Pattern] optionally
|
||||
* with the specified [flags] from [Pattern] or'd together
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
@file:kotlin.jvm.JvmVersion
|
||||
package test.text
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import org.junit.Test as test
|
||||
|
||||
class ParsePrimitivesJVMTest {
|
||||
|
||||
@test fun toBoolean() {
|
||||
assertEquals(true, "true".toBoolean())
|
||||
assertEquals(true, "True".toBoolean())
|
||||
assertEquals(false, "false".toBoolean())
|
||||
assertEquals(false, "not so true".toBoolean())
|
||||
}
|
||||
|
||||
@test fun toByte() {
|
||||
assertEquals(77.toByte(), "77".toByte())
|
||||
assertFails { "255".toByte() }
|
||||
}
|
||||
|
||||
@test fun toShort() {
|
||||
assertEquals(77.toShort(), "77".toShort())
|
||||
}
|
||||
|
||||
@test fun toInt() {
|
||||
assertEquals(77, "77".toInt())
|
||||
}
|
||||
|
||||
@test fun toLong() {
|
||||
assertEquals(77.toLong(), "77".toLong())
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
package test.text
|
||||
|
||||
import test.collections.assertArrayNotSameButEquals
|
||||
import java.util.Locale
|
||||
import java.util.*
|
||||
import kotlin.test.*
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
Reference in New Issue
Block a user