Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/number formatter with optional exponential separator #682

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package io.fair_acc.chartfx.utils;

import static java.lang.Math.*;

import static io.fair_acc.chartfx.utils.Schubfach.*;

import static io.fair_acc.chartfx.utils.Schubfach.H_DOUBLE;
import static io.fair_acc.chartfx.utils.Schubfach.getDecimalLength;
import static io.fair_acc.chartfx.utils.Schubfach.getNormalizationScale;
import static java.lang.Math.multiplyHigh;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Pattern;

import javafx.util.StringConverter;

public class NumberFormatterImpl extends StringConverter<Number> implements NumberFormatter {
public final static char DEFAULT_DECIMAL_SEPARATOR = ' ';


private static final Charset CHARSET = StandardCharsets.UTF_8;

private static final Pattern UNICODE_WHITESPACE_PATTERN = Pattern.compile("(?U)\\s");

public NumberFormatterImpl() {
super();
setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
Expand All @@ -26,7 +35,12 @@ public NumberFormatterImpl(final int decimalPlaces, final boolean exponentialFor

@Override
public Number fromString(final String string) {
return Double.parseDouble(string);
if (exponentialSeparator.length == 0) {
return Double.parseDouble(string);
}

String cleanedString = UNICODE_WHITESPACE_PATTERN.matcher(string).replaceAll("");
return Double.parseDouble(cleanedString);
}

@Override
Expand All @@ -38,6 +52,10 @@ public int getDecimalPlaces() {
public boolean isExponentialForm() {
return isExponentialForm;
}

public final char getExponentialSeparator() {
return CHARSET.decode(ByteBuffer.wrap(exponentialSeparator)).get();
}

@Override
public NumberFormatter setExponentialForm(final boolean state) {
Expand All @@ -50,6 +68,16 @@ public NumberFormatter setDecimalPlaces(final int decimalPlaces) {
this.decimalPlaces = Math.max(ALL_DIGITS, decimalPlaces);
return this;
}

/**
* Sets the separator to use between decimal places and exponential e.g. {@code 1_HERE_E-10}. This <em>can</em> break the
* functionality of {@link #fromString(String)} if a non-whitespace character is used. Default: none.
*
* @param separator Character to use
*/
public final void setExponentialSeparator(char separator) {
exponentialSeparator = charToBytes(separator);
}

@Override
public String toString(final double val) {
Expand Down Expand Up @@ -116,13 +144,14 @@ private void encodeDouble(boolean negative, long f, int e) {
private void toExponentialFormat(int h, int m, int l, int e) {
appendDigit(h);
if (decimalPlaces > 0) {
append(DOT);
append(dot);
appendNDigits(m, l, decimalPlaces);
} else if (decimalPlaces == ALL_DIGITS) {
append(DOT);
append(dot);
append8Digits(m);
lowDigits(l);
}
append(exponentialSeparator);
exponent(e - 1);
}

Expand All @@ -139,7 +168,7 @@ private void toPlainFormat(int h, int m, int l, int e) {
if (decimalPlaces == 0) {
return;
}
append(DOT);
append(dot);
if (decimalPlaces == ALL_DIGITS) {
for (; i <= 8; ++i) {
t = 10 * y;
Expand All @@ -164,7 +193,7 @@ private void toPlainFormatWithLeadingZeros(int h, int m, int l, int e) {
if (decimalPlaces == 0) {
return;
}
append(DOT);
append(dot);
int spaceLeft = bytes.length - length;
if (decimalPlaces == ALL_DIGITS) {
for (; e < 0 && spaceLeft > 0; ++e) {
Expand Down Expand Up @@ -193,13 +222,13 @@ private void encodeZero() {
length = 0;
append(ZERO);
if (decimalPlaces > 0) {
append(DOT);
append(dot);
for (int i = 0; i < decimalPlaces; i++) {
append(ZERO);
}
}
if (isExponentialForm) {
append(EXP);
append(exp);
append(ZERO);
}
}
Expand Down Expand Up @@ -277,8 +306,11 @@ private void removeTrailingZeroes() {
length--;
}
// remove trailing comma
if (bytes[length - 1] == DOT) {
length--;
if (length >= dot.length &&
Arrays.equals(
bytes, (length - dot.length), length - dot.length + 1,
dot, 0, dot.length)) {
length -= dot.length;
}
}

Expand All @@ -299,7 +331,7 @@ private int y(int a) {
}

private void exponent(int e) {
append(EXP);
append(exp);
if (e < 0) {
append(MINUS);
e = -e;
Expand All @@ -318,7 +350,11 @@ private void exponent(int e) {
}

private String bytesToString() {
return new String(bytes, 0, length, StandardCharsets.ISO_8859_1);
return new String(bytes, 0, length, CHARSET);
}

private static byte[] charToBytes(char c) {
return CHARSET.encode(CharBuffer.wrap(new char[] { c })).array();
}

static final int ALL_DIGITS = -1;
Expand Down Expand Up @@ -346,23 +382,24 @@ private String bytesToString() {
int length = 0;

boolean isExponentialForm = false;
private byte[] exponentialSeparator = {};

// Used for left-to-tight digit extraction.
private static final int MASK_28 = (1 << 28) - 1;

public NumberFormatterImpl setDecimalFormatSymbols(DecimalFormatSymbols symbols) {
String exp = symbols.getExponentSeparator();
if (exp.length() > MAX_EXP_LENGTH) {
String expString = symbols.getExponentSeparator();
if (expString.length() > MAX_EXP_LENGTH) {
throw new IllegalArgumentException("Exponent separator can't be longer than " + MAX_EXP_LENGTH);
}
this.EXP = Objects.equals(exp, "E") ? DEFAULT_EXP : exp.getBytes(StandardCharsets.ISO_8859_1);
this.DOT = (byte) symbols.getDecimalSeparator();
this.exp = Objects.equals(expString, "E") ? DEFAULT_EXP : expString.getBytes(CHARSET);
this.dot = charToBytes(symbols.getDecimalSeparator());
return this;
}

byte DOT = '.';
byte[] EXP = DEFAULT_EXP;
private static final byte[] DEFAULT_EXP = new byte[] { 'E' };
byte[] dot = charToBytes('.');
byte[] exp = DEFAULT_EXP;
private static final byte[] DEFAULT_EXP = charToBytes('E');
private static final byte ZERO = (byte) '0';
private static final byte MINUS = (byte) '-';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.fair_acc.chartfx.axes.spi.format;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import io.fair_acc.dataset.spi.fastutil.DoubleArrayList;

class DefaultFormatterTest {

@Test
void formatAndParsingWorks() {
DefaultFormatter formatter = new DefaultFormatter();
formatter.updateFormatter(DoubleArrayList.wrap(new double[]{1e-5, 10}), 1.);

final double value = 0.01;
String formatted = formatter.toString(value);
assertEquals("1E-2", formatted);

Number parsed = formatter.fromString(formatted);
assertEquals(value, parsed.doubleValue());
}

@Test
void formatAndParsingWorksWithExponentialSeparator() {
DefaultFormatter formatter = new DefaultFormatter() {
{
formatter.setExponentialSeparator('\u202F');
}
};
formatter.updateFormatter(DoubleArrayList.wrap(new double[]{1e-5, 10}), 1.);

final double value = 0.01;
String formatted = formatter.toString(value);
assertEquals("1\u202FE-2", formatted);

Number parsed = formatter.fromString(formatted);
assertEquals(value, parsed.doubleValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static io.fair_acc.chartfx.utils.NumberFormatterImpl.*;

import java.util.Locale;
import java.util.Optional;
import java.util.function.DoubleFunction;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -54,6 +55,15 @@ void exponentialFormat() {
assertEquals("1.23456789E14", formatter.apply(123.456789E12));
assertEquals("1.23456789E-10", formatter.apply(123.456789E-12));
}

@Test
void exponentialFormatParsing() {
Locale.setDefault(Locale.US);
var formatter = createFormatterImpl(true, ALL_DIGITS, Optional.empty());
assertEquals(0., formatter.fromString("0E0"));
assertEquals(2.1,formatter.fromString("2.1E0"));
assertEquals(123.456789E-12, formatter.fromString("1.23456789E-10"));
}

@Test
void plain5Decimals() {
Expand Down Expand Up @@ -109,11 +119,40 @@ void testPlainRounding() {
assertEquals("0.01", formatter.apply(0.010000000000000004));
assertEquals("0.00", formatter.apply(0.001000000000000004));
}

private static DoubleFunction<String> createFormatter(boolean exponentialForm, int decimalPlaces) {

@Test
void exponentialSeparator() {
Locale.setDefault(Locale.US);
var formatter = createFormatterImpl(true, 5, Optional.of('\u202F'));
assertEquals("2.10000\u202FE0", formatter.toString(2.1));
assertEquals("1.00000\u202FE1", formatter.toString(10));
assertEquals("1.23457\u202FE14", formatter.toString(123.456789E12));
assertEquals("1.23457\u202FE-10", formatter.toString(123.456789E-12));
}

@Test
void exponentialSeparatorParsing() {
Locale.setDefault(Locale.US);
var formatter = createFormatterImpl(true, 5, Optional.of('\u202F'));
assertEquals(2.1, formatter.fromString("2.10000\u202FE0"));
assertEquals(10., formatter.fromString("1.00000\u202FE1"));
assertEquals(1.23457E14, formatter.fromString("1.23457\u202FE14"));
assertEquals(1.23457E-10, formatter.fromString("1.23457\u202FE-10"));
}

private static NumberFormatterImpl createFormatterImpl(
boolean exponentialForm,
int decimalPlaces,
Optional<Character> exponentialSeparator) {
var formatter = new NumberFormatterImpl();
formatter.setExponentialForm(exponentialForm);
formatter.setDecimalPlaces(decimalPlaces);
exponentialSeparator.ifPresent(sep -> formatter.setExponentialSeparator(sep));
return formatter;
}

private static DoubleFunction<String> createFormatter(boolean exponentialForm, int decimalPlaces) {
var formatter = createFormatterImpl(exponentialForm, decimalPlaces, Optional.empty());
return formatter::toString;
}
}
Loading