172 lines
5.4 KiB
C++
172 lines
5.4 KiB
C++
#ifndef CCString_H
|
||
#define CCString_H
|
||
|
||
#include <sstream>
|
||
#include <cstdio>
|
||
#include <string>
|
||
#include "cstdarg"
|
||
#include "CCByteArray.h"
|
||
#include <locale>
|
||
#include <codecvt>
|
||
#include <sstream>
|
||
#ifdef __linux__
|
||
#include <iconv.h>
|
||
#endif
|
||
|
||
class CCWString;
|
||
|
||
using CCStream = std::ostringstream;
|
||
using CCSStream = std::stringstream;
|
||
using WString = std::wstring;
|
||
namespace CTL {
|
||
/**
|
||
* @brief 字符串类,继承自std::string,提供额外的功能
|
||
*/
|
||
class String : public std::string
|
||
{
|
||
public:
|
||
// 默认构造函数
|
||
String() = default;
|
||
explicit String(const char *fmt, ...);
|
||
String(const std::initializer_list<std::string> list){
|
||
for (auto& i : list) {
|
||
this->append(i);
|
||
}
|
||
}
|
||
explicit String(const CCSStream& stream);
|
||
// 继承std::string的构造函数
|
||
using std::string::string;
|
||
// 转换字符串为字节
|
||
[[nodiscard]] Byte* toBytes() const;
|
||
[[nodiscard]] Byte* toBytes();
|
||
// 从basic_string构造String对象
|
||
String(const basic_string &basicString);
|
||
// 格式化字符串
|
||
String Format(const char * standardCharsets) const;
|
||
String Format(const char * standardCharsets);
|
||
// 指定大小构造字符串
|
||
String(size_t Size);
|
||
// 字符串转换为整数
|
||
int to_int() const;
|
||
float to_float() const;
|
||
double to_double() const;
|
||
// 字符串转换为整数(非const版本)
|
||
int to_int();
|
||
float to_float();
|
||
double to_double();
|
||
/**
|
||
* @brief 格式化字符串的静态方法
|
||
* @param fmt 格式化字符串的格式
|
||
* @param ... 可变参数,与格式化字符串匹配
|
||
* @return 返回格式化后的String对象
|
||
*/
|
||
static String format(const char* fmt, ...);
|
||
// 转换String对象为WString对象
|
||
[[nodiscard]] WString to_CCWString() const {
|
||
#ifdef _WIN32
|
||
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||
return converter.from_bytes(data());
|
||
#elif __linux__
|
||
iconv_t cd = iconv_open("UTF-8", "WCHAR_T");
|
||
if (cd == (iconv_t)-1) {
|
||
return L"";
|
||
}
|
||
size_t inBytes = size() * sizeof(wchar_t);
|
||
size_t outBytes = inBytes * 2;
|
||
char* inbuf = (char*)data();
|
||
char* outbuf = new char[outBytes];
|
||
char* outbufEnd = outbuf + outBytes;
|
||
size_t convertedBytes = iconv(cd, &inbuf, &inBytes, &outbuf, &outBytes);
|
||
iconv_close(cd);
|
||
if (convertedBytes == (size_t)-1) {
|
||
delete[] outbuf;
|
||
return L"";
|
||
}
|
||
auto* wcBuf = reinterpret_cast<wchar_t*>(outbuf);
|
||
size_t wcLength = (outbufEnd - outbuf) / sizeof(wchar_t);
|
||
WString result(wcBuf, wcLength); // 正确调用wchar_t数组构造
|
||
delete[] outbuf;
|
||
return result;
|
||
#endif
|
||
}
|
||
String& operator<<(const char* str) {
|
||
this->append(str);
|
||
return *this;
|
||
}
|
||
String& operator<<(const int value) {
|
||
this->append(std::to_string(value));
|
||
return *this;
|
||
}
|
||
String& operator<<(const float value) {
|
||
this->append(std::to_string(value));
|
||
return *this;
|
||
}
|
||
String& operator<<(const double value) {
|
||
this->append(std::to_string(value));
|
||
return *this;
|
||
}
|
||
String& operator<<(const String& other) {
|
||
this->append(other);
|
||
return *this;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* @brief 宽字符串类,继承自std::wstring,提供额外的功能
|
||
*/
|
||
class CCWString:public WString
|
||
{
|
||
public:
|
||
// 默认构造函数
|
||
CCWString() = default;
|
||
// 从std::wstring构造CCWString对象
|
||
CCWString(const std::wstring& wstr) {
|
||
assign(wstr);
|
||
}
|
||
// 转换CCWString对象为String对象
|
||
[[nodiscard]] String to_CCString() const {
|
||
#ifdef _WIN32
|
||
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||
return converter.to_bytes(data());
|
||
#elif __linux__
|
||
iconv_t cd = iconv_open("UTF-8", "WCHAR_T"); // 修正转换方向
|
||
if (cd == (iconv_t)-1) return "";
|
||
|
||
size_t inBytes = size() * sizeof(wchar_t);
|
||
char* inbuf = (char*)data();
|
||
size_t outBytes = inBytes * 4; // 扩大输出缓冲区
|
||
char* outbuf = new char[outBytes];
|
||
char* outptr = outbuf;
|
||
|
||
if (iconv(cd, &inbuf, &inBytes, &outptr, &outBytes) == (size_t)-1) {
|
||
delete[] outbuf;
|
||
iconv_close(cd);
|
||
return "";
|
||
}
|
||
iconv_close(cd);
|
||
|
||
// 使用转换后的char数组构造String
|
||
String result(outbuf, outptr - outbuf);
|
||
delete[] outbuf;
|
||
return result;
|
||
#endif
|
||
}
|
||
};
|
||
|
||
}
|
||
|
||
inline CTL::String& operator<<(const char* lhs, CTL::String& rhs) {
|
||
rhs.append(lhs);
|
||
return rhs;
|
||
}
|
||
|
||
// 为CTL::String类型提供哈希函数
|
||
template <>
|
||
struct std::hash<CTL::String> {
|
||
std::size_t operator()(const CTL::String& s) const noexcept {
|
||
return std::hash<std::string>()(s);
|
||
}
|
||
};
|
||
|
||
#endif
|