-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
103 lines (94 loc) · 2.63 KB
/
utils.cpp
File metadata and controls
103 lines (94 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "utils.h"
std::vector<std::string>* split(const std::string &str, const std::string& split)
{
auto tokens = new std::vector<std::string>;
std::regex reg(split); // 匹配split
std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
decltype(pos) end; // 自动推导类型
for (; pos != end; ++pos)
{
tokens->push_back(pos->str());
}
return tokens;
}
// 重载 << 运算符
std::ostream& operator<<(std::ostream& out, ValueType type)
{
switch (type) {
case ValueType::UNKNOWN:
out << "UNKNOWN";
break;
case ValueType::INT:
out << "INT";
break;
case ValueType::FLOAT:
out << "FLOAT";
break;
case ValueType::STRING:
out << "STRING";
break;
case ValueType::BOOL:
out << "BOOL";
break;
default:
out << "Unknown ValueType";
break;
}
return out;
}
// 使用正则表达式判断是否包含字母
bool containsLetter(const std::string& str)
{
std::regex letter_regex("[a-zA-Z]");
return std::regex_search(str, letter_regex);
}
// 是否为整数
bool isInt(const std::string& str)
{
std::regex numeric_regex("^[0-9]+$");
return std::regex_match(str, numeric_regex);
}
// 尝试解析字符串的类型
ValueType getValueType(const std::string& str)
{
if (str == "true" || str == "false")
{
return ValueType::BOOL;
}
if (containsLetter(str) || "." == str) // 如果包含字母或者只有一个小数点
{
return ValueType::STRING; // 那一定是字符串类型
}
if (isInt(str)) // 如果是整数
{
return ValueType::INT; // 是整数类型
}
if (str.find('.') != std::string::npos && (std::count(str.begin(), str.end(), '.') == 1))
{
return ValueType::FLOAT; // 小数类型
}
// 如果所有逻辑都没有命中,默认为字符串类型
return ValueType::STRING;
}
// 类型检查,不合格就抛出异常那种
void checkInt(const std::string input)
{
if(getValueType(input) != ValueType::INT)
{
throw std::invalid_argument("字符串" + input + " int类型检查失败!");
}
}
void checkBool(const std::string& input)
{
if(getValueType(input) != ValueType::BOOL)
{
throw std::invalid_argument("字符串" + input + " bool类型检查失败!");
}
}
void checkFloat(const std::string& input)
{
if(getValueType(input) != ValueType::FLOAT)
{
throw std::invalid_argument("字符串" + input + " float类型检查失败!");
}
}