mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-17 20:42:24 +02:00
Added a test that adds a string with a backslash in it
This commit is contained in:
@ -37,6 +37,13 @@ namespace JsonGeneratorTests
|
|||||||
AssertJsonIs("[\"\\\"\"]");
|
AssertJsonIs("[\"\\\"\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(AddOneStringContainingBackslash)
|
||||||
|
{
|
||||||
|
arr.add("\\");
|
||||||
|
|
||||||
|
AssertJsonIs("[\"\\\\\"]");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_METHOD(AddTwoStrings)
|
TEST_METHOD(AddTwoStrings)
|
||||||
{
|
{
|
||||||
arr.add("hello");
|
arr.add("hello");
|
||||||
|
@ -5,7 +5,7 @@ void StringBuilder::append(const char* s)
|
|||||||
{
|
{
|
||||||
char* tail = buffer + length;
|
char* tail = buffer + length;
|
||||||
|
|
||||||
strcpy(tail, s);
|
strncpy(tail, s, capacity - length);
|
||||||
|
|
||||||
length += strlen(tail);
|
length += strlen(tail);
|
||||||
}
|
}
|
||||||
@ -16,17 +16,35 @@ void StringBuilder::appendEscaped(const char* s)
|
|||||||
|
|
||||||
buffer[length++] = '"';
|
buffer[length++] = '"';
|
||||||
|
|
||||||
while (*s && length<capacity-2)
|
// keep one slot for the end quote
|
||||||
{
|
capacity--;
|
||||||
if (*s == '"')
|
|
||||||
buffer[length++] = '\\';
|
|
||||||
|
|
||||||
|
while (*s && length<capacity)
|
||||||
|
{
|
||||||
|
switch (*s)
|
||||||
|
{
|
||||||
|
case '"':
|
||||||
|
append("\\\"");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\\':
|
||||||
|
append("\\\\");
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
buffer[length++] = *s;
|
buffer[length++] = *s;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
s++;
|
s++;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[length++] = '"';
|
buffer[length++] = '"';
|
||||||
|
buffer[length] = 0;
|
||||||
|
|
||||||
|
// restore the original capacity
|
||||||
|
capacity++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringBuilder::appendFormatted(const char* format, ...)
|
void StringBuilder::appendFormatted(const char* format, ...)
|
||||||
|
Reference in New Issue
Block a user