New J2K: correctly convert number literals with underscores
#KT-32837 fixed
This commit is contained in:
@@ -54,7 +54,7 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
|
||||
JKKtLiteralExpressionImpl(literal.replace("((?:\\\\)*)\\\\([0-3]?[0-7]{1,2})".toRegex()) { matchResult ->
|
||||
val leadingBackslashes = matchResult.groupValues[1]
|
||||
if (leadingBackslashes.length % 2 == 0)
|
||||
kotlin.String.format("%s\\u%04x", leadingBackslashes, java.lang.Integer.parseInt(matchResult.groupValues[2], 8))
|
||||
String.format("%s\\u%04x", leadingBackslashes, Integer.parseInt(matchResult.groupValues[2], 8))
|
||||
else matchResult.value
|
||||
}.replace("\\$([A-Za-z]+|\\{)".toRegex(), "\\\\$0"), JKLiteralExpression.LiteralType.STRING)
|
||||
|
||||
@@ -71,7 +71,7 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
|
||||
private fun JKLiteralExpression.toIntLiteral(): JKKtLiteralExpression =
|
||||
JKKtLiteralExpressionImpl(
|
||||
literal
|
||||
.replace("l", "", ignoreCase = true)
|
||||
.cleanIntAndLongLiterals()
|
||||
.convertHexLiteral(isLongLiteral = false)
|
||||
.convertOctalLiteral(),
|
||||
JKLiteralExpression.LiteralType.INT
|
||||
@@ -81,7 +81,7 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
|
||||
private fun JKLiteralExpression.toLongLiteral(): JKKtLiteralExpression =
|
||||
JKKtLiteralExpressionImpl(
|
||||
literal
|
||||
.replace("l", "", ignoreCase = true)
|
||||
.cleanIntAndLongLiterals()
|
||||
.convertHexLiteral(isLongLiteral = true)
|
||||
.convertOctalLiteral() + "L",
|
||||
JKLiteralExpression.LiteralType.LONG
|
||||
@@ -113,4 +113,10 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
|
||||
.replace(".e", "e", ignoreCase = true)
|
||||
.replace(".f", "", ignoreCase = true)
|
||||
.replace("f", "", ignoreCase = true)
|
||||
.replace("_", "")
|
||||
|
||||
private fun String.cleanIntAndLongLiterals() =
|
||||
replace("l", "", ignoreCase = true)
|
||||
.replace("_", "")
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
public class TestNumber {
|
||||
long longLiteral = 123_456_7;
|
||||
long longHexLiteral = 0xFF_EC_DE_5E;
|
||||
int intLiteral = 123_4;
|
||||
float floatLiteral = 123_4.f;
|
||||
double doubleLiteral = 123_4d;
|
||||
short shortLiteral = 1_2_3;
|
||||
byte byteLiteral = 1_2_3;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class TestNumber {
|
||||
internal var longLiteral: Long = 1234567
|
||||
internal var longHexLiteral: Long = -0x1321a2
|
||||
internal var intLiteral = 1234
|
||||
internal var floatLiteral = 1234f
|
||||
internal var doubleLiteral = 1234.0
|
||||
internal var shortLiteral: Short = 123
|
||||
internal var byteLiteral: Byte = 123
|
||||
}
|
||||
+5
@@ -3375,6 +3375,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
|
||||
public void testTrueOrFalse() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/literalExpression/trueOrFalse.java");
|
||||
}
|
||||
|
||||
@TestMetadata("underscores.java")
|
||||
public void testUnderscores() throws Exception {
|
||||
runTest("nj2k/testData/newJ2k/literalExpression/underscores.java");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("nj2k/testData/newJ2k/localVariable")
|
||||
|
||||
Reference in New Issue
Block a user