Fix QuantityType UNIT_PATTERN regex (#3841)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/3842/head
jimtng 2023-10-10 18:08:45 +10:00 committed by GitHub
parent 783c57cbb9
commit e9d969c31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -70,11 +70,11 @@ public class QuantityType<T extends Quantity<T>> extends Number
public static final QuantityType<Dimensionless> ZERO = new QuantityType<>(0, AbstractUnit.ONE); public static final QuantityType<Dimensionless> ZERO = new QuantityType<>(0, AbstractUnit.ONE);
public static final QuantityType<Dimensionless> ONE = new QuantityType<>(1, AbstractUnit.ONE); public static final QuantityType<Dimensionless> ONE = new QuantityType<>(1, AbstractUnit.ONE);
// Regular expression to split unit from value. Split on any blank character, even none (\\s*) // Regular expression to split unit from value. Split on any blank character, or
// which occurs after a digit (?<=\\d) and before a "unit" character ?=[a-zA-Z°µ\u03BC%'] // between a digit (?<=\\d) and a non-digit character (?=\\D)
// which itself must not be preceded by plus/minus digit (?![\\+\\-]?\\d). // which must not be preceded by plus/minus digit (?![\\+\\-]?\\d).
// The latter would be an exponent from the scalar value. // The latter would be an exponent from the scalar value.
private static final String UNIT_PATTERN = "(?<=\\d)\\s*(?=[a-zA-Z°µ\u03BC%'](?![\\+\\-]?\\d))"; private static final String UNIT_PATTERN = "\\s+|(?<=\\d)(?=\\D(?![\\+\\-]?\\d))";
static { static {
UnitInitializer.init(); UnitInitializer.init();

View File

@ -143,6 +143,11 @@ public class QuantityTypeTest {
new QuantityType<>("0E-22 m"); new QuantityType<>("0E-22 m");
new QuantityType<>("10E-3"); new QuantityType<>("10E-3");
new QuantityType<>("10E3"); new QuantityType<>("10E3");
new QuantityType<>("1 /s");
new QuantityType<>("1/s");
new QuantityType<>("1Ω·m");
new QuantityType<>("1 Ω·m");
new QuantityType<>("1.5E2Ω·m");
QuantityType.valueOf("2m"); QuantityType.valueOf("2m");
} }
@ -158,6 +163,10 @@ public class QuantityTypeTest {
assertEquals(QuantityType.valueOf("1.1e3\u03BCm"), QuantityType.valueOf("1.1E3 µm")); assertEquals(QuantityType.valueOf("1.1e3\u03BCm"), QuantityType.valueOf("1.1E3 µm"));
assertEquals(QuantityType.valueOf("1.1e3 \u00B5m"), QuantityType.valueOf("1.1E3 µm")); assertEquals(QuantityType.valueOf("1.1e3 \u00B5m"), QuantityType.valueOf("1.1E3 µm"));
assertEquals(QuantityType.valueOf("1.1e3\u00B5m"), QuantityType.valueOf("1.1E3 µm")); assertEquals(QuantityType.valueOf("1.1e3\u00B5m"), QuantityType.valueOf("1.1E3 µm"));
assertEquals(QuantityType.valueOf("1.1e3Ω·m"), QuantityType.valueOf("1.1E3 Ω·m"));
assertEquals(QuantityType.valueOf("1.1e3 Ω·m"), QuantityType.valueOf("1.1E3 Ω·m"));
assertEquals(QuantityType.valueOf("1.1e3/s"), QuantityType.valueOf("1.1E3 /s"));
assertEquals(QuantityType.valueOf("1.1e3 /s"), QuantityType.valueOf("1.1E3 /s"));
} }
@ParameterizedTest @ParameterizedTest