mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-11-01 08:48:30 +01:00 
			
		
		
		
	Refactored StringBuilder into StringStorage
This commit is contained in:
		| @@ -5,25 +5,51 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <ArduinoJson/Memory/MemoryPool.hpp> | ||||
| #include <ArduinoJson/Memory/StringBuilder.hpp> | ||||
|  | ||||
| namespace ARDUINOJSON_NAMESPACE { | ||||
|  | ||||
| class StringCopier { | ||||
|  public: | ||||
|   typedef ARDUINOJSON_NAMESPACE::StringBuilder StringBuilder; | ||||
|  | ||||
|   StringCopier(MemoryPool* pool) : _pool(pool) {} | ||||
|  | ||||
|   StringBuilder startString() { | ||||
|     return StringBuilder(_pool); | ||||
|   void startString(MemoryPool* pool) { | ||||
|     _slot = pool->allocExpandableString(); | ||||
|     _size = 0; | ||||
|   } | ||||
|  | ||||
|   void reclaim(const char* s) { | ||||
|     _pool->reclaimLastString(s); | ||||
|   void commit(MemoryPool* pool) { | ||||
|     ARDUINOJSON_ASSERT(_slot.value); | ||||
|     pool->freezeString(_slot, _size); | ||||
|   } | ||||
|  | ||||
|   void append(const char* s) { | ||||
|     while (*s) append(*s++); | ||||
|   } | ||||
|  | ||||
|   void append(const char* s, size_t n) { | ||||
|     while (n-- > 0) append(*s++); | ||||
|   } | ||||
|  | ||||
|   void append(char c) { | ||||
|     if (!_slot.value) | ||||
|       return; | ||||
|  | ||||
|     if (_size >= _slot.size) { | ||||
|       _slot.value = 0; | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     _slot.value[_size++] = c; | ||||
|   } | ||||
|  | ||||
|   bool isValid() { | ||||
|     return _slot.value != 0; | ||||
|   } | ||||
|  | ||||
|   const char* c_str() { | ||||
|     return _slot.value; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   MemoryPool* _pool; | ||||
|   size_t _size; | ||||
|   StringSlot _slot; | ||||
| }; | ||||
| }  // namespace ARDUINOJSON_NAMESPACE | ||||
|   | ||||
		Reference in New Issue
	
	Block a user