Skip to content

Commit a777245

Browse files
Update WString.h
WString Fix int64_t Fixed int64_t String support. Resolves issue espressif#7760. Background: sprintf on esp32 doesn't support "%lld" parameter. It's possible to recompile the underlying libraries to add that option, but I have an easier solution. This has already been solved in ESP8266 version of WString by replacing sprintf() with itoa/ltoa/lltoa. This PR does the following: Fixes integer print issues by replacing sprintf() with itoa/ltoa/lltoa Moves concat(long long num), concat(unsigned long long num) location (match ESP8266) Cleans up code formatting (matches ESP8266)
1 parent 23d715a commit a777245

File tree

2 files changed

+48
-60
lines changed

2 files changed

+48
-60
lines changed

Diff for: cores/esp32/WString.cpp

+40-53
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222
*/
2323

24-
#include <Arduino.h>
24+
#include "Arduino.h"
2525
#include "WString.h"
2626
#include "stdlib_noniso.h"
2727
#include "esp32-hal-log.h"
@@ -80,11 +80,7 @@ String::String(unsigned char value, unsigned char base) {
8080
String::String(int value, unsigned char base) {
8181
init();
8282
char buf[2 + 8 * sizeof(int)];
83-
if (base == 10) {
84-
sprintf(buf, "%d", value);
85-
} else {
86-
itoa(value, buf, base);
87-
}
83+
itoa(value, buf, base);
8884
*this = buf;
8985
}
9086

@@ -98,11 +94,7 @@ String::String(unsigned int value, unsigned char base) {
9894
String::String(long value, unsigned char base) {
9995
init();
10096
char buf[2 + 8 * sizeof(long)];
101-
if (base==10) {
102-
sprintf(buf, "%ld", value);
103-
} else {
104-
ltoa(value, buf, base);
105-
}
97+
ltoa(value, buf, base);
10698
*this = buf;
10799
}
108100

@@ -140,11 +132,7 @@ String::String(double value, unsigned int decimalPlaces) {
140132
String::String(long long value, unsigned char base) {
141133
init();
142134
char buf[2 + 8 * sizeof(long long)];
143-
if (base==10) {
144-
sprintf(buf, "%lld", value); // NOT SURE - NewLib Nano ... does it support %lld?
145-
} else {
146-
lltoa(value, buf, base);
147-
}
135+
lltoa(value, buf, base);
148136
*this = buf;
149137
}
150138

@@ -159,9 +147,9 @@ String::~String() {
159147
invalidate();
160148
}
161149

162-
// /*********************************************/
163-
// /* Memory Management */
164-
// /*********************************************/
150+
/*********************************************/
151+
/* Memory Management */
152+
/*********************************************/
165153

166154
inline void String::init(void) {
167155
setSSO(false);
@@ -222,8 +210,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
222210
// Copy the SSO buffer into allocated space
223211
memmove(newbuffer, sso.buff, sizeof(sso.buff));
224212
}
225-
if (newSize > oldSize)
226-
{
213+
if (newSize > oldSize) {
227214
memset(newbuffer + oldSize, 0, newSize - oldSize);
228215
}
229216
setSSO(false);
@@ -235,9 +222,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
235222
return 0;
236223
}
237224

238-
// /*********************************************/
239-
// /* Copy and Move */
240-
// /*********************************************/
225+
/*********************************************/
226+
/* Copy and Move */
227+
/*********************************************/
241228

242229
String & String::copy(const char *cstr, unsigned int length) {
243230
if(!reserve(length)) {
@@ -293,12 +280,10 @@ void String::move(String &rhs) {
293280
String & String::operator =(const String &rhs) {
294281
if(this == &rhs)
295282
return *this;
296-
297283
if(rhs.buffer())
298284
copy(rhs.buffer(), rhs.len());
299285
else
300286
invalidate();
301-
302287
return *this;
303288
}
304289

@@ -321,7 +306,6 @@ String & String::operator =(const char *cstr) {
321306
copy(cstr, strlen(cstr));
322307
else
323308
invalidate();
324-
325309
return *this;
326310
}
327311

@@ -334,9 +318,9 @@ String & String::operator =(const __FlashStringHelper *pstr) {
334318
return *this;
335319
}
336320

337-
// /*********************************************/
338-
// /* concat */
339-
// /*********************************************/
321+
/*********************************************/
322+
/* concat */
323+
/*********************************************/
340324

341325
unsigned char String::concat(const String &s) {
342326
// Special case if we're concatting ourself (s += s;) since we may end up
@@ -389,12 +373,14 @@ unsigned char String::concat(char c) {
389373

390374
unsigned char String::concat(unsigned char num) {
391375
char buf[1 + 3 * sizeof(unsigned char)];
392-
return concat(buf, sprintf(buf, "%d", num));
376+
utoa(num, buf, 10);
377+
return concat(buf, strlen(buf));
393378
}
394379

395380
unsigned char String::concat(int num) {
396381
char buf[2 + 3 * sizeof(int)];
397-
return concat(buf, sprintf(buf, "%d", num));
382+
itoa(num, buf, 10);
383+
return concat(buf, strlen(buf));
398384
}
399385

400386
unsigned char String::concat(unsigned int num) {
@@ -405,7 +391,8 @@ unsigned char String::concat(unsigned int num) {
405391

406392
unsigned char String::concat(long num) {
407393
char buf[2 + 3 * sizeof(long)];
408-
return concat(buf, sprintf(buf, "%ld", num));
394+
ltoa(num, buf, 10);
395+
return concat(buf, strlen(buf));
409396
}
410397

411398
unsigned char String::concat(unsigned long num) {
@@ -428,7 +415,8 @@ unsigned char String::concat(double num) {
428415

429416
unsigned char String::concat(long long num) {
430417
char buf[2 + 3 * sizeof(long long)];
431-
return concat(buf, sprintf(buf, "%lld", num)); // NOT SURE - NewLib Nano ... does it support %lld?
418+
lltoa(num, buf, 10);
419+
return concat(buf, strlen(buf));
432420
}
433421

434422
unsigned char String::concat(unsigned long long num) {
@@ -544,9 +532,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
544532
return a;
545533
}
546534

547-
// /*********************************************/
548-
// /* Comparison */
549-
// /*********************************************/
535+
/*********************************************/
536+
/* Comparison */
537+
/*********************************************/
550538

551539
int String::compareTo(const String &s) const {
552540
if(!buffer() || !s.buffer()) {
@@ -648,9 +636,9 @@ unsigned char String::endsWith(const String &s2) const {
648636
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
649637
}
650638

651-
// /*********************************************/
652-
// /* Character Access */
653-
// /*********************************************/
639+
/*********************************************/
640+
/* Character Access */
641+
/*********************************************/
654642

655643
char String::charAt(unsigned int loc) const {
656644
return operator[](loc);
@@ -690,9 +678,9 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind
690678
buf[n] = 0;
691679
}
692680

693-
// /*********************************************/
694-
// /* Search */
695-
// /*********************************************/
681+
/*********************************************/
682+
/* Search */
683+
/*********************************************/
696684

697685
int String::indexOf(char c) const {
698686
return indexOf(c, 0);
@@ -701,7 +689,7 @@ int String::indexOf(char c) const {
701689
int String::indexOf(char ch, unsigned int fromIndex) const {
702690
if(fromIndex >= len())
703691
return -1;
704-
const char* temp = strchr(buffer() + fromIndex, ch);
692+
const char *temp = strchr(buffer() + fromIndex, ch);
705693
if(temp == NULL)
706694
return -1;
707695
return temp - buffer();
@@ -771,9 +759,9 @@ String String::substring(unsigned int left, unsigned int right) const {
771759
return out;
772760
}
773761

774-
// /*********************************************/
775-
// /* Modification */
776-
// /*********************************************/
762+
/*********************************************/
763+
/* Modification */
764+
/*********************************************/
777765

778766
void String::replace(char find, char replace) {
779767
if(!buffer())
@@ -784,7 +772,7 @@ void String::replace(char find, char replace) {
784772
}
785773
}
786774

787-
void String::replace(const String& find, const String& replace) {
775+
void String::replace(const String &find, const String &replace) {
788776
if(len() == 0 || find.len() == 0)
789777
return;
790778
int diff = replace.len() - find.len();
@@ -890,9 +878,9 @@ void String::trim(void) {
890878
wbuffer()[newlen] = 0;
891879
}
892880

893-
// /*********************************************/
894-
// /* Parsing / Conversion */
895-
// /*********************************************/
881+
/*********************************************/
882+
/* Parsing / Conversion */
883+
/*********************************************/
896884

897885
long String::toInt(void) const {
898886
if (buffer())
@@ -906,8 +894,7 @@ float String::toFloat(void) const {
906894
return 0;
907895
}
908896

909-
double String::toDouble(void) const
910-
{
897+
double String::toDouble(void) const {
911898
if (buffer())
912899
return atof(buffer());
913900
return 0.0;

Diff for: cores/esp32/WString.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@
2323
#define String_class_h
2424
#ifdef __cplusplus
2525

26+
#include <pgmspace.h>
27+
2628
#include <stdlib.h>
29+
#include <stdint.h>
2730
#include <string.h>
2831
#include <ctype.h>
29-
#include <pgmspace.h>
30-
#include <stdint.h>
3132

32-
// An inherited class for holding the result of a concatenation. These
33-
// result objects are assumed to be writable by subsequent concatenations.
34-
class StringSumHelper;
3533

3634
// an abstract class used as a means to proide a unique pointer type
3735
// but really has no body
3836
class __FlashStringHelper;
3937
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
4038
#define F(string_literal) (FPSTR(PSTR(string_literal)))
4139

40+
// An inherited class for holding the result of a concatenation. These
41+
// result objects are assumed to be writable by subsequent concatenations.
42+
class StringSumHelper;
43+
4244
// The string class
4345
class String {
4446
// use a function pointer to allow for "if (s)" without the
@@ -107,7 +109,7 @@ class String {
107109
String & operator =(StringSumHelper &&rval);
108110
#endif
109111

110-
// concatenate (works w/ built-in types)
112+
// concatenate (works w/ built-in types, same as assignment)
111113

112114
// returns true on success, false on failure (in which case, the string
113115
// is left unchanged). if the argument is null or invalid, the
@@ -265,7 +267,6 @@ class String {
265267
String substring(unsigned int beginIndex) const {
266268
return substring(beginIndex, len());
267269
}
268-
;
269270
String substring(unsigned int beginIndex, unsigned int endIndex) const;
270271

271272
// modification

0 commit comments

Comments
 (0)