Skip to content

Commit

Permalink
Added FixedSizeFlashString
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 9, 2018
1 parent 2aaf6ed commit 1f49769
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/ProgmemExample/ProgmemExample.ino
Expand Up @@ -39,6 +39,7 @@ void setup() {

// It works with RawJson too:
obj["sensor"] = RawJson(F("\"gps\""));
obj["sensor"] = RawJson(F("\xA3gps"), 3);

// You can compare the content of a JsonVariant to a Flash String
if (obj["sensor"] == F("gps")) {
Expand Down
47 changes: 47 additions & 0 deletions src/ArduinoJson/Strings/FixedSizeFlashString.hpp
@@ -0,0 +1,47 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License

#pragma once

namespace ArduinoJson {
namespace Internals {

class FixedSizeFlashString {
public:
FixedSizeFlashString(const __FlashStringHelper* str, size_t sz)
: _str(str), _size(sz) {}

bool equals(const char* expected) const {
const char* actual = reinterpret_cast<const char*>(_str);
if (!actual || !expected) return actual == expected;
return strcmp_P(actual, expected) == 0;
}

bool is_null() const {
return !_str;
}

template <typename Buffer>
const char* save(Buffer* buffer) const {
if (!_str) return NULL;
void* dup = buffer->alloc(_size);
if (dup != NULL) memcpy_P(dup, (const char*)_str, _size);
return static_cast<const char*>(dup);
}

size_t size() const {
return strlen_P(reinterpret_cast<const char*>(_str));
}

private:
const __FlashStringHelper* _str;
size_t _size;
};

inline FixedSizeFlashString makeString(const __FlashStringHelper* str,
size_t sz) {
return FixedSizeFlashString(str, sz);
}
} // namespace Internals
} // namespace ArduinoJson
3 changes: 2 additions & 1 deletion src/ArduinoJson/Strings/StringTypes.hpp
Expand Up @@ -31,5 +31,6 @@ struct IsString<T&> : IsString<T> {};
#endif

#if ARDUINOJSON_ENABLE_PROGMEM
#include "FlashString.hpp"
#include "FixedSizeFlashString.hpp"
#include "ZeroTerminatedFlashString.hpp"
#endif
Expand Up @@ -7,9 +7,9 @@
namespace ArduinoJson {
namespace Internals {

class FlashString {
class ZeroTerminatedFlashString {
public:
FlashString(const __FlashStringHelper* str) : _str(str) {}
ZeroTerminatedFlashString(const __FlashStringHelper* str) : _str(str) {}

bool equals(const char* expected) const {
const char* actual = reinterpret_cast<const char*>(_str);
Expand All @@ -24,12 +24,7 @@ class FlashString {
template <typename Buffer>
const char* save(Buffer* buffer) const {
if (!_str) return NULL;
return save(buffer, size() + 1);
}

template <typename Buffer>
const char* save(Buffer* buffer, size_t n) const {
if (!_str) return NULL;
size_t n = size() + 1; // copy the terminator
void* dup = buffer->alloc(n);
if (dup != NULL) memcpy_P(dup, (const char*)_str, n);
return static_cast<const char*>(dup);
Expand All @@ -43,8 +38,8 @@ class FlashString {
const __FlashStringHelper* _str;
};

inline FlashString makeString(const __FlashStringHelper* str) {
return FlashString(str);
inline ZeroTerminatedFlashString makeString(const __FlashStringHelper* str) {
return ZeroTerminatedFlashString(str);
}

template <>
Expand Down

0 comments on commit 1f49769

Please sign in to comment.