mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Implement JsonString
from RamString
This commit is contained in:
@ -14,8 +14,8 @@ template <>
|
|||||||
struct StringAdapter<JsonString> {
|
struct StringAdapter<JsonString> {
|
||||||
using AdaptedString = RamString;
|
using AdaptedString = RamString;
|
||||||
|
|
||||||
static AdaptedString adapt(const JsonString& s) {
|
static const AdaptedString& adapt(const JsonString& s) {
|
||||||
return AdaptedString(s.c_str(), s.size(), s.isLinked());
|
return s.str_;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -13,54 +13,56 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
// A string.
|
// A string.
|
||||||
// https://arduinojson.org/v7/api/jsonstring/
|
// https://arduinojson.org/v7/api/jsonstring/
|
||||||
class JsonString {
|
class JsonString {
|
||||||
|
friend struct detail::StringAdapter<JsonString>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum Ownership { Copied, Linked };
|
enum Ownership { Copied, Linked };
|
||||||
|
|
||||||
JsonString() : data_(0), size_(0), ownership_(Linked) {}
|
JsonString() : str_(nullptr, 0, true) {}
|
||||||
|
|
||||||
JsonString(const char* data, Ownership ownership = Linked)
|
JsonString(const char* data, Ownership ownership = Linked)
|
||||||
: data_(data), size_(data ? ::strlen(data) : 0), ownership_(ownership) {}
|
: str_(data, data ? ::strlen(data) : 0, ownership == Linked) {}
|
||||||
|
|
||||||
JsonString(const char* data, size_t sz, Ownership ownership = Linked)
|
JsonString(const char* data, size_t sz, Ownership ownership = Linked)
|
||||||
: data_(data), size_(sz), ownership_(ownership) {}
|
: str_(data, sz, ownership == Linked) {}
|
||||||
|
|
||||||
// Returns a pointer to the characters.
|
// Returns a pointer to the characters.
|
||||||
const char* c_str() const {
|
const char* c_str() const {
|
||||||
return data_;
|
return str_.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is null.
|
// Returns true if the string is null.
|
||||||
bool isNull() const {
|
bool isNull() const {
|
||||||
return !data_;
|
return str_.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is stored by address.
|
// Returns true if the string is stored by address.
|
||||||
// Returns false if the string is stored by copy.
|
// Returns false if the string is stored by copy.
|
||||||
bool isLinked() const {
|
bool isLinked() const {
|
||||||
return ownership_ == Linked;
|
return str_.isLinked();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns length of the string.
|
// Returns length of the string.
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return size_;
|
return str_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the string is non-null
|
// Returns true if the string is non-null
|
||||||
explicit operator bool() const {
|
explicit operator bool() const {
|
||||||
return data_ != 0;
|
return str_.data() != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if strings are equal.
|
// Returns true if strings are equal.
|
||||||
friend bool operator==(JsonString lhs, JsonString rhs) {
|
friend bool operator==(JsonString lhs, JsonString rhs) {
|
||||||
if (lhs.size_ != rhs.size_)
|
if (lhs.size() != rhs.size())
|
||||||
return false;
|
return false;
|
||||||
if (lhs.data_ == rhs.data_)
|
if (lhs.c_str() == rhs.c_str())
|
||||||
return true;
|
return true;
|
||||||
if (!lhs.data_)
|
if (!lhs.c_str())
|
||||||
return false;
|
return false;
|
||||||
if (!rhs.data_)
|
if (!rhs.c_str())
|
||||||
return false;
|
return false;
|
||||||
return memcmp(lhs.data_, rhs.data_, lhs.size_) == 0;
|
return memcmp(lhs.c_str(), rhs.c_str(), lhs.size()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if strings differs.
|
// Returns true if strings differs.
|
||||||
@ -76,9 +78,7 @@ class JsonString {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* data_;
|
detail::RamString str_;
|
||||||
size_t size_;
|
|
||||||
Ownership ownership_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user