Skip to content

Commit 21947eb

Browse files
authored
Make String::concat() faster for some types. (#5307)
* Make String::concat() faster for some types. This patch removes the unneeded call to `strlen()` when concatenating some types to a `String`. Additionally it fixes some whitespace for consistency. * Update WString.cpp
1 parent c7bdb23 commit 21947eb

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

cores/esp32/WString.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ String::String(StringSumHelper &&rval) {
5959

6060
String::String(char c) {
6161
init();
62-
char buf[2];
63-
buf[0] = c;
64-
buf[1] = 0;
62+
char buf[] = { c, '\0' };
6563
*this = buf;
6664
}
6765

@@ -290,10 +288,11 @@ String & String::operator =(const char *cstr) {
290288
return *this;
291289
}
292290

293-
String & String::operator = (const __FlashStringHelper *pstr)
294-
{
295-
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
296-
else invalidate();
291+
String & String::operator =(const __FlashStringHelper *pstr) {
292+
if(pstr)
293+
copy(pstr, strlen_P((PGM_P)pstr));
294+
else
295+
invalidate();
297296

298297
return *this;
299298
}
@@ -347,22 +346,18 @@ unsigned char String::concat(const char *cstr) {
347346
}
348347

349348
unsigned char String::concat(char c) {
350-
char buf[2];
351-
buf[0] = c;
352-
buf[1] = 0;
349+
char buf[] = { c, '\0' };
353350
return concat(buf, 1);
354351
}
355352

356353
unsigned char String::concat(unsigned char num) {
357354
char buf[1 + 3 * sizeof(unsigned char)];
358-
sprintf(buf, "%d", num);
359-
return concat(buf, strlen(buf));
355+
return concat(buf, sprintf(buf, "%d", num));
360356
}
361357

362358
unsigned char String::concat(int num) {
363359
char buf[2 + 3 * sizeof(int)];
364-
sprintf(buf, "%d", num);
365-
return concat(buf, strlen(buf));
360+
return concat(buf, sprintf(buf, "%d", num));
366361
}
367362

368363
unsigned char String::concat(unsigned int num) {
@@ -373,8 +368,7 @@ unsigned char String::concat(unsigned int num) {
373368

374369
unsigned char String::concat(long num) {
375370
char buf[2 + 3 * sizeof(long)];
376-
sprintf(buf, "%ld", num);
377-
return concat(buf, strlen(buf));
371+
return concat(buf, sprintf(buf, "%ld", num));
378372
}
379373

380374
unsigned char String::concat(unsigned long num) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy