mirror of
https://github.com/NixOS/nix
synced 2025-07-01 16:41:47 +02:00
All of this code doesn't actually depend on anything from
libexpr. Because Pos is so tigtly coupled with Error, it
makes sense to have in the same library.
(cherry picked from commit a53b184e63
)
37 lines
1 KiB
C++
37 lines
1 KiB
C++
#include "pos-table.hh"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace nix {
|
|
|
|
/* Position table. */
|
|
|
|
Pos PosTable::operator[](PosIdx p) const
|
|
{
|
|
auto origin = resolve(p);
|
|
if (!origin)
|
|
return {};
|
|
|
|
const auto offset = origin->offsetOf(p);
|
|
|
|
Pos result{0, 0, origin->origin};
|
|
auto lines = this->lines.lock();
|
|
auto linesForInput = (*lines)[origin->offset];
|
|
|
|
if (linesForInput.empty()) {
|
|
auto source = result.getSource().value_or("");
|
|
const char * begin = source.data();
|
|
for (Pos::LinesIterator it(source), end; it != end; it++)
|
|
linesForInput.push_back(it->data() - begin);
|
|
if (linesForInput.empty())
|
|
linesForInput.push_back(0);
|
|
}
|
|
// as above: the first line starts at byte 0 and is always present
|
|
auto lineStartOffset = std::prev(std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
|
|
|
|
result.line = 1 + (lineStartOffset - linesForInput.begin());
|
|
result.column = 1 + (offset - *lineStartOffset);
|
|
return result;
|
|
}
|
|
|
|
}
|