-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cpp
86 lines (73 loc) · 1.97 KB
/
Utilities.cpp
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
#include <vector>
#include <set>
#include <string>
#include "Utilities.h"
#include <algorithm>
using namespace std;
void addspace(string& s){
string regex = "-|+*(){}:=!><";
string tmp = " ";
for(char c : regex){
tmp[1] = c;
replace_all(s,string(1,c),tmp);
}
replace_all(s,"\\ ","\\");
}
vector<string> expand(vector<string> v){
vector<string> res;
res.push_back("(");
for(string s : v){
if(s.find('-') != -1){
res.push_back("(");
for(char c = s[0]; c <= s[2]; c++){
res.push_back(string(1,c));
if(c != s[2]) res.push_back("|");
}
res.push_back(")");
}
else res.push_back(s);
}
res.push_back(")");
// cout << "-------------------------------\n";
// for(string i : res) cout << i << " ";
// cout << "--------------------------------\n";
return res;
}
vector<string> divide_on(string s, char divider){
vector<string> divided;
divided.push_back("");
for(char c : s){
if(c == divider){
if(divided.back().size()) divided.push_back("");
}
else {
if(c == '=' && divided.back().size()) divided.back().pop_back();
divided.back().push_back(c);
}
}
if(divided.back().empty()) divided.pop_back();
return divided;
}
bool replace_all(string& s, string match, string replace){
string res = "";
int n = s.size();
bool done = false;
for(int i = 0; i < n; i++){
if(s.substr(i,match.size()) == match){
done = true;
for(char r : replace){
res.push_back(r);
}
i+= match.size()-1;
}
else res.push_back(s[i]);
}
s = res;
return done;
}
void truncate(string& s){
while(!s.empty() && s.back() == ' ') s.pop_back();
reverse(s.begin(),s.end());
while(!s.empty() && s.back() == ' ') s.pop_back();
reverse(s.begin(),s.end());
}