73 lines
1.8 KiB
C
Raw Normal View History

#pragma once
#include "XNCore_global.h"
#include "XNType.h"
#include <string>
#include <vector>
namespace XNSim {
// 字符串类型抽象
using XN_STRING = std::string;
using XN_WSTRING = std::wstring;
using XN_STRINGLIST = std::vector<XN_STRING>;
using XN_WSTRINGLIST = std::vector<XN_WSTRING>;
// 字符串转换辅助函数
template <typename T> FORCEINLINE XN_STRING To_XNString(const T &value) {
return std::to_string(value);
}
template <typename T> FORCEINLINE XN_WSTRING To_XNWString(const T &value) {
return std::to_wstring(value);
}
FORCEINLINE XN_STRINGLIST XNSplit(const XN_STRING &str,
const XN_STRING &delim) {
XN_STRINGLIST tokens;
XN_SIZE prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == XN_STRING::npos)
pos = str.length();
XN_STRING token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
FORCEINLINE XN_INT32 XNSafe_stoi(const XN_STRING &str,
XN_INT32 defaultValue = 0) {
if (str.empty()) {
return defaultValue;
}
try {
return std::stoi(str);
} catch (const std::exception &) {
return defaultValue;
}
}
FORCEINLINE XN_DOUBLE XNSafe_stod(const XN_STRING &str,
XN_DOUBLE defaultValue = 0) {
if (str.empty()) {
return defaultValue;
}
try {
return std::stod(str);
} catch (const std::exception &) {
return defaultValue;
}
}
FORCEINLINE XN_INT64 XNSafe_stoll(const XN_STRING &str,
XN_INT64 defaultValue = 0) {
if (str.empty()) {
return defaultValue;
}
try {
return std::stoll(str);
} catch (const std::exception &) {
return defaultValue;
}
}
} // namespace XNSim