mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
add Stream::readStringUntil function that uses string terminator (#9011)
* add readStringUntil function with string terminator * rename count parameter to untilTotalNumberOfOccurrences
This commit is contained in:
parent
fb8d6d668d
commit
31c1592ad6
@ -262,6 +262,32 @@ String Stream::readStringUntil(char terminator) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
String Stream::readStringUntil(const char* terminator, uint32_t untilTotalNumberOfOccurrences) {
|
||||
String ret;
|
||||
int c;
|
||||
uint32_t occurrences = 0;
|
||||
size_t termLen = strlen(terminator);
|
||||
size_t termIndex = 0;
|
||||
size_t index = 0;
|
||||
|
||||
while ((c = timedRead()) > 0) {
|
||||
ret += (char) c;
|
||||
index++;
|
||||
|
||||
if (terminator[termIndex] == c) {
|
||||
if (++termIndex == termLen && ++occurrences == untilTotalNumberOfOccurrences) {
|
||||
// don't include terminator in returned string
|
||||
ret.remove(index - termIndex, termLen);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
termIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// read what can be read, immediate exit on unavailable data
|
||||
// prototype similar to Arduino's `int Client::read(buf, len)`
|
||||
int Stream::read (uint8_t* buffer, size_t maxLen)
|
||||
|
@ -115,6 +115,7 @@ class Stream: public Print {
|
||||
// Arduino String functions to be added here
|
||||
virtual String readString();
|
||||
String readStringUntil(char terminator);
|
||||
String readStringUntil(const char* terminator, uint32_t untilTotalNumberOfOccurrences = 1);
|
||||
|
||||
virtual int read (uint8_t* buffer, size_t len);
|
||||
int read (char* buffer, size_t len) { return read((uint8_t*)buffer, len); }
|
||||
|
Loading…
x
Reference in New Issue
Block a user