DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
DAStringUtil.h
1#ifndef DASTRINGUTIL_H
2#define DASTRINGUTIL_H
3#include "DAUtilsAPI.h"
4#include <QSet>
5#include <QString>
6#include <string>
7#ifndef DA_STRING_CAST_D_H
8#define DA_STRING_CAST_D_H(type, value) DAUTILS_API type fromQString(const QString& str, type defaultValue = value);
9#endif
10#ifndef DA_STRING_CAST_H
11#define DA_STRING_CAST_H(type) DAUTILS_API type fromQString(const QString& str, const type& defaultValue = type());
12#endif
16namespace DA
17{
18// 生成一个唯一的字符串,如果出现了重复字符串,将会在这个字符串后面加上_1,这个函数常用于生成一个唯一的名字
19DAUTILS_API QString makeUniqueString(const QSet< QString >& stringSet, const QString& str, const QString& split = "_");
20
26DAUTILS_API std::wstring qstringToSystemWString(const QString& qstr);
27DAUTILS_API std::wstring stringToSystemWString(const std::string& str);
28DAUTILS_API int getSystemCodePage();
29#ifdef Q_OS_WIN
30DAUTILS_API std::string convertToUtf8(const std::string& str, unsigned int codePage);
31#endif
32// 字符串转换为值
33// type fromQString(const QString& str,type defaultval = 0)
34DA_STRING_CAST_D_H(short, 0)
35DA_STRING_CAST_D_H(unsigned short, 0)
36DA_STRING_CAST_D_H(int, 0)
37DA_STRING_CAST_D_H(unsigned int, 0)
38DA_STRING_CAST_D_H(long, 0)
39DA_STRING_CAST_D_H(unsigned long, 0)
40DA_STRING_CAST_D_H(long long, 0)
41DA_STRING_CAST_D_H(unsigned long long, 0)
42DA_STRING_CAST_D_H(float, 0.0f)
43DA_STRING_CAST_D_H(double, 0.0)
44DA_STRING_CAST_H(std::string)
45DA_STRING_CAST_H(std::u16string)
46DA_STRING_CAST_H(std::u32string)
47DA_STRING_CAST_H(std::wstring)
48
72template< typename Ite >
73QString vectorToString(Ite begin, Ite end, const QString& split = ";")
74{
75 QString res;
76 for (Ite i = begin; i != end; ++i) {
77 if (!res.isEmpty()) {
78 res.append(split);
79 }
80 res.append(QString("%1").arg(*i));
81 }
82 return res;
83}
84
113template< typename T, typename FpStrCastTo >
114std::vector< T > stringToVector(const QString& str, FpStrCastTo fp, const QString& split = ";")
115{
116 std::vector< T > res;
117 const QStringList splitlist = str.split(split);
118 for (const auto& s : splitlist) {
119 res.emplace_back(fp(s));
120 }
121 return res;
122}
123
124} // end of DA
125#endif // DASTRINGUTIL_H
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44
std::vector< T > stringToVector(const QString &str, FpStrCastTo fp, const QString &split=";")
将字符串按照指定的分隔符分割,并将分割后的字符串转换为指定类型的元素存储在vector中。
Definition DAStringUtil.h:114
QString makeUniqueString(const QSet< QString > &stringSet, const QString &str, const QString &split)
生成一个唯一的字符串
Definition DAStringUtil.cpp:41
QString vectorToString(Ite begin, Ite end, const QString &split=";")
将容器中的元素转换为字符串,并使用指定的分隔符连接。
Definition DAStringUtil.h:73
std::wstring qstringToSystemWString(const QString &qstr)
QString转换为系统编码的wstring
Definition DAStringUtil.cpp:85