std::string_view 的使用

This commit is contained in:
2026-06-29 09:40:55 +08:00
parent ccf7eecc29
commit c25e58ae17
40 changed files with 532 additions and 522 deletions
+18 -8
View File
@@ -4,6 +4,7 @@
#include <iostream>
#include <memory_resource>
#include <string>
#include <string_view>
#include <type_traits>
#include <algorithm>
#include <utility>
@@ -49,6 +50,12 @@ namespace Psc {
}
};
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);}
@@ -159,9 +166,9 @@ namespace Psc {
inline size_t get_bin2mem_size(size_t num) {return num/8;};
void bin2mem(void* dest, const char* data, size_t size);
Base
STR_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<STR_Type>(get_bin2mem_size(size), resource);
auto ret = create_string<ret_type>(get_bin2mem_size(size), resource);
bin2mem((void*)ret.data(), (const char*)cacl_type<STR_Type>::data(mem), size);
return ret;
}
@@ -226,20 +233,23 @@ namespace Psc {
return std::bitset<num>(static_cast<unsigned long long>(n)).to_string();
}
template <typename T>
T bin2(const std::string& binstr) {
return std::stoull(binstr, nullptr, 2);
T bin2(std::string_view binstr) {
auto text = std::string(binstr);
return std::stoull(text, nullptr, 2);
}
template <typename T>
unsigned long long hex2(const std::string& hexstr) {
return std::stoull(hexstr, nullptr, 16);
unsigned long long hex2(std::string_view hexstr) {
auto text = std::string(hexstr);
return std::stoull(text, nullptr, 16);
}
template <>
inline int bin2<int>(const std::string& binstr) {
inline int bin2<int>(std::string_view binstr) {
int bit_limit = sizeof(int) * 8;
if (binstr.size() > bit_limit) {
std::cout << "Bitset.h inline int bin2<int> error! Binary string exceeds int bit limit" << std::endl;
}
return std::stoi(binstr, nullptr, 2);
auto text = std::string(binstr);
return std::stoi(text, nullptr, 2);
}