Skip to content

Commit 94b91bd

Browse files
committed
Allow access to full parseInt/Float implementation.
This commit allows users to access the full versions of parseInt/Float as the previous implementation had some overloads marked as protected. Ultimately this will allow someone to correctly parse a formatted string like '-1,234.567' correctly using parseFloat.
1 parent 94007e2 commit 94b91bd

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
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. This allows format characters
127+
// (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

+8-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum StreamParseOpt{
4141
SKIP_WHITESPACE
4242
};
4343

44+
#define NO_SKIP_CHAR '\x01' // a magic char not found in a valid ASCII numeric field
45+
4446
class Stream : public Print
4547
{
4648
protected:
@@ -79,11 +81,12 @@ class Stream : public Print
7981
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
8082

8183

82-
long parseInt(StreamParseOpt skipMode = SKIP_ALL); // returns the first valid (long) integer value from the current position.
83-
// initial characters that are not digits (or the minus sign) are skipped
84-
// integer is terminated by the first character that is not a digit.
84+
long parseInt(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR); // returns the first valid (long) integer value from the current position.
85+
// skipMode determines how parseInt looks ahead in the stream.
86+
// See StreamParseOpt enumeration at the top of the file.
87+
// Lookahead is terminated by the first character that is not a valid part of an integer.
8588

86-
float parseFloat(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
89+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR); // float version of parseInt
8790

8891
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
8992
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -100,11 +103,6 @@ class Stream : public Print
100103
String readStringUntil(char terminator);
101104

102105
protected:
103-
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
104-
// as above but the given skipChar is ignored
105-
// this allows format characters (typically commas) in values to be ignored
106-
107-
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
108106

109107
struct MultiTarget {
110108
const char *str; // string you're searching for
@@ -117,5 +115,5 @@ class Stream : public Print
117115
int findMulti(struct MultiTarget *targets, int tCount);
118116
};
119117

120-
118+
#undef NO_SKIP_CHAR
121119
#endif

hardware/arduino/sam/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. This allows format characters
127+
// (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

+8-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum StreamParseOpt{
4141
SKIP_WHITESPACE
4242
};
4343

44+
#define NO_SKIP_CHAR '\x01' // a magic char not found in a valid ASCII numeric field
45+
4446
class Stream : public Print
4547
{
4648
protected:
@@ -79,11 +81,12 @@ class Stream : public Print
7981
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
8082

8183

82-
long parseInt(StreamParseOpt skipMode = SKIP_ALL); // returns the first valid (long) integer value from the current position.
83-
// initial characters that are not digits (or the minus sign) are skipped
84-
// integer is terminated by the first character that is not a digit.
84+
long parseInt(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR); // returns the first valid (long) integer value from the current position.
85+
// skipMode determines how parseInt looks ahead in the stream.
86+
// See StreamParseOpt enumeration at the top of the file.
87+
// Lookahead is terminated by the first character that is not a valid part of an integer.
8588

86-
float parseFloat(StreamParseOpt skipMode = SKIP_ALL); // float version of parseInt
89+
float parseFloat(StreamParseOpt skipMode = SKIP_ALL, char skipChar = NO_SKIP_CHAR); // float version of parseInt
8790

8891
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
8992
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@@ -100,11 +103,6 @@ class Stream : public Print
100103
String readStringUntil(char terminator);
101104

102105
protected:
103-
long parseInt(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
104-
// as above but the given skipChar is ignored
105-
// this allows format characters (typically commas) in values to be ignored
106-
107-
float parseFloat(StreamParseOpt skipMode, char skipChar); // as above but the given skipChar is ignored
108106

109107
struct MultiTarget {
110108
const char *str; // string you're searching for
@@ -117,4 +115,5 @@ class Stream : public Print
117115
int findMulti(struct MultiTarget *targets, int tCount);
118116
};
119117

118+
#undef NO_SKIP_CHAR
120119
#endif

0 commit comments

Comments
 (0)