linux 编译
This commit is contained in:
+128
-97
@@ -14,71 +14,74 @@
|
||||
#undef min
|
||||
#undef max
|
||||
namespace Psc {
|
||||
|
||||
|
||||
template <typename T = std::pmr::string>
|
||||
template<typename T = std::pmr::string>
|
||||
typename std::enable_if<std::is_same_v<T, std::pmr::string>, T>::type
|
||||
create_string(size_t size, std::pmr::memory_resource* resource = nullptr) {
|
||||
std::pmr::memory_resource* r = resource ? resource : std::pmr::get_default_resource();
|
||||
std::pmr::string ret(r);
|
||||
ret.resize(size);
|
||||
return ret; // 返回 std::pmr::string
|
||||
create_string(size_t size, std::pmr::memory_resource *resource = nullptr) {
|
||||
std::pmr::memory_resource *r = resource ? resource : std::pmr::get_default_resource();
|
||||
std::pmr::string ret(r);
|
||||
ret.resize(size);
|
||||
return ret; // 返回 std::pmr::string
|
||||
}
|
||||
|
||||
template <typename T = std::string>
|
||||
template<typename T = std::string>
|
||||
std::enable_if_t<std::is_same_v<T, std::string>, T>
|
||||
create_string(size_t size, std::pmr::memory_resource* resource = nullptr) {
|
||||
create_string(size_t size, std::pmr::memory_resource *resource = nullptr) {
|
||||
std::string ret;
|
||||
ret.resize(size);
|
||||
return ret; // 返回 std::string
|
||||
return ret; // 返回 std::string
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
struct cacl_type {
|
||||
using type = T;
|
||||
static size_t size(T& a) {
|
||||
|
||||
static size_t size(T &a) {
|
||||
return a.size();
|
||||
}
|
||||
static void* data(T& a) {
|
||||
|
||||
static void *data(T &a) {
|
||||
return static_cast<void *>(a.data());
|
||||
}
|
||||
static size_t size(const T& a) {
|
||||
|
||||
static size_t size(const T &a) {
|
||||
return a.size();
|
||||
}
|
||||
static void* data(const T& a) {
|
||||
return (void*)a.data();
|
||||
|
||||
static void *data(const T &a) {
|
||||
return (void *) a.data();
|
||||
}
|
||||
};
|
||||
template <>
|
||||
|
||||
template<>
|
||||
struct cacl_type<std::string_view> {
|
||||
using type = std::string;
|
||||
static size_t size(std::string_view a) {return a.size();}
|
||||
static void* data(std::string_view a) {return (void*)a.data();}
|
||||
};
|
||||
template <>
|
||||
struct cacl_type<const char*> {
|
||||
using type = std::string;
|
||||
static size_t size(const char* a) {return std::strlen(a);}
|
||||
static void* data(const char* a) {return (void*)a;}
|
||||
static size_t size(std::string_view a) { return a.size(); }
|
||||
static void *data(std::string_view a) { return (void *) a.data(); }
|
||||
};
|
||||
|
||||
template <size_t N>
|
||||
template<>
|
||||
struct cacl_type<const char *> {
|
||||
using type = std::string;
|
||||
static size_t size(const char *a) { return std::strlen(a); }
|
||||
static void *data(const char *a) { return (void *) a; }
|
||||
};
|
||||
|
||||
template<size_t N>
|
||||
struct cacl_type<char[N]> {
|
||||
using type = std::string;
|
||||
static size_t size(const char (&a)[N]) { return N - 1; }
|
||||
static void* data(const char (&a)[N]) { return (void*)a; }
|
||||
static void *data(const char (&a)[N]) { return (void *) a; }
|
||||
};
|
||||
|
||||
template <size_t N>
|
||||
template<size_t N>
|
||||
struct cacl_type<const char[N]> {
|
||||
using type = std::string;
|
||||
static size_t size(const char (&a)[N]) { return N - 1; }
|
||||
static void* data(const char (&a)[N]) { return (void*)a; }
|
||||
static void *data(const char (&a)[N]) { return (void *) a; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define Base template <typename allocator_type = std::allocator<char>, \
|
||||
typename STR_Type = std::basic_string<char, std::char_traits<char>, allocator_type>, \
|
||||
typename ret_type = typename cacl_type<STR_Type>::type \
|
||||
@@ -92,34 +95,40 @@ typename ret_type = typename cacl_type<STR_Type>::type \
|
||||
namespace Psc {
|
||||
// 检查当前机器是否是大端字节序
|
||||
extern const bool is_big_endian;
|
||||
void big_endian_2_platform(void* dest, const char* d, size_t size);
|
||||
void little_endian_2_platform(void* dest, const char* d, size_t size);
|
||||
|
||||
void big_endian_2_platform(void *dest, const char *d, size_t size);
|
||||
|
||||
void little_endian_2_platform(void *dest, const char *d, size_t size);
|
||||
|
||||
|
||||
void big_endian_2_platform(const std::string &big_endian, char *dest);
|
||||
|
||||
void big_endian_2_platform(const std::string& big_endian, char* dest);
|
||||
void little_endian_2_platform(const std::string& little_endian, char* dest);
|
||||
void little_endian_2_platform(const std::string &little_endian, char *dest);
|
||||
|
||||
|
||||
std::string big_endian_2_platform(const std::string& big_endian);
|
||||
std::string little_endian_2_platform(const std::string& little_endian);
|
||||
std::string platform_2_big_endian(void* memory, int size);
|
||||
std::string platform_2_little_endian(void* memory, int size);
|
||||
std::string big_endian_2_platform(const std::string &big_endian);
|
||||
|
||||
std::string little_endian_2_platform(const std::string &little_endian);
|
||||
|
||||
std::string platform_2_big_endian(void *memory, int size);
|
||||
|
||||
std::string platform_2_little_endian(void *memory, int size);
|
||||
|
||||
|
||||
inline size_t get_xor_hex_size(size_t n1, size_t n2) {
|
||||
return n1 > n2 ? n1 : n2;
|
||||
}
|
||||
void xor_hex_Ex(void* dest, const char* s1, size_t n1, const char* s2, size_t n2, bool big_dian, bool is_upper);
|
||||
|
||||
void xor_hex_Ex(void *dest, const char *s1, size_t n1, const char *s2, size_t n2, bool big_dian, bool is_upper);
|
||||
|
||||
Base
|
||||
ret_type xorHex(const STR_Type& s1, const STR_Type& s2, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type xorHex(const STR_Type &s1, const STR_Type &s2, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto n1 = cacl_type<STR_Type>::size(s1);
|
||||
auto n2 = cacl_type<STR_Type>::size(s2);
|
||||
auto size = get_xor_hex_size(n1, n2);
|
||||
auto ret = create_string<ret_type>(size, resource);
|
||||
xor_hex_Ex((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(s1), n1, (const char*)cacl_type<STR_Type>::data(s2), n2, true, true);
|
||||
xor_hex_Ex((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(s1), n1,
|
||||
(const char *) cacl_type<STR_Type>::data(s2), n2, true, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -127,122 +136,142 @@ namespace Psc {
|
||||
inline size_t get_xor_bin_size(size_t n1, size_t n2) {
|
||||
return n1 > n2 ? n1 : n2;
|
||||
}
|
||||
void xor_bin_Ex(void* dest, const char* s1, size_t n1, const char* s2, size_t n2, bool big_dian);
|
||||
|
||||
void xor_bin_Ex(void *dest, const char *s1, size_t n1, const char *s2, size_t n2, bool big_dian);
|
||||
|
||||
Base
|
||||
ret_type xorBin(const STR_Type& s1, const STR_Type& s2, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type xorBin(const STR_Type &s1, const STR_Type &s2, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto n1 = cacl_type<STR_Type>::size(s1);
|
||||
auto n2 = cacl_type<STR_Type>::size(s2);
|
||||
auto size = get_xor_bin_size(n1, n2);
|
||||
auto ret = create_string<ret_type>(size, resource);
|
||||
xor_bin_Ex((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(s1), n1, (const char*)cacl_type<STR_Type>::data(s2), n2, true);
|
||||
xor_bin_Ex((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(s1), n1,
|
||||
(const char *) cacl_type<STR_Type>::data(s2), n2, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
inline size_t get_bin2hex_size(size_t num) { return (num + 3) / 4; }
|
||||
|
||||
void bin2hex_Ex(void *dest, const char *data, size_t size, bool big_dian, bool isupper);
|
||||
|
||||
inline size_t get_bin2hex_size(size_t num) {return (num + 3) / 4;}
|
||||
void bin2hex_Ex(void* dest, const char* data, size_t size, bool big_dian, bool isupper);
|
||||
Base
|
||||
ret_type bin2hex(const STR_Type& mem, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type bin2hex(const STR_Type &mem, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret = create_string<ret_type>(get_bin2hex_size(size), resource);
|
||||
bin2hex_Ex((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(mem), size, true, true);
|
||||
bin2hex_Ex((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(mem), size, true, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
inline size_t get_hex2bin_size(size_t num) { return num * 4; }
|
||||
|
||||
void hex2bin_Ex(void *dest, const char *data, size_t size, bool big_dian);
|
||||
|
||||
inline size_t get_hex2bin_size(size_t num) {return num * 4;}
|
||||
void hex2bin_Ex(void* dest, const char* data, size_t size, bool big_dian);
|
||||
Base
|
||||
ret_type hex2bin(const STR_Type& mem, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type hex2bin(const STR_Type &mem, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret = create_string<ret_type>(get_hex2bin_size(size), resource);
|
||||
hex2bin_Ex((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(mem), size, true);
|
||||
hex2bin_Ex((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(mem), size, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline size_t get_bin2mem_size(size_t num) {return num/8;};
|
||||
void bin2mem(void* dest, const char* data, size_t size);
|
||||
inline size_t get_bin2mem_size(size_t num) { return num / 8; };
|
||||
|
||||
void bin2mem(void *dest, const char *data, size_t size);
|
||||
|
||||
Base
|
||||
ret_type bin2mem(const STR_Type& mem, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type bin2mem(const STR_Type &mem, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret = create_string<ret_type>(get_bin2mem_size(size), resource);
|
||||
bin2mem((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(mem), size);
|
||||
bin2mem((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(mem), size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline size_t get_hex2mem_size(size_t hex_hum) {return hex_hum/2;};
|
||||
void hex2mem_Ex(void* dest, const char* data, size_t size);
|
||||
inline size_t get_hex2mem_size(size_t hex_hum) { return hex_hum / 2; };
|
||||
|
||||
void hex2mem_Ex(void *dest, const char *data, size_t size);
|
||||
|
||||
Base
|
||||
STR_Type hex2mem(const STR_Type& mem, std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type hex2mem(const STR_Type &mem, std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret = create_string<STR_Type>(get_hex2mem_size(size), resource);
|
||||
hex2mem_Ex((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(mem), size);
|
||||
auto ret = create_string<ret_type>(get_hex2mem_size(size), resource);
|
||||
hex2mem_Ex((void *) ret.data(), (const char *) cacl_type<STR_Type>::data(mem), size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline size_t get_mem2hex_size(size_t num, size_t middle_len) { return num * 2 + (num - 1) * middle_len;}
|
||||
void mem2hex_Ex2(void* dest, void* mem, size_t num, bool uppercase = true, const char* middle = "", size_t middle_len = std::numeric_limits<size_t>::max());
|
||||
inline size_t get_mem2hex_size(size_t num, size_t middle_len) { return num * 2 + (num - 1) * middle_len; }
|
||||
|
||||
void mem2hex_Ex2(void *dest, void *mem, size_t num, bool uppercase = true, const char *middle = "",
|
||||
size_t middle_len = std::numeric_limits<size_t>::max());
|
||||
|
||||
Base
|
||||
STR_Type mem2hex(const STR_Type& mem, bool uppercase = true, const std::string& middle = "", std::pmr::memory_resource* resource = nullptr) {
|
||||
ret_type mem2hex(const STR_Type &mem, bool uppercase = true, const std::string &middle = "",
|
||||
std::pmr::memory_resource *resource = nullptr) {
|
||||
auto size = cacl_type<STR_Type>::size(mem);
|
||||
auto ret = create_string<STR_Type>(get_mem2hex_size(size, middle.size()), resource);
|
||||
auto ret = create_string<ret_type>(get_mem2hex_size(size, middle.size()), resource);
|
||||
mem2hex_Ex2(ret.data(), cacl_type<STR_Type>::data(mem), size, uppercase, middle.data(), middle.size());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template <int start, int len>
|
||||
void get_bin(std::string& msg, int& n) {
|
||||
template<int start, int len>
|
||||
void get_bin(std::string &msg, int &n) {
|
||||
n = std::bitset<len>(msg.substr(start, len)).to_ullong();
|
||||
}
|
||||
template <int start, int len, typename Enum>
|
||||
void get_bin(std::string& msg, Enum& n) {
|
||||
|
||||
template<int start, int len, typename Enum>
|
||||
void get_bin(std::string &msg, Enum &n) {
|
||||
n = static_cast<Enum>(std::bitset<len>(msg.substr(start, len)).to_ullong());
|
||||
}
|
||||
template <int start, int len, typename Enum>
|
||||
Enum get_bin(std::string& msg) {
|
||||
|
||||
template<int start, int len, typename Enum>
|
||||
Enum get_bin(std::string &msg) {
|
||||
return static_cast<Enum>(std::bitset<len>(msg.substr(start, len)).to_ullong());
|
||||
}
|
||||
template <int start, int len>
|
||||
int get_bin(const std::string& msg) {
|
||||
|
||||
template<int start, int len>
|
||||
int get_bin(const std::string &msg) {
|
||||
return std::bitset<len>(msg.substr(start, len)).to_ullong();
|
||||
}
|
||||
template <int start, int len>
|
||||
void set_bin(std::string& msg, unsigned long long n) {
|
||||
|
||||
template<int start, int len>
|
||||
void set_bin(std::string &msg, unsigned long long n) {
|
||||
msg.replace(start, len, std::bitset<len>(n).to_string());
|
||||
}
|
||||
template <int start, int len>
|
||||
void set_bin(std::string& msg, const std::string& value) {
|
||||
|
||||
template<int start, int len>
|
||||
void set_bin(std::string &msg, const std::string &value) {
|
||||
if (value.size() != len) {
|
||||
std::cout << "len == " << len << " bits.size() == " << value.size() << std::endl;
|
||||
throw std::out_of_range("Start position and length exceed bit string size");
|
||||
}
|
||||
msg.replace(start, len, value);
|
||||
}
|
||||
template <int len>
|
||||
|
||||
template<int len>
|
||||
std::string to_bin(unsigned long long n) {
|
||||
return std::bitset<len>(n).to_string();
|
||||
}
|
||||
template <int num>
|
||||
|
||||
template<int num>
|
||||
std::string to_bin(double n) {
|
||||
return std::bitset<num>(static_cast<unsigned long long>(n)).to_string();
|
||||
}
|
||||
template <typename T>
|
||||
|
||||
template<typename T>
|
||||
T bin2(std::string_view binstr) {
|
||||
auto text = std::string(binstr);
|
||||
return std::stoull(text, nullptr, 2);
|
||||
}
|
||||
template <typename T>
|
||||
|
||||
template<typename T>
|
||||
unsigned long long hex2(std::string_view hexstr) {
|
||||
auto text = std::string(hexstr);
|
||||
return std::stoull(text, nullptr, 16);
|
||||
}
|
||||
template <>
|
||||
|
||||
template<>
|
||||
inline int bin2<int>(std::string_view binstr) {
|
||||
int bit_limit = sizeof(int) * 8;
|
||||
if (binstr.size() > bit_limit) {
|
||||
@@ -254,9 +283,9 @@ namespace Psc {
|
||||
|
||||
|
||||
// 设置dest的从off位起n位,写入value的低n位
|
||||
template <typename T = std::uint64_t>
|
||||
void set_bits(void* dest, size_t off, size_t n, T value) {
|
||||
auto d = static_cast<uint8_t*>(dest);
|
||||
template<typename T = std::uint64_t>
|
||||
void set_bits(void *dest, size_t off, size_t n, T value) {
|
||||
auto d = static_cast<uint8_t *>(dest);
|
||||
size_t bit_pos = off;
|
||||
size_t val_pos = 0;
|
||||
while (n > 0) {
|
||||
@@ -276,15 +305,18 @@ namespace Psc {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 大批量位处理性能不好 但是通常也没有这样的场景
|
||||
void bit_move(void* d, size_t doff, const void* s, size_t soff, size_t n);
|
||||
void bit_xor(void* d, size_t doff, const void* s, size_t soff, size_t n);
|
||||
void bit_or(void* d, size_t doff, const void* s, size_t soff, size_t n);
|
||||
void bit_and(void* d, size_t doff, const void* s, size_t soff, size_t n);
|
||||
template <typename T = std::uint64_t>
|
||||
T get_bits(void* src, size_t off, size_t n) {
|
||||
auto s = static_cast<const uint8_t*>(src);
|
||||
void bit_move(void *d, size_t doff, const void *s, size_t soff, size_t n);
|
||||
|
||||
void bit_xor(void *d, size_t doff, const void *s, size_t soff, size_t n);
|
||||
|
||||
void bit_or(void *d, size_t doff, const void *s, size_t soff, size_t n);
|
||||
|
||||
void bit_and(void *d, size_t doff, const void *s, size_t soff, size_t n);
|
||||
|
||||
template<typename T = std::uint64_t>
|
||||
T get_bits(void *src, size_t off, size_t n) {
|
||||
auto s = static_cast<const uint8_t *>(src);
|
||||
size_t bit_pos = off;
|
||||
size_t val_pos = 0;
|
||||
T result = 0;
|
||||
@@ -304,4 +336,3 @@ namespace Psc {
|
||||
}
|
||||
|
||||
#undef Base
|
||||
|
||||
|
||||
Reference in New Issue
Block a user