-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
171 lines (162 loc) · 3.77 KB
/
utils.cpp
File metadata and controls
171 lines (162 loc) · 3.77 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
#include "utils.hpp"
string removeWhiteSpace(string const &str)
{
string tmp;
for (int i = 0; i < (int)str.length(); i++)
if (!isWhiteSpace(str[i]))
tmp += str[i];
return tmp;
}
vector<string> splitString(string const &str, char sep)
{
vector<string> out;
string tmp;
for (int idx = 0; idx < (int)str.length(); idx++)
{
if (str[idx] == sep)
{
out.push_back(tmp);
tmp.clear();
continue;
}
tmp += str[idx];
}
out.push_back(tmp);
return out;
}
void trim(string &str)
{
int i = 0;
while(i < (int)str.length() - 1 && str[i] == '0')
i++;
str = str.substr(i, str.length());
}
bool isWhiteSpace(char c)
{
if (c >= 9 && c <= 13)
return true;
if (c == ' ')
return true;
return false;
}
string trimWhitSpace(const string &str)
{
int i;
int j;
i = 0;
j = str.length()-1;
while(i < (int)str.length() && isWhiteSpace(str[i]))
i++;
while(j >= 0 && isWhiteSpace(str[j]))
j--;
return str.substr(i,j-i+1);
}
bool isAlpha(string const &src)
{
for (char c : src)
if (!(c >= 'a' && c <='z') &&
!(c >= 'A' && c <= 'Z'))
return false;
return true;
}
string toLower(string const &src)
{
string out;
for (int idx = 0; idx < (int)src.size(); idx++)
{
if (src[idx] >= 'A' && src[idx] <= 'Z')
out += src[idx] + 'a'-'A';
else
out += src[idx];
}
return out;
}
int priority(char c)
{
string firstOrder = "-+";
string secondOrder = "/*%";
string thirdOrder = "^";
string forthOrder = "()";
if (firstOrder.find(c) != string::npos)
return 1;
if (secondOrder.find(c) != string::npos)
return 2;
if (thirdOrder.find(c) != string::npos)
return 3;
if (forthOrder.find(c) != string::npos)
return 4;
return -1;
}
int getCount(string const &str, char c)
{
int out = 0;
for (int idx = 0; idx < (int)str.length(); idx++)
if (str[idx] == c)
out++;
return out;
}
string highlightError(string tmp, char c, int mode)
{
string underline;
if (mode == 0 || c == 1)
{
if (c == 0)
{
underline = string(tmp.length(), ' ');
underline += "$^$";
}
else if (c == 1)
{
for (int idx = 0; idx < (int)tmp.length(); idx++)
{
if (idx == mode)
underline += "$^$";
else
underline += " ";
}
}
else
{
for (int idx = 0; idx < (int)tmp.length(); idx++)
{
if (tmp[idx] == c)
underline += "$^$";
else
underline += " ";
}
}
}
else if (mode == 1)
{
for (int idx = 0; idx < (int)tmp.length(); idx++)
{
if (tmp[idx] < 'A' || tmp[idx] > 'z' || (tmp[idx] > 'Z' && tmp[idx] < 'a'))
underline += "$^$";
else
underline += " ";
}
}
else if (mode == 2)
{
if (tmp[0] == ',')
underline += "$^$";
else
{
for (int idx = 0; idx < (int)tmp.length() - 1; idx++)
{
if (tmp[idx] == ',' && tmp[idx + 1] == ',')
{
underline += "$^$";
break;
}
else
underline += " ";
}
if (underline.find("^") == string::npos && tmp.back() == ',')
underline += "$^$";
}
}
if (mode == 1)
return tmp;
return tmp + '\n' + underline;
}