1
0
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:
AriaN 2023-11-07 16:49:31 +03:30 committed by GitHub
parent fb8d6d668d
commit 31c1592ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -262,6 +262,32 @@ String Stream::readStringUntil(char terminator) {
return ret; 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 // read what can be read, immediate exit on unavailable data
// prototype similar to Arduino's `int Client::read(buf, len)` // prototype similar to Arduino's `int Client::read(buf, len)`
int Stream::read (uint8_t* buffer, size_t maxLen) int Stream::read (uint8_t* buffer, size_t maxLen)

View File

@ -115,6 +115,7 @@ class Stream: public Print {
// Arduino String functions to be added here // Arduino String functions to be added here
virtual String readString(); virtual String readString();
String readStringUntil(char terminator); String readStringUntil(char terminator);
String readStringUntil(const char* terminator, uint32_t untilTotalNumberOfOccurrences = 1);
virtual int read (uint8_t* buffer, size_t len); virtual int read (uint8_t* buffer, size_t len);
int read (char* buffer, size_t len) { return read((uint8_t*)buffer, len); } int read (char* buffer, size_t len) { return read((uint8_t*)buffer, len); }