Skip to content

Commit 6876081

Browse files
matthijskooijmanaentinger
authored andcommitted
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.
1 parent 4bd75df commit 6876081

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

api/String.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,49 +293,49 @@ unsigned char String::concat(unsigned char num)
293293
{
294294
char buf[1 + 3 * sizeof(unsigned char)];
295295
itoa(num, buf, 10);
296-
return concat(buf, strlen(buf));
296+
return concat(buf);
297297
}
298298

299299
unsigned char String::concat(int num)
300300
{
301301
char buf[2 + 3 * sizeof(int)];
302302
itoa(num, buf, 10);
303-
return concat(buf, strlen(buf));
303+
return concat(buf);
304304
}
305305

306306
unsigned char String::concat(unsigned int num)
307307
{
308308
char buf[1 + 3 * sizeof(unsigned int)];
309309
utoa(num, buf, 10);
310-
return concat(buf, strlen(buf));
310+
return concat(buf);
311311
}
312312

313313
unsigned char String::concat(long num)
314314
{
315315
char buf[2 + 3 * sizeof(long)];
316316
ltoa(num, buf, 10);
317-
return concat(buf, strlen(buf));
317+
return concat(buf);
318318
}
319319

320320
unsigned char String::concat(unsigned long num)
321321
{
322322
char buf[1 + 3 * sizeof(unsigned long)];
323323
ultoa(num, buf, 10);
324-
return concat(buf, strlen(buf));
324+
return concat(buf);
325325
}
326326

327327
unsigned char String::concat(float num)
328328
{
329329
char buf[20];
330330
char* string = dtostrf(num, 4, 2, buf);
331-
return concat(string, strlen(string));
331+
return concat(string);
332332
}
333333

334334
unsigned char String::concat(double num)
335335
{
336336
char buf[20];
337337
char* string = dtostrf(num, 4, 2, buf);
338-
return concat(string, strlen(string));
338+
return concat(string);
339339
}
340340

341341
unsigned char String::concat(const __FlashStringHelper * str)
@@ -364,7 +364,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
364364
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
365365
{
366366
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
367-
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
367+
if (!cstr || !a.concat(cstr)) a.invalidate();
368368
return a;
369369
}
370370

0 commit comments

Comments
 (0)