Skip to content

Commit 5d8c994

Browse files
d-a-vhasenradball
authored andcommitted
Stream: +helpers to stream regular String (esp8266#9043)
This allows to use a String as a destination Stream by using a temporary with streaming helpers.
1 parent 47b631d commit 5d8c994

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

Diff for: cores/esp8266/Stream.h

+4
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,25 @@ class Stream: public Print {
196196
// returns number of transferred bytes
197197
size_t sendAvailable (Stream* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
198198
size_t sendAvailable (Stream& to) { return sendAvailable(&to); }
199+
size_t sendAvailable (Stream&& to) { return sendAvailable(&to); }
199200

200201
// transfers data until timeout
201202
// returns number of transferred bytes
202203
size_t sendAll (Stream* to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, -1, timeoutMs); }
203204
size_t sendAll (Stream& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
205+
size_t sendAll (Stream&& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
204206

205207
// transfers data until a char is encountered (the char is swallowed but not transferred) with timeout
206208
// returns number of transferred bytes
207209
size_t sendUntil (Stream* to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, readUntilChar, timeoutMs); }
208210
size_t sendUntil (Stream& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
211+
size_t sendUntil (Stream&& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
209212

210213
// transfers data until requested size or timeout
211214
// returns number of transferred bytes
212215
size_t sendSize (Stream* to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, maxLen, -1, timeoutMs); }
213216
size_t sendSize (Stream& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
217+
size_t sendSize (Stream&& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
214218

215219
// remaining size (-1 by default = unknown)
216220
virtual ssize_t streamRemaining () { return -1; }

Diff for: cores/esp8266/StreamString.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "WString.h"
3030

3131
///////////////////////////////////////////////////////////////
32-
// S2Stream points to a String and makes it a Stream
32+
// S2Stream ("String to Stream") points to a String and makes it a Stream
3333
// (it is also the helper for StreamString)
3434

3535
class S2Stream: public Stream
@@ -184,26 +184,26 @@ class S2Stream: public Stream
184184
return peekPointer < 0 ? string->length() : string->length() - peekPointer;
185185
}
186186

187-
// calling setConsume() will consume bytes as the stream is read
188-
// (enabled by default)
187+
// calling setConsume() will make the string consumed as the stream is read.
188+
// (default behaviour)
189189
void setConsume()
190190
{
191191
peekPointer = -1;
192192
}
193193

194-
// Reading this stream will mark the string as read without consuming
195-
// (not enabled by default)
196-
// Calling resetPointer() resets the read state and allows rereading.
197-
void resetPointer(int pointer = 0)
194+
// Calling resetPointer() resets the read cursor and allows rereading.
195+
// (this is the opposite of default mode set by setConsume())
196+
void resetPointer(size_t pointer = 0)
198197
{
199-
peekPointer = pointer;
198+
peekPointer = std::min(pointer, (size_t)string->length());
200199
}
201200

202201
protected:
203202
String* string;
204203
int peekPointer; // -1:String is consumed / >=0:resettable pointer
205204
};
206205

206+
///////////////////////////////////////////////////////////////
207207
// StreamString is a S2Stream holding the String
208208

209209
class StreamString: public String, public S2Stream

Diff for: libraries/esp8266/examples/StreamString/StreamString.ino

+16-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void testStreamString() {
5454
{
5555
// We use a a lighter StreamConstPtr(input) to make a read-only Stream out of
5656
// a String that obviously should not be modified during the time the
57-
// StreamConstPtr instance is used. It is used as a source to be sent to
57+
// StreamConstPtr instance is used. It is used as a read-only source to be sent to
5858
// 'result'.
5959

6060
result.clear();
@@ -77,7 +77,7 @@ void testStreamString() {
7777
// Now inputString is made into a Stream using S2Stream,
7878
// and set in non-consume mode (using ::resetPointer()).
7979

80-
// Then, after that input is read once, it won't be anymore readable
80+
// Then, after input is read once, it won't be anymore readable
8181
// until the pointer is reset.
8282

8383
S2Stream input(inputString);
@@ -87,7 +87,7 @@ void testStreamString() {
8787
input.sendAll(result);
8888
input.sendAll(result);
8989
check("S2Stream.sendAll(StreamString)", result.c_str(), "hello");
90-
check("unmodified String given to S2Stream", inputString.c_str(), "hello");
90+
check("String given to S2Stream is unmodified", inputString.c_str(), "hello");
9191
}
9292

9393
{
@@ -103,6 +103,17 @@ void testStreamString() {
103103
check("S2Stream.resetPointer(2):", result.c_str(), "llo");
104104
}
105105

106+
{
107+
// Streaming to a regular String
108+
109+
String someSource{ F("hello") };
110+
String someDestString;
111+
112+
StreamConstPtr(someSource).sendAll(S2Stream(someDestString));
113+
StreamConstPtr(someSource).sendAll(S2Stream(someDestString));
114+
check("StreamConstPtr(someSource).sendAll(S2Stream(someDestString))", someDestString.c_str(), "hellohello");
115+
}
116+
106117
{
107118
// inputString made into a Stream
108119
// reading the Stream consumes the String
@@ -181,7 +192,8 @@ void setup() {
181192

182193
testStreamString();
183194

184-
Serial.printf("sizeof: String:%d Stream:%d StreamString:%d SStream:%d\n", (int)sizeof(String), (int)sizeof(Stream), (int)sizeof(StreamString), (int)sizeof(S2Stream));
195+
Serial.printf("sizeof: String:%zu Stream:%zu StreamString:%zu S2Stream:%zu StreamConstPtr:%zu\n",
196+
sizeof(String), sizeof(Stream), sizeof(StreamString), sizeof(S2Stream), sizeof(StreamConstPtr));
185197
}
186198

187199
#endif

0 commit comments

Comments
 (0)