1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-07 16:23:38 +03:00

Fix Stream::parseFloat() (#8785)

cherry-pick updates for Stream::peekNextDigit() from AVR implementation: decimal dot is optionally allowed
This commit is contained in:
david gauchard 2023-01-03 20:14:10 +01:00 committed by GitHub
parent 204d2c93a7
commit 5a3da9a7d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 9 deletions

View File

@ -58,16 +58,16 @@ int Stream::timedPeek() {
// returns peek of the next digit in the stream or -1 if timeout // returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters // discards non-numeric characters
int Stream::peekNextDigit() { int Stream::peekNextDigit(bool detectDecimal) {
int c; int c;
while(1) { while(1) {
c = timedPeek(); c = timedPeek();
if(c < 0) if( c < 0 || // timeout
return c; // timeout c == '-' ||
if(c == '-') ( c >= '0' && c <= '9' ) ||
return c; ( detectDecimal && c == '.' ) ) {
if(c >= '0' && c <= '9')
return c; return c;
}
read(); // discard non-numeric read(); // discard non-numeric
} }
} }
@ -141,7 +141,7 @@ long Stream::parseInt(char skipChar) {
long value = 0; long value = 0;
int c; int c;
c = peekNextDigit(); c = peekNextDigit(false);
// ignore non numeric leading characters // ignore non numeric leading characters
if(c < 0) if(c < 0)
return 0; // zero returned if timeout return 0; // zero returned if timeout
@ -176,7 +176,7 @@ float Stream::parseFloat(char skipChar) {
int c; int c;
float fraction = 1.0f; float fraction = 1.0f;
c = peekNextDigit(); c = peekNextDigit(true);
// ignore non numeric leading characters // ignore non numeric leading characters
if(c < 0) if(c < 0)
return 0; // zero returned if timeout return 0; // zero returned if timeout

View File

@ -53,7 +53,7 @@ class Stream: public Print {
unsigned long _startMillis; // used for timeout measurement unsigned long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout int timedRead(); // private method to read stream with timeout
int timedPeek(); // private method to peek stream with timeout int timedPeek(); // private method to peek stream with timeout
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout int peekNextDigit(bool detectDecimal = false); // returns the next numeric digit in the stream or -1 if timeout
public: public:
virtual int available() = 0; virtual int available() = 0;