// Copyright Benoit Blanchon 2014-2016 // MIT License // // Arduino JSON library // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! #pragma once #include "../Configuration.hpp" #if ARDUINOJSON_ENABLE_ARDUINO_STRING #include #endif #if ARDUINOJSON_ENABLE_STD_STRING #include #endif namespace ArduinoJson { namespace Internals { template struct StringFuncs {}; template struct StringFuncs : StringFuncs {}; template struct StringFuncs : StringFuncs {}; struct CharPtrFuncs { class Iterator { const char* _ptr; public: Iterator(const char* ptr) : _ptr(ptr ? ptr : "") {} char next() { return *_ptr++; } }; static bool equals(const char* str, const char* expected) { return strcmp(str, expected) == 0; } template static char* duplicate(const char* str, Buffer* buffer) { if (!str) return NULL; size_t size = strlen(str) + 1; void* dup = buffer->alloc(size); if (dup != NULL) memcpy(dup, str, size); return static_cast(dup); } static const bool has_append = false; static const bool has_equals = true; static const bool should_duplicate = false; }; template <> struct StringFuncs : CharPtrFuncs {}; template <> struct StringFuncs : CharPtrFuncs {}; template struct StringFuncs : CharPtrFuncs {}; template struct StringFuncs : CharPtrFuncs {}; template struct StdStringFuncs { template static char* duplicate(const TString& str, Buffer* buffer) { if (!str.c_str()) return NULL; // <- Arduino string can return NULL size_t size = str.length() + 1; void* dup = buffer->alloc(size); if (dup != NULL) memcpy(dup, str.c_str(), size); return static_cast(dup); } struct Iterator : CharPtrFuncs::Iterator { Iterator(const TString& str) : CharPtrFuncs::Iterator(str.c_str()) {} }; static bool equals(const TString& str, const char* expected) { return str == expected; } static void append(TString& str, char c) { str += c; } static const bool has_append = true; static const bool has_equals = true; static const bool should_duplicate = true; }; #if ARDUINOJSON_ENABLE_ARDUINO_STRING template <> struct StringFuncs : StdStringFuncs {}; template <> struct StringFuncs : StdStringFuncs {}; #endif #if ARDUINOJSON_ENABLE_STD_STRING template <> struct StringFuncs : StdStringFuncs {}; #endif #if ARDUINOJSON_ENABLE_PROGMEM template <> struct StringFuncs { class Iterator { const char* _ptr; public: Iterator(const __FlashStringHelper* ptr) : _ptr(reinterpret_cast(ptr)) {} char next() { return pgm_read_byte_near(_ptr++); } }; static bool equals(const __FlashStringHelper* str, const char* expected) { return strcmp_P(expected, (PGM_P)str) == 0; } template static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) { if (!str) return NULL; size_t size = strlen_P((PGM_P)str) + 1; void* dup = buffer->alloc(size); if (dup != NULL) memcpy_P(dup, (PGM_P)str, size); return static_cast(dup); } static const bool has_append = false; static const bool has_equals = true; static const bool should_duplicate = true; }; #endif } }