Fixed incorrect string comparison on some platforms (fixes #1198)

This commit is contained in:
Benoit Blanchon
2020-02-27 11:44:09 +01:00
parent 2996503b27
commit 2641697e0b
10 changed files with 110 additions and 57 deletions

View File

@ -10,23 +10,23 @@
namespace ARDUINOJSON_NAMESPACE {
inline int8_t safe_strcmp(const char* a, const char* b) {
inline int safe_strcmp(const char* a, const char* b) {
if (a == b)
return 0;
if (!a)
return -1;
if (!b)
return 1;
return static_cast<int8_t>(strcmp(a, b));
return strcmp(a, b);
}
inline int8_t safe_strncmp(const char* a, const char* b, size_t n) {
inline int safe_strncmp(const char* a, const char* b, size_t n) {
if (a == b)
return 0;
if (!a)
return -1;
if (!b)
return 1;
return static_cast<int8_t>(strncmp(a, b, n));
return strncmp(a, b, n);
}
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -31,7 +31,7 @@ class ArduinoStringAdapter {
return !_str->c_str();
}
int8_t compare(const char* other) const {
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);

View File

@ -17,7 +17,7 @@ class ConstRamStringAdapter {
public:
ConstRamStringAdapter(const char* str = 0) : _str(str) {}
int8_t compare(const char* other) const {
int compare(const char* other) const {
return safe_strcmp(_str, other);
}

View File

@ -15,14 +15,14 @@ class FlashStringAdapter {
public:
FlashStringAdapter(const __FlashStringHelper* str) : _str(str) {}
int8_t compare(const char* other) const {
int compare(const char* other) const {
if (!other && !_str)
return 0;
if (!_str)
return -1;
if (!other)
return 1;
return int8_t(-strcmp_P(other, reinterpret_cast<const char*>(_str)));
return -strcmp_P(other, reinterpret_cast<const char*>(_str));
}
bool equals(const char* expected) const {

View File

@ -15,15 +15,14 @@ class SizedFlashStringAdapter {
SizedFlashStringAdapter(const __FlashStringHelper* str, size_t sz)
: _str(str), _size(sz) {}
int8_t compare(const char* other) const {
int compare(const char* other) const {
if (!other && !_str)
return 0;
if (!_str)
return -1;
if (!other)
return 1;
return int8_t(
-strncmp_P(other, reinterpret_cast<const char*>(_str), _size));
return -strncmp_P(other, reinterpret_cast<const char*>(_str), _size);
}
bool equals(const char* expected) const {

View File

@ -16,8 +16,8 @@ class SizedRamStringAdapter {
public:
SizedRamStringAdapter(const char* str, size_t n) : _str(str), _size(n) {}
int8_t compare(const char* other) const {
return safe_strncmp(_str, other, _size) == 0;
int compare(const char* other) const {
return safe_strncmp(_str, other, _size);
}
bool equals(const char* expected) const {

View File

@ -30,10 +30,10 @@ class StlStringAdapter {
return false;
}
int8_t compare(const char* other) const {
int compare(const char* other) const {
if (!other)
return 1;
return static_cast<int8_t>(_str->compare(other));
return _str->compare(other);
}
bool equals(const char* expected) const {