Skip to content

Commit b4349b0

Browse files
matsujirushiPillar1989
authored andcommitted
Add String::format()
1 parent d1d8c19 commit b4349b0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

cores/arduino/WString.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "WString.h"
2323
#include "itoa.h"
2424
#include "avr/dtostrf.h"
25+
#include <stdio.h>
26+
#include <stdarg.h>
2527

2628
/*********************************************/
2729
/* Constructors */
@@ -126,6 +128,18 @@ String::~String()
126128
free(buffer);
127129
}
128130

131+
String String::format(const char* format, ...)
132+
{
133+
va_list arg;
134+
va_start(arg, format);
135+
const int len = vsnprintf(nullptr, 0, format, arg);
136+
char str[len + 1];
137+
vsnprintf(str, sizeof(str), format, arg);
138+
va_end(arg);
139+
140+
return String(str);
141+
}
142+
129143
/*********************************************/
130144
/* Memory Management */
131145
/*********************************************/

cores/arduino/WString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class String
7373
explicit String(double, unsigned char decimalPlaces=2);
7474
~String(void);
7575

76+
static String format(const char* format, ...);
77+
7678
// memory management
7779
// return true on success, false on failure (in which case, the string
7880
// is left unchanged). reserve(0), if successful, will validate an

0 commit comments

Comments
 (0)