Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52e0dae

Browse filesBrowse files
committed
[core] sync upstream ArduinoCore-API
- Stream.cpp/.h - WString.cpp/.h - Binary.h - Print.cpp/.h tag: ArduinoCore-API 4a02bfc0a924e1fec34c3bb82ffd5dfba7643a0c
1 parent 2c35c7c commit 52e0dae
Copy full SHA for 52e0dae

File tree

Expand file treeCollapse file tree

10 files changed

+915
-764
lines changed
Filter options
Expand file treeCollapse file tree

10 files changed

+915
-764
lines changed

‎core/Arduino.h

Copy file name to clipboardExpand all lines: core/Arduino.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#include <stdint.h>
5757
#include <stdbool.h>
5858
#include <math.h>
59-
#include "binary.h"
59+
#include "Binary.h"
6060
#include "avr/io.h"
6161
#include "avr/stdlib.h"
6262
#include "avr/pgmspace.h"

‎core/Binary.h

Copy file name to clipboardExpand all lines: core/Binary.h
+552Lines changed: 552 additions & 0 deletions
Large diffs are not rendered by default.

‎core/Print.cpp

Copy file name to clipboardExpand all lines: core/Print.cpp
+123-3Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ size_t Print::print(unsigned long n, int base)
114114
else return printNumber(n, base);
115115
}
116116

117+
size_t Print::print(long long n, int base)
118+
{
119+
if (base == 0) {
120+
return write(n);
121+
} else if (base == 10) {
122+
if (n < 0) {
123+
int t = print('-');
124+
n = -n;
125+
return printULLNumber(n, 10) + t;
126+
}
127+
return printULLNumber(n, 10);
128+
} else {
129+
return printULLNumber(n, base);
130+
}
131+
}
132+
133+
size_t Print::print(unsigned long long n, int base)
134+
{
135+
if (base == 0) return write(n);
136+
else return printULLNumber(n, base);
137+
}
138+
117139
size_t Print::print(double n, int digits)
118140
{
119141
return printFloat(n, digits);
@@ -192,6 +214,20 @@ size_t Print::println(unsigned long num, int base)
192214
return n;
193215
}
194216

217+
size_t Print::println(long long num, int base)
218+
{
219+
size_t n = print(num, base);
220+
n += println();
221+
return n;
222+
}
223+
224+
size_t Print::println(unsigned long long num, int base)
225+
{
226+
size_t n = print(num, base);
227+
n += println();
228+
return n;
229+
}
230+
195231
size_t Print::println(double num, int digits)
196232
{
197233
size_t n = print(num, digits);
@@ -228,8 +264,92 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
228264
return write(str);
229265
}
230266

