-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
177 lines (147 loc) · 5.22 KB
/
helpers.cpp
File metadata and controls
177 lines (147 loc) · 5.22 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
#include "pch.h"
#include "helpers.h"
#include <fstream>
#include <iostream>
#include <string_view>
#include <ranges>
#include <sstream>
#include <iomanip>
#include <algorithm>
std::string Helpers::formatFileSize(uint32_t size)
{
const int64_t KB = 1024;
const int64_t MB = 1024 * KB;
const int64_t GB = 1024 * MB;
const int64_t TB = 1024 * GB;
std::ostringstream oss;
if (size >= TB)
{
oss << std::fixed << std::setprecision(2) << (size / static_cast<double>(TB)) << " TB";
}
else if (size >= GB)
{
oss << std::fixed << std::setprecision(2) << (size / static_cast<double>(GB)) << " GB";
}
else if (size >= MB)
{
oss << std::fixed << std::setprecision(2) << (size / static_cast<double>(MB)) << " MB";
}
else if (size >= KB)
{
oss << std::fixed << std::setprecision(2) << (size / static_cast<double>(KB)) << " KB";
}
else
{
oss << size << " bytes";
}
return oss.str();
}
struct file_size_facet : public std::numpunct<char> {
explicit file_size_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
virtual char do_thousands_sep() const { return '.'; }
virtual std::string do_grouping() const { return "\003"; }
};
std::string Helpers::formatCodeLines(uint32_t lines)
{
static std::locale withgroupings(std::locale(), new file_size_facet);
return std::format(withgroupings, "{:L}", lines);
}
uint32_t Helpers::countCodeLines(const std::string& filePath)
{
std::ifstream file(filePath);
if (file.is_open())
{
uint32_t lineCount = 0;
std::string line;
bool inBlockComment = false; // carries /* ... */ state across lines
while (std::getline(file, line))
{
std::string code; // the line with // and /* */ comments stripped out
bool inString = false;
bool inChar = false;
for (size_t i = 0; i < line.size(); ++i)
{
char c = line[i];
char next = (i + 1 < line.size()) ? line[i + 1] : '\0';
if (inBlockComment)
{
if (c == '*' && next == '/')
{
inBlockComment = false;
++i; // also consume the '/'
}
continue;
}
if (inString || inChar)
{
code += c;
if (c == '\\' && next != '\0') // keep escaped char as-is
code += line[++i];
else if (inString && c == '"')
inString = false;
else if (inChar && c == '\'')
inChar = false;
continue;
}
if (c == '/' && next == '/')
break; // rest of the line is a comment
if (c == '/' && next == '*')
{
inBlockComment = true;
++i; // also consume the '*'
continue;
}
if (c == '"')
inString = true;
else if (c == '\'')
inChar = true;
code += c;
}
// Trim leading and trailing whitespace
code.erase(code.begin(), std::find_if(code.begin(), code.end(), [](unsigned char ch) { return !std::isspace(ch); }));
code.erase(std::find_if(code.rbegin(), code.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), code.end());
if (code == "}" || code == "{") // we're not interested in these lines
continue;
if (!code.empty())
{
++lineCount;
}
}
file.close();
return lineCount;
}
std::cerr << "Cannot open file: " << filePath << std::endl;
return static_cast<uint32_t>(-1); // or return 0 if you prefer
}
// Helper function to convert a string to lowercase using C++20 features
std::string toLowerCase(const std::string& str) {
std::string result;
std::ranges::transform(str, std::back_inserter(result), [](unsigned char c) {
return std::tolower(c);
});
return result;
}
bool Helpers::startsWith(const std::string& mainStr, const std::string& toMatch)
{
if (mainStr.size() < toMatch.size())
return false;
// Create views for the beginning of mainStr and the entire toMatch
auto mainStrView = mainStr | std::ranges::views::take(toMatch.size());
auto toMatchView = toMatch;
// Use std::ranges::equal with a case-insensitive comparator
return std::ranges::equal(mainStrView, toMatchView,
[](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b));
});
}
bool Helpers::compareNoCase(const std::string& str1, const std::string& str2)
{
return toLowerCase(str1) == toLowerCase(str2);
}
bool Helpers::containsNoCase(const std::string& str1, const std::string& str2)
{
std::string lowerStr1 = toLowerCase(str1);
std::string lowerStr2 = toLowerCase(str2);
// Check if lowerStr1 contains lowerStr2
return lowerStr1.find(lowerStr2) != std::string::npos;
}