added an error detection for missing imaginary character while parsing complex string

JIRA: MATH-198

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@640191 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-03-23 12:22:59 +00:00
parent 07b329c5fd
commit cd5f65c4c7
3 changed files with 13 additions and 1 deletions

View File

@ -374,7 +374,9 @@ public class ComplexFormat extends Format implements Serializable {
int n = getImaginaryCharacter().length();
startIndex = pos.getIndex();
int endIndex = startIndex + n;
if (source.substring(startIndex, endIndex).compareTo(
if ((startIndex >= source.length()) ||
(endIndex > source.length()) ||
source.substring(startIndex, endIndex).compareTo(
getImaginaryCharacter()) != 0) {
// set index back to initial, error index should be the start index
// character examined.

View File

@ -44,6 +44,9 @@ Commons Math Release Notes</title>
<action dev="brentworden" type="fix" issue="MATH-193" due-to="Michael Heuer and Sebb">
Javadoc and style fixes.
</action>
<action dev="luc" type="fix" issue="MATH-198" due-to="Frederick Salardi">
added an error detection for missing imaginary character while parsing complex string
</action>
</release>
<release version="1.2" date="2008-02-24"
description="This release combines bug fixes and new features. Most notable

View File

@ -19,6 +19,7 @@ package org.apache.commons.math.complex;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import junit.framework.TestCase;
@ -348,4 +349,10 @@ public abstract class ComplexFormatAbstractTest extends TestCase {
// success
}
}
public void testForgottenImaginaryCharacter() {
ParsePosition pos = new ParsePosition(0);
assertNull(new ComplexFormat().parse("1 + 1", pos));
assertEquals(5, pos.getErrorIndex());
}
}