231-
size_t Print::printFloat(double number, uint8_t digits)
267+
// REFERENCE IMPLEMENTATION FOR ULL
268+
// size_t Print::printULLNumber(unsigned long long n, uint8_t base)
269+
// {
270+
// // if limited to base 10 and 16 the bufsize can be smaller
271+
// char buf[65];
272+
// char *str = &buf[64];
273+
274+
// *str = '\0';
275+
276+
// // prevent crash if called with base == 1
277+
// if (base < 2) base = 10;
278+
279+
// do {
280+
// unsigned long long t = n / base;
281+
// char c = n - t * base; // faster than c = n%base;
282+
// n = t;
283+
// *--str = c < 10 ? c + '0' : c + 'A' - 10;
284+
// } while(n);
285+
286+
// return write(str);
287+
// }
288+
289+
// FAST IMPLEMENTATION FOR ULL
290+
size_t Print::printULLNumber(unsigned long long n64, uint8_t base)
291+
{
292+
// if limited to base 10 and 16 the bufsize can be 20
293+
char buf[64];
294+
uint8_t i = 0;
295+
uint8_t innerLoops = 0;
296+
297+
// Special case workaround https://github.com/arduino/ArduinoCore-API/issues/178
298+
if (n64 == 0) {
299+
write('0');
300+
return 1;
301+
}
302+
303+
// prevent crash if called with base == 1
304+
if (base < 2) base = 10;
305+
306+
// process chunks that fit in "16 bit math".
307+
uint16_t top = 0xFFFF / base;
308+
uint16_t th16 = 1;
309+
while (th16 < top)
310+
{
311+
th16 *= base;
312+
innerLoops++;
313+
}
314+
315+
while (n64 > th16)
316+
{
317+
// 64 bit math part
318+
uint64_t q = n64 / th16;
319+
uint16_t r = n64 - q*th16;
320+
n64 = q;
321+
322+
// 16 bit math loop to do remainder. (note buffer is filled reverse)
323+
for (uint8_t j=0; j < innerLoops; j++)
324+
{
325+
uint16_t qq = r/base;
326+
buf[i++] = r - qq*base;
327+
r = qq;
328+
}
329+
}
330+
331+
uint16_t n16 = n64;
332+
while (n16 > 0)
333+
{
334+
uint16_t qq = n16/base;
335+
buf[i++] = n16 - qq*base;
336+
n16 = qq;
337+
}
338+
339+
size_t bytes = i;
340+
for (; i > 0; i--)
341+
write((char) (buf[i - 1] < 10 ?
342+
'0' + buf[i - 1] :
343+
'A' + buf[i - 1] - 10));
344+
345+
return bytes;
346+
}
347+
348+
size_t Print::printFloat(double number, int digits)
232349
{
350+
if (digits < 0)
351+
digits = 2;
352+
233353
size_t n = 0;
234354

235355
if (isnan(number)) return print("nan");
@@ -258,14 +378,14 @@ size_t Print::printFloat(double number, uint8_t digits)
258378

259379
// Print the decimal point, but only if there are digits beyond
260380
if (digits > 0) {
261-
n += print('.');
381+
n += print(".");
262382
}
263383

264384
// Extract digits from the remainder one at a time
265385
while (digits-- > 0)
266386
{
267387
remainder *= 10.0;
268-
unsigned int toPrint = (unsigned int)(remainder);
388+
unsigned int toPrint = (unsigned int)remainder;
269389
n += print(toPrint);
270390
remainder -= toPrint;
271391
}

‎core/Print.h

Copy file name to clipboardExpand all lines: core/Print.h
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class Print
4848
private:
4949
int write_error;
5050
size_t printNumber(unsigned long, uint8_t);
51-
size_t printFloat(double, uint8_t);
51+
size_t printULLNumber(unsigned long long, uint8_t);
52+
size_t printFloat(double, int);
5253
protected:
5354
void setWriteError(int err = 1) { write_error = err; }
5455
public:
@@ -57,7 +58,7 @@ class Print
5758
int getWriteError() { return write_error; }
5859
void clearWriteError() { setWriteError(0); }
5960

60-
virtual size_t write(uint8_t c) = 0;
61+
virtual size_t write(uint8_t) = 0;
6162
size_t write(const char *str) {
6263
if (str == NULL) return 0;
6364
return write((const uint8_t *)str, strlen(str));
@@ -80,6 +81,8 @@ class Print
8081
size_t print(unsigned int, int = DEC);
8182
size_t print(long, int = DEC);
8283
size_t print(unsigned long, int = DEC);
84+
size_t print(long long, int = DEC);
85+
size_t print(unsigned long long, int = DEC);
8386
size_t print(double, int = 2);
8487
size_t print(const Printable&);
8588

@@ -92,6 +95,8 @@ class Print
9295
size_t println(unsigned int, int = DEC);
9396
size_t println(long, int = DEC);
9497
size_t println(unsigned long, int = DEC);
98+
size_t println(long long, int = DEC);
99+
size_t println(unsigned long long, int = DEC);
95100
size_t println(double, int = 2);
96101
size_t println(const Printable&);
97102
size_t println(void);

‎core/Stream.cpp

Copy file name to clipboardExpand all lines: core/Stream.cpp
+26-27Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ int Stream::timedRead(void)
4545
do {
4646
c = read();
4747
if(c >= 0) return c;
48-
if(_timeout == 0) return -1;
49-
yield();
48+
//yield(); //cherry-pick from esp8266 arduino 55f07f1e08868b855f2ae2fb3b7b42004ff8022f
5049
} while(millis() - _startMillis < _timeout);
5150
return -1; // -1 indicates timeout
5251
}
@@ -59,8 +58,7 @@ int Stream::timedPeek(void)
5958
do {
6059
c = peek();
6160
if(c >= 0) return c;
62-
if(_timeout == 0) return -1;
63-
yield();
61+
//yield(); //cherry-pick from esp8266 arduino 55f07f1e08868b855f2ae2fb3b7b42004ff8022f
6462
} while(millis() - _startMillis < _timeout);
6563
return -1; // -1 indicates timeout
6664
}
@@ -104,35 +102,35 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
104102
}
105103

106104
// find returns true if the target string is found
107-
bool Stream::find(char *target)
105+
bool Stream::find(const char *target)
108106
{
109107
return findUntil(target, strlen(target), NULL, 0);
110108
}
111109

