1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 23:12:44 +01:00
nix/src/libutil/pos-table.cc
Graham Christensen e4f62e4608 Apply clang-format universally.
* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
2025-07-18 12:47:27 -04:00

51 lines
1.5 KiB
C++

#include "nix/util/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 linesCache = this->linesCache.lock();
/* Try the origin's line cache */
const auto * linesForInput = linesCache->getOrNullptr(origin->offset);
auto fillCacheForOrigin = [](std::string_view content) {
auto contentLines = Lines();
const char * begin = content.data();
for (Pos::LinesIterator it(content), end; it != end; it++)
contentLines.push_back(it->data() - begin);
if (contentLines.empty())
contentLines.push_back(0);
return contentLines;
};
/* Calculate line offsets and fill the cache */
if (!linesForInput) {
auto originContent = result.getSource().value_or("");
linesCache->upsert(origin->offset, fillCacheForOrigin(originContent));
linesForInput = linesCache->getOrNullptr(origin->offset);
}
assert(linesForInput);
// 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;
}
} // namespace nix