Skip to content

Commit 264b7bd

Browse files
committed
LineColumn handling for CharStream.
1 parent c04fca7 commit 264b7bd

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

liblangutil/CharStream.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,32 @@ string CharStream::singleLineSnippet(string const& _sourceCode, SourceLocation c
144144

145145
return cut;
146146
}
147+
148+
optional<int> CharStream::translateLineColumnToPosition(LineColumn const& _lineColumn) const
149+
{
150+
return translateLineColumnToPosition(m_source, _lineColumn);
151+
}
152+
153+
optional<int> CharStream::translateLineColumnToPosition(std::string const& _text, LineColumn const& _input)
154+
{
155+
if (_input.line < 0)
156+
return nullopt;
157+
158+
size_t offset = 0;
159+
for (int i = 0; i < _input.line; i++)
160+
{
161+
offset = _text.find('\n', offset);
162+
if (offset == _text.npos)
163+
return nullopt;
164+
offset++; // Skip linefeed.
165+
}
166+
167+
size_t endOfLine = _text.find('\n', offset);
168+
if (endOfLine == string::npos)
169+
endOfLine = _text.size();
170+
171+
if (offset + static_cast<size_t>(_input.column) > endOfLine)
172+
return nullopt;
173+
return offset + static_cast<size_t>(_input.column);
174+
}
175+

liblangutil/CharStream.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#pragma once
5252

5353
#include <cstdint>
54+
#include <optional>
5455
#include <string>
5556
#include <tuple>
5657
#include <utility>
@@ -101,6 +102,12 @@ class CharStream
101102
LineColumn translatePositionToLineColumn(int _position) const;
102103
///@}
103104

105+
/// Translates a line:column to the absolute position.
106+
std::optional<int> translateLineColumnToPosition(LineColumn const& _lineColumn) const;
107+
108+
/// Translates a line:column to the absolute position for the given input text.
109+
static std::optional<int> translateLineColumnToPosition(std::string const& _text, LineColumn const& _input);
110+
104111
/// Tests whether or not given octet sequence is present at the current position in stream.
105112
/// @returns true if the sequence could be found, false otherwise.
106113
bool prefixMatch(std::string_view _sequence)

test/liblangutil/CharStream.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@
2828

2929
#include <boost/test/unit_test.hpp>
3030

31+
using namespace std;
32+
using namespace solidity::test;
33+
34+
namespace boost::test_tools::tt_detail
35+
{
36+
37+
template<>
38+
struct print_log_value<std::optional<int>>
39+
{
40+
void operator()(std::ostream& _out, std::optional<int> const& _value) const
41+
{
42+
_out << (_value ? to_string(*_value) : "[nullopt]");
43+
}
44+
};
45+
46+
template<>
47+
struct print_log_value<nullopt_t>
48+
{
49+
void operator()(std::ostream& _out, nullopt_t const&) const
50+
{
51+
_out << "[nullopt]";
52+
}
53+
};
54+
55+
} // namespace boost::test_tools::tt_detail
56+
57+
3158
namespace solidity::langutil::test
3259
{
3360

@@ -48,6 +75,62 @@ BOOST_AUTO_TEST_CASE(test_fail)
4875
);
4976
}
5077

78+
namespace
79+
{
80+
std::optional<int> toPosition(int _line, int _column, string const& _text)
81+
{
82+
return CharStream{_text, "source"}.translateLineColumnToPosition(
83+
LineColumn{_line, _column}
84+
);
85+
}
86+
87+
}
88+
89+
BOOST_AUTO_TEST_CASE(translateLineColumnToPosition)
90+
{
91+
BOOST_CHECK_EQUAL(toPosition(-1, 0, "ABC"), nullopt);
92+
BOOST_CHECK_EQUAL(toPosition(0, -1, "ABC"), nullopt);
93+
94+
BOOST_CHECK_EQUAL(toPosition(0, 0, ""), 0);
95+
BOOST_CHECK_EQUAL(toPosition(1, 0, ""), nullopt);
96+
BOOST_CHECK_EQUAL(toPosition(0, 1, ""), nullopt);
97+
98+
// With last line containing no LF
99+
BOOST_CHECK_EQUAL(toPosition(0, 0, "ABC"), 0);
100+
BOOST_CHECK_EQUAL(toPosition(0, 1, "ABC"), 1);
101+
BOOST_CHECK_EQUAL(toPosition(0, 2, "ABC"), 2);
102+
BOOST_CHECK_EQUAL(toPosition(0, 3, "ABC"), 3);
103+
BOOST_CHECK_EQUAL(toPosition(0, 4, "ABC"), nullopt);
104+
BOOST_CHECK_EQUAL(toPosition(1, 0, "ABC"), nullopt);
105+
106+
BOOST_CHECK_EQUAL(toPosition(0, 3, "ABC\nDEF"), 3);
107+
BOOST_CHECK_EQUAL(toPosition(0, 4, "ABC\nDEF"), nullopt);
108+
BOOST_CHECK_EQUAL(toPosition(1, 0, "ABC\nDEF"), 4);
109+
BOOST_CHECK_EQUAL(toPosition(1, 1, "ABC\nDEF"), 5);
110+
BOOST_CHECK_EQUAL(toPosition(1, 2, "ABC\nDEF"), 6);
111+
BOOST_CHECK_EQUAL(toPosition(1, 3, "ABC\nDEF"), 7);
112+
BOOST_CHECK_EQUAL(toPosition(1, 4, "ABC\nDEF"), nullopt);
113+
BOOST_CHECK_EQUAL(toPosition(2, 0, "ABC\nDEF"), nullopt);
114+
BOOST_CHECK_EQUAL(toPosition(2, 1, "ABC\nDEF"), nullopt);
115+
116+
// With last line containing LF
117+
BOOST_CHECK_EQUAL(toPosition(0, 0, "ABC\nDEF\n"), 0);
118+
BOOST_CHECK_EQUAL(toPosition(0, 1, "ABC\nDEF\n"), 1);
119+
BOOST_CHECK_EQUAL(toPosition(0, 2, "ABC\nDEF\n"), 2);
120+
121+
BOOST_CHECK_EQUAL(toPosition(1, 0, "ABC\nDEF\n"), 4);
122+
BOOST_CHECK_EQUAL(toPosition(1, 1, "ABC\nDEF\n"), 5);
123+
BOOST_CHECK_EQUAL(toPosition(1, 2, "ABC\nDEF\n"), 6);
124+
BOOST_CHECK_EQUAL(toPosition(1, 3, "ABC\nDEF\n"), 7);
125+
BOOST_CHECK_EQUAL(toPosition(1, 4, "ABC\nDEF\n"), nullopt);
126+
BOOST_CHECK_EQUAL(toPosition(2, 0, "ABC\nDEF\n"), 8);
127+
BOOST_CHECK_EQUAL(toPosition(2, 1, "ABC\nDEF\n"), nullopt);
128+
129+
BOOST_CHECK_EQUAL(toPosition(2, 0, "ABC\nDEF\nGHI\n"), 8);
130+
BOOST_CHECK_EQUAL(toPosition(2, 1, "ABC\nDEF\nGHI\n"), 9);
131+
BOOST_CHECK_EQUAL(toPosition(2, 2, "ABC\nDEF\nGHI\n"), 10);
132+
}
133+
51134
BOOST_AUTO_TEST_SUITE_END()
52135

53-
} // end namespaces
136+
}

0 commit comments

Comments
 (0)