2014-07-07 13:38:35 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EscapedString.h"
|
|
|
|
|
2014-07-07 14:05:41 +02:00
|
|
|
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)
|
|
|
|
{
|
2014-07-09 12:57:50 +02:00
|
|
|
// Optimized for code size on a 8-bit AVR
|
2014-07-09 12:50:03 +02:00
|
|
|
|
2014-07-09 12:57:50 +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
|
|
|
|
2014-07-09 12:57:50 +02:00
|
|
|
if (specials[0] == c)
|
|
|
|
return specials[1];
|
2014-07-09 12:50:03 +02:00
|
|
|
|
2014-07-09 12:57:50 +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;
|
|
|
|
}
|