Repository: JetBrains/kotlin
File: kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/FloatingPointParser.kt (Kotlin)

Code before:
        // Check if the value could be a hexadecimal representation and parse it properly.
        if (parseAsHex(s)) {
            return hexParserImpl(s)
        }

        val info = initialParse(s)

        // Two kinds of situation will directly return 0.0/0.0f:
        // 1. info.s is 0;
        // 2. actual exponent is less than double or float minimum exponent .
        if ("0" == info.s || info.e + info.s.length - 1 < minExp) {
            return if (info.negative) unaryMinus(zero) else zero
        }
        // If actual exponent is larger than maximum exponent then will return infinity.
        // To prevent overflow checking twice.
        if (info.e > maxExp || info.e + info.s.length - 1 > maxExp) {
            return if (info.negative) negativeInf else positiveInf
        }

        val result = parserImpl(info.s, info.e)
        return if (info.negative) unaryMinus(result) else result
    }

    /**
     * To unify the logic of Double and Float parsing we need a common method to calculate a negative value.
     * Unfortunately in this case we need to make uncheck cast to generic type T to have a proper return type.
     */
    @Suppress("UNCHECKED_CAST")
    private fun <T> unaryMinus(value: T): T {
        return when (value) {
            is Float -> -value as T
            is Double -> -value as T
            else -> throw NumberFormatException()
        }
    }

    /**
     * Takes a String and does some initial parsing. Should return a
     * StringExponentPair containing a String with no leading or trailing white
     * space and trailing zeroes eliminated. The exponent of the
     * StringExponentPair will be used to calculate the floating point number by
     * taking the positive integer the String represents and multiplying by 10
     * raised to the power of the exponent.
     *
     * This method is not optimal, can cause performance issues. This logic can be done in one single run.
     * It also violates Kotlin coding practices and should be refactored in the future after stabilizing the logic.
     *
     * @param string the String that will be parsed to a floating point
     * @return a StringExponentPair with necessary values
     * @exception NumberFormatException if the String doesn't pass basic tests
     */

Review comment (performance):
What if make the method `inline` with `reified T`? It will also allow us to get rid of boxing. I don't think that boxing has significant performance impact comparing to the other parsing machinery, but still.

Code after:
        // Check if the value could be a hexadecimal representation and parse it properly.
        if (parseAsHex(s)) {
            return hexParserImpl(s)
        }

        val info = initialParse(s)

        // Two kinds of situation will directly return 0.0/0.0f:
        // 1. info.s is 0;
        // 2. actual exponent is less than double or float minimum exponent .
        if ("0" == info.s || info.e + info.s.length - 1 < minExp) {
            return if (info.negative) unaryMinus(zero) else zero
        }
        // If actual exponent is larger than maximum exponent then will return infinity.
        // To prevent overflow checking twice.
        if (info.e > maxExp || info.e + info.s.length - 1 > maxExp) {
            return if (info.negative) negativeInf else positiveInf
        }

        val result = parserImpl(info.s, info.e)
        return if (info.negative) unaryMinus(result) else result
    }

    /**
     * To unify the logic of Double and Float parsing we need a common method to calculate a negative value.
     * Unfortunately in this case we need to make uncheck cast to generic type T to have a proper return type.
     */
    @Suppress("UNCHECKED_CAST")
    private inline fun <reified T> unaryMinus(value: T): T {
        return when (value) {
            is Float -> -value as T
            is Double -> -value as T
            else -> throw NumberFormatException()
        }
    }

    /**
     * Takes a String and does some initial parsing. Should return a
     * StringExponentPair containing a String with no leading or trailing white
     * space and trailing zeroes eliminated. The exponent of the
     * StringExponentPair will be used to calculate the floating point number by
     * taking the positive integer the String represents and multiplying by 10
     * raised to the power of the exponent.
     *
     * This method is not optimal, can cause performance issues. This logic can be done in one single run.
     * It also violates Kotlin coding practices and should be refactored in the future after stabilizing the logic.
     *
     * @param string the String that will be parsed to a floating point
     * @return a StringExponentPair with necessary values
     * @exception NumberFormatException if the String doesn't pass basic tests
     */