8000 Update WString.h · espressif/arduino-esp32@38142dd · GitHub
[go: up one dir, main page]

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 38142dd

Browse files
Update WString.h
WString Fix int64_t Fixed int64_t String support. Resolves issue #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 5b0a7d0 commit 38142dd

File tree

2 files changed

+48
-60
lines changed

2 files changed

+48
-60
lines changed

cores/esp32/WString.cpp

Lines changed: 40 additions & 53 deletions
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
222 8000 2
*/
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);
@@ -221,8 +209,7 @@ bool String::changeBuffer(unsigned int maxStrLen) {
221209
// Copy the SSO buffer into allocated space
222210
memmove(newbuffer, sso.buff, sizeof(sso.buff));
223211
}
224-
if (newSize > oldSize)
225-
{
212+
if (newSize > oldSize) {
226213
memset(newbuffer + oldSize, 0, newSize - oldSize);
227214
}
228215
setSSO(false);
@@ -234,9 +221,9 @@ bool String::changeBuffer(unsigned int maxStrLen) {
234221
return false;
235222
}
236223

237-
// /*********************************************/
238-
// /* Copy and Move */
239-
// /*********************************************/
224+
/*********************************************/
225+
/* Copy and Move */
226+
/*********************************************/
240227

241228
String & String::copy(const char *cstr, unsigned int length) {
242229
if(!reserve(length)) {
@@ -292,12 +279,10 @@ void String::move(String &rhs) {
292279
String & String::operator =(const String &rhs) {
293280
if(this == &rhs)
294281
return *this;
295-
296282
if(rhs.buffer())
297283
copy(rhs.buffer(), rhs.len());
298284
else
299285
invalidate();
300-
301286
return *this;
302287
}
303288

@@ -320,7 +305,6 @@ String & String::operator =(const char *cstr) {
320305
copy(cstr, strlen(cstr));
321306
else
322307
invalidate();
323-
324308
return *this;
325309
}
326310

@@ -333,9 +317,9 @@ String & String::operator =(const __FlashStringHelper *pstr) {
333317
return *this;
334318
}
335319

336-
// /*********************************************/
337-
// /* concat */
338-
// /*********************************************/
320+
/*********************************************/
321+
/* concat */
322+
/*********************************************/
339323

340324
bool String::concat(const String &s) {
341325
// Special case if we're concatting ourself (s += s;) since we may end up
@@ -388,12 +372,14 @@ bool String::concat(char c) {
388372

389373
bool String::concat(unsigned char num) {
390374
char buf[1 + 3 * sizeof(unsigned char)];
391-
return concat(buf, sprintf(buf, "%d", num));
375+
utoa(num, buf, 10);
376+
return concat(buf, strlen(buf));
392377
}
393378

394379
bool String::concat(int num) {
395380
char buf[2 + 3 * sizeof(int)];
396-
return concat(buf, sprintf(buf, "%d", num));
381+
itoa(num, buf, 10);
382+
return concat(buf, strlen(buf));
397383
}
398384

399385
bool String::concat(unsigned int num) {
@@ -404,7 +390,8 @@ bool String::concat(unsigned int num) {
404390

405391
bool String::concat(long num) {
406392
char buf[2 + 3 * sizeof(long)];
407-
return concat(buf, sprintf(buf, "%ld", num));
393+
ltoa(num, buf, 10);
394+
return concat(buf, strlen(buf));
408395
}
409396

410397
bool String::concat(unsigned long num) {
@@ -427,7 +414,8 @@ bool String::concat(double num) {
427414

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

433421
bool String::concat(unsigned long long num) {
@@ -546,9 +534,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
546534
return a;
547535
}
548536

549-
// /*********************************************/
550-
// /* Comparison */
551-
// /*********************************************/
537+
/*********************************************/
538+
/* Comparison */
539+
/*********************************************/
552540

553541
int String::compareTo(const String &s) const {
554542
if(!buffer() || !s.buffer()) {
@@ -650,9 +638,9 @@ bool String::endsWith(const String &s2) const {
650638
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
651639
}
652640

653-
// /*********************************************/
654-
// /* Character Access */
655-
// /*********************************************/
641+
/*********************************************/
642+
/* Character Access */
643+
/*********************************************/
656644

657645
char String::charAt(unsigned int loc) const {
658646
return operator[](loc);
@@ -692,9 +680,9 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind
692680
buf[n] = 0;
693681
}
694682

695-
// /*********************************************/
696-
// /* Search */
697-
// /*********************************************/
683+
/*********************************************/
684+
/* Search */
685+
/*********************************************/
698686

699687
int String::indexOf(char c) const {
700688
return indexOf(c, 0);
@@ -703,7 +691,7 @@ int String::indexOf(char c) const {
703691
int String::indexOf(char ch, unsigned int fromIndex) const {
704692
if(fromIndex >= len())
705693
return -1;
706-
const char* temp = strchr(buffer() + fromIndex, ch);
694+
const char *temp = strchr(buffer() + fromIndex, ch);
707695
if(temp == NULL)
708696
return -1;
709697
return temp - buffer();
@@ -773,9 +761,9 @@ String String::substring(unsigned int left, unsigned int right) const {
773761
return out;
774762
}
775763

776-
// /*********************************************/
777-
// /* Modification */
778-
// /*********************************************/
764+
/*********************************************/
765+
/* Modification */
766+
/*********************************************/
779767

780768
void String::replace(char find, char replace) {
781769
if(!buffer())
@@ -786,7 +774,7 @@ void String::replace(char find, char replace) {
786774
}
787775
}
788776

789-
void String::replace(const String& find, const String& replace) {
777+
void String::replace(const String &find, const String &replace) {
790778
if(len() == 0 || find.len() == 0)
791779
return;
792780
int diff = replace.len() - find.len();
@@ -892,9 +880,9 @@ void String::trim(void) {
892880
wbuffer()[newlen] = 0;
893881
}
894882

895-
// /*********************************************/
896-
// /* Parsing / Conversion */
897-
// /*********************************************/
883+
/*********************************************/
884+
/* Parsing / Conversion */
885+
/*********************************************/
898886

899887
long String::toInt(void) const {
900888
if (buffer())
@@ -908,8 +896,7 @@ float String::toFloat(void) const {
908896
return 0;
909897
}
910898

911-
double String::toDouble(void) const
912-
{
899+
double String::toDouble(void) const {
913900
if (buffer())
914901
return atof(buffer());
915902
return 0.0;

cores/esp32/WString.h

Lines changed: 8 additions & 7 deletions
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)
0