forked from bblanchon/ArduinoJson
Simplified string adapters
This commit is contained in:
@ -7,6 +7,7 @@
|
|||||||
#include <ArduinoJson/Memory/Alignment.hpp>
|
#include <ArduinoJson/Memory/Alignment.hpp>
|
||||||
#include <ArduinoJson/Polyfills/assert.hpp>
|
#include <ArduinoJson/Polyfills/assert.hpp>
|
||||||
#include <ArduinoJson/Polyfills/mpl/max.hpp>
|
#include <ArduinoJson/Polyfills/mpl/max.hpp>
|
||||||
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||||
|
|
||||||
#include <string.h> // memmove
|
#include <string.h> // memmove
|
||||||
@ -64,7 +65,7 @@ class MemoryPool {
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
||||||
const char* existingCopy = findString(str.begin());
|
const char* existingCopy = findString(str);
|
||||||
if (existingCopy)
|
if (existingCopy)
|
||||||
return existingCopy;
|
return existingCopy;
|
||||||
#endif
|
#endif
|
||||||
@ -86,7 +87,7 @@ class MemoryPool {
|
|||||||
|
|
||||||
const char* saveStringFromFreeZone(size_t len) {
|
const char* saveStringFromFreeZone(size_t len) {
|
||||||
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
||||||
const char* dup = findString(_left);
|
const char* dup = findString(adaptString(_left));
|
||||||
if (dup)
|
if (dup)
|
||||||
return dup;
|
return dup;
|
||||||
#endif
|
#endif
|
||||||
@ -163,16 +164,11 @@ class MemoryPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
#if ARDUINOJSON_ENABLE_STRING_DEDUPLICATION
|
||||||
template <typename TIterator>
|
template <typename TAdaptedString>
|
||||||
const char* findString(TIterator str) {
|
const char* findString(const TAdaptedString& str) {
|
||||||
for (char* next = _begin; next < _left; ++next) {
|
for (char* next = _begin; next < _left; ++next) {
|
||||||
char* begin = next;
|
if (str.equals(next))
|
||||||
|
return next;
|
||||||
// try to match
|
|
||||||
for (TIterator it = str; *it == *next; ++it) {
|
|
||||||
if (*next++ == 0)
|
|
||||||
return begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
// jump to next terminator
|
// jump to next terminator
|
||||||
while (*next) ++next;
|
while (*next) ++next;
|
||||||
|
@ -39,10 +39,6 @@ class ArduinoStringAdapter {
|
|||||||
return _str->length();
|
return _str->length();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* begin() const {
|
|
||||||
return _str->c_str();
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_copy storage_policy;
|
typedef storage_policies::store_by_copy storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -39,10 +39,6 @@ class ConstRamStringAdapter {
|
|||||||
return _str;
|
return _str;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* begin() const {
|
|
||||||
return _str;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_address storage_policy;
|
typedef storage_policies::store_by_address storage_policy;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Polyfills/pgmspace.hpp>
|
#include <ArduinoJson/Polyfills/pgmspace.hpp>
|
||||||
#include <ArduinoJson/Strings/FlashStringIterator.hpp>
|
|
||||||
#include <ArduinoJson/Strings/IsString.hpp>
|
#include <ArduinoJson/Strings/IsString.hpp>
|
||||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
@ -43,10 +42,6 @@ class FlashStringAdapter {
|
|||||||
return strlen_P(reinterpret_cast<const char*>(_str));
|
return strlen_P(reinterpret_cast<const char*>(_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
FlashStringIterator begin() const {
|
|
||||||
return FlashStringIterator(_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_copy storage_policy;
|
typedef storage_policies::store_by_copy storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
// 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
|
|
@ -5,7 +5,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <ArduinoJson/Namespace.hpp>
|
#include <ArduinoJson/Namespace.hpp>
|
||||||
#include <ArduinoJson/Strings/FlashStringIterator.hpp>
|
|
||||||
#include <ArduinoJson/Strings/IsString.hpp>
|
#include <ArduinoJson/Strings/IsString.hpp>
|
||||||
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
#include <ArduinoJson/Strings/StoragePolicy.hpp>
|
||||||
|
|
||||||
@ -42,10 +41,6 @@ class SizedFlashStringAdapter {
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
FlashStringIterator begin() const {
|
|
||||||
return FlashStringIterator(_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_copy storage_policy;
|
typedef storage_policies::store_by_copy storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -36,10 +36,6 @@ class SizedRamStringAdapter {
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* begin() const {
|
|
||||||
return _str;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_copy storage_policy;
|
typedef storage_policies::store_by_copy storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -41,10 +41,6 @@ class StdStringAdapter {
|
|||||||
return _str->size();
|
return _str->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* begin() const {
|
|
||||||
return _str->c_str();
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef storage_policies::store_by_copy storage_policy;
|
typedef storage_policies::store_by_copy storage_policy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user