diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-09-17 10:02:07 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-09-17 10:02:07 +0200 |
commit | 4f6253aa9fec99260b8bb7b9b2e9003f5259b600 (patch) | |
tree | 2d0a3fdd10e258ecce5fb220547b1c43b870d6d2 /include/lib/ArduinoJson/Polyfills | |
parent | 30c4f72770568749b4230a6b598e3fb87a065e91 (diff) |
Import arduinojson and ubjson. Only partially working at the moment
Diffstat (limited to 'include/lib/ArduinoJson/Polyfills')
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/attributes.hpp | 29 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/ctype.hpp | 18 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/isFloat.hpp | 38 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/isInteger.hpp | 19 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/math.hpp | 19 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/parseFloat.hpp | 90 | ||||
-rw-r--r-- | include/lib/ArduinoJson/Polyfills/parseInteger.hpp | 41 |
7 files changed, 254 insertions, 0 deletions
diff --git a/include/lib/ArduinoJson/Polyfills/attributes.hpp b/include/lib/ArduinoJson/Polyfills/attributes.hpp new file mode 100644 index 0000000..b49091d --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/attributes.hpp @@ -0,0 +1,29 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#ifdef _MSC_VER // Visual Studio + +#define FORCE_INLINE // __forceinline causes C4714 when returning std::string +#define NO_INLINE __declspec(noinline) +#define DEPRECATED(msg) __declspec(deprecated(msg)) + +#elif defined(__GNUC__) // GCC or Clang + +#define FORCE_INLINE __attribute__((always_inline)) +#define NO_INLINE __attribute__((noinline)) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +#define DEPRECATED(msg) __attribute__((deprecated(msg))) +#else +#define DEPRECATED(msg) __attribute__((deprecated)) +#endif + +#else // Other compilers + +#define FORCE_INLINE +#define NO_INLINE +#define DEPRECATED(msg) + +#endif diff --git a/include/lib/ArduinoJson/Polyfills/ctype.hpp b/include/lib/ArduinoJson/Polyfills/ctype.hpp new file mode 100644 index 0000000..2d52703 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/ctype.hpp @@ -0,0 +1,18 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { + +inline bool isdigit(char c) { + return '0' <= c && c <= '9'; +} + +inline bool issign(char c) { + return '-' == c || c == '+'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isFloat.hpp b/include/lib/ArduinoJson/Polyfills/isFloat.hpp new file mode 100644 index 0000000..973b89f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isFloat.hpp @@ -0,0 +1,38 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include <string.h> // for strcmp +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isFloat(const char* s) { + if (!s) return false; + + if (!strcmp(s, "NaN")) return true; + if (issign(*s)) s++; + if (!strcmp(s, "Infinity")) return true; + if (*s == '\0') return false; + + while (isdigit(*s)) s++; + + if (*s == '.') { + s++; + while (isdigit(*s)) s++; + } + + if (*s == 'e' || *s == 'E') { + s++; + if (issign(*s)) s++; + if (!isdigit(*s)) return false; + while (isdigit(*s)) s++; + } + + return *s == '\0'; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/isInteger.hpp b/include/lib/ArduinoJson/Polyfills/isInteger.hpp new file mode 100644 index 0000000..8049079 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/isInteger.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { + +inline bool isInteger(const char* s) { + if (!s || !*s) return false; + if (issign(*s)) s++; + while (isdigit(*s)) s++; + return *s == '\0'; +} +} // namespace Internals +} // namespace ArduinoJson diff --git a/include/lib/ArduinoJson/Polyfills/math.hpp b/include/lib/ArduinoJson/Polyfills/math.hpp new file mode 100644 index 0000000..48773ed --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/math.hpp @@ -0,0 +1,19 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +namespace ArduinoJson { +namespace Internals { +template <typename T> +bool isNaN(T x) { + return x != x; +} + +template <typename T> +bool isInfinity(T x) { + return x != 0.0 && x * 2 == x; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseFloat.hpp b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp new file mode 100644 index 0000000..49b0f6f --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseFloat.hpp @@ -0,0 +1,90 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include "../TypeTraits/FloatTraits.hpp" +#include "./ctype.hpp" +#include "./math.hpp" + +namespace ArduinoJson { +namespace Internals { + +template <typename T> +inline T parseFloat(const char* s) { + typedef FloatTraits<T> traits; + typedef typename traits::mantissa_type mantissa_t; + typedef typename traits::exponent_type exponent_t; + + if (!s) return 0; // NULL + + bool negative_result = false; + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + if (*s == 't') return 1; // true + if (*s == 'n' || *s == 'N') return traits::nan(); + if (*s == 'i' || *s == 'I') + return negative_result ? -traits::inf() : traits::inf(); + + mantissa_t mantissa = 0; + exponent_t exponent_offset = 0; + + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) + mantissa = mantissa * 10 + (*s - '0'); + else + exponent_offset++; + s++; + } + + if (*s == '.') { + s++; + while (isdigit(*s)) { + if (mantissa < traits::mantissa_max / 10) { + mantissa = mantissa * 10 + (*s - '0'); + exponent_offset--; + } + s++; + } + } + + int exponent = 0; + if (*s == 'e' || *s == 'E') { + s++; + bool negative_exponent = false; + if (*s == '-') { + negative_exponent = true; + s++; + } else if (*s == '+') { + s++; + } + + while (isdigit(*s)) { + exponent = exponent * 10 + (*s - '0'); + if (exponent + exponent_offset > traits::exponent_max) { + if (negative_exponent) + return negative_result ? -0.0f : 0.0f; + else + return negative_result ? -traits::inf() : traits::inf(); + } + s++; + } + if (negative_exponent) exponent = -exponent; + } + exponent += exponent_offset; + + T result = traits::make_float(static_cast<T>(mantissa), exponent); + + return negative_result ? -result : result; +} +} +} diff --git a/include/lib/ArduinoJson/Polyfills/parseInteger.hpp b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp new file mode 100644 index 0000000..e8f1974 --- /dev/null +++ b/include/lib/ArduinoJson/Polyfills/parseInteger.hpp @@ -0,0 +1,41 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2018 +// MIT License + +#pragma once + +#include <stdlib.h> + +#include "../Configuration.hpp" +#include "./ctype.hpp" + +namespace ArduinoJson { +namespace Internals { +template <typename T> +T parseInteger(const char *s) { + if (!s) return 0; // NULL + + if (*s == 't') return 1; // "true" + + T result = 0; + bool negative_result = false; + + switch (*s) { + case '-': + negative_result = true; + s++; + break; + case '+': + s++; + break; + } + + while (isdigit(*s)) { + result = T(result * 10 + T(*s - '0')); + s++; + } + + return negative_result ? T(~result + 1) : result; +} +} +} |