FlashString.hpp 1.1 KB

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