Service_NSSM/CC_SDK/Include/basic/CCByteArray.h

260 lines
9.0 KiB
C
Raw Normal View History

2025-09-27 14:24:18 +08:00
#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<Byte> 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<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 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<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> 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<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 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