From 24a994019f3ddf236202c31cc5004762ef37db49 Mon Sep 17 00:00:00 2001 From: Chris--A Date: Fri, 19 Jun 2015 13:05:06 +1000 Subject: [PATCH] =?UTF-8?q?This=20commit=20improves=20the=20parsing=20capa?= =?UTF-8?q?bility=20by=20allowing=20decimals=20only=20prefixed=20by=20an?= =?UTF-8?q?=20'.'=20character.=20Previously=20the=20preceeding=20zero=20mu?= =?UTF-8?q?st=20be=20present:=20'0.'=EF=BB=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hardware/arduino/avr/cores/arduino/Stream.cpp | 15 +++++++++------ hardware/arduino/avr/cores/arduino/Stream.h | 2 +- hardware/arduino/sam/cores/arduino/Stream.cpp | 15 +++++++++------ hardware/arduino/sam/cores/arduino/Stream.h | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/Stream.cpp b/hardware/arduino/avr/cores/arduino/Stream.cpp index b31942f29..1f2f074f0 100644 --- a/hardware/arduino/avr/cores/arduino/Stream.cpp +++ b/hardware/arduino/avr/cores/arduino/Stream.cpp @@ -54,14 +54,17 @@ int Stream::timedPeek() // returns peek of the next digit in the stream or -1 if timeout // discards non-numeric characters -int Stream::peekNextDigit() +int Stream::peekNextDigit( bool detectDecimal ) { int c; while (1) { c = timedPeek(); - if (c < 0) return c; // timeout - if (c == '-') return c; - if (c >= '0' && c <= '9') return c; + + if( c < 0 || + c == '-' || + c >= '0' && c <= '9' || + detectDecimal && c == '.') return c; + read(); // discard non-numeric } } @@ -124,7 +127,7 @@ long Stream::parseInt(char skipChar) long value = 0; int c; - c = peekNextDigit(); + c = peekNextDigit(false); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout @@ -162,7 +165,7 @@ float Stream::parseFloat(char skipChar){ char c; float fraction = 1.0; - c = peekNextDigit(); + c = peekNextDigit(true); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout diff --git a/hardware/arduino/avr/cores/arduino/Stream.h b/hardware/arduino/avr/cores/arduino/Stream.h index 15f6761f0..4bc0036ca 100644 --- a/hardware/arduino/avr/cores/arduino/Stream.h +++ b/hardware/arduino/avr/cores/arduino/Stream.h @@ -42,7 +42,7 @@ class Stream : public Print unsigned long _startMillis; // used for timeout measurement int timedRead(); // private method to read 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 ); // returns the next numeric digit in the stream or -1 if timeout public: virtual int available() = 0; diff --git a/hardware/arduino/sam/cores/arduino/Stream.cpp b/hardware/arduino/sam/cores/arduino/Stream.cpp index b31942f29..1f2f074f0 100644 --- a/hardware/arduino/sam/cores/arduino/Stream.cpp +++ b/hardware/arduino/sam/cores/arduino/Stream.cpp @@ -54,14 +54,17 @@ int Stream::timedPeek() // returns peek of the next digit in the stream or -1 if timeout // discards non-numeric characters -int Stream::peekNextDigit() +int Stream::peekNextDigit( bool detectDecimal ) { int c; while (1) { c = timedPeek(); - if (c < 0) return c; // timeout - if (c == '-') return c; - if (c >= '0' && c <= '9') return c; + + if( c < 0 || + c == '-' || + c >= '0' && c <= '9' || + detectDecimal && c == '.') return c; + read(); // discard non-numeric } } @@ -124,7 +127,7 @@ long Stream::parseInt(char skipChar) long value = 0; int c; - c = peekNextDigit(); + c = peekNextDigit(false); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout @@ -162,7 +165,7 @@ float Stream::parseFloat(char skipChar){ char c; float fraction = 1.0; - c = peekNextDigit(); + c = peekNextDigit(true); // ignore non numeric leading characters if(c < 0) return 0; // zero returned if timeout diff --git a/hardware/arduino/sam/cores/arduino/Stream.h b/hardware/arduino/sam/cores/arduino/Stream.h index 0d9a49aca..15f18106a 100644 --- a/hardware/arduino/sam/cores/arduino/Stream.h +++ b/hardware/arduino/sam/cores/arduino/Stream.h @@ -42,7 +42,7 @@ class Stream : public Print unsigned long _startMillis; // used for timeout measurement int timedRead(); // private method to read 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 ); // returns the next numeric digit in the stream or -1 if timeout public: virtual int available() = 0;