Skip to content

Commit 8cead55

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. During the implementation of 1397023 the protected method was effectively replaced by an incompatible two parameter version. The single parameter version has been re-included for backwards compatibility with classes deriving Stream that may have called the protected member. To keep the public API simpler, this overload remains protected. However its functionality is available in the public interface using the two parameter overload.
1 parent 9353e33 commit 8cead55

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

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

+2-17
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.
126124
// initial characters that are not digits (or the minus sign) are skipped
127125
// function is terminated by the first character that is not a digit.
128-
long Stream::parseInt(StreamParseOpt skipMode)
129-
{
130-
return parseInt(skipMode, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
131-
}
132-
133-
// as above but a given skipChar is ignored
134-
// this allows format characters (typically commas) in values to be ignored
126+
// If provided, skipChar is ignored while parsing. This allows format
127+
// characters (typically commas) in values to be ignored.
135128
long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
136129
{
137130
bool isNegative = false;
@@ -160,15 +153,7 @@ long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
160153
return value;
161154
}
162155

163-
164156
// as parseInt but returns a floating point value
165-
float Stream::parseFloat(StreamParseOpt skipMode)
166-
{
167-
return parseFloat(skipMode, NO_SKIP_CHAR);
168-
}
169-
170-
// as above but the given skipChar is ignored
171-
// this allows format characters (typically commas) in values to be ignored
172157
float Stream::parseFloat(StreamParseOpt skipMode, char skipChar){
173158
bool isNegative = false;
174159
bool isFraction = false;

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

+16-10
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_SKIP_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(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR);
87+
// returns the first valid (long) integer value from the current position.
88+
// skipMode determines how parseInt looks ahead in the stream.
89+
// See StreamParseOpt 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, skipChar will be ignored in the stream.
8492

85-
long parseInt(StreamParseOpt skipMode = 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(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
93+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_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); }
@@ -103,11 +108,12 @@ class Stream : public Print
103108
String readStringUntil(char terminator);
104109

105110
protected:
106-
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
107-
// as above but the given skipChar is ignored
108-
// this allows format characters (typically commas) in values to be ignored
109111

110-
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
112+
long parseInt(char skipChar) { return parseInt(SKIP_ALL, skipChar); }
113+
float parseFloat(char skipChar) { return parseFloat(SKIP_ALL, skipChar); }
114+
// These overload exists for compatibility with any class that has derived
115+
// Stream and used parseFloat/Int with a custom skip character. To keep
116+
// the public API simple, these overload remains protected.
111117

112118
struct MultiTarget {
113119
const char *str; // string you're searching for
@@ -120,5 +126,5 @@ class Stream : public Print
120126
int findMulti(struct MultiTarget *targets, int tCount);
121127
};
122128

123-
129+
#undef NO_SKIP_CHAR
124130
#endif

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

+3-18
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()
@@ -54,7 +53,7 @@ int Stream::timedPeek()
5453

5554
// returns peek of the next digit in the stream or -1 if timeout
5655
// discards non-numeric characters
57-
int Stream::peekNextDigit(StreamParseOpt skipMode, bool detectDecimal )
56+
int Stream::peekNextDigit(StreamParseOpt skipMode, bool detectDecimal)
5857
{
5958
int c;
6059
while (1) {
@@ -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.
126124
// initial characters that are not digits (or the minus sign) are skipped
127125
// function is terminated by the first character that is not a digit.
128-
long Stream::parseInt(StreamParseOpt skipMode)
129-
{
130-
return parseInt(skipMode, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
131-
}
132-
133-
// as above but a given skipChar is ignored
134-
// this allows format characters (typically commas) in values to be ignored
126+
// If provided, skipChar is ignored while parsing. This allows format
127+
// characters (typically commas) in values to be ignored.
135128
long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
136129
{
137130
bool isNegative = false;
@@ -160,15 +153,7 @@ long Stream::parseInt(StreamParseOpt skipMode, char skipChar)
160153
return value;
161154
}
162155

163-
164156
// as parseInt but returns a floating point value
165-
float Stream::parseFloat(StreamParseOpt skipMode)
166-
{
167-
return parseFloat(skipMode, NO_SKIP_CHAR);
168-
}
169-
170-
// as above but the given skipChar is ignored
171-
// this allows format characters (typically commas) in values to be ignored
172157
float Stream::parseFloat(StreamParseOpt skipMode, char skipChar){
173158
bool isNegative = false;
174159
bool isFraction = false;

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

+17-10
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_SKIP_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(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR);
87+
// returns the first valid (long) integer value from the current position.
88+
// skipMode determines how parseInt looks ahead in the stream.
89+
// See StreamParseOpt 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, skipChar will be ignored in the stream.
8492

85-
long parseInt(StreamParseOpt skipMode = 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(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
93+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_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); }
@@ -103,11 +108,12 @@ class Stream : public Print
103108
String readStringUntil(char terminator);
104109

105110
protected:
106-
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
107-
// as above but the given skipChar is ignored
108-
// this allows format characters (typically commas) in values to be ignored
109111

110-
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
112+
long parseInt(char skipChar) { return parseInt(SKIP_ALL, skipChar); }
113+
float parseFloat(char skipChar) { return parseFloat(SKIP_ALL, skipChar); }
114+
// These overload exists for compatibility with any class that has derived
115+
// Stream and used parseFloat/Int with a custom skip character. To keep
116+
// the public API simple, these overload remains protected.
111117

112118
struct MultiTarget {
113119
const char *str; // string you're searching for
@@ -120,4 +126,5 @@ class Stream : public Print
120126
int findMulti(struct MultiTarget *targets, int tCount);
121127
};
122128

129+
#undef NO_SKIP_CHAR
123130
#endif

0 commit comments

Comments
 (0)