-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
51 lines (41 loc) · 1.51 KB
/
Copy pathutils.cpp
File metadata and controls
51 lines (41 loc) · 1.51 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
#include "utils.h"
#include <regex>
using namespace std;
vector<string> extractVariables(const string& line) {
vector<string> result;
// Match type followed by variable names and optional assignments
// Example: int a = 1, b, c;
regex varDeclRegex("\\b(int|float|char)\\s+([^;]+);");
smatch match;
if (regex_search(line, match, varDeclRegex)) {
string varPart = match[2].str();
// Extract individual variable names
regex nameRegex("\\b([a-zA-Z_]\\w*)\\b");
auto begin = sregex_iterator(varPart.begin(), varPart.end(), nameRegex);
auto end = sregex_iterator();
for (auto it = begin; it != end; ++it) {
string name = (*it)[1].str();
// Basic check to exclude numbers if they matched (they shouldn't with \b word regex but let's be safe)
if (!isdigit(name[0])) {
result.push_back(name);
}
}
}
return result;
}
vector<string> extractFunctions(const string& line) {
regex funcRegex("\\b(int|void|float|char)\\s+(\\w+)\\s*\\(");
vector<string> result;
auto begin = sregex_iterator(line.begin(), line.end(), funcRegex);
auto end = sregex_iterator();
for (auto it = begin; it != end; ++it) {
result.push_back((*it)[2]);
}
return result;
}
string trim(const string& str) {
size_t first = str.find_first_not_of(" \t");
if (first == string::npos) return "";
size_t last = str.find_last_not_of(" \t");
return str.substr(first, last - first + 1);
}