Skip to content

Commit 901ae05

Browse files
committed
Don't consume trailing char in parseInt() and parseFloat (Paul Stoffregen).
http://code.google.com/p/arduino/issues/detail?id=624
1 parent 7ac9755 commit 901ae05

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

hardware/arduino/cores/arduino/Stream.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,39 @@
2929
// private method to read stream with timeout
3030
int Stream::timedRead()
3131
{
32-
//Serial.println(_timeout);
33-
this->_startMillis = millis();
34-
while(millis() - this->_startMillis < this->_timeout)
35-
{
36-
if (this->available() > 0) {
37-
return this->read();
38-
}
39-
}
32+
int c;
33+
_startMillis = millis();
34+
do {
35+
c = read();
36+
if (c >= 0) return c;
37+
} while(millis() - _startMillis < _timeout);
38+
return -1; // -1 indicates timeout
39+
}
40+
41+
// private method to peek stream with timeout
42+
int Stream::timedPeek()
43+
{
44+
int c;
45+
_startMillis = millis();
46+
do {
47+
c = peek();
48+
if (c >= 0) return c;
49+
} while(millis() - _startMillis < _timeout);
4050
return -1; // -1 indicates timeout
4151
}
4252

43-
// returns the next digit in the stream or -1 if timeout
53+
// returns peek of the next digit in the stream or -1 if timeout
4454
// discards non-numeric characters
45-
int Stream::getNextDigit()
55+
int Stream::peekNextDigit()
4656
{
4757
int c;
48-
do{
49-
c = timedRead();
50-
if( c < 0)
51-
return c; // timeout
58+
while (1) {
59+
c = timedPeek();
60+
if (c < 0) return c; // timeout
61+
if (c == '-') return c;
62+
if (c >= '0' && c <= '9') return c;
63+
read(); // discard non-numeric
5264
}
53-
while( c != '-' && (c < '0' || c > '9') ) ;
54-
55-
return c;
5665
}
5766

5867
// Public Methods
@@ -130,7 +139,7 @@ long Stream::parseInt(char skipChar)
130139
long value = 0;
131140
int c;
132141

133-
c = getNextDigit();
142+
c = peekNextDigit();
134143
// ignore non numeric leading characters
135144
if(c < 0)
136145
return 0; // zero returned if timeout
@@ -142,9 +151,10 @@ long Stream::parseInt(char skipChar)
142151
isNegative = true;
143152
else if(c >= '0' && c <= '9') // is c a digit?
144153
value = value * 10 + c - '0';
145-
c = timedRead();
154+
read(); // consume the character we got with peek
155+
c = timedPeek();
146156
}
147-
while( (c >= '0' && c <= '9') || c == skipChar );
157+
while( (c >= '0' && c <= '9') || c == skipChar );
148158

149159
if(isNegative)
150160
value = -value;
@@ -168,7 +178,7 @@ float Stream::parseFloat(char skipChar){
168178
char c;
169179
float fraction = 1.0;
170180

171-
c = getNextDigit();
181+
c = peekNextDigit();
172182
// ignore non numeric leading characters
173183
if(c < 0)
174184
return 0; // zero returned if timeout
@@ -185,7 +195,8 @@ float Stream::parseFloat(char skipChar){
185195
if(isFraction)
186196
fraction *= 0.1;
187197
}
188-
c = timedRead();
198+
read(); // consume the character we got with peek
199+
c = timedPeek();
189200
}
190201
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
191202

hardware/arduino/cores/arduino/Stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Stream : public Print
4141
long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4242
long _startMillis; // used for timeout measurement
4343
int timedRead(); // private method to read stream with timeout
44-
int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
44+
int timedPeek(); // private method to peek stream with timeout
45+
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
4546

4647
public:
4748
virtual int available() = 0;

0 commit comments

Comments
 (0)