-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathutil.hpp
67 lines (53 loc) · 1.44 KB
/
util.hpp
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
//
// Created by root on 4/24/18.
//
#ifndef FEATHER_UTIL_HPP
#define FEATHER_UTIL_HPP
namespace feather {
inline std::string cur_time() {
std::time_t t = std::time(nullptr);
std::stringstream ss;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
inline std::string sv2s(std::string_view sv) {
return std::string(sv.data(), sv.length());
}
inline bool is_integer(const std::string& str) {
char* p;
long converted = strtol(str.data(), &p, 10);
if (*p) {
return false;
}
return true;
}
inline bool is_integer(std::string_view str) {
for (auto ch : str) {
if (!std::isdigit(ch))
return false;
}
return true;
}
inline bool has_special_char(std::string_view str, bool is_email = false) {
for (char ch : str) {
if (ch == ';' || ch == ',' || ch == '%' || ch == '"' || ch == '\\' ||
ch == '-' || ch == '|' || ch == '(' || ch == ')' || ch == '[' ||
ch == ']' || ch == '{' || ch == '}' || (!is_email && ch == '@') ||
ch == '*' || ch == '!' || ch == '\'' || ch == '#')
return true;
}
return false;
}
inline std::string_view utf8substr(std::string_view str, int sub_len) {
int len = 0;
int byte_index = 0;
for (byte_index = 0; byte_index < str.length(); byte_index++) {
if ((str[byte_index] & 0xc0) != 0x80)
len += 1;
if (len >= sub_len)
break;
}
return str.substr(0, byte_index);
}
} // namespace feather
#endif // FEATHER_UTIL_HPP