Files
ArduinoJson/srcs/Internals/EscapedString.cpp

45 lines
850 B
C++
Raw Normal View History

2014-10-16 00:11:23 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
2014-10-16 00:11:23 +02:00
#include "ArduinoJson/Internals/EscapedString.h"
using namespace ArduinoJson::Internals;
static inline char getSpecialChar(char c)
{
// Optimized for code size on a 8-bit AVR
const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
while (p[0] && p[0] != c)
{
p += 2;
}
return p[1];
}
static inline size_t printCharTo(char c, Print* p)
{
char specialChar = getSpecialChar(c);
return specialChar != 0
? p->write('\\') + p->write(specialChar)
: p->write(c);
}
size_t EscapedString::printTo(const char* s, Print* p)
{
if (!s) return p->print("null");
size_t n = p->write('\"');
while (*s)
{
n += printCharTo(*s++, p);
}
return n + p->write('\"');
}