diff options
Diffstat (limited to 'include/lib/ArduinoJson/Strings')
13 files changed, 622 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Strings/ArduinoStringAdapter.hpp b/include/lib/ArduinoJson/Strings/ArduinoStringAdapter.hpp new file mode 100644 index 0000000..24646cc --- /dev/null +++ b/include/lib/ArduinoJson/Strings/ArduinoStringAdapter.hpp @@ -0,0 +1,62 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <Arduino.h> + +#include <ArduinoJson/Polyfills/safe_strcmp.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class ArduinoStringAdapter { + public: + ArduinoStringAdapter(const ::String& str) : _str(&str) {} + + void copyTo(char* p, size_t n) const { + memcpy(p, _str->c_str(), n); + } + + bool isNull() const { + // Arduino's String::c_str() can return NULL + return !_str->c_str(); + } + + int compare(const char* other) const { + // Arduino's String::c_str() can return NULL + const char* me = _str->c_str(); + return safe_strcmp(me, other); + } + + bool equals(const char* expected) const { + return compare(expected) == 0; + } + + size_t size() const { + return _str->length(); + } + + const char* begin() const { + return _str->c_str(); + } + + typedef storage_policies::store_by_copy storage_policy; + + private: + const ::String* _str; +}; + +template <> +struct IsString< ::String> : true_type {}; + +template <> +struct IsString< ::StringSumHelper> : true_type {}; + +inline ArduinoStringAdapter adaptString(const ::String& str) { + return ArduinoStringAdapter(str); +} + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/ConstRamStringAdapter.hpp b/include/lib/ArduinoJson/Strings/ConstRamStringAdapter.hpp new file mode 100644 index 0000000..ec7d53a --- /dev/null +++ b/include/lib/ArduinoJson/Strings/ConstRamStringAdapter.hpp @@ -0,0 +1,62 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <stddef.h> // size_t +#include <string.h> // strcmp + +#include <ArduinoJson/Polyfills/safe_strcmp.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class ConstRamStringAdapter { + public: + ConstRamStringAdapter(const char* str = 0) : _str(str) {} + + int compare(const char* other) const { + return safe_strcmp(_str, other); + } + + bool equals(const char* expected) const { + return compare(expected) == 0; + } + + bool isNull() const { + return !_str; + } + + size_t size() const { + if (!_str) + return 0; + return strlen(_str); + } + + const char* data() const { + return _str; + } + + const char* begin() const { + return _str; + } + + typedef storage_policies::store_by_address storage_policy; + + protected: + const char* _str; +}; + +template <> +struct IsString<const char*> : true_type {}; + +template <int N> +struct IsString<const char[N]> : true_type {}; + +inline ConstRamStringAdapter adaptString(const char* str) { + return ConstRamStringAdapter(str); +} + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/FlashStringAdapter.hpp b/include/lib/ArduinoJson/Strings/FlashStringAdapter.hpp new file mode 100644 index 0000000..292e348 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/FlashStringAdapter.hpp @@ -0,0 +1,62 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Polyfills/pgmspace.hpp> +#include <ArduinoJson/Strings/FlashStringIterator.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class FlashStringAdapter { + public: + FlashStringAdapter(const __FlashStringHelper* str) : _str(str) {} + + int compare(const char* other) const { + if (!other && !_str) + return 0; + if (!_str) + return -1; + if (!other) + return 1; + return -strcmp_P(other, reinterpret_cast<const char*>(_str)); + } + + bool equals(const char* expected) const { + return compare(expected) == 0; + } + + bool isNull() const { + return !_str; + } + + void copyTo(char* p, size_t n) const { + memcpy_P(p, reinterpret_cast<const char*>(_str), n); + } + + size_t size() const { + if (!_str) + return 0; + return strlen_P(reinterpret_cast<const char*>(_str)); + } + + FlashStringIterator begin() const { + return FlashStringIterator(_str); + } + + typedef storage_policies::store_by_copy storage_policy; + + private: + const __FlashStringHelper* _str; +}; + +inline FlashStringAdapter adaptString(const __FlashStringHelper* str) { + return FlashStringAdapter(str); +} + +template <> +struct IsString<const __FlashStringHelper*> : true_type {}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/FlashStringIterator.hpp b/include/lib/ArduinoJson/Strings/FlashStringIterator.hpp new file mode 100644 index 0000000..9a97f32 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/FlashStringIterator.hpp @@ -0,0 +1,44 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +namespace ARDUINOJSON_NAMESPACE { + +class FlashStringIterator { + public: + explicit FlashStringIterator(const __FlashStringHelper* ptr) + : _ptr(reinterpret_cast<const char*>(ptr)) {} + + explicit FlashStringIterator(const char* ptr) : _ptr(ptr) {} + + FlashStringIterator operator+(ptrdiff_t d) const { + return FlashStringIterator(_ptr + d); + } + + ptrdiff_t operator-(FlashStringIterator other) const { + return _ptr - other._ptr; + } + + FlashStringIterator operator++(int) { + return FlashStringIterator(_ptr++); + } + + FlashStringIterator operator++() { + return FlashStringIterator(++_ptr); + } + + bool operator!=(FlashStringIterator other) const { + return _ptr != other._ptr; + } + + char operator*() const { + return char(pgm_read_byte(_ptr)); + } + + private: + const char* _ptr; +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/IsString.hpp b/include/lib/ArduinoJson/Strings/IsString.hpp new file mode 100644 index 0000000..af5a91a --- /dev/null +++ b/include/lib/ArduinoJson/Strings/IsString.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Polyfills/type_traits.hpp> + +namespace ARDUINOJSON_NAMESPACE { +template <typename> +struct IsString : false_type {}; + +template <typename T> +struct IsString<const T> : IsString<T> {}; + +template <typename T> +struct IsString<T&> : IsString<T> {}; +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/IsWriteableString.hpp b/include/lib/ArduinoJson/Strings/IsWriteableString.hpp new file mode 100644 index 0000000..32039e3 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/IsWriteableString.hpp @@ -0,0 +1,37 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Configuration.hpp> +#include <ArduinoJson/Polyfills/type_traits.hpp> + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +#include <Arduino.h> +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING +#include <string> +#endif + +namespace ARDUINOJSON_NAMESPACE { + +template <typename> +struct IsWriteableString : false_type {}; + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING + +template <> +struct IsWriteableString< ::String> : true_type {}; + +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING + +template <typename TCharTraits, typename TAllocator> +struct IsWriteableString<std::basic_string<char, TCharTraits, TAllocator> > + : true_type {}; + +#endif +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/RamStringAdapter.hpp b/include/lib/ArduinoJson/Strings/RamStringAdapter.hpp new file mode 100644 index 0000000..eded6c9 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/RamStringAdapter.hpp @@ -0,0 +1,43 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class RamStringAdapter : public ConstRamStringAdapter { + public: + RamStringAdapter(const char* str) : ConstRamStringAdapter(str) {} + + void copyTo(char* p, size_t n) const { + memcpy(p, _str, n); + } + + typedef ARDUINOJSON_NAMESPACE::storage_policies::store_by_copy storage_policy; +}; + +template <typename TChar> +inline RamStringAdapter adaptString(const TChar* str) { + return RamStringAdapter(reinterpret_cast<const char*>(str)); +} + +inline RamStringAdapter adaptString(char* str) { + return RamStringAdapter(str); +} + +template <typename TChar> +struct IsString<TChar*> { + static const bool value = sizeof(TChar) == 1; +}; + +template <> +struct IsString<void*> { + static const bool value = false; +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/SizedFlashStringAdapter.hpp b/include/lib/ArduinoJson/Strings/SizedFlashStringAdapter.hpp new file mode 100644 index 0000000..b9cc0bf --- /dev/null +++ b/include/lib/ArduinoJson/Strings/SizedFlashStringAdapter.hpp @@ -0,0 +1,60 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Namespace.hpp> +#include <ArduinoJson/Strings/FlashStringIterator.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class SizedFlashStringAdapter { + public: + SizedFlashStringAdapter(const __FlashStringHelper* str, size_t sz) + : _str(str), _size(sz) {} + + int compare(const char* other) const { + if (!other && !_str) + return 0; + if (!_str) + return -1; + if (!other) + return 1; + return -strncmp_P(other, reinterpret_cast<const char*>(_str), _size); + } + + bool equals(const char* expected) const { + return compare(expected) == 0; + } + + bool isNull() const { + return !_str; + } + + void copyTo(char* p, size_t n) const { + memcpy_P(p, reinterpret_cast<const char*>(_str), n); + } + + size_t size() const { + return _size; + } + + FlashStringIterator begin() const { + return FlashStringIterator(_str); + } + + typedef storage_policies::store_by_copy storage_policy; + + private: + const __FlashStringHelper* _str; + size_t _size; +}; + +inline SizedFlashStringAdapter adaptString(const __FlashStringHelper* str, + size_t sz) { + return SizedFlashStringAdapter(str, sz); +} +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/SizedRamStringAdapter.hpp b/include/lib/ArduinoJson/Strings/SizedRamStringAdapter.hpp new file mode 100644 index 0000000..fe23408 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/SizedRamStringAdapter.hpp @@ -0,0 +1,55 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Namespace.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +#include <string.h> // strcmp + +namespace ARDUINOJSON_NAMESPACE { + +class SizedRamStringAdapter { + public: + SizedRamStringAdapter(const char* str, size_t n) : _str(str), _size(n) {} + + int compare(const char* other) const { + return safe_strncmp(_str, other, _size); + } + + bool equals(const char* expected) const { + return compare(expected) == 0; + } + + bool isNull() const { + return !_str; + } + + void copyTo(char* p, size_t n) const { + memcpy(p, _str, n); + } + + size_t size() const { + return _size; + } + + const char* begin() const { + return _str; + } + + typedef storage_policies::store_by_copy storage_policy; + + private: + const char* _str; + size_t _size; +}; + +template <typename TChar> +inline SizedRamStringAdapter adaptString(const TChar* str, size_t size) { + return SizedRamStringAdapter(reinterpret_cast<const char*>(str), size); +} + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/StdStringAdapter.hpp b/include/lib/ArduinoJson/Strings/StdStringAdapter.hpp new file mode 100644 index 0000000..ebf4c39 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/StdStringAdapter.hpp @@ -0,0 +1,65 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Namespace.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +#include <string> + +namespace ARDUINOJSON_NAMESPACE { + +template <typename TString> +class StdStringAdapter { + public: + StdStringAdapter(const TString& str) : _str(&str) {} + + void copyTo(char* p, size_t n) const { + memcpy(p, _str->c_str(), n); + } + + bool isNull() const { + return false; + } + + int compare(const char* other) const { + if (!other) + return 1; + return _str->compare(other); + } + + bool equals(const char* expected) const { + if (!expected) + return false; + return *_str == expected; + } + + size_t size() const { + return _str->size(); + } + + const char* begin() const { + return _str->c_str(); + } + + typedef storage_policies::store_by_copy storage_policy; + + private: + const TString* _str; +}; + +template <typename TCharTraits, typename TAllocator> +struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type { +}; + +template <typename TCharTraits, typename TAllocator> +inline StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> > +adaptString(const std::basic_string<char, TCharTraits, TAllocator>& str) { + return StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> >( + str); +} + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/StoragePolicy.hpp b/include/lib/ArduinoJson/Strings/StoragePolicy.hpp new file mode 100644 index 0000000..df0d62b --- /dev/null +++ b/include/lib/ArduinoJson/Strings/StoragePolicy.hpp @@ -0,0 +1,15 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +namespace ARDUINOJSON_NAMESPACE { + +namespace storage_policies { +struct store_by_address {}; +struct store_by_copy {}; +struct decide_at_runtime {}; +} // namespace storage_policies + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/String.hpp b/include/lib/ArduinoJson/Strings/String.hpp new file mode 100644 index 0000000..4f2abde --- /dev/null +++ b/include/lib/ArduinoJson/Strings/String.hpp @@ -0,0 +1,77 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> +#include <ArduinoJson/Strings/IsString.hpp> +#include <ArduinoJson/Strings/StoragePolicy.hpp> + +namespace ARDUINOJSON_NAMESPACE { + +class String { + public: + String() : _data(0), _isStatic(true) {} + String(const char* data, bool isStaticData = true) + : _data(data), _isStatic(isStaticData) {} + + const char* c_str() const { + return _data; + } + + bool isNull() const { + return !_data; + } + + bool isStatic() const { + return _isStatic; + } + + friend bool operator==(String lhs, String rhs) { + if (lhs._data == rhs._data) + return true; + if (!lhs._data) + return false; + if (!rhs._data) + return false; + return strcmp(lhs._data, rhs._data) == 0; + } + + friend bool operator!=(String lhs, String rhs) { + if (lhs._data == rhs._data) + return false; + if (!lhs._data) + return true; + if (!rhs._data) + return true; + return strcmp(lhs._data, rhs._data) != 0; + } + + private: + const char* _data; + bool _isStatic; +}; + +class StringAdapter : public RamStringAdapter { + public: + StringAdapter(const String& str) + : RamStringAdapter(str.c_str()), _isStatic(str.isStatic()) {} + + bool isStatic() const { + return _isStatic; + } + + typedef storage_policies::decide_at_runtime storage_policy; + + private: + bool _isStatic; +}; + +template <> +struct IsString<String> : true_type {}; + +inline StringAdapter adaptString(const String& str) { + return StringAdapter(str); +} +} // namespace ARDUINOJSON_NAMESPACE diff --git a/include/lib/ArduinoJson/Strings/StringAdapters.hpp b/include/lib/ArduinoJson/Strings/StringAdapters.hpp new file mode 100644 index 0000000..3d294d2 --- /dev/null +++ b/include/lib/ArduinoJson/Strings/StringAdapters.hpp @@ -0,0 +1,22 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include <ArduinoJson/Strings/ConstRamStringAdapter.hpp> +#include <ArduinoJson/Strings/RamStringAdapter.hpp> +#include <ArduinoJson/Strings/SizedRamStringAdapter.hpp> + +#if ARDUINOJSON_ENABLE_STD_STRING +#include <ArduinoJson/Strings/StdStringAdapter.hpp> +#endif + +#if ARDUINOJSON_ENABLE_ARDUINO_STRING +#include <ArduinoJson/Strings/ArduinoStringAdapter.hpp> +#endif + +#if ARDUINOJSON_ENABLE_PROGMEM +#include <ArduinoJson/Strings/FlashStringAdapter.hpp> +#include <ArduinoJson/Strings/SizedFlashStringAdapter.hpp> +#endif |