-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
224 lines (197 loc) · 7.37 KB
/
StringUtils.cpp
File metadata and controls
224 lines (197 loc) · 7.37 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "StringUtils.hpp"
using std::string;
using std::vector;
namespace MAT_NS_BEGIN
{
void StringUtils::SplitString(const string& s, const char separator, vector<string>& parts)
{
if (s.size())
{
size_t cur = 0;
for (size_t i = 0; i != s.size(); ++i)
{
if (s[i] == separator)
{
parts.push_back(s.substr(cur, i - cur));
cur = i;
cur++;
}
}
if (s.size() > 0)
{
parts.push_back(s.substr(cur));
}
}
}
bool StringUtils::AreAllCharactersAllowlisted(const string& stringToTest, const string& allowlist) noexcept
{
return (stringToTest.find_first_not_of(allowlist) == string::npos);
}
std::string toString(char const* value) { return std::string { value }; }
std::string toString(bool value) { return value ? std::string { "true" } : std::string { "false" }; }
std::string toString(char value) { return std::to_string(static_cast<signed char>(value)); }
std::string toString(int value) { return std::to_string(value); }
std::string toString(long value) { return std::to_string(value); }
std::string toString(long long value) { return std::to_string(value); }
std::string toString(unsigned char value) { return std::to_string(value); }
std::string toString(unsigned int value) { return std::to_string(value); }
std::string toString(unsigned long value) { return std::to_string(value); }
std::string toString(unsigned long long value) { return std::to_string(value); }
std::string toString(float value) { return std::to_string(value); }
std::string toString(double value) { return std::to_string(value); }
std::string toString(long double value) { return std::to_string(value); }
std::string to_string(const GUID_t& uuid)
{
static char inttoHex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
const unsigned buffSize = 36 + 1; // 36 + null-termination
char buf[buffSize] = { 0 };
int test = (uuid.Data1 >> 28 & 0x0000000F);
buf[0] = inttoHex[test];
test = (int)(uuid.Data1 >> 24 & 0x0000000F);
buf[1] = inttoHex[test];
test = (int)(uuid.Data1 >> 20 & 0x0000000F);
buf[2] = inttoHex[test];
test = (int)(uuid.Data1 >> 16 & 0x0000000F);
buf[3] = inttoHex[test];
test = (int)(uuid.Data1 >> 12 & 0x0000000F);
buf[4] = inttoHex[test];
test = (int)(uuid.Data1 >> 8 & 0x0000000F);
buf[5] = inttoHex[test];
test = (int)(uuid.Data1 >> 4 & 0x0000000F);
buf[6] = inttoHex[test];
test = (int)(uuid.Data1 & 0x0000000F);
buf[7] = inttoHex[test];
buf[8] = '-';
test = (int)(uuid.Data2 >> 12 & 0x000F);
buf[9] = inttoHex[test];
test = (int)(uuid.Data2 >> 8 & 0x000F);
buf[10] = inttoHex[test];
test = (int)(uuid.Data2 >> 4 & 0x000F);
buf[11] = inttoHex[test];
test = (int)(uuid.Data2 & 0x000F);
buf[12] = inttoHex[test];
buf[13] = '-';
test = (int)(uuid.Data3 >> 12 & 0x000F);
buf[14] = inttoHex[test];
test = (int)(uuid.Data3 >> 8 & 0x000F);
buf[15] = inttoHex[test];
test = (int)(uuid.Data3 >> 4 & 0x000F);
buf[16] = inttoHex[test];
test = (int)(uuid.Data3 & 0x000F);
buf[17] = inttoHex[test];
buf[18] = '-';
test = (int)(uuid.Data4[0] >> 4 & 0x0F);
buf[19] = inttoHex[test];
test = (int)(uuid.Data4[0] & 0x0F);
buf[20] = inttoHex[test];
test = (int)(uuid.Data4[1] >> 4 & 0x0F);
buf[21] = inttoHex[test];
test = (int)(uuid.Data4[1] & 0x0F);
buf[22] = inttoHex[test];
buf[23] = '-';
test = (int)(uuid.Data4[2] >> 4 & 0x0F);
buf[24] = inttoHex[test];
test = (int)(uuid.Data4[2] & 0x0F);
buf[25] = inttoHex[test];
test = (int)(uuid.Data4[3] >> 4 & 0x0F);
buf[26] = inttoHex[test];
test = (int)(uuid.Data4[3] & 0x0F);
buf[27] = inttoHex[test];
test = (int)(uuid.Data4[4] >> 4 & 0x0F);
buf[28] = inttoHex[test];
test = (int)(uuid.Data4[4] & 0x0F);
buf[29] = inttoHex[test];
test = (int)(uuid.Data4[5] >> 4 & 0x0F);
buf[30] = inttoHex[test];
test = (int)(uuid.Data4[5] & 0x0F);
buf[31] = inttoHex[test];
test = (int)(uuid.Data4[6] >> 4 & 0x0F);
buf[32] = inttoHex[test];
test = (int)(uuid.Data4[6] & 0x0F);
buf[33] = inttoHex[test];
test = (int)(uuid.Data4[7] >> 4 & 0x0F);
buf[34] = inttoHex[test];
test = (int)(uuid.Data4[7] & 0x0F);
buf[35] = inttoHex[test];
buf[36] = 0;
return std::string(buf);
}
std::string toLower(const std::string& str)
{
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(),
[](unsigned char c) noexcept { return (char)::tolower(c); });
return result;
}
std::string toUpper(const std::string& str)
{
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(),
[](unsigned char c) noexcept { return (char)::toupper(c); });
return result;
}
bool equalsIgnoreCase(const std::string& str1, const std::string& str2)
{
return (str1.size() == str2.size()) && (toLower(str1) == toLower(str2));
}
std::string sanitizeIdentifier(const std::string& str)
{
#if 0
// TODO: [MG] - we have to add some sanitizing logic, but definitely NOT replacing dots by underscores
std::replace(str.begin(), str.end(), '.', '_');
#endif
return str;
}
const char* priorityToStr(EventPriority priority) noexcept
{
switch (priority)
{
case EventPriority_Unspecified:
return "Unspecified";
case EventPriority_Off:
return "Off";
case EventPriority_Low:
return "Low";
case EventPriority_Normal:
return "Normal";
case EventPriority_High:
return "High";
case EventPriority_Immediate:
return "Immediate";
default:
return "???";
}
}
const char* latencyToStr(EventLatency latency) noexcept
{
switch (latency)
{
case EventLatency_Unspecified:
return "Unspecified";
case EventLatency_Off:
return "Off";
case EventLatency_Normal:
return "Normal";
case EventLatency_CostDeferred:
return "CostDeferred";
case EventLatency_RealTime:
return "RealTime";
case EventLatency_Max:
return "Immediate";
default:
return "???";
}
}
inline bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
} MAT_NS_END