Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1397023

Browse files
committed
This adds the capability to control the Stream::parseInt/float
lookahead. Its default is SKIP_ALL which reflects previous versions. However SKIP_NONE, and SKIP_WHITESPACE can refine this behaviour.
1 parent 9ecc150 commit 1397023

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ 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(bool detectDecimal)
57+
int Stream::peekNextDigit(StreamParseOpt skipMode, bool detectDecimal)
5858
{
5959
int c;
6060
while (1) {
@@ -65,6 +65,17 @@ int Stream::peekNextDigit( bool detectDecimal )
6565
c >= '0' && c <= '9' ||
6666
detectDecimal && c == '.') return c;
6767

68+
switch( skipMode ){
69+
case SKIP_NONE: return -1; // Fail code.
70+
case SKIP_WHITESPACE:
71+
switch( c ){
72+
case ' ':
73+
case '\t':
74+
case '\r':
75+
case '\n': break;
76+
default: return -1; // Fail code.
77+
}
78+
}
6879
read(); // discard non-numeric
6980
}
7081
}
@@ -114,20 +125,20 @@ bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t
114125
// returns the first valid (long) integer value from the current position.
115126
// initial characters that are not digits (or the minus sign) are skipped
116127
// function is terminated by the first character that is not a digit.
117-
long Stream::parseInt()
128+
long Stream::parseInt(StreamParseOpt skipMode)
118129
{
119-
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
130+
return parseInt(skipMode, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
120131
}
121132

122133
// as above but a given skipChar is ignored
123134
// this allows format characters (typically commas) in values to be ignored
124-
long Stream::parseInt(char skipChar)
135+
long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
125136
{
126137
bool isNegative = false;
127138
long value = 0;
128139
int c;
129140

130-
c = peekNextDigit(false);
141+
c = peekNextDigit(skipMode, false);
131142
// ignore non numeric leading characters
132143
if(c < 0)
133144
return 0; // zero returned if timeout
@@ -151,21 +162,21 @@ long Stream::parseInt(char skipChar)
151162

152163

153164
// as parseInt but returns a floating point value
154-
float Stream::parseFloat()
165+
float Stream::parseFloat(StreamParseOpt skipMode)
155166
{
156-
return parseFloat(NO_SKIP_CHAR);
167+
return parseFloat(skipMode, NO_SKIP_CHAR);
157168
}
158169

159170
// as above but the given skipChar is ignored
160171
// this allows format characters (typically commas) in values to be ignored
161-
float Stream::parseFloat(char skipChar){
172+
float Stream::parseFloat(StreamParseOpt skipMode, char skipChar){
162173
bool isNegative = false;
163174
bool isFraction = false;
164175
long value = 0;
165176
char c;
166177
float fraction = 1.0;
167178

168-
c = peekNextDigit(true);
179+
c = peekNextDigit(skipMode, true);
169180
// ignore non numeric leading characters
170181
if(c < 0)
171182
return 0; // zero returned if timeout

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@
3535
readBytesBetween( pre_string, terminator, buffer, length)
3636
*/
3737

38+
enum StreamParseOpt{
39+
SKIP_ALL,
40+
SKIP_NONE,
41+
SKIP_WHITESPACE
42+
};
43+
3844
class Stream : public Print
3945
{
4046
protected:
4147
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4248
unsigned long _startMillis; // used for timeout measurement
4349
int timedRead(); // private method to read stream with timeout
4450
int timedPeek(); // private method to peek stream with timeout
45-
int peekNextDigit(bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
51+
int peekNextDigit(StreamParseOpt skipMode, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
4652

4753
public:
4854
virtual int available() = 0;
@@ -73,11 +79,11 @@ class Stream : public Print
7379
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
7480

7581

76-
long parseInt(); // returns the first valid (long) integer value from the current position.
82+
long parseInt(StreamParseOpt skipMode = SKIP_ALL); // returns the first valid (long) integer value from the current position.
7783
// initial characters that are not digits (or the minus sign) are skipped
7884
// integer is terminated by the first character that is not a digit.
7985

80-
float parseFloat(); // float version of parseInt
86+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
8187

8288
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
8389
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -94,11 +100,11 @@ class Stream : public Print
94100
String readStringUntil(char terminator);
95101

96102
protected:
97-
long parseInt(char skipChar); // as above but the given skipChar is ignored
103+
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
98104
// as above but the given skipChar is ignored
99105
// this allows format characters (typically commas) in values to be ignored
100106

101-
float parseFloat(char skipChar); // as above but the given skipChar is ignored
107+
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
102108

103109
struct MultiTarget {
104110
const char *str; // string you're searching for

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ 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( bool detectDecimal )
57+
int Stream::peekNextDigit(StreamParseOpt skipMode, bool detectDecimal )
5858
{
5959
int c;
6060
while (1) {
@@ -65,6 +65,17 @@ int Stream::peekNextDigit( bool detectDecimal )
6565
c >= '0' && c <= '9' ||
6666
detectDecimal && c == '.') return c;
6767

68+
switch( skipMode ){
69+
case SKIP_NONE: return -1; // Fail code.
70+
case SKIP_WHITESPACE:
71+
switch( c ){
72+
case ' ':
73+
case '\t':
74+
case '\r':
75+
case '\n': break;
76+
default: return -1; // Fail code.
77+
}
78+
}
6879
read(); // discard non-numeric
6980
}
7081
}
@@ -114,20 +125,20 @@ bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t
114125
// returns the first valid (long) integer value from the current position.
115126
// initial characters that are not digits (or the minus sign) are skipped
116127
// function is terminated by the first character that is not a digit.
117-
long Stream::parseInt()
128+
long Stream::parseInt(StreamParseOpt skipMode)
118129
{
119-
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
130+
return parseInt(skipMode, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
120131
}
121132

122133
// as above but a given skipChar is ignored
123134
// this allows format characters (typically commas) in values to be ignored
124-
long Stream::parseInt(char skipChar)
135+
long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
125136
{
126137
bool isNegative = false;
127138
long value = 0;
128139
int c;
129140

130-
c = peekNextDigit(false);
141+
c = peekNextDigit(skipMode, false);
131142
// ignore non numeric leading characters
132143
if(c < 0)
133144
return 0; // zero returned if timeout
@@ -151,21 +162,21 @@ long Stream::parseInt(char skipChar)
151162

152163

153164
// as parseInt but returns a floating point value
154-
float Stream::parseFloat()
165+
float Stream::parseFloat(StreamParseOpt skipMode)
155166
{
156-
return parseFloat(NO_SKIP_CHAR);
167+
return parseFloat(skipMode, NO_SKIP_CHAR);
157168
}
158169

159170
// as above but the given skipChar is ignored
160171
// this allows format characters (typically commas) in values to be ignored
161-
float Stream::parseFloat(char skipChar){
172+
float Stream::parseFloat(StreamParseOpt skipMode, char skipChar){
162173
bool isNegative = false;
163174
bool isFraction = false;
164175
long value = 0;
165176
char c;
166177
float fraction = 1.0;
167178

168-
c = peekNextDigit(true);
179+
c = peekNextDigit(skipMode, true);
169180
// ignore non numeric leading characters
170181
if(c < 0)
171182
return 0; // zero returned if timeout

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@
3535
readBytesBetween( pre_string, terminator, buffer, length)
3636
*/
3737

38+
enum StreamParseOpt{
39+
SKIP_ALL,
40+
SKIP_NONE,
41+
SKIP_WHITESPACE
42+
};
43+
3844
class Stream : public Print
3945
{
4046
protected:
4147
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4248
unsigned long _startMillis; // used for timeout measurement
4349
int timedRead(); // private method to read stream with timeout
4450
int timedPeek(); // private method to peek stream with timeout
45-
int peekNextDigit(bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
51+
int peekNextDigit(StreamParseOpt skipMode, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
4652

4753
public:
4854
virtual int available() = 0;
@@ -71,11 +77,11 @@ class Stream : public Print
7177
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
7278

7379

74-
long parseInt(); // returns the first valid (long) integer value from the current position.
80+
long parseInt(StreamParseOpt skipMode = SKIP_ALL); // returns the first valid (long) integer value from the current position.
7581
// initial characters that are not digits (or the minus sign) are skipped
7682
// integer is terminated by the first character that is not a digit.
7783

78-
float parseFloat(); // float version of parseInt
84+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
7985

8086
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
8187
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -92,11 +98,11 @@ class Stream : public Print
9298
String readStringUntil(char terminator);
9399

94100
protected:
95-
long parseInt(char skipChar); // as above but the given skipChar is ignored
101+
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
96102
// as above but the given skipChar is ignored
97103
// this allows format characters (typically commas) in values to be ignored
98104

99-
float parseFloat(char skipChar); // as above but the given skipChar is ignored
105+
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
100106

101107
struct MultiTarget {
102108
const char *str; // string you're searching for

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /