Skip to content

Commit af50354

Browse files
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 4672eee commit af50354

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

hardware/arduino/avr/cores/arduino/WString.cpp

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

295295
unsigned char String::concat(int num)
296296
{
297297
char buf[2 + 3 * sizeof(int)];
298298
itoa(num, buf, 10);
299-
return concat(buf, strlen(buf));
299+
return concat(buf);
300300
}
301301

302302
unsigned char String::concat(unsigned int num)
303303
{
304304
char buf[1 + 3 * sizeof(unsigned int)];
305305
utoa(num, buf, 10);
306-
return concat(buf, strlen(buf));
306+
return concat(buf);
307307
}
308308

309309
unsigned char String::concat(long num)
310310
{
311311
char buf[2 + 3 * sizeof(long)];
312312
ltoa(num, buf, 10);
313-
return concat(buf, strlen(buf));
313+
return concat(buf);
314314
}
315315

316316
unsigned char String::concat(unsigned long num)
317317
{
318318
char buf[1 + 3 * sizeof(unsigned long)];
319319
ultoa(num, buf, 10);
320-
return concat(buf, strlen(buf));
320+
return concat(buf);
321321
}
322322

323323
unsigned char String::concat(float num)
324324
{
325325
char buf[20];
326326
char* string = dtostrf(num, 4, 2, buf);
327-
return concat(string, strlen(string));
327+
return concat(string);
328328
}
329329

330330
unsigned char String::concat(double num)
331331
{
332332
char buf[20];
333333
char* string = dtostrf(num, 4, 2, buf);
334-
return concat(string, strlen(string));
334+
return concat(string);
335335
}
336336

337337
unsigned char String::concat(const __FlashStringHelper * str)
@@ -360,7 +360,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
360360
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
361361
{
362362
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
363-
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
363+
if (!cstr || !a.concat(cstr)) a.invalidate();
364364
return a;
365365
}
366366

0 commit comments

Comments
 (0)