#ifndef CCBYTE_CCBYTEARRAY_H #define CCBYTE_CCBYTEARRAY_H #include "CCByte.h" #include "CCEncode.h" #include "CCVector.h" typedef long long ByteHander; #define HanderSize sizeof(ByteHander) namespace CTL { /** * ByteArray类用于处理字节数组,提供多种方式的字节操作和转换。 */ class ByteArray { private: std::vector bytes; public: // 默认构造函数 ByteArray() = default; /** * 构造函数,从std::string初始化字节数组。 * @param str 用于初始化字节数组的字符串。 */ ByteArray(const std::string& str); /** * 构造函数,从C风格字符串初始化字节数组。 * @param str 用于初始化字节数组的C风格字符串。 */ ByteArray(const char* str); /** * 复制构造函数,从另一个ByteArray对象初始化字节数组。 * @param other 用于初始化的另一个ByteArray对象。 */ ByteArray(const ByteArray& other); /** * 构造函数,从std::vector初始化字节数组。 * @param bytes 用于初始化字节数组的vector。 */ ByteArray(const std::vector& bytes); /** * 构造函数,从std::list初始化字节数组。 * @param bytes 用于初始化字节数组的list。 */ ByteArray(const std::list& bytes); /** * 构造函数,从Byte指针初始化字节数组。 * @param bytes 用于初始化字节数组的Byte指针。 */ ByteArray(const Byte* bytes); /** * 构造函数,从初始化列表初始化字节数组。 * @param initList 用于初始化字节数组的初始化列表。 */ ByteArray(std::initializer_list initList); /** * 添加一个字节到字节数组末尾。 * @param byte 要添加的字节。 */ void add(Byte byte); /** * 获取指定索引处的字节。 * @param index 字节的索引。 * @return 指定索引处的字节。 */ 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(char * buffer, unsigned int size); // /** // * 用指定的缓冲区和大小赋值字节数组。 // * @param buffer 指定的缓冲区。 // * @param size 缓冲区的大小。 // */ // void assign(char * buffer, int size); #ifdef __linux__ /** * 用指定的缓冲区和大小赋值字节数组。 * @param buffer 指定的缓冲区 * @param size 缓冲区的大小。 */ // void assign(char* buffer, std::ptrdiff_t size); #endif /** * 在字节数组末尾追加一个字符串。 * @param str 要追加的字符串。 */ void append(const std::string& str); /** * 在字节数组末尾追加另一个字节数组。 * @param str 要追加的字节数组。 */ void append(const ByteArray& str); /** * 在字节数组末尾追加另一个数组列表。 * @param initList 要追加的数组列表。 */ void append(std::initializer_list initList); /** * 返回字节数组的开始迭代器。 * @return 字节数组的开始迭代器。 */ std::vector::iterator begin(); /** * 返回字节数组的结束迭代器。 * @return 字节数组的结束迭代器。 */ std::vector::iterator end(); /** * 返回常量字节数组的开始迭代器。 * @return 常量字节数组的开始迭代器。 */ std::vector::const_iterator begin() const; /** * 返回常量字节数组的结束迭代器。 * @return 常量字节数组的结束迭代器。 */ std::vector::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 data(); /** * 获取字节数组的缓冲区。 * @return 字节数组的缓冲区。 */ char* buffer(); /** * 获取字节数组的缓冲区。 * @return 字节数组的缓冲区。 */ [[nodiscard]] const char* buffer() 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 initList); /** * 比较运算符重载,用于比较两个字节数组是否相等。 * @param other 要比较的另一个字节数组。 * @return 如果两个字节数组相等则返回true,否则返回false。 */ bool operator == (const ByteArray& other) const; /** * 比较运算符重载,用于比较两个字节数组是否不相等。 * @param other 要比较的另一个字节数组。 * @return 如果两个字节数组不相等则返回true,否则返回false。 */ bool operator != (const ByteArray& other) const; /** * 加法运算符重载,用于追加另一个字节数组到当前字节数组末尾。 * @param other 要追加的另一个字节数组。 */ void operator += (const ByteArray& other); /** * 减法运算符重载,用于从当前字节数组中移除另一个字节数组的内容。 * @param other 要移除的另一个字节数组。 */ void operator -= (const ByteArray& other); }; /** * 在字节数组末尾追加另一个字节数组。 * @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(arr.get(i).get()); } os << "]"; return os; } } #endif