Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SizedFlashStringAdapter.hpp Source File

SizedFlashStringAdapter.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Namespace.hpp>
00008 #include <ArduinoJson/Strings/FlashStringIterator.hpp>
00009 #include <ArduinoJson/Strings/IsString.hpp>
00010 #include <ArduinoJson/Strings/StoragePolicy.hpp>
00011 
00012 namespace ARDUINOJSON_NAMESPACE {
00013 
00014 class SizedFlashStringAdapter {
00015  public:
00016   SizedFlashStringAdapter(const __FlashStringHelper* str, size_t sz)
00017       : _str(str), _size(sz) {}
00018 
00019   int compare(const char* other) const {
00020     if (!other && !_str)
00021       return 0;
00022     if (!_str)
00023       return -1;
00024     if (!other)
00025       return 1;
00026     return -strncmp_P(other, reinterpret_cast<const char*>(_str), _size);
00027   }
00028 
00029   bool equals(const char* expected) const {
00030     return compare(expected) == 0;
00031   }
00032 
00033   bool isNull() const {
00034     return !_str;
00035   }
00036 
00037   void copyTo(char* p, size_t n) const {
00038     memcpy_P(p, reinterpret_cast<const char*>(_str), n);
00039   }
00040 
00041   size_t size() const {
00042     return _size;
00043   }
00044 
00045   FlashStringIterator begin() const {
00046     return FlashStringIterator(_str);
00047   }
00048 
00049   typedef storage_policies::store_by_copy storage_policy;
00050 
00051  private:
00052   const __FlashStringHelper* _str;
00053   size_t _size;
00054 };
00055 
00056 inline SizedFlashStringAdapter adaptString(const __FlashStringHelper* str,
00057                                            size_t sz) {
00058   return SizedFlashStringAdapter(str, sz);
00059 }
00060 }  // namespace ARDUINOJSON_NAMESPACE