Distribution_Service/CC_SDK/Include/basic/CCByteArray.h

385 lines
14 KiB
C
Raw Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CC_BYTEARRAY_H
#define CC_BYTEARRAY_H
#include "CCByte.h"
#include "CCEncode.h"
#include "CCVector.h"
#include "TL/AutoDestruct.h"
typedef size_t ByteHander; //
#define HanderSize sizeof(ByteHander)
namespace CTL {
/**
* ByteArray类用于处理字节数组
*/
class ByteArray{
std::vector<Byte> bytes;
public:
ByteArray() = default;
/**
* std::string初始化字节数组
* @param str
*/
ByteArray(const std::string& str);
/**
*
* @param str
* @param size
*/
ByteArray(const void* str, size_t size);
/**
* C风格字符串初始化字节数组
* @param str C风格字符串
*/
ByteArray(const char* str);
/**
* ByteArray对象初始化字节数组
* @param other ByteArray对象
*/
ByteArray(const ByteArray& other);
/**
* std::vector<Byte>
* @param bytes vector
*/
ByteArray(const std::vector<Byte>& bytes);
/**
* std::list<Byte>
* @param bytes list
*/
ByteArray(const std::list<Byte>& bytes);
/**
* Byte指针初始化字节数组
* @param bytes Byte指针
*/
ByteArray(const Byte* bytes);
/**
*
* @param initList
*/
ByteArray(std::initializer_list<Byte> initList);
/**
*
* @param size
*/
ByteArray(ByteHander size);
/**
*
* @param byte
*/
void add(Byte byte);
/**
*
* @param index
* @return
*/
[[nodiscard]] Byte get(int index) const;
/**
*
* @param index
* @param byte
*/
void set(int index, Byte byte);
/**
*
* @param index
*/
void remove(int index);
/**
*
* @return
*/
[[nodiscard]] size_t size() const;
/**
*
*/
void clear();
/**
*
* @param buffer
* @param size
*/
void assign(char * buffer, ByteHander size);
/**
*
* @param buffer
* @param size
*/
void assign(const void* buffer, ByteHander size);
/**
*
* @return
*/
[[nodiscard]] bool IsEmpty() const;
/**
*
* @param str
*/
void append(const std::string& str);
/**
*
* @param str
*/
void append(const ByteArray& str);
/**
*
* @param initList
*/
void append(std::initializer_list<Byte> initList);
/**
*
* @return
*/
std::vector<Byte>::iterator begin();
/**
*
* @return
*/
std::vector<Byte>::iterator end();
/**
*
* @return
*/
std::vector<Byte>::const_iterator begin() const;
/**
*
* @return
*/
std::vector<Byte>::const_iterator end() const;
/**
*
* @param str
* @return
*/
static ByteArray fromString(const std::string& str);
/**
*
* @return
*/
std::string toString() const;
/**
*
* @param EncodeStr "GBK"
* @return
*/
std::string Format(const std::string& EncodeStr = "GBK") const;
/**
*
* @param EncodeStr "GBK"
* @return
*/
ByteArray toFormat(const std::string& EncodeStr = "GBK") const;
/**
* vector
* @return vector
*/
std::vector<Byte> toVector();
/**
* vector
* @return vector
*/
std::vector<Byte> toVector() const;
/**
*
* @return
*/
char* buffer();
/**
*
* @return
*/
[[nodiscard]] const char* buffer() const;
/**
*
* @return ()
*/
char* newBuffer() const;
/**
*
* @param len
*/
void resize(size_t len);
/**
*
* @param other
*/
void operator = (const ByteArray& other);
/**
*
* @param str
*/
void operator = (const std::string& str);
/**
*
* @param initList
*/
void operator = (std::initializer_list<Byte> initList);
/**
*
* @param other
* @return truefalse
*/
bool operator == (const ByteArray& other) const;
/**
*
* @param other
* @return truefalse
*/
bool operator != (const ByteArray& other) const;
/**
*
* @param other
*/
void operator += (const ByteArray& other);
/**
*
* @param other
*/
void operator -= (const ByteArray& other);
/**
*
* @param index
* @return
*/
Byte& operator [] (int index);
/**
*
* @param index
* @return
*/
Byte operator [] (int index) const;
/**
*
* @param other
* @return ()
*/
template<typename T>
static T* Conversion(const ByteArray& other) {
const T* t_const = reinterpret_cast<const T*>(other.newBuffer());
T* t = const_cast<T*>(t_const);
return t;
}
/**
*
* @return (RAII管理内存)
*/
template<typename T>
AutoDestruct<T> Conversion() {
const T* t_const = reinterpret_cast<const T*>(this->newBuffer());
T* t = const_cast<T*>(t_const);
return CTL::AutoDestruct<T>(t);
}
/**
*
* @param str
* @param size
* @return (RAII管理内存)
*/
template<typename T>
void Conversion(T* str, const ByteHander size) {
const auto c_str = reinterpret_cast<const char*>(str);
2026-03-24 18:14:23 +08:00
bytes.assign(&c_str[0], &c_str[0] + size);
2025-11-11 17:46:19 +08:00
}
/**
*
* @param start
* @param end
* @return
*/
[[nodiscard]] ByteArray subBuffer(ByteHander start, ByteHander end) const;
/**
* Base64字符串
* @return Base64字符串
*/
std::string toBase64();
/**
* Base64字符串转换为字节数组
* @param base64Str Base64字符串
* @return
*/
static ByteArray fromBase64(const std::string& base64Str);
/**
*
* @param value
* @param start
* @param end
*/
void fill(Byte value, size_t start = 0, size_t end = -1);
/**
* ByteArray
* @param dest ByteArray
* @param srcStart
* @param destStart
* @param count
*/
void copyTo(ByteArray& dest, size_t srcStart = 0, size_t destStart = 0, size_t count = -1) const;
/**
* ByteArray
* @return ByteArray副本
*/
ByteArray clone() const;
/**
*
* @param value
* @param start
* @return -1
*/
int indexOf(Byte value, size_t start = 0) const;
/**
*
* @param value
* @param start
* @return -1
*/
int lastIndexOf(Byte value, size_t start = -1) const;
/**
*
* @param pattern
* @param start
* @return -1
*/
int indexOf(const ByteArray& pattern, size_t start = 0) const;
/**
*
* @param pattern
* @param start
* @return -1
*/
int lastIndexOf(const ByteArray& pattern, size_t start = -1) const;
/**
* ByteArray是否相等
* @param other ByteArray
* @return truefalse
*/
bool equals(const ByteArray& other) const;
};
/**
*
* @param str
*/
inline void ByteArray::append(const ByteArray &str) {
bytes.insert(bytes.end(), str.bytes.begin(), str.bytes.end());
}
/**
*
* @param os
* @param arr
* @return
*/
inline std::ostream& operator<<(std::ostream& os, const ByteArray& arr) {
os << "[";
for (size_t i = 0; i < arr.size(); ++i) {
if (i != 0) {
os << ", ";
}
os << static_cast<int>(arr.get(i).get());
}
os << "]";
return os;
}
}
#endif