From e624b56ca9a1cae89a852f32700380d4875ead53 Mon Sep 17 00:00:00 2001 From: Arian Rezazadeh Date: Sat, 28 Oct 2023 12:30:33 +0330 Subject: [PATCH 1/2] add readStringUntil function with string terminator --- cores/esp8266/Stream.cpp | 26 ++++++++++++++++++++++++++ cores/esp8266/Stream.h | 1 + 2 files changed, 27 insertions(+) diff --git a/cores/esp8266/Stream.cpp b/cores/esp8266/Stream.cpp index a901c8d437..2218314fdd 100644 --- a/cores/esp8266/Stream.cpp +++ b/cores/esp8266/Stream.cpp @@ -262,6 +262,32 @@ String Stream::readStringUntil(char terminator) { return ret; } +String Stream::readStringUntil(const char* terminator, uint32_t count) { + String ret; + int c; + uint32_t termCount = 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 && ++termCount == count) { + // 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) diff --git a/cores/esp8266/Stream.h b/cores/esp8266/Stream.h index f39bb423f2..024c3ae037 100644 --- a/cores/esp8266/Stream.h +++ b/cores/esp8266/Stream.h @@ -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 count = 1); virtual int read (uint8_t* buffer, size_t len); int read (char* buffer, size_t len) { return read((uint8_t*)buffer, len); } From 6a1cea15b288b26f3c267118219a7f7e3840d15a Mon Sep 17 00:00:00 2001 From: Arian Rezazadeh Date: Tue, 7 Nov 2023 06:59:53 +0330 Subject: [PATCH 2/2] rename count parameter to untilTotalNumberOfOccurrences --- cores/esp8266/Stream.cpp | 6 +++--- cores/esp8266/Stream.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/Stream.cpp b/cores/esp8266/Stream.cpp index 2218314fdd..b9b5b95f65 100644 --- a/cores/esp8266/Stream.cpp +++ b/cores/esp8266/Stream.cpp @@ -262,10 +262,10 @@ String Stream::readStringUntil(char terminator) { return ret; } -String Stream::readStringUntil(const char* terminator, uint32_t count) { +String Stream::readStringUntil(const char* terminator, uint32_t untilTotalNumberOfOccurrences) { String ret; int c; - uint32_t termCount = 0; + uint32_t occurrences = 0; size_t termLen = strlen(terminator); size_t termIndex = 0; size_t index = 0; @@ -275,7 +275,7 @@ String Stream::readStringUntil(const char* terminator, uint32_t count) { index++; if (terminator[termIndex] == c) { - if (++termIndex == termLen && ++termCount == count) { + if (++termIndex == termLen && ++occurrences == untilTotalNumberOfOccurrences) { // don't include terminator in returned string ret.remove(index - termIndex, termLen); break; diff --git a/cores/esp8266/Stream.h b/cores/esp8266/Stream.h index 024c3ae037..21f319ee6b 100644 --- a/cores/esp8266/Stream.h +++ b/cores/esp8266/Stream.h @@ -115,7 +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 count = 1); + 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); }