#pragma once #include #include #include #include #include #include #include "util/rsprint.h" #include "util/rsdebug.h" // This class re-implements QByteArray from Qt library. class ByteArray: public std::vector { public: ByteArray() =default; explicit ByteArray(int n) : std::vector(n) {} explicit ByteArray(const unsigned char *d,int n) : std::vector(n) { memcpy(data(),d,n); } virtual ~ByteArray() =default; ByteArray(const std::string& c) { resize(c.size()); memcpy(data(),c.c_str(),c.size()); } const ByteArray& operator=(const std::string& c) { resize(c.size()); memcpy(data(),c.c_str(),c.size()); return *this; } bool isNull() const { return empty(); } ByteArray toHex() const { return ByteArray(RsUtil::BinToHex(data(),size(),0)); } std::string toString() const { std::string res; for(auto c:*this) res += c; return res; } ByteArray operator+(const ByteArray& b) const { auto res(*this); for(unsigned char c:b) res.push_back(c); return res; } ByteArray operator+(const std::string& b) const { return operator+(ByteArray(b)); } void append(const ByteArray& b) { for(auto c:b) push_back(c); } void append(const char *b) { for(uint32_t n=0;b[n]!=0;++n) push_back(b[n]); } template void append(const T) = delete;// Prevents any implicit when calling the preceding functions which actually causes real bugs. ByteArray& operator+=(char b) { push_back(b); return *this; } ByteArray& operator+=(const ByteArray& b) { for(auto c:b) push_back(c); return *this; } ByteArray& operator+=(const char *b) { for(uint32_t n=0;b[n]!=0;++n) push_back(b[n]); return *this;} ByteArray left(uint32_t l) const { auto res = *this; res.resize(std::min((uint32_t)size(),l)); return res; } ByteArray toUpper() const { auto res = *this; for(uint32_t i=0;i='a') res[i] += int('A')-int('a'); return res; } ByteArray toLower() const { auto res = *this; for(uint32_t i=0;i='A') res[i] += int('a')-int('A'); return res; } int toInt() const { std::istringstream is(toString().c_str()); int res = -1; is >> res ; return res; } bool endsWith(const ByteArray& b) const { return size() >= b.size() && !memcmp(&data()[size()-b.size()],b.data(),b.size()); } bool endsWith(char b) const { return size() > 0 && back()==b; } bool startsWith(const ByteArray& b) const { return b.size() <= size() && !strncmp((char*)b.data(),(char*)data(),std::min(size(),b.size())); } bool startsWith(const char *b) const { for(uint32_t n=0;b[n]!=0;++n) if(n >= size() || b[n]!=(*this)[n]) return false; return true; } bool operator==(const char *b) const { uint32_t n; for(n=0;b[n]!=0;++n) if(n >= size() || b[n]!=(*this)[n]) return false; return n==size(); } ByteArray mid(uint32_t n,int s=-1) const { ByteArray res((s>=0)?s:(size()-n)); memcpy(res.data(),&data()[n],res.size()); return res; } int indexOf(unsigned char c,int from=0) const { for(uint32_t i=from;i split(unsigned char sep) { std::list res; ByteArray current_block; for(uint32_t i=0;i split(const ByteArray& sep) { std::list res; ByteArray current_block; for(uint32_t i=0;i=0;--i) if(operator[](i) == s) return i; return -1; } };