Modified ProperFractionFormat to reject embedded minus signs.
JIRA: MATH-60 Reported by Nhung Nnguyen git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@411647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
445d94e08d
commit
9ebfb870a2
|
@ -24,6 +24,10 @@ import org.apache.commons.math.util.MathUtils;
|
|||
/**
|
||||
* Formats a Fraction number in proper format. The number format for each of
|
||||
* the whole number, numerator and, denominator can be configured.
|
||||
* <p>
|
||||
* Minus signs are only allowed in the whole number part - i.e.,
|
||||
* "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and
|
||||
* will result in a <code>ParseException</code>.
|
||||
*
|
||||
* @since 1.1
|
||||
* @version $Revision$ $Date$
|
||||
|
@ -114,6 +118,11 @@ public class ProperFractionFormat extends FractionFormat {
|
|||
/**
|
||||
* Parses a string to produce a {@link Fraction} object. This method
|
||||
* expects the string to be formatted as a proper fraction.
|
||||
* <p>
|
||||
* Minus signs are only allowed in the whole number part - i.e.,
|
||||
* "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and
|
||||
* will result in a <code>ParseException</code>.
|
||||
*
|
||||
* @param source the string to parse
|
||||
* @param pos input/ouput parsing parameter.
|
||||
* @return the parsed {@link Fraction} object.
|
||||
|
@ -153,6 +162,12 @@ public class ProperFractionFormat extends FractionFormat {
|
|||
return null;
|
||||
}
|
||||
|
||||
if (num.intValue() < 0) {
|
||||
// minus signs should be leading, invalid expression
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse '/'
|
||||
int startIndex = pos.getIndex();
|
||||
char c = parseNextCharacter(source, pos);
|
||||
|
@ -186,6 +201,12 @@ public class ProperFractionFormat extends FractionFormat {
|
|||
return null;
|
||||
}
|
||||
|
||||
if (den.intValue() < 0) {
|
||||
// minus signs must be leading, invalid
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
int w = whole.intValue();
|
||||
int n = num.intValue();
|
||||
int d = den.intValue();
|
||||
|
|
|
@ -229,6 +229,23 @@ public class FractionFormatTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testParseProperInvalidMinus() {
|
||||
String source = "2 -2 / 3";
|
||||
try {
|
||||
Fraction c = properFormat.parse(source);
|
||||
fail("invalid minus in improper fraction.");
|
||||
} catch (ParseException ex) {
|
||||
// expected
|
||||
}
|
||||
source = "2 2 / -3";
|
||||
try {
|
||||
Fraction c = properFormat.parse(source);
|
||||
fail("invalid minus in improper fraction.");
|
||||
} catch (ParseException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testNumeratorFormat() {
|
||||
NumberFormat old = properFormat.getNumeratorFormat();
|
||||
NumberFormat nf = NumberFormat.getInstance();
|
||||
|
|
|
@ -49,6 +49,9 @@ Commons Math Release Notes</title>
|
|||
<action dev="psteitz" type="update" issue="MATH-148" due-to="Joni Salonen">
|
||||
Added QR Decomposition.
|
||||
</action>
|
||||
<action dev="psteitz" type="fix" issue="MATH-60" due-to="Nhung Nnguyen">
|
||||
Modified ProperFractionFormat to reject embedded minus signs.
|
||||
</action>
|
||||
</release>
|
||||
<release version="1.1" date="2005-12-17"
|
||||
description="This is a maintenance release containing bug fixes and enhancements.
|
||||
|
|
Loading…
Reference in New Issue