Skip to content

Commit 1474bbb

Browse files
committed
Enables public access to features previously marked private.
Stream::parseInt & Stream::parseFloat previously had protected overloads which allowed skipping a custom character. This commit brings this feature to the public interface. To keep the public API simpler, the single paramter overload remains protected. However its functionality is available in the public interface using the two parameter overload.
1 parent d6bd32c commit 1474bbb

File tree

4 files changed

+41
-62
lines changed

4 files changed

+41
-62
lines changed

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

+5-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "Stream.h"
2727

2828
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
29-
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
3029

3130
// private method to read stream with timeout
3231
int Stream::timedRead()
@@ -121,17 +120,11 @@ bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t
121120
}
122121
}
123122

124-
125123
// returns the first valid (long) integer value from the current position.
126-
// initial characters that are not digits (or the minus sign) are skipped
127-
// function is terminated by the first character that is not a digit.
128-
long Stream::parseInt(LookaheadMode lookahead)
129-
{
130-
return parseInt(lookahead, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
131-
}
132-
133-
// as above but 'ignore' is ignored
134-
// this allows format characters (typically commas) in values to be ignored
124+
// lookahead determines how parseInt looks ahead in the stream.
125+
// See LookaheadMode enumeration at the top of the file.
126+
// Lookahead is terminated by the first character that is not a valid part of an integer.
127+
// Once parsing commences, 'ignore' will be skipped in the stream.
135128
long Stream::parseInt(LookaheadMode lookahead, char ignore)
136129
{
137130
bool isNegative = false;
@@ -160,16 +153,9 @@ long Stream::parseInt(LookaheadMode lookahead, char ignore)
160153
return value;
161154
}
162155

163-
164156
// as parseInt but returns a floating point value
165-
float Stream::parseFloat(LookaheadMode lookahead)
157+
float Stream::parseFloat(LookaheadMode lookahead, char ignore)
166158
{
167-
return parseFloat(lookahead, NO_SKIP_CHAR);
168-
}
169-
170-
// as above but the given ignore is ignored
171-
// this allows format characters (typically commas) in values to be ignored
172-
float Stream::parseFloat(LookaheadMode lookahead, char ignore){
173159
bool isNegative = false;
174160
bool isFraction = false;
175161
long value = 0;

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ enum LookaheadMode{
4444
SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
4545
};
4646

47+
#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
48+
4749
class Stream : public Print
4850
{
4951
protected:
@@ -81,12 +83,15 @@ class Stream : public Print
8183
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
8284
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
8385

86+
long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
87+
// returns the first valid (long) integer value from the current position.
88+
// lookahead determines how parseInt looks ahead in the stream.
89+
// See LookaheadMode enumeration at the top of the file.
90+
// Lookahead is terminated by the first character that is not a valid part of an integer.
91+
// Once parsing commences, 'ignore' will be skipped in the stream.
8492

85-
long parseInt(LookaheadMode lookahead = SKIP_ALL); // returns the first valid (long) integer value from the current position.
86-
// initial characters that are not digits (or the minus sign) are skipped
87-
// integer is terminated by the first character that is not a digit.
88-
89-
float parseFloat(LookaheadMode lookahead = SKIP_ALL); // float version of parseInt
93+
float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
94+
// float version of parseInt
9095

9196
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
9297
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -104,12 +109,10 @@ class Stream : public Print
104109

105110
protected:
106111
long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
107-
long parseInt(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
108-
// as above but 'ignore' is ignored
109-
// this allows format characters (typically commas) in values to be ignored
110-
111-
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
112-
float parseFloat(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
112+
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
113+
// These overload exists for compatibility with any class that has derived
114+
// Stream and used parseFloat/Int with a custom ignore character. To keep
115+
// the public API simple, these overload remains protected.
113116

114117
struct MultiTarget {
115118
const char *str; // string you're searching for
@@ -122,5 +125,5 @@ class Stream : public Print
122125
int findMulti(struct MultiTarget *targets, int tCount);
123126
};
124127

125-
128+
#undef NO_IGNORE_CHAR
126129
#endif

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

+5-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "Stream.h"
2727

2828
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
29-
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
3029

3130
// private method to read stream with timeout
3231
int Stream::timedRead()
@@ -121,17 +120,11 @@ bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t
121120
}
122121
}
123122

124-
125123
// returns the first valid (long) integer value from the current position.
126-
// initial characters that are not digits (or the minus sign) are skipped
127-
// function is terminated by the first character that is not a digit.
128-
long Stream::parseInt(LookaheadMode lookahead)
129-
{
130-
return parseInt(lookahead, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
131-
}
132-
133-
// as above but 'ignore' is ignored
134-
// this allows format characters (typically commas) in values to be ignored
124+
// lookahead determines how parseInt looks ahead in the stream.
125+
// See LookaheadMode enumeration at the top of the file.
126+
// Lookahead is terminated by the first character that is not a valid part of an integer.
127+
// Once parsing commences, 'ignore' will be skipped in the stream.
135128
long Stream::parseInt(LookaheadMode lookahead, char ignore)
136129
{
137130
bool isNegative = false;
@@ -160,16 +153,9 @@ long Stream::parseInt(LookaheadMode lookahead, char ignore)
160153
return value;
161154
}
162155

163-
164156
// as parseInt but returns a floating point value
165-
float Stream::parseFloat(LookaheadMode lookahead)
157+
float Stream::parseFloat(LookaheadMode lookahead, char ignore)
166158
{
167-
return parseFloat(lookahead, NO_SKIP_CHAR);
168-
}
169-
170-
// as above but the given ignore is ignored
171-
// this allows format characters (typically commas) in values to be ignored
172-
float Stream::parseFloat(LookaheadMode lookahead, char ignore){
173159
bool isNegative = false;
174160
bool isFraction = false;
175161
long value = 0;

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ enum StreamParseOpt{
4444
SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
4545
};
4646

47+
#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
48+
4749
class Stream : public Print
4850
{
4951
protected:
@@ -74,19 +76,22 @@ class Stream : public Print
7476
// returns true if target string is found, false if timed out
7577

7678
bool find(char target) { return find (&target, 1); }
77-
79+
7880
bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
7981
bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
8082

8183
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
8284
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
8385

86+
long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
87+
// returns the first valid (long) integer value from the current position.
88+
// lookahead determines how parseInt looks ahead in the stream.
89+
// See LookaheadMode enumeration at the top of the file.
90+
// Lookahead is terminated by the first character that is not a valid part of an integer.
91+
// Once parsing commences, 'ignore' will be skipped in the stream.
8492

85-
long parseInt(LookaheadMode lookahead = SKIP_ALL); // returns the first valid (long) integer value from the current position.
86-
// initial characters that are not digits (or the minus sign) are skipped
87-
// integer is terminated by the first character that is not a digit.
88-
89-
float parseFloat(LookaheadMode lookahead = SKIP_ALL); // float version of parseInt
93+
float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
94+
// float version of parseInt
9095

9196
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
9297
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -104,12 +109,10 @@ class Stream : public Print
104109

105110
protected:
106111
long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
107-
long parseInt(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
108-
// as above but 'ignore' is ignored
109-
// this allows format characters (typically commas) in values to be ignored
110-
111-
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
112-
float parseFloat(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
112+
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
113+
// These overload exists for compatibility with any class that has derived
114+
// Stream and used parseFloat/Int with a custom ignore character. To keep
115+
// the public API simple, these overload remains protected.
113116

114117
struct MultiTarget {
115118
const char *str; // string you're searching for
@@ -122,4 +125,5 @@ class Stream : public Print
122125
int findMulti(struct MultiTarget *targets, int tCount);
123126
};
124127

128+
#undef NO_IGNORE_CHAR
125129
#endif

0 commit comments

Comments
 (0)