112110
// reads data from the stream until the target string of given length is found
113111
// returns true if target string is found, false if timed out
114-
bool Stream::find(char *target, size_t length)
112+
bool Stream::find(const char *target, size_t length)
115113
{
116114
return findUntil(target, length, NULL, 0);
117115
}
118116

119117
// as find but search ends if the terminator string is found
120-
bool Stream::findUntil(char *target, char *terminator)
118+
bool Stream::findUntil(const char *target, const char *terminator)
121119
{
122120
return findUntil(target, strlen(target), terminator, strlen(terminator));
123121
}
124122

125123
// reads data from the stream until the target string of the given length is found
126124
// search terminated if the terminator string is found
127125
// returns true if target string is found, false if terminated or timed out
128-
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
126+
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
129127
{
130128
if (terminator == NULL) {
131129
MultiTarget t[1] = {{target, targetLen, 0}};
132-
return findMulti(t, 1) == 0 ? true : false;
130+
return findMulti(t, 1) == 0;
133131
} else {
134132
MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
135-
return findMulti(t, 2) == 0 ? true : false;
133+
return findMulti(t, 2) == 0;
136134
}
137135
}
138136

@@ -153,7 +151,7 @@ long Stream::parseInt(LookaheadMode lookahead, char ignore)
153151
return 0; // zero returned if timeout
154152

155153
do{
156-
if(c == ignore)
154+
if((char)c == ignore)
157155
; // ignore this character
158156
else if(c == '-')
159157
isNegative = true;
@@ -162,7 +160,7 @@ long Stream::parseInt(LookaheadMode lookahead, char ignore)
162160
read(); // consume the character we got with peek
163161
c = timedPeek();
164162
}
165-
while( (c >= '0' && c <= '9') || c == ignore );
163+
while( (c >= '0' && c <= '9') || (char)c == ignore );
166164

167165
if(isNegative)
168166
value = -value;
@@ -174,38 +172,39 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
174172
{
175173
bool isNegative = false;
176174
bool isFraction = false;
177-
long value = 0;
175+
double value = 0.0;
178176
int c;
179-
float fraction = 1.0;
177+
double fraction = 1.0;
180178

181179
c = peekNextDigit(lookahead, true);
182180
// ignore non numeric leading characters
183181
if(c < 0)
184182
return 0; // zero returned if timeout
185183

186184
do{
187-
if(c == ignore)
185+
if((char)c == ignore)
188186
; // ignore
189187
else if(c == '-')
190188
isNegative = true;
191189
else if (c == '.')
192190
isFraction = true;
193191
else if(c >= '0' && c <= '9') { // is c a digit?
194-
value = value * 10 + c - '0';
195-
if(isFraction)
196-
fraction *= 0.1f;
192+
if(isFraction) {
193+
fraction *= 0.1;
194+
value = value + fraction * (c - '0');
195+
} else {
196+
value = value * 10 + c - '0';
197+
}
197198
}
198199
read(); // consume the character we got with peek
199200
c = timedPeek();
200201
}
201-
while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore );
202+
while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || (char)c == ignore );
202203

203204
if(isNegative)
204205
value = -value;
205-
if(isFraction)
206-
return value * fraction;
207-
else
208-
return value;
206+
207+
return value;
209208
}
210209

211210
// read characters from stream into buffer
@@ -235,7 +234,7 @@ size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
235234
size_t index = 0;
236235
while (index < length) {
237236
int c = timedRead();
238-
if (c < 0 || c == terminator) break;
237+
if (c < 0 || (char)c == terminator) break;
239238
*buffer++ = (char)c;
240239
index++;
241240
}
@@ -258,7 +257,7 @@ String Stream::readStringUntil(char terminator)
258257
{
259258
String ret;
260259
int c = timedRead();
261-
while (c >= 0 && c != terminator)
260+
while (c >= 0 && (char)c != terminator)
262261
{
263262
ret += (char)c;
264263
c = timedRead();
@@ -281,7 +280,7 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
281280

282281
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
283282
// the simple case is if we match, deal with that first.
284-
if (c == t->str[t->index]) {
283+
if ((char)c == t->str[t->index]) {
285284
if (++t->index == t->len)
286285
return t - targets;
287286
else
@@ -299,7 +298,7 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
299298
do {
300299
--t->index;
301300
// first check if current char works against the new current index
302-
if (c != t->str[t->index])
301+
if ((char)c != t->str[t->index])
303302
continue;
304303

305304
// if it's the only char then we're good, nothing more to check

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.