forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamString.ino
199 lines (160 loc) · 5.65 KB
/
StreamString.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// this example sketch in the public domain is also a host and device test
#include <StreamDev.h>
#include <StreamString.h>
void loop() {
delay(1000);
}
void checksketch(const char* what, const char* res1, const char* res2) {
if (strcmp(res1, res2) == 0) {
Serial << "PASSED: Test " << what << " (result: '" << res1 << "')\n";
} else {
Serial << "FAILED: Test " << what << ": '" << res1 << "' <> '" << res2 << "' !\n";
}
}
#ifndef check
#define check(what, res1, res2) checksketch(what, res1, res2)
#endif
void testStringPtrProgmem() {
static const char inProgmem[] PROGMEM = "I am in progmem";
auto inProgmem2 = F("I am too in progmem");
int heap = (int)ESP.getFreeHeap();
auto stream1 = StreamConstPtr(inProgmem, sizeof(inProgmem) - 1);
auto stream2 = StreamConstPtr(inProgmem2);
Serial << stream1 << " - " << stream2 << "\n";
heap -= (int)ESP.getFreeHeap();
check("NO heap occupation while streaming progmem strings", String(heap).c_str(), "0");
}
void testStreamString() {
String inputString = "hello";
StreamString result;
// By default, reading a S2Stream(String) or a StreamString will consume the String.
// It can be disabled by calling ::resetPointer(), (not default)
// and re-enabled by calling ::setConsume(). (default)
//
// In default consume mode, reading a byte or a block will remove it from
// the String. Operations are O(n²).
//
// In non-default non-consume mode, it will just move a pointer. That one
// can be ::resetPointer(pos) anytime. See the example below.
// The String included in 'result' will not be modified by read:
// (this is not the default)
result.resetPointer();
{
// We use a a lighter StreamConstPtr(input) to make a read-only Stream out of
// a String that obviously should not be modified during the time the
// StreamConstPtr instance is used. It is used as a read-only source to be sent to
// 'result'.
result.clear();
StreamConstPtr(inputString).sendAll(result);
StreamConstPtr(inputString).sendAll(result);
StreamConstPtr(inputString).sendAll(result);
check("StreamConstPtr.sendAll(StreamString)", result.c_str(), "hellohellohello");
}
{
// equivalent of the above
result.clear();
result << inputString;
result << inputString << inputString;
check("StreamString<<String", result.c_str(), "hellohellohello");
}
{
// Now inputString is made into a Stream using S2Stream,
// and set in non-consume mode (using ::resetPointer()).
// Then, after input is read once, it won't be anymore readable
// until the pointer is reset.
S2Stream input(inputString);
input.resetPointer();
result.clear();
input.sendAll(result);
input.sendAll(result);
check("S2Stream.sendAll(StreamString)", result.c_str(), "hello");
check("String given to S2Stream is unmodified", inputString.c_str(), "hello");
}
{
// Same as above, with an offset
result.clear();
S2Stream input(inputString);
// stream position set to offset 2 (0 by default)
input.resetPointer(2);
input.sendAll(result);
input.sendAll(result);
check("S2Stream.resetPointer(2):", result.c_str(), "llo");
}
{
// Streaming to a regular String
String someSource{ F("hello") };
String someDestString;
StreamConstPtr(someSource).sendAll(S2Stream(someDestString));
StreamConstPtr(someSource).sendAll(S2Stream(someDestString));
check("StreamConstPtr(someSource).sendAll(S2Stream(someDestString))", someDestString.c_str(), "hellohello");
}
{
// inputString made into a Stream
// reading the Stream consumes the String
result.clear();
S2Stream input(inputString);
// reading stream will consume the string
input.setConsume(); // can be omitted, this is the default
input.sendSize(result, 1);
input.sendSize(result, 2);
check("setConsume(): S2Stream().sendSize(StreamString,3)", result.c_str(), "hel");
check("setConsume(): String given from S2Stream is swallowed", inputString.c_str(), "lo");
}
// Streaming with common String constructors
{
StreamString cons(inputString);
check("StreamString(String)", cons.c_str(), inputString.c_str());
}
{
StreamString cons(result);
check("StreamString(char*)", cons.c_str(), result.c_str());
}
{
StreamString cons("abc");
check("StreamString(char*)", cons.c_str(), "abc");
}
{
StreamString cons(F("abc"));
check("StreamString(F())", cons.c_str(), "abc");
}
{
StreamString cons(23);
check("StreamString(int)", cons.c_str(), "23");
}
{
StreamString cons('a');
check("StreamString(char)", cons.c_str(), "a");
}
{
StreamString cons(23.2);
check("StreamString(float)", cons.c_str(), "23.20");
}
#if !CORE_MOCK
// A progmem won't use Heap when StringPtr is used
testStringPtrProgmem();
// .. but it does when S2Stream or StreamString is used
{
int heap = (int)ESP.getFreeHeap();
auto stream = StreamString(F("I am in progmem"));
Serial << stream << "\n";
heap -= (int)ESP.getFreeHeap();
String heapStr(heap);
if (heap != 0) {
check("heap is occupied by String/StreamString(progmem)", heapStr.c_str(), heapStr.c_str());
} else {
check("ERROR: heap should be occupied by String/StreamString(progmem)", heapStr.c_str(), "-1");
}
}
// (check again to be sure)
testStringPtrProgmem();
#endif
}
#ifndef TEST_CASE
void setup() {
Serial.begin(115200);
delay(1000);
testStreamString();
Serial.printf("sizeof: String:%zu Stream:%zu StreamString:%zu S2Stream:%zu StreamConstPtr:%zu\n",
sizeof(String), sizeof(Stream), sizeof(StreamString), sizeof(S2Stream), sizeof(StreamConstPtr));
}
#endif