Cleanup and improve snippet code a bit
This commit is contained in:
parent
862b546532
commit
ec264aa0eb
@ -14,11 +14,16 @@ namespace cpptrace {
|
|||||||
namespace detail {
|
namespace detail {
|
||||||
constexpr std::int64_t max_size = 1024 * 1024 * 10; // 10 MiB
|
constexpr std::int64_t max_size = 1024 * 1024 * 10; // 10 MiB
|
||||||
|
|
||||||
|
struct line_range {
|
||||||
|
std::size_t begin;
|
||||||
|
std::size_t end; // one past the end
|
||||||
|
};
|
||||||
|
|
||||||
class snippet_manager {
|
class snippet_manager {
|
||||||
bool loaded_contents;
|
bool loaded_contents;
|
||||||
std::string contents;
|
std::string contents;
|
||||||
// for index i, gives the index in `contents` of one past the end of the line (i.e. the \n or contents.end())
|
// 1-based indexing
|
||||||
std::vector<std::size_t> line_table;
|
std::vector<line_range> line_table;
|
||||||
public:
|
public:
|
||||||
snippet_manager(const std::string& path) : loaded_contents(false) {
|
snippet_manager(const std::string& path) : loaded_contents(false) {
|
||||||
std::ifstream file;
|
std::ifstream file;
|
||||||
@ -43,13 +48,12 @@ namespace detail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_line(std::size_t line) const { // 0-indexed line TODO: reconsider
|
// takes a 1-index line
|
||||||
if(!loaded_contents || line >= line_table.size()) {
|
std::string get_line(std::size_t line) const {
|
||||||
|
if(!loaded_contents || line > line_table.size()) {
|
||||||
return "";
|
return "";
|
||||||
} else if(line == 0) {
|
|
||||||
return contents.substr(0, line_table[line]);
|
|
||||||
} else {
|
} else {
|
||||||
return contents.substr(line_table[line - 1] + 1, line_table[line] - line_table[line - 1] - 1);
|
return contents.substr(line_table[line].begin, line_table[line].end - line_table[line].begin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,15 +66,21 @@ namespace detail {
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void build_line_table() {
|
void build_line_table() {
|
||||||
std::size_t pos = 0;
|
line_table.push_back({0, 0});
|
||||||
|
std::size_t pos = 0; // stores the start of the current line
|
||||||
while(true) {
|
while(true) {
|
||||||
std::size_t new_pos = contents.find('\n', pos);
|
// find the end of the current line
|
||||||
if(new_pos == std::string::npos) {
|
std::size_t terminator_pos = contents.find('\n', pos);
|
||||||
line_table.push_back(contents.size());
|
if(terminator_pos == std::string::npos) {
|
||||||
|
line_table.push_back({pos, contents.size()});
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
line_table.push_back(new_pos);
|
std::size_t end_pos = terminator_pos; // one past the end of the current line
|
||||||
pos = new_pos + 1;
|
if(end_pos > 0 && contents[end_pos - 1] == '\r') {
|
||||||
|
end_pos--;
|
||||||
|
}
|
||||||
|
line_table.push_back({pos, end_pos});
|
||||||
|
pos = terminator_pos + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,13 +102,13 @@ namespace detail {
|
|||||||
// how wide the margin for the line number should be
|
// how wide the margin for the line number should be
|
||||||
constexpr std::size_t margin_width = 8;
|
constexpr std::size_t margin_width = 8;
|
||||||
|
|
||||||
|
// 1-indexed line
|
||||||
std::string get_snippet(const std::string& path, std::size_t target_line, std::size_t context_size, bool color) {
|
std::string get_snippet(const std::string& path, std::size_t target_line, std::size_t context_size, bool color) {
|
||||||
target_line--;
|
|
||||||
const auto& manager = get_manager(path);
|
const auto& manager = get_manager(path);
|
||||||
if(!manager.ok()) {
|
if(!manager.ok()) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
auto begin = target_line <= context_size ? 0 : target_line - context_size;
|
auto begin = target_line <= context_size + 1 ? 1 : target_line - context_size;
|
||||||
auto original_begin = begin;
|
auto original_begin = begin;
|
||||||
auto end = std::min(target_line + context_size, manager.num_lines() - 1);
|
auto end = std::min(target_line + context_size, manager.num_lines() - 1);
|
||||||
std::vector<std::string> lines;
|
std::vector<std::string> lines;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user