private bool ParseFloat(TextParser input, FormatSpecifier spec)
{
// Skip any whitespace
input.MovePastWhitespace();
// Parse leading sign
int start = input.Position;
if (input.Peek() == '+' || input.Peek() == '-')
input.MoveAhead();
// Parse digits
bool hasPoint = false;
while (Char.IsDigit(input.Peek()) || input.Peek() == '.')
{
if (input.Peek() == '.')
{
if (hasPoint)
break;
hasPoint = true;
}
input.MoveAhead();
}
// Parse exponential notation
if (Char.ToLower(input.Peek()) == 'e')
{
input.MoveAhead();
if (input.Peek() == '+' || input.Peek() == '-')
input.MoveAhead();
while (Char.IsDigit(input.Peek()))
input.MoveAhead();
}
// Don't exceed field width
if (spec.Width > 0)
{
int count = input.Position - start;
if (spec.Width < count)
input.MoveAhead(spec.Width - count);
}
// Because we parse the exponential notation before we apply
// any field-width constraint, it becomes awkward to verify
// we have a valid floating point token. To prevent an
// exception, we use TryParse() here instead of Parse().
double result;
// Extract token
if (input.Position > start
&& double.TryParse(input.Extract(start, input.Position), NumberStyles.Float, CultureInfo.InvariantCulture, out result)
)
{
if (!spec.NoResult)
{
if (spec.Modifier == Modifiers.Long ||
spec.Modifier == Modifiers.LongLong)
Results.Add(result);
else
Results.Add((float)result);
}
return true;
}
return false;
}