forked from bblanchon/ArduinoJson
Added fallbacks strlen_P, strncmp_P, strcmp_P and memcpy_P (fixes #1073)
This commit is contained in:
@ -15,7 +15,7 @@ class UnsafeFlashStringReader {
|
||||
: _ptr(reinterpret_cast<const char*>(ptr)) {}
|
||||
|
||||
int read() {
|
||||
return pgm_read_byte_near(_ptr++);
|
||||
return pgm_read_byte(_ptr++);
|
||||
}
|
||||
};
|
||||
|
||||
@ -29,7 +29,7 @@ class SafeFlashStringReader {
|
||||
|
||||
int read() {
|
||||
if (_ptr < _end)
|
||||
return pgm_read_byte_near(_ptr++);
|
||||
return pgm_read_byte(_ptr++);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
62
src/ArduinoJson/Polyfills/pgmspace.hpp
Normal file
62
src/ArduinoJson/Polyfills/pgmspace.hpp
Normal file
@ -0,0 +1,62 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
// Wraps a const char* so that the our functions are picked only if the
|
||||
// originals are missing
|
||||
struct pgm_p {
|
||||
pgm_p(const char* p) : address(p) {}
|
||||
const char* address;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#ifndef strlen_P
|
||||
inline size_t strlen_P(ARDUINOJSON_NAMESPACE::pgm_p s) {
|
||||
const char* p = s.address;
|
||||
while (pgm_read_byte(p)) p++;
|
||||
return size_t(p - s.address);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef strncmp_P
|
||||
inline int strncmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b, size_t n) {
|
||||
const char* s1 = a;
|
||||
const char* s2 = b.address;
|
||||
while (n-- > 0) {
|
||||
char c1 = *s1++;
|
||||
char c2 = static_cast<char>(pgm_read_byte(s2++));
|
||||
if (c1 < c2) return -1;
|
||||
if (c1 > c2) return 1;
|
||||
if (c1 == 0 /* and c2 as well */) return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef strcmp_P
|
||||
inline int strcmp_P(const char* a, ARDUINOJSON_NAMESPACE::pgm_p b) {
|
||||
const char* s1 = a;
|
||||
const char* s2 = b.address;
|
||||
for (;;) {
|
||||
char c1 = *s1++;
|
||||
char c2 = static_cast<char>(pgm_read_byte(s2++));
|
||||
if (c1 < c2) return -1;
|
||||
if (c1 > c2) return 1;
|
||||
if (c1 == 0 /* and c2 as well */) return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef memcpy_P
|
||||
inline void* memcpy_P(void* dst, ARDUINOJSON_NAMESPACE::pgm_p src, size_t n) {
|
||||
uint8_t* d = reinterpret_cast<uint8_t*>(dst);
|
||||
const char* s = src.address;
|
||||
while (n-- > 0) {
|
||||
*d++ = pgm_read_byte(s++);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
#endif
|
@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/pgmspace.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class FlashStringAdapter {
|
||||
|
Reference in New Issue
Block a user