2014-06-25 13:02:39 +02:00
|
|
|
#include "StringBuilder.h"
|
|
|
|
|
|
|
|
|
2014-06-25 13:50:28 +02:00
|
|
|
void StringBuilder::append(const char* s)
|
2014-06-25 13:02:39 +02:00
|
|
|
{
|
|
|
|
char* tail = buffer + length;
|
|
|
|
|
2014-06-25 13:55:18 +02:00
|
|
|
strncpy(tail, s, capacity - length);
|
2014-06-25 13:02:39 +02:00
|
|
|
|
|
|
|
length += strlen(tail);
|
2014-06-25 13:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::appendEscaped(const char* s)
|
|
|
|
{
|
|
|
|
if (length > capacity - 3) return;
|
|
|
|
|
|
|
|
buffer[length++] = '"';
|
|
|
|
|
2014-06-25 13:55:18 +02:00
|
|
|
// keep one slot for the end quote
|
|
|
|
capacity--;
|
|
|
|
|
|
|
|
while (*s && length<capacity)
|
2014-06-25 13:47:28 +02:00
|
|
|
{
|
2014-06-25 13:55:18 +02:00
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
append("\\\"");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
append("\\\\");
|
|
|
|
break;
|
2014-06-25 13:47:28 +02:00
|
|
|
|
2014-06-25 13:55:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
buffer[length++] = *s;
|
|
|
|
break;
|
|
|
|
}
|
2014-06-25 13:47:28 +02:00
|
|
|
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[length++] = '"';
|
2014-06-25 13:55:18 +02:00
|
|
|
buffer[length] = 0;
|
|
|
|
|
|
|
|
// restore the original capacity
|
|
|
|
capacity++;
|
2014-06-25 13:50:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::appendFormatted(const char* format, ...)
|
|
|
|
{
|
|
|
|
char* tail = buffer + length;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(tail, capacity - length, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
length += strlen(tail);
|
2014-06-25 13:02:39 +02:00
|
|
|
}
|