| 79 | } |
| 80 | |
| 81 | std::string CharStream::lineAtPosition(int _position) const |
| 82 | { |
| 83 | // if _position points to \n, it returns the line before the \n |
| 84 | using size_type = std::string::size_type; |
| 85 | size_type searchStart = std::min<size_type>(m_source.size(), size_type(_position)); |
| 86 | if (searchStart > 0) |
| 87 | searchStart--; |
| 88 | size_type lineStart = m_source.rfind('\n', searchStart); |
| 89 | if (lineStart == std::string::npos) |
| 90 | lineStart = 0; |
| 91 | else |
| 92 | lineStart++; |
| 93 | std::string line = m_source.substr( |
| 94 | lineStart, |
| 95 | std::min(m_source.find('\n', lineStart), m_source.size()) - lineStart |
| 96 | ); |
| 97 | if (!line.empty() && line.back() == '\r') |
| 98 | line.pop_back(); |
| 99 | return line; |
| 100 | } |
| 101 | |
| 102 | LineColumn CharStream::translatePositionToLineColumn(int _position) const |
| 103 | { |