Skip to content

Commit 483519f

Browse files
authored
String: compatibility with 64 bits scalars (#7863)
time_t is now 64 bits. String(time_t) was ambiguous tests added
1 parent a886515 commit 483519f

File tree

7 files changed

+139
-4
lines changed

7 files changed

+139
-4
lines changed

Diff for: cores/esp8266/WString.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ String::String(unsigned long value, unsigned char base) {
9898
*this = buf;
9999
}
100100

101+
String::String(long long value) {
102+
init();
103+
char buf[2 + 8 * sizeof(long long)];
104+
sprintf(buf, "%lld", value);
105+
*this = buf;
106+
}
107+
108+
String::String(unsigned long long value) {
109+
init();
110+
char buf[1 + 8 * sizeof(unsigned long long)];
111+
sprintf(buf, "%llu", value);
112+
*this = buf;
113+
}
114+
115+
String::String(long long value, unsigned char base) {
116+
init();
117+
char buf[2 + 8 * sizeof(long long)];
118+
*this = lltoa(value, buf, sizeof(buf), base);
119+
}
120+
121+
String::String(unsigned long long value, unsigned char base) {
122+
init();
123+
char buf[1 + 8 * sizeof(unsigned long long)];
124+
*this = ulltoa(value, buf, sizeof(buf), base);
125+
}
126+
101127
String::String(float value, unsigned char decimalPlaces) {
102128
init();
103129
char buf[33];
@@ -313,6 +339,16 @@ unsigned char String::concat(unsigned long num) {
313339
return concat(buf, strlen(buf));
314340
}
315341

342+
unsigned char String::concat(long long num) {
343+
char buf[2 + 3 * sizeof(long long)];
344+
return concat(buf, sprintf(buf, "%lld", num));
345+
}
346+
347+
unsigned char String::concat(unsigned long long num) {
348+
char buf[1 + 3 * sizeof(unsigned long long)];
349+
return concat(buf, sprintf(buf, "%llu", num));
350+
}
351+
316352
unsigned char String::concat(float num) {
317353
char buf[20];
318354
char *string = dtostrf(num, 4, 2, buf);

Diff for: cores/esp8266/WString.h

+22
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class String {
7272
explicit String(unsigned int, unsigned char base = 10);
7373
explicit String(long, unsigned char base = 10);
7474
explicit String(unsigned long, unsigned char base = 10);
75+
explicit String(long long /* base 10 */);
76+
explicit String(long long, unsigned char base);
77+
explicit String(unsigned long long /* base 10 */);
78+
explicit String(unsigned long long, unsigned char base);
7579
explicit String(float, unsigned char decimalPlaces = 2);
7680
explicit String(double, unsigned char decimalPlaces = 2);
7781
~String() {
@@ -117,6 +121,8 @@ class String {
117121
unsigned char concat(unsigned int num);
118122
unsigned char concat(long num);
119123
unsigned char concat(unsigned long num);
124+
unsigned char concat(long long num);
125+
unsigned char concat(unsigned long long num);
120126
unsigned char concat(float num);
121127
unsigned char concat(double num);
122128
unsigned char concat(const __FlashStringHelper *str);
@@ -156,6 +162,14 @@ class String {
156162
concat(num);
157163
return *this;
158164
}
165+
String &operator +=(long long num) {
166+
concat(num);
167+
return *this;
168+
}
169+
String &operator +=(unsigned long long num) {
170+
concat(num);
171+
return *this;
172+
}
159173
String &operator +=(float num) {
160174
concat(num);
161175
return *this;
@@ -177,6 +191,8 @@ class String {
177191
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned int num);
178192
friend StringSumHelper &operator +(const StringSumHelper &lhs, long num);
179193
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long num);
194+
friend StringSumHelper &operator +(const StringSumHelper &lhs, long long num);
195+
friend StringSumHelper &operator +(const StringSumHelper &lhs, unsigned long long num);
180196
friend StringSumHelper &operator +(const StringSumHelper &lhs, float num);
181197
friend StringSumHelper &operator +(const StringSumHelper &lhs, double num);
182198
friend StringSumHelper &operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
@@ -375,6 +391,12 @@ class StringSumHelper: public String {
375391
StringSumHelper(unsigned long num) :
376392
String(num) {
377393
}
394+
StringSumHelper(long long num) :
395+
String(num) {
396+
}
397+
StringSumHelper(unsigned long long num) :
398+
String(num) {
399+
}
378400
StringSumHelper(float num) :
379401
String(num) {
380402
}

Diff for: cores/esp8266/debug.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols)
3030
while (len > 0)
3131
{
3232
uint32_t linesize = cols > len ? len : cols;
33-
os_printf("\n[%p] 0x%04x: ", src, src - (const char*)mem);
33+
os_printf("\n[%p] 0x%04x: ", src, (int)(src - (const char*)mem));
3434
for (uint32_t i = 0; i < linesize; i++)
3535
{
3636
os_printf("%02x ", *(src + i));

Diff for: cores/esp8266/stdlib_noniso.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
stdlib_noniso.h - nonstandard (but usefull) conversion functions
3+
4+
Copyright (c) 2021 David Gauchard. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#include "stdlib_noniso.h"
23+
24+
// ulltoa() is slower than std::to_char() (1.6 times)
25+
// but is smaller by ~800B/flash and ~250B/rodata
26+
27+
// ulltoa fills str backwards and can return a pointer different from str
28+
char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix)
29+
{
30+
str += --slen;
31+
*str = 0;
32+
do
33+
{
34+
auto mod = val % radix;
35+
val /= radix;
36+
*--str = mod + ((mod > 9) ? ('a' - 10) : '0');
37+
} while (--slen && val);
38+
return val? nullptr: str;
39+
}
40+
41+
// lltoa fills str backwards and can return a pointer different from str
42+
char* lltoa (long long val, char* str, int slen, unsigned int radix)
43+
{
44+
bool neg;
45+
if (val < 0)
46+
{
47+
val = -val;
48+
neg = true;
49+
}
50+
else
51+
{
52+
neg = false;
53+
}
54+
char* ret = ulltoa(val, str, slen, radix);
55+
if (neg)
56+
{
57+
if (ret == str || ret == nullptr)
58+
return nullptr;
59+
*--ret = '-';
60+
}
61+
return ret;
62+
}

Diff for: cores/esp8266/stdlib_noniso.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
stdlib_noniso.h - nonstandard (but usefull) conversion functions
33
44
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
55
This file is part of the esp8266 core for Arduino environment.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -36,10 +36,14 @@ char* itoa (int val, char *s, int radix);
3636

3737
char* ltoa (long val, char *s, int radix);
3838

39+
char* lltoa (long long val, char* str, int slen, unsigned int radix);
40+
3941
char* utoa (unsigned int val, char *s, int radix);
4042

4143
char* ultoa (unsigned long val, char *s, int radix);
42-
44+
45+
char* ulltoa (unsigned long long val, char* str, int slen, unsigned int radix);
46+
4347
char* dtostrf (double val, signed char width, unsigned char prec, char *s);
4448

4549
void reverse(char* begin, char* end);

Diff for: tests/host/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ CORE_CPP_FILES := \
8080
Stream.cpp \
8181
WString.cpp \
8282
Print.cpp \
83+
stdlib_noniso.cpp \
8384
FS.cpp \
8485
spiffs_api.cpp \
8586
MD5Builder.cpp \

Diff for: tests/host/core/test_string.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ TEST_CASE("String concantenation", "[core][String]")
132132
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.01");
133133
str += (double)1.01;
134134
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01");
135+
str += LLONG_MIN;
136+
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808");
137+
str += String(LLONG_MIN, 10);
138+
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808");
139+
str += LLONG_MAX;
140+
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-92233720368547758089223372036854775807");
141+
str += ULLONG_MAX;
142+
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808922337203685477580718446744073709551615");
143+
str += String(ULLONG_MAX, 16);
144+
REQUIRE(str == "abcdeabcde9872147483647-2147483648691969-123321-1.011.01-9223372036854775808-9223372036854775808922337203685477580718446744073709551615ffffffffffffffff");
135145
str = "clean";
136146
REQUIRE(str.concat(str) == true);
137147
REQUIRE(str == "cleanclean");

0 commit comments

Comments
 (0)