Skip to content

Commit 2709365

Browse files
Chris--Asandeepmistry
authored andcommitted
This commit improves the parsing capability by allowing decimals only
prefixed by an '.' character. Previously the preceeding zero must be present: '0.'
1 parent ba1beeb commit 2709365

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

cores/arduino/Stream.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ int Stream::timedPeek()
5454

5555
// returns peek of the next digit in the stream or -1 if timeout
5656
// discards non-numeric characters
57-
int Stream::peekNextDigit()
57+
int Stream::peekNextDigit( bool detectDecimal )
5858
{
5959
int c;
6060
while (1) {
6161
c = timedPeek();
62-
if (c < 0) return c; // timeout
63-
if (c == '-') return c;
64-
if (c >= '0' && c <= '9') return c;
62+
63+
if( c < 0 ||
64+
c == '-' ||
65+
c >= '0' && c <= '9' ||
66+
detectDecimal && c == '.') return c;
67+
6568
read(); // discard non-numeric
6669
}
6770
}
@@ -124,7 +127,7 @@ long Stream::parseInt(char skipChar)
124127
long value = 0;
125128
int c;
126129

127-
c = peekNextDigit();
130+
c = peekNextDigit(false);
128131
// ignore non numeric leading characters
129132
if(c < 0)
130133
return 0; // zero returned if timeout
@@ -162,7 +165,7 @@ float Stream::parseFloat(char skipChar){
162165
char c;
163166
float fraction = 1.0;
164167

165-
c = peekNextDigit();
168+
c = peekNextDigit(true);
166169
// ignore non numeric leading characters
167170
if(c < 0)
168171
return 0; // zero returned if timeout

cores/arduino/Stream.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Stream : public Print
4242
unsigned long _startMillis; // used for timeout measurement
4343
int timedRead(); // private method to read stream with timeout
4444
int timedPeek(); // private method to peek stream with timeout
45-
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
45+
int peekNextDigit( bool detectDecimal ); // returns the next numeric digit in the stream or -1 if timeout
4646

4747
public:
4848
virtual int available() = 0;

0 commit comments

Comments
 (0)