Skip to content
Next Next commit
Remove strlen in calls like String::concat(s, strlen(s))
Instead of calling strlen in a dozen places, instead just call
String::concat(s), which will then call strlen. This shrinks the code
size of these calls significantly, the StringAppendOperator example on
the Arduino Uno shrinks by 72 bytes. This change does incur a slight
runtime cost, because there is one extra function call.
  • Loading branch information
matthijskooijman committed Jan 25, 2021
commit 184dd3e812fe16c66c5bf3a78626c1a47bb900e8
16 changes: 8 additions & 8 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,49 +307,49 @@ unsigned char String::concat(unsigned char num)
{
char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(int num)
{
char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(unsigned int num)
{
char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(long num)
{
char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(unsigned long num)
{
char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(string);
}

unsigned char String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(string);
}

unsigned char String::concat(const __FlashStringHelper * str)
Expand Down Expand Up @@ -378,7 +378,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
if (!cstr || !a.concat(cstr)) a.invalidate();
return a;
}

Expand Down