-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip_comments.cpp
More file actions
34 lines (33 loc) · 796 Bytes
/
strip_comments.cpp
File metadata and controls
34 lines (33 loc) · 796 Bytes
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
#include <string>
#include <unordered_set>
std::string stripComments(
const std::string& str,
const std::unordered_set<char>& markers
) {
std::string s;
bool b = false;
for (size_t i = 0; i < str.length(); ++i) {
if (markers.find(str[i]) != markers.end()) {
b = (true);
continue;
}
b = (str[i] == '\n') ? (false) : (b);
if(!b)
s += str[i];
}
for (size_t i = 0; i <= s.length(); ++i) {
if(s[i] == '\n' || s[i] == '\0'){
if(i == 0) continue;
size_t j = (i);
--j;
while(1) {
if (std::isalpha(static_cast<unsigned char>(s[j])))
break;
s.erase(j, 1);
if (j == 0) break;
--j;
}
}
}
return (s);
}