mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "StringBuilder.h"
 | |
| 
 | |
| 
 | |
| void StringBuilder::append(const char* s)
 | |
| {
 | |
|     char* tail = buffer + length;
 | |
| 
 | |
|     strncpy(tail, s, capacity - length);
 | |
| 
 | |
|     length += strlen(tail);
 | |
| }
 | |
| 
 | |
| void StringBuilder::appendEscaped(const char* s)
 | |
| {
 | |
|     if (length > capacity - 3) return;
 | |
| 
 | |
|     buffer[length++] = '"';
 | |
| 
 | |
|     // keep one slot for the end quote
 | |
|     capacity--;
 | |
| 
 | |
|     while (*s && length<capacity)
 | |
|     {
 | |
|         switch (*s)
 | |
|         {
 | |
|         case '"':
 | |
|             append("\\\"");
 | |
|             break;
 | |
| 
 | |
|         case '\\':
 | |
|             append("\\\\");
 | |
|             break;
 | |
| 
 | |
| 
 | |
|         default:
 | |
|             buffer[length++] = *s;
 | |
|             break;
 | |
|         }
 | |
| 
 | |
|         s++;
 | |
|     }
 | |
| 
 | |
|     buffer[length++] = '"';
 | |
|     buffer[length] = 0;
 | |
| 
 | |
|     // restore the original capacity
 | |
|     capacity++;
 | |
| }
 | |
| 
 | |
| 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);
 | |
| } |