for displaying source code around error position
| 27 | |
| 28 | // for displaying source code around error position |
| 29 | static std::string peak_source(const std::string & source, size_t pos, size_t max_peak_chars = 40) { |
| 30 | if (source.empty()) { |
| 31 | return "(no source available)"; |
| 32 | } |
| 33 | std::string output; |
| 34 | size_t start = (pos >= max_peak_chars) ? (pos - max_peak_chars) : 0; |
| 35 | size_t end = std::min(pos + max_peak_chars, source.length()); |
| 36 | std::string substr = source.substr(start, end - start); |
| 37 | string_replace_all(substr, "\n", "↵"); |
| 38 | output += "..." + substr + "...\n"; |
| 39 | std::string spaces(pos - start + 3, ' '); |
| 40 | output += spaces + "^"; |
| 41 | return output; |
| 42 | } |
| 43 | |
| 44 | static std::string fmt_error_with_source(const std::string & tag, const std::string & msg, const std::string & source, size_t pos) { |
| 45 | std::ostringstream oss; |
no test coverage detected