FlashString.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #pragma once
  8. #if ARDUINOJSON_ENABLE_PROGMEM
  9. namespace ArduinoJson {
  10. namespace Internals {
  11. template <>
  12. struct StringTraits<const __FlashStringHelper*, void> {
  13. class Reader {
  14. const char* _ptr;
  15. public:
  16. Reader(const __FlashStringHelper* ptr)
  17. : _ptr(reinterpret_cast<const char*>(ptr)) {}
  18. void move() {
  19. _ptr++;
  20. }
  21. char current() const {
  22. return pgm_read_byte_near(_ptr);
  23. }
  24. char next() const {
  25. return pgm_read_byte_near(_ptr + 1);
  26. }
  27. };
  28. static bool equals(const __FlashStringHelper* str, const char* expected) {
  29. return strcmp_P(expected, (PGM_P)str) == 0;
  30. }
  31. template <typename Buffer>
  32. static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) {
  33. if (!str) return NULL;
  34. size_t size = strlen_P((PGM_P)str) + 1;
  35. void* dup = buffer->alloc(size);
  36. if (dup != NULL) memcpy_P(dup, (PGM_P)str, size);
  37. return static_cast<char*>(dup);
  38. }
  39. static const bool has_append = false;
  40. static const bool has_equals = true;
  41. static const bool should_duplicate = true;
  42. };
  43. }
  44. }
  45. #endif