New J2K: correctly convert number literals with underscores

#KT-32837 fixed
This commit is contained in:
Ilya Kirillov
2019-07-24 22:35:02 +03:00
parent 4ead820395
commit 72a09ab59b
4 changed files with 32 additions and 3 deletions
@@ -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;
}
+9
View File
@@ -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
}
@@ -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")