Files
ArduinoJson/JsonGenerator/EscapedString.cpp

47 lines
843 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
2014-07-09 13:30:08 +02:00
const char* p = "\"\"\\\\\bb\ff\nn\rr\tt\0";
2014-07-09 12:50:03 +02:00
while (p[0] && p[0] != c)
{
p += 2;
2014-07-09 12:50:03 +02:00
}
2014-07-09 13:30:08 +02:00
return p[1];
2014-07-09 12:50:03 +02:00
}
static inline size_t printCharTo(char c, Print& p)
{
char specialChar = getSpecialChar(c);
2014-07-09 13:30:08 +02:00
return specialChar != 0
? p.write('\\') + p.write(specialChar)
: p.write(c);
}
2014-07-07 13:38:35 +02:00
size_t EscapedString::printTo(Print& p) const
{
const char* s = rawString;
2014-07-09 13:30:08 +02:00
if (!s) return p.print("null");
size_t n = p.write('\"');
2014-07-07 13:38:35 +02:00
while (*s)
{
2014-07-09 13:30:08 +02:00
n += printCharTo(*s++, p);
2014-07-07 13:38:35 +02:00
}
2014-07-09 13:30:08 +02:00
return n + p.write('\"');
2014-07-07 13:38:35 +02:00
}