[java-decompiler] IDEA-346312 adapt patches

- fix rounding with leading zeros

GitOrigin-RevId: 9da47bb0fc1a86b7a3b87588a20a49c20c8898bb
This commit is contained in:
Mikhail Pyltsin
2024-09-26 13:27:57 +02:00
committed by intellij-monorepo-bot
parent 1e1832b281
commit c6e43f31ec
2 changed files with 56 additions and 6 deletions

View File

@@ -2,6 +2,7 @@
package org.jetbrains.java.decompiler.modules.decompiler.exps;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.ClassesProcessor;
import org.jetbrains.java.decompiler.main.DecompilerContext;
@@ -421,7 +422,8 @@ public class ConstExprent extends Exprent {
// Different JVM implementations/version display Floats and Doubles with different String representations
// for the same thing. This trims them all down to only the necessary amount.
private static String trimFloat(String value, float start) {
@VisibleForTesting
public static String trimFloat(String value, float start) {
// Includes NaN and simple numbers
if (value.length() <= 3 || !DecompilerContext.getOption(IFernflowerPreferences.STANDARDIZE_FLOATING_POINT_NUMBERS))
return value;
@@ -452,18 +454,24 @@ public class ConstExprent extends Exprent {
return rounded;
long decimalVal = 1;
int leadingZeros = 0;
for (int i = 0; i < decimal.length() - 1; i++) {
if (decimal.charAt(i) == '0' && leadingZeros == i) {
leadingZeros++;
}
decimalVal = (decimalVal - 1) * 10 + decimal.charAt(i) - '0' + 1;
rounded = integer + '.' + decimalVal + exp;
if (Float.parseFloat(rounded) == start)
rounded = integer + '.' + "0".repeat(leadingZeros) + decimalVal + exp;
if (Float.parseFloat(rounded) == start) {
return rounded;
}
}
}
return value + exp;
}
private static String trimDouble(String value, double start) {
@VisibleForTesting
public static String trimDouble(String value, double start) {
// Includes NaN and simple numbers
if (value.length() <= 3 || !DecompilerContext.getOption(IFernflowerPreferences.STANDARDIZE_FLOATING_POINT_NUMBERS))
return value;
@@ -494,11 +502,16 @@ public class ConstExprent extends Exprent {
return rounded;
long decimalVal = 1;
int leadingZeros = 0;
for (int i = 0; i < decimal.length() - 1; i++) {
if(decimal.charAt(i) == '0' && leadingZeros == i) {
leadingZeros++;
}
decimalVal = (decimalVal - 1) * 10 + decimal.charAt(i) - '0' + 1;
rounded = integer + '.' + decimalVal + exp;
if (Double.parseDouble(rounded) == start)
rounded = integer + '.' + "0".repeat(leadingZeros) + decimalVal + exp;
if (Double.parseDouble(rounded) == start) {
return rounded;
}
}
}
return value + exp;

View File

@@ -0,0 +1,37 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.java.decompiler;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
import org.junit.Test;
import java.util.Map;
import static junit.framework.TestCase.assertEquals;
public class TrimHelperTest extends SingleClassesTestBase {
@Override
protected Map<String, String> getDecompilerOptions() {
return Map.of(IFernflowerPreferences.STANDARDIZE_FLOATING_POINT_NUMBERS, "1"
);
}
@Test
public void testTrimFloat() {
assertEquals("1.0123457", ConstExprent.trimFloat("1.012345678f", 1.012345678f));
assertEquals("1.1023457", ConstExprent.trimFloat("1.102345678f", 1.102345678f));
assertEquals("1.0012345", ConstExprent.trimFloat("1.0012345678f", 1.0012345678f));
assertEquals("1.1012345", ConstExprent.trimFloat("1.1012345678f", 1.1012345678f));
assertEquals("1.1123457", ConstExprent.trimFloat("1.112345678f", 1.112345678f));
}
@Test
public void testTrimDouble() {
assertEquals("1.0123456781234567", ConstExprent.trimDouble("1.01234567812345678", 1.01234567812345678));
assertEquals("1.0012345678123457", ConstExprent.trimDouble("1.001234567812345678", 1.001234567812345678));
assertEquals("1.1012345678123456", ConstExprent.trimDouble("1.101234567812345678", 1.101234567812345678));
assertEquals("1.1023456781234568", ConstExprent.trimDouble("1.102345678123456789", 1.102345678123456789));
assertEquals("1.1123456781234568", ConstExprent.trimDouble("1.11234567812345678", 1.11234567812345678));
}
}