Skip to content

Commit 5c77245

Browse files
committed
clang format
1 parent 02375de commit 5c77245

File tree

5 files changed

+240
-292
lines changed

5 files changed

+240
-292
lines changed

src/utils/StringHelper.cpp

+86-117
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#define vsprintf_s vsprintf
1010
#endif
1111

12-
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter)
13-
{
12+
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter) {
1413
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
1514
std::string token;
1615
std::vector<std::string> res;
@@ -25,14 +24,12 @@ std::vector<std::string> StringHelper::Split(std::string s, const std::string& d
2524
return res;
2625
}
2726

28-
std::vector<std::string_view> StringHelper::Split(std::string_view s, const std::string& delimiter)
29-
{
27+
std::vector<std::string_view> StringHelper::Split(std::string_view s, const std::string& delimiter) {
3028
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
3129
std::string_view token;
3230
std::vector<std::string_view> res;
3331

34-
while ((pos_end = s.find(delimiter, pos_start)) != std::string_view::npos)
35-
{
32+
while ((pos_end = s.find(delimiter, pos_start)) != std::string_view::npos) {
3633
token = s.substr(pos_start, pos_end - pos_start);
3734
pos_start = pos_end + delim_len;
3835
res.push_back(token);
@@ -42,150 +39,122 @@ std::vector<std::string_view> StringHelper::Split(std::string_view s, const std:
4239
return res;
4340
}
4441

45-
std::string StringHelper::Strip(std::string s, const std::string& delimiter)
46-
{
47-
size_t pos = 0;
48-
std::string token;
42+
std::string StringHelper::Strip(std::string s, const std::string& delimiter) {
43+
size_t pos = 0;
44+
std::string token;
4945

50-
while ((pos = s.find(delimiter)) != std::string::npos)
51-
{
52-
token = s.substr(0, pos);
53-
s.erase(pos, pos + delimiter.length());
54-
}
46+
while ((pos = s.find(delimiter)) != std::string::npos) {
47+
token = s.substr(0, pos);
48+
s.erase(pos, pos + delimiter.length());
49+
}
5550

56-
return s;
51+
return s;
5752
}
5853

59-
std::string StringHelper::Replace(std::string str, const std::string& from,
60-
const std::string& to)
61-
{
62-
size_t start_pos = str.find(from);
54+
std::string StringHelper::Replace(std::string str, const std::string& from, const std::string& to) {
55+
size_t start_pos = str.find(from);
6356

64-
while (start_pos != std::string::npos)
65-
{
66-
str.replace(start_pos, from.length(), to);
67-
start_pos = str.find(from);
68-
}
57+
while (start_pos != std::string::npos) {
58+
str.replace(start_pos, from.length(), to);
59+
start_pos = str.find(from);
60+
}
6961

70-
return str;
62+
return str;
7163
}
7264

73-
void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to)
74-
{
75-
size_t start_pos = str.find(from);
65+
void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to) {
66+
size_t start_pos = str.find(from);
7667

77-
while (start_pos != std::string::npos)
78-
{
79-
str.replace(start_pos, from.length(), to);
80-
start_pos = str.find(from);
81-
}
68+
while (start_pos != std::string::npos) {
69+
str.replace(start_pos, from.length(), to);
70+
start_pos = str.find(from);
71+
}
8272
}
8373

84-
bool StringHelper::StartsWith(const std::string& s, const std::string& input)
85-
{
74+
bool StringHelper::StartsWith(const std::string& s, const std::string& input) {
8675
#if __cplusplus >= 202002L
87-
return s.starts_with(input.c_str());
76+
return s.starts_with(input.c_str());
8877
#else
89-
return s.rfind(input, 0) == 0;
78+
return s.rfind(input, 0) == 0;
9079
#endif
9180
}
9281

93-
bool StringHelper::Contains(const std::string& s, const std::string& input)
94-
{
95-
return s.find(input) != std::string::npos;
82+
bool StringHelper::Contains(const std::string& s, const std::string& input) {
83+
return s.find(input) != std::string::npos;
9684
}
9785

98-
bool StringHelper::EndsWith(const std::string& s, const std::string& input)
99-
{
100-
size_t inputLen = strlen(input.c_str());
101-
return s.rfind(input) == (s.size() - inputLen);
86+
bool StringHelper::EndsWith(const std::string& s, const std::string& input) {
87+
size_t inputLen = strlen(input.c_str());
88+
return s.rfind(input) == (s.size() - inputLen);
10289
}
10390

104-
std::string StringHelper::Sprintf(const char* format, ...)
105-
{
106-
char buffer[32768];
107-
// char buffer[2048];
108-
std::string output;
109-
va_list va;
91+
std::string StringHelper::Sprintf(const char* format, ...) {
92+
char buffer[32768];
93+
// char buffer[2048];
94+
std::string output;
95+
va_list va;
11096

111-
va_start(va, format);
112-
vsprintf_s(buffer, format, va);
113-
va_end(va);
97+
va_start(va, format);
98+
vsprintf_s(buffer, format, va);
99+
va_end(va);
114100

115-
output = buffer;
116-
return output;
101+
output = buffer;
102+
return output;
117103
}
118104

119-
std::string StringHelper::Implode(std::vector<std::string>& elements,
120-
const char* const separator)
121-
{
122-
return "";
105+
std::string StringHelper::Implode(std::vector<std::string>& elements, const char* const separator) {
106+
return "";
123107

124-
// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
125-
//[separator](std::string& ss, std::string& s) {
126-
// return ss.empty() ? s : ss + separator + s;
127-
//});
108+
// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
109+
//[separator](std::string& ss, std::string& s) {
110+
// return ss.empty() ? s : ss + separator + s;
111+
//});
128112
}
129113

130-
int64_t StringHelper::StrToL(const std::string& str, int32_t base)
131-
{
132-
return std::strtoull(str.c_str(), nullptr, base);
114+
int64_t StringHelper::StrToL(const std::string& str, int32_t base) {
115+
return std::strtoull(str.c_str(), nullptr, base);
133116
}
134117

135-
std::string StringHelper::BoolStr(bool b)
136-
{
137-
return b ? "true" : "false";
118+
std::string StringHelper::BoolStr(bool b) {
119+
return b ? "true" : "false";
138120
}
139121

140-
bool StringHelper::HasOnlyDigits(const std::string& str)
141-
{
142-
return std::all_of(str.begin(), str.end(), ::isdigit);
122+
bool StringHelper::HasOnlyDigits(const std::string& str) {
123+
return std::all_of(str.begin(), str.end(), ::isdigit);
143124
}
144125

145126
// Validate a hex string based on the c89 standard
146127
// https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Integer-Constants
147-
bool StringHelper::IsValidHex(std::string_view str)
148-
{
149-
if (str.length() < 3)
150-
{
151-
return false;
152-
}
153-
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
154-
{
155-
return std::all_of(str.begin() + 2, str.end(), ::isxdigit);
156-
}
157-
return false;
158-
}
159-
160-
161-
bool StringHelper::IsValidHex(const std::string& str)
162-
{
163-
return IsValidHex(std::string_view(str.c_str()));
164-
}
165-
166-
bool StringHelper::IsValidOffset(std::string_view str)
167-
{
168-
if (str.length() == 1)
169-
{
170-
// 0 is a valid offset
171-
return isdigit(str[0]);
172-
}
173-
return IsValidHex(str);
174-
}
175-
176-
bool StringHelper::IsValidOffset(const std::string& str)
177-
{
178-
if (str.length() == 1)
179-
{
180-
// 0 is a valid offset
181-
return isdigit(str[0]);
182-
}
183-
return IsValidHex(str);
184-
}
185-
186-
187-
bool StringHelper::IEquals(const std::string& a, const std::string& b)
188-
{
189-
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
190-
[](char a, char b) { return tolower(a) == tolower(b); });
128+
bool StringHelper::IsValidHex(std::string_view str) {
129+
if (str.length() < 3) {
130+
return false;
131+
}
132+
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
133+
return std::all_of(str.begin() + 2, str.end(), ::isxdigit);
134+
}
135+
return false;
136+
}
137+
138+
bool StringHelper::IsValidHex(const std::string& str) {
139+
return IsValidHex(std::string_view(str.c_str()));
140+
}
141+
142+
bool StringHelper::IsValidOffset(std::string_view str) {
143+
if (str.length() == 1) {
144+
// 0 is a valid offset
145+
return isdigit(str[0]);
146+
}
147+
return IsValidHex(str);
148+
}
149+
150+
bool StringHelper::IsValidOffset(const std::string& str) {
151+
if (str.length() == 1) {
152+
// 0 is a valid offset
153+
return isdigit(str[0]);
154+
}
155+
return IsValidHex(str);
156+
}
157+
158+
bool StringHelper::IEquals(const std::string& a, const std::string& b) {
159+
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) { return tolower(a) == tolower(b); });
191160
}

src/utils/StringHelper.h

+20-21
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99
#include <vector>
1010
#include <cstdint>
1111

12-
class StringHelper
13-
{
14-
public:
15-
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
16-
static std::vector<std::string_view> Split(std::string_view s, const std::string& delimiter);
17-
static std::string Strip(std::string s, const std::string& delimiter);
18-
static std::string Replace(std::string str, const std::string& from, const std::string& to);
19-
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
20-
static bool StartsWith(const std::string& s, const std::string& input);
21-
static bool Contains(const std::string& s, const std::string& input);
22-
static bool EndsWith(const std::string& s, const std::string& input);
23-
static std::string Sprintf(const char* format, ...);
24-
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
25-
static int64_t StrToL(const std::string& str, int32_t base = 10);
26-
static std::string BoolStr(bool b);
27-
static bool HasOnlyDigits(const std::string& str);
28-
static bool IsValidHex(std::string_view str);
29-
static bool IsValidHex(const std::string& str);
30-
static bool IsValidOffset(std::string_view str);
31-
static bool IsValidOffset(const std::string& str);
32-
static bool IEquals(const std::string& a, const std::string& b);
12+
class StringHelper {
13+
public:
14+
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
15+
static std::vector<std::string_view> Split(std::string_view s, const std::string& delimiter);
16+
static std::string Strip(std::string s, const std::string& delimiter);
17+
static std::string Replace(std::string str, const std::string& from, const std::string& to);
18+
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
19+
static bool StartsWith(const std::string& s, const std::string& input);
20+
static bool Contains(const std::string& s, const std::string& input);
21+
static bool EndsWith(const std::string& s, const std::string& input);
22+
static std::string Sprintf(const char* format, ...);
23+
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
24+
static int64_t StrToL(const std::string& str, int32_t base = 10);
25+
static std::string BoolStr(bool b);
26+
static bool HasOnlyDigits(const std::string& str);
27+
static bool IsValidHex(std::string_view str);
28+
static bool IsValidHex(const std::string& str);
29+
static bool IsValidOffset(std::string_view str);
30+
static bool IsValidOffset(const std::string& str);
31+
static bool IEquals(const std::string& a, const std::string& b);
3332
};

src/utils/filesystemtools/Directory.h

+35-41
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,39 @@ namespace fs = std::experimental::filesystem;
1616
#undef GetCurrentDirectory
1717
#undef CreateDirectory
1818

19-
class Directory
20-
{
21-
public:
22-
#ifndef PATH_HACK
23-
static std::string GetCurrentDirectory() { return fs::current_path().string(); }
24-
#endif
25-
26-
static bool Exists(const fs::path& path) { return fs::exists(path); }
27-
28-
// Stupid hack because of Windows.h
29-
static void MakeDirectory(const std::string& path)
30-
{
31-
CreateDirectory(path);
32-
}
33-
34-
static void CreateDirectory(const std::string& path)
35-
{
36-
try
37-
{
38-
fs::create_directories(path);
39-
}
40-
catch (...)
41-
{
42-
}
43-
}
44-
45-
static std::vector<std::string> ListFiles(const std::string& dir)
46-
{
47-
std::vector<std::string> lst;
48-
49-
if (Exists(dir))
50-
{
51-
for (auto& p : fs::recursive_directory_iterator(dir))
52-
{
53-
if (!p.is_directory())
54-
lst.push_back(p.path().generic_string());
55-
}
56-
}
57-
58-
return lst;
59-
}
19+
class Directory {
20+
public:
21+
#ifndef PATH_HACK
22+
static std::string GetCurrentDirectory() {
23+
return fs::current_path().string();
24+
}
25+
#endif
26+
27+
static bool Exists(const fs::path& path) {
28+
return fs::exists(path);
29+
}
30+
31+
// Stupid hack because of Windows.h
32+
static void MakeDirectory(const std::string& path) {
33+
CreateDirectory(path);
34+
}
35+
36+
static void CreateDirectory(const std::string& path) {
37+
try {
38+
fs::create_directories(path);
39+
} catch (...) {}
40+
}
41+
42+
static std::vector<std::string> ListFiles(const std::string& dir) {
43+
std::vector<std::string> lst;
44+
45+
if (Exists(dir)) {
46+
for (auto& p : fs::recursive_directory_iterator(dir)) {
47+
if (!p.is_directory())
48+
lst.push_back(p.path().generic_string());
49+
}
50+
}
51+
52+
return lst;
53+
}
6054
};

0 commit comments

Comments
 (0)