Skip to content

Commit 61a8de2

Browse files
SourceFile::lines shall be numbered (#312)
The member function `ipr::input::SourcFile::lines()` should return physical line numbers in addition to the morsel descriptors.
1 parent 72e6288 commit 61a8de2

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

include/ipr/input

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ namespace ipr::input {
4444
std::uint64_t length : 16; // number of bytes from the start
4545
};
4646

47+
// Descriptor for a physical line from an input source file.
48+
struct PhysicalLine {
49+
Morsel morsel { };
50+
std::uint32_t number { };
51+
};
52+
4753
// Input source file mapped to memory as sequence of raw bytes.
4854
// UTF-8 is assumed as the encoding of the text.
4955
struct SourceFile {
@@ -71,7 +77,7 @@ namespace ipr::input {
7177
private:
7278
const SourceFile* src;
7379
const char8_t* ptr;
74-
Morsel cache { };
80+
PhysicalLine cache { };
7581
void next_line() noexcept;
7682
};
7783

@@ -82,7 +88,7 @@ namespace ipr::input {
8288
using iterator_category = std::input_iterator_tag;
8389

8490
explicit iterator(LineRange* r) noexcept : range{r} { }
85-
Morsel operator*() const noexcept;
91+
PhysicalLine operator*() const noexcept;
8692
iterator& operator++() noexcept;
8793
void operator++(int) noexcept { ++(*this); }
8894
bool operator==(const iterator& that) const noexcept { return range == that.range; }

src/input.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ namespace ipr::input {
118118
while (idx < limit and ptr[idx] != carriage_return and ptr[idx] != line_feed)
119119
++idx;
120120
assert(idx < max_extent);
121-
cache.offset = offset;
122-
cache.length = idx;
121+
cache.morsel.offset = offset;
122+
cache.morsel.length = idx;
123+
++cache.number;
123124

124125
// Skip the new line marker.
125126
if (idx < limit)
@@ -139,7 +140,7 @@ namespace ipr::input {
139140
next_line();
140141
}
141142

142-
Morsel SourceFile::LineRange::iterator::operator*() const noexcept
143+
PhysicalLine SourceFile::LineRange::iterator::operator*() const noexcept
143144
{
144145
assert(range != nullptr);
145146
return range->cache;

tests/unit-tests/lines.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
TEST_CASE("echo input file") {
1414
ipr::input::SystemPath path = WIDEN(__FILE__);
1515
ipr::input::SourceFile file{path};
16-
auto n = 1;
1716
std::cout << "file.size: " << file.contents().size() << std::endl;
17+
std::uint32_t last_line_number = 0;
1818
for (auto line : file.lines())
1919
{
20-
std::cout << '[' << n << ']'
21-
<< " -> {offset: " << line.offset
22-
<< ", length: " << line.length << "}\n";
23-
++n;
20+
std::cout << '[' << line.number << ']'
21+
<< " -> {offset: " << line.morsel.offset
22+
<< ", length: " << line.morsel.length << "}\n";
23+
last_line_number = line.number;
2424
}
25-
CHECK(n == 27); // Adjust this number based on the actual number of lines in the file
25+
CHECK(last_line_number == 26); // Adjust this number based on the actual number of lines in the file
2626
}

0 commit comments

Comments
 (0)