Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SizedRamStringAdapter.hpp Source File

SizedRamStringAdapter.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/IsString.hpp>
00009 #include <ArduinoJson/Strings/StoragePolicy.hpp>
00010 
00011 #include <string.h>  // strcmp
00012 
00013 namespace ARDUINOJSON_NAMESPACE {
00014 
00015 class SizedRamStringAdapter {
00016  public:
00017   SizedRamStringAdapter(const char* str, size_t n) : _str(str), _size(n) {}
00018 
00019   int compare(const char* other) const {
00020     return safe_strncmp(_str, other, _size);
00021   }
00022 
00023   bool equals(const char* expected) const {
00024     return compare(expected) == 0;
00025   }
00026 
00027   bool isNull() const {
00028     return !_str;
00029   }
00030 
00031   void copyTo(char* p, size_t n) const {
00032     memcpy(p, _str, n);
00033   }
00034 
00035   size_t size() const {
00036     return _size;
00037   }
00038 
00039   const char* begin() const {
00040     return _str;
00041   }
00042 
00043   typedef storage_policies::store_by_copy storage_policy;
00044 
00045  private:
00046   const char* _str;
00047   size_t _size;
00048 };
00049 
00050 template <typename TChar>
00051 inline SizedRamStringAdapter adaptString(const TChar* str, size_t size) {
00052   return SizedRamStringAdapter(reinterpret_cast<const char*>(str), size);
00053 }
00054 
00055 }  // namespace ARDUINOJSON_NAMESPACE