Push all code
This commit is contained in:
parent
347987bb66
commit
ba2bf4b404
10 changed files with 1671 additions and 0 deletions
75
src/preprocessor.cpp
Normal file
75
src/preprocessor.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include "preprocessor.hpp"
|
||||
#include <vector>
|
||||
|
||||
#include <iostream> // TODO
|
||||
|
||||
std::string DeleteComments(std::string&& text) {
|
||||
std::vector<std::pair<int, int>> text_without_comments;
|
||||
size_t n = text.size();
|
||||
int begin_segment = 0;
|
||||
|
||||
text_without_comments.emplace_back(begin_segment, n);
|
||||
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (text[i] == '/' && i + 1 < n && text[i + 1] == '/') {
|
||||
text_without_comments.pop_back();
|
||||
text_without_comments.emplace_back(begin_segment, i);
|
||||
while (i + 1 < n && text[i + 1] != '\n') {
|
||||
++i;
|
||||
}
|
||||
begin_segment = i + 1;
|
||||
text_without_comments.emplace_back(begin_segment, n);
|
||||
}
|
||||
}
|
||||
|
||||
int current_place = 0;
|
||||
for (auto i: text_without_comments) {
|
||||
std::copy(text.begin() + i.first, text.begin() + i.second, text.begin() +
|
||||
current_place);
|
||||
current_place += i.second - i.first;
|
||||
}
|
||||
text.resize(current_place);
|
||||
return std::move(text);
|
||||
}
|
||||
|
||||
std::string DeleteMultiLineComments(std::string&& text) {
|
||||
std::vector<std::pair<int, int>> text_without_comments;
|
||||
size_t n = text.size();
|
||||
int begin_segment = 0;
|
||||
|
||||
text_without_comments.emplace_back(begin_segment, n);
|
||||
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (text[i] == '/' && i + 1 < n && text[i + 1] == '*') {
|
||||
text_without_comments.pop_back();
|
||||
text_without_comments.emplace_back(begin_segment, i);
|
||||
i++;
|
||||
while (i < n && (text[i - 1] != '*' || text[i] != '/')) {
|
||||
++i;
|
||||
}
|
||||
begin_segment = i + 1;
|
||||
text_without_comments.emplace_back(begin_segment, n);
|
||||
}
|
||||
}
|
||||
|
||||
int current_place = 0;
|
||||
for (auto i: text_without_comments) {
|
||||
std::copy(text.begin() + i.first, text.begin() + i.second, text.begin() +
|
||||
current_place);
|
||||
current_place += i.second - i.first;
|
||||
}
|
||||
text.resize(current_place);
|
||||
return std::move(text);
|
||||
}
|
||||
|
||||
std::string Preprocessor(std::string&& text) {
|
||||
text = DeleteComments(std::move(text));
|
||||
text = DeleteMultiLineComments(std::move(text));
|
||||
return std::move(text);
|
||||
}
|
||||
|
||||
std::string Preprocessor(const std::string& text) {
|
||||
std::string copy(text);
|
||||
copy = Preprocessor(std::move(copy));
|
||||
return copy;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue