Files
ArduinoJson/JsonGenerator/EscapedString.cpp

61 lines
1014 B
C++
Raw Normal View History

2014-07-07 13:38:35 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "EscapedString.h"
using namespace ArduinoJson::Internals;
2014-07-07 13:38:35 +02:00
2014-07-09 12:50:03 +02:00
static inline char getSpecialChar(char c)
{
// Optimized for code size on a 8-bit AVR
2014-07-09 12:50:03 +02:00
const char* specials = "\"\"\\\\\bb\ff\nn\rr\tt";
while (true)
{
if (specials[0] == 0)
return 0;
2014-07-09 12:50:03 +02:00
if (specials[0] == c)
return specials[1];
2014-07-09 12:50:03 +02:00
specials += 2;
2014-07-09 12:50:03 +02:00
}
}
2014-07-07 13:38:35 +02:00
size_t EscapedString::printTo(Print& p) const
{
const char* s = rawString;
if (!s)
{
return p.print("null");
}
size_t n = 0;
n += p.write('\"');
while (*s)
{
2014-07-09 12:50:03 +02:00
char specialChar = getSpecialChar(*s);
2014-07-07 13:38:35 +02:00
2014-07-09 12:50:03 +02:00
if (specialChar)
{
n += p.write('\\');
n += p.write(specialChar);
}
else
{
2014-07-07 13:38:35 +02:00
n += p.write(*s);
}
2014-07-09 12:50:03 +02:00
s++;
2014-07-07 13:38:35 +02:00
}
n += p.write('\"');
return n;
}