Skip to content

Commit 125acd5

Browse files
committed
This patch improves the parsing capabilites of
Stream::parseFloat() The function will now accept decimals starting with an '.' character not just '0.'. This commit also fixes an error in the function where 1.23.45 is parsed as 1.2345 rather than 1.23 and subsequent calls reading 0.45
1 parent 64fcc71 commit 125acd5

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

hardware/arduino/avr/cores/arduino/Stream.cpp

Lines changed: 10 additions & 7 deletions
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
@@ -182,7 +185,7 @@ float Stream::parseFloat(char skipChar){
182185
read(); // consume the character we got with peek
183186
c = timedPeek();
184187
}
185-
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
188+
while( (c >= '0' && c <= '9') || c == '.' && !isFraction || c == skipChar );
186189

187190
if(isNegative)
188191
value = -value;

hardware/arduino/avr/cores/arduino/Stream.h

Lines changed: 1 addition & 1 deletion
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;

hardware/arduino/sam/cores/arduino/Stream.cpp

Lines changed: 10 additions & 7 deletions
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
@@ -182,7 +185,7 @@ float Stream::parseFloat(char skipChar){
182185
read(); // consume the character we got with peek
183186
c = timedPeek();
184187
}
185-
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
188+
while( (c >= '0' && c <= '9') || c == '.' && !isFraction || c == skipChar );
186189

187190
if(isNegative)
188191
value = -value;

hardware/arduino/sam/cores/arduino/Stream.h

Lines changed: 1 addition & 1 deletion
